76284


Pythonで書いたコードをexe化しました. その際に使用したコマンドラインオプションは

[code]

    pyinstaller test.py -F
    
[/code]

(※一度 `-F` を付けずに作成しましたが，起動してタスクバーに棒が一瞬表示されすぐ消えました．)

exeファイルを起動すると入力待ちの状態になります．  
↑キーまたは↓キーを押すと下のエラーがコマンドプロンプトに表示され，すぐに閉じてしまいます．

原因を教えていただけると幸いです．よろしくお願いいたします．

**エラーメッセージ:**

[code]

    pygame 2.0.1 (SDL 2.0.14, Python 3.8.5)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    Trackback (most recent call last):
     File "test.py", line 456, in <module>
     File "test.py", line 331, in main
     File "test.py", line 272, in draw_text
    TypeError: expected str, bytes or os.PathLike object, not _io_BytesIO
    [28628] Failed to execute script test
    
[/code]

**コード:**

[code]

    import pygame
    import sys
    import math
    import random
    from pygame.locals import *
    
    BLACK  = (  0,   0,   0)
    SILVER = (192, 208, 224)
    RED    = (255,   0,   0)
    CYAN   = (  0, 224, 255)
    
    
    #SEを読み込む
    se_barrage = None
    se_damage = None
    se_explosion = None
    se_shot = None
    
    stage_idx = 0
    
    switch = False
    idx = 0
    tmr = 0
    bomb_tmr = 0
    score = 0
    hisco = 10000
    new_record = False
    bg_y = 0
    #自機の移動範囲，小さい時とフルスクリーンの時とでの変数[MINI*4, FULL*4],[上, 下, 左, 右]
    WAKU = [65, 665, 80, 608]
    
    
    ss_x = 344                     #自機のx座標
    ss_y = 620                     #自機のy座標
    ss_d = 0                     #自機の傾き(0:なし, 1:左, 2:右　)    
    ss_shield = 5                #残機
    ss_muteki = 0                #無敵判定用
    key_spc = 0
    key_z = 0
    key_x = 0
    key_shift = 0
    MISSILE_MAX = 200            #自機の弾の最大数
    msl_no = 0                   #弾の発射に使う変数
    speed_mod = False            #自機の移動速度遅くする変数(True:遅い, False:速い)
    speed_jiki = 10              #自機の移動距離の変数
    
    msl_f = [False]*MISSILE_MAX  #弾の存在を確認する変数
    msl_x = [0]*MISSILE_MAX      #弾のx座標
    msl_y = [0]*MISSILE_MAX      #弾のy座標
    msl_a = [0]*MISSILE_MAX      #弾の角度
    msl_type = [0]*MISSILE_MAX     #弾の種類
    kakudo = 0 #弾の角度
    BOMB = 3
    graze = 0
    
    ENEMY_MAX = 200             #敵の最大数
    emy_no = 0                   #敵を出すときに使うリストの添え字用の変数
    emy_f = [False]*ENEMY_MAX    #敵が出現しているか管理するフラグのリスト
    emy_x = [0]*ENEMY_MAX        #敵のx座標
    emy_y = [0]*ENEMY_MAX        #敵のy座標
    emy_a = [0]*ENEMY_MAX        #敵の角度
    emy_type = [0]*ENEMY_MAX     #敵の種類
    emy_speed = [0]*ENEMY_MAX    #敵の速さ
    emy_shield = [0]*ENEMY_MAX   #敵の体力
    emy_decay = [0]*ENEMY_MAX     #敵の弾の種類
    emy_count = [0]*ENEMY_MAX    #敵の動きを管理するリスト
    
    LINE_T = 0                 #敵が消える上ラインの座標
    LINE_B = 702                 #敵が消える下ラインの座標
    LINE_L = 22                 #敵が消える左ラインの座標
    LINE_R = 630                #敵が消える右ラインの座標
    
    EFFECT_MAX = 150             #爆発の最大数
    eff_no = 0                   #爆発を使う変数
    eff_p = [0]*EFFECT_MAX       #爆発の画像番号用のリスト
    eff_x = [0]*EFFECT_MAX       #爆発のx座標
    eff_y = [0]*EFFECT_MAX       #爆発のy座標
    
    eff_nog = 0                   #爆発を使う変数
    eff_pg = [0]*EFFECT_MAX       #爆発の画像番号用のリスト
    eff_xg = [0]*EFFECT_MAX       #爆発のx座標
    eff_yg = [0]*EFFECT_MAX       #爆発のy座標
    key_if = False
    
    shot_ang=0
    rooper = 0
    
    bar_x = 160
    bar_y = 280
    
    tamadashi = True
    
    
    def get_dis(x1, y1, x2, y2): #二点間の距離を求める関数
    
        return( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) )
    
    def move_starship(scrn, key): #自機の操作に関する関数
        global idx, tmr, ss_x, ss_y, ss_d, key_spc, key_z, key_x, key_shift, ss_shield, ss_muteki, speed_mod, speed_jiki, mini_big, BOMB, bomb_tmr, graze, score
        ss_d = 0 #傾きを０（なし）にする
    
        if speed_mod == True:
            speed_jiki = 4
        else:
            speed_jiki = 15
    
        if key[pygame.K_UP] == 1: #上操作
            ss_y = ss_y - speed_jiki
            if ss_y < WAKU[0]:
                ss_y = WAKU[0]
        if key[pygame.K_DOWN] == 1: #下操作
            ss_y = ss_y + speed_jiki
            if ss_y > WAKU[1]:
                ss_y = WAKU[1]
        if key[pygame.K_LEFT] == 1: #左操作
            ss_d = 1 #傾きを１（左）にする
            ss_x = ss_x - speed_jiki
            if ss_x < WAKU[2]:
                ss_x = WAKU[2]
        if key[pygame.K_RIGHT] == 1: #右操作
            ss_d = 2 #傾きを２（右）にする
            ss_x = ss_x + speed_jiki
            if ss_x > WAKU[3]:
                ss_x = WAKU[3]
    
        key_z = (key_z+1)*key[K_z]   #Zキーで通常ショット
        if key_z%4 == 1:
            set_missile(0)
    
        key_x = (key_x+1)*key[K_x]   #Xキーでボム
        if key_x == 1 and BOMB > 0 and bomb_tmr == 0:
            set_missile(10)
            BOMB = BOMB - 1
    
        if key[pygame.K_LSHIFT] == 1: #Shiftで低速
            speed_mod = True
        else:
            speed_mod = False
    
        if bomb_tmr > 0:
            bomb_tmr = bomb_tmr - 1
    
        if ss_muteki > 0:
            ss_muteki = ss_muteki - 1
            return
        elif idx == 1:
            for i in range(ENEMY_MAX):
                if (emy_f[i] ==True) and (emy_type[i] != 17) and (emy_type[i] != 18):
                    w = 100
                    h = 100
                    r = int ((w+h)/4 + (18+18)/4)
                    if get_dis(emy_x[i], emy_y[i], ss_x-10, ss_y-8) < r*r:
                        ss_shield = ss_shield - 1
                        BOMB = 3
                            
                        if ss_muteki == 0:
                            ss_muteki = 180
                        if ss_shield <= 0:
                            ss_shield = 0
                            idx = 2
                            tmr = 0
                        if emy_type[i] != 13:
                            emy_f[i] = False #自機の操作に関する関数
    
        for i in range(ENEMY_MAX):
                if (emy_f[i] ==True) and (emy_type[i] != 17) and (emy_type[i] != 18) and (emy_type[i] != 0) and (emy_type[i] != 13) and (emy_type[i] != 14):
                    w = 100
                    h = 100
                    r = int ((w+h)/4 + (18+18)/4)
                    if get_dis(emy_x[i], emy_y[i], ss_x-14, ss_y-8) < r*r+1000:
                        graze = graze + 1
                        score = score + 10
    
    
    def set_missile(typ):
        global msl_no, ss_muteki, bomb_tmr
        if typ == 0:
            msl_f[msl_no] = True
            msl_x[msl_no] = ss_x-8
            msl_y[msl_no] = ss_y-50
            msl_a[msl_no] = 270
            msl_type[msl_no] = 1
            msl_no = (msl_no+1)%MISSILE_MAX
        if typ == 10:
            msl_f[msl_no] = True
            msl_x[msl_no] = ss_x
            msl_y[msl_no] = ss_y-50
            msl_a[msl_no] = 270
            msl_type[msl_no] = 2
            ss_muteki = 240
            bomb_tmr = 180
            msl_no = (msl_no+1)%MISSILE_MAX#自機の弾をセット
    
    def move_missile(scrn):
        for i in range(MISSILE_MAX):
            if msl_f[i] == True:
                if msl_type[i] == 1:#通常弾
                    msl_x[i] = msl_x[i] + 36*math.cos(math.radians(msl_a[i]))
                    msl_y[i] = msl_y[i] + 36*math.sin(math.radians(msl_a[i]))
    
                elif msl_type[i] == 2:#ボム
                    msl_x[i] = msl_x[i] + 36*math.cos(math.radians(msl_a[i]))
                    msl_y[i] = msl_y[i] + 3*math.sin(math.radians(msl_a[i]))
    
                if msl_type[i] == 1:
                    if msl_y[i] < 0 or msl_x[i] < 0 or msl_x[i] > 960:
                        msl_f[i] = False#弾の移動
    
                elif msl_type[i] == 2:
                    if msl_y[i] < -300 or msl_x[i] < -300 or msl_x[i] > 960:
                        msl_f[i] = False#弾の移動
    
    def bring_enemy():#敵を出すシナリオ
        global ss_x, ss_y, kakudo, stage_idx, idx
        sec = tmr/30
        if sec%1 == 0:
            set_enemy(334, LINE_T, 90, 0, 3, 3, 0)
    
    
    def set_enemy(x, y, a, ty, sp, sh, dec):
        global emy_no
        while True:
            if emy_f[emy_no] == False:
                emy_f[emy_no] = True
                emy_x[emy_no] = x         #敵のX
                emy_y[emy_no] = y         #敵のY
                emy_a[emy_no] = a         #敵の角度
                emy_type[emy_no] = ty     #敵のtype
                emy_speed[emy_no] = sp    #敵のspeed
                emy_shield[emy_no] = sh   #敵の体力
                emy_decay[emy_no] = dec
                emy_count[emy_no] = 0
                break
            emy_no = (emy_no+1)%ENEMY_MAX#敵機のセット
    
    def move_enemy(scrn):#敵機の移動
        global tmr, shot_ang, sec, idx, score, ss_shield, hisco, new_record, stage_idx, tamadashi, ss_x, ss_y, BOMB, switch
        for i in range(ENEMY_MAX):
            if emy_f[i] == True:
    
                if emy_type[i] == 0: #ヒットチェック
                    w = 100
                    h = 100
                    r = int((w+h)/4)+12
                    er = int((w+h)/4)
                    for n in range(MISSILE_MAX):
                        if msl_f[n] == True and get_dis(emy_x[i], emy_y[i], msl_x[n], msl_y[n]) < r*r:
                            msl_f[n] = False
                            if msl_type[i] == 1 and emy_count[i] != 2:
                                emy_shield[i] = emy_shield[i] - 1
                            if msl_type[i] == 2 and emy_count[i] != 2:
                                emy_shield[i] = emy_shield[i] - 10
                            score = score + 100
                            if score > hisco:
                                hisco = score
                                new_record = True
                            if emy_shield[i] == 0:
                                emy_f[i] = False
    
        for i in range(ENEMY_MAX):
            if emy_f[i] == True and tamadashi == True:
                if emy_x[i] < LINE_L or emy_x[i] > LINE_R or LINE_B < emy_y[i] or LINE_T > emy_y[i]:#敵が画面外に出たら消す
                    emy_f[i] = False
    
    def draw_text(scrn, txt, x, y, siz, col): #文字を表示する関数
        fnt = pygame.font.Font(None, siz)
        cr = int(col[0]/2)
        cg = int(col[0]/2)
        cb = int(col[0]/2)
        sur = fnt.render(txt, True, (cr, cg, cb))
        x = x - sur.get_width()/2
        y = y - sur.get_height()/2
        scrn.blit(sur, [x+1, y+1])
        cr = col[0]+128
        if cr > 255: cr = 255
        cg = col[1]+128
        if cg > 255: cg = 255
        cb= col[1]+128
        if cb > 255: cb = 255
        sur = fnt.render(txt, True, (cr, cg, cb))
        scrn.blit(sur, [x-1,y-1])
        sur = fnt.render(txt, True, col)
        scrn.blit(sur, [x,y])#文字を表示する関数
    
    def main():
        global idx, bg_y, tmr, score, ss_x, ss_y, ss_d, ss_shield, ss_muteki, new_record, bar_x, bar_y, tamadashi, stage_idx, hisco, BOMB
        global se_barrage, se_damage, se_explosion, se_shot
    
        pygame.init()
        pygame.display.set_caption("東方薬生郷")
        screen = pygame.display.set_mode((960, 720))
        clock = pygame.time.Clock()
        up = 0
        down = 0
        zoom_s = 1.0
        zoom_q = 1.0
    
        tmr_keep = 0
        continue_nokori = 3
        warning_count = 0
    
        while True:
            if score > hisco:
                                hisco = score
                                new_record = True
            tmr = tmr + 1
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
    
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE and idx == 1:
                        idx = 4
                        tmr = 0
    
            key = pygame.key.get_pressed()
    
            if idx == 0:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                            draw_text(screen, "START", 334, 300, 80, RED)
                            bar_y = 200
    
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_DOWN:
                            draw_text(screen, "QUIT", 334, 380, 80, RED)
                            bar_y = 450
                        
                if key[K_z] == 1 and bar_y == 200:
                    idx = 1
                    tmr = 0
                    score = 0
                    new_record = False
                    ss_x = 334
                    ss_y = 600
                    ss_d = 0
                    ss_shield = 5
                    BOMB = 3
                    ss_muteki = 0
                    for i in range(ENEMY_MAX):
                        emy_f[i] = False
                    for i in range(MISSILE_MAX):
                        msl_f[i] = False
    
                if key[K_z] == 1 and bar_y == 450:
                    pygame.quit()
                    sys.exit()
    
    
            if idx == 1:#ゲームプレイ中
                tmr_keep = tmr
                move_starship(screen, key)
                move_missile(screen)
                bring_enemy()
                move_enemy(screen)
                draw_text(screen, str(score), 750, 190, 50, (255, 255, 255))
                draw_text(screen, str(hisco), 750, 100, 50, (255, 255, 255))
    
            if idx == 1.25:#ゲームプレイ中(BOSS 会話)
                tmr_keep = tmr
                draw_text(screen, str(score), 750, 190, 50, (255, 255, 255))
                draw_text(screen, str(hisco), 750, 100, 50, (255, 255, 255))
    
            if idx == 2:#ゲームオーバー
                move_missile(screen)
                move_enemy(screen)
                draw_text(screen, str(score), 750, 190, 50, (255, 255, 255))
                draw_text(screen, str(hisco), 750, 100, 50, (255, 255, 255))
                if tmr > 120:
                    draw_text(screen, "GAME OVER", 334, 300, 80, RED)
                    if new_record == True:
                        draw_text(screen, "NEW RECORD "+str(hisco), 334, 400, 60, CYAN)
    
                if tmr >= 300:
                    draw_text(screen, "Press Z key to return to Title.", 334, 430, 60, SILVER)
                    if continue_nokori != 0:
                        draw_text(screen, "Press C key to continue.", 334, 480, 60, SILVER)
                        draw_text(screen, "NOKORI"+str(continue_nokori)+"KAI", 334, 530, 60, SILVER)
                    if key[K_z] == 1 and key[K_c] != 1:
                        idx = 0
                        tmr = 0
                        bar_y = 0
    
                    if key[K_c] == 1 and key[K_z] != 1 and continue_nokori > 0:
                        ss_shield = 3
                        idx = 1
                        tmr = tmr_keep
                        continue_nokori = continue_nokori - 1
                        tamadashi = True
                        ss_muteki = 180
                        BOMB = 3
                        ss_x = 344                     #自機のx座標
                        ss_y = 620                     #自機のy座標
    
            if idx == 3:#ゲームクリア
               
                draw_text(screen, str(score), 750, 190, 50, (255, 255, 255))
                draw_text(screen, str(hisco), 750, 100, 50, (255, 255, 255))
                if tmr > 120:
                    draw_text(screen, "GAME CLEAR", 334, 300, 80, SILVER)
                    if new_record == True:
                        draw_text(screen, "NEW RECORD "+str(hisco), 334, 400, 60, CYAN)
                    elif new_record == False:
                        draw_text(screen, "SCORE"+str(score), 334, 400, 60, (255, 255, 255))
                if tmr >= 300:
                    draw_text(screen, "Press Z key to return to Title.", 334, 440, 60, SILVER)
                    if key[K_z] == 1:
                        idx = 0
                        tmr = 0
                        bar_y = 0
                        stage_idx = 0
    
            if idx == 4:
                move_missile(screen)
                move_enemy(screen)
                draw_text(screen, str(score), 750, 190, 50, (255, 255, 255))
                draw_text(screen, str(hisco), 750, 100, 50, (255, 255, 255))
                if tmr >= 15:
                        draw_text(screen, "Press Z key to return to Title.", 334, 300, 60, SILVER)
                        draw_text(screen, "Press C key to continue.", 334, 365, 60, SILVER)
                        if key[K_z] == 1 and key[K_c] != 1:
                            idx = 0
                            stage_idx = 0
                            tmr = 0
                            tmr_keep = 0
                            bar_y = 0
                            score = 0
    
                        if key[K_c] == 1 and key[K_z] != 1:
                            idx = 1
                            tmr = tmr_keep
    
            pygame.display.update()
            clock.tick(60)#フレームレート
            
            #メインループ
    
    if __name__ == '__main__':
        main()
    
[/code]


CC BY-SA 4.0
2021-06-01T01:13:41.997
76236
2021-06-03T01:13:30.760
2021-06-03T01:12:16.570
3060
45588
question
python pygame pyinstaller
Pythonで書いたものをexe化したが，すぐに閉じてしまう．


272行目の記述を以下の通り変更したところ，無事に動作しました．

**修正前:**

[code]

    fnt = pygame.font.Font(None, siz)
    
[/code]

**修正後:**

[code]

    fnt = pygame.font.Sysfont("Arial", siz)
    
[/code]


CC BY-SA 4.0
2021-06-03T00:28:28.910
76284
2021-06-03T01:13:30.760
2021-06-03T01:13:30.760
3060
45588
76236
answer