python - 在使用 pyinstaller 制作的可执行文件中获取运行时错误

标签 python pygame pyinstaller

所以我使用 Pygame 制作了一个游戏,现在我想用它制作一个可执行文件(首选独立可执行文件)。所以我用它来制作可执行文件:

pyinstaller --onefile main.py

编译顺利,但运行时出现错误。这是错误:

Traceback (most recent call last):
  File "main.py", line 15, in <module>
TypeError: expected str, bytes or os.PathLike object, not BytesIO
[5604] Failed to execute script 'main' due to unhandled exception!

我有一些与 pygame 一起使用的图像和声音文件,我想它们是造成问题的原因。 这是我的 main.py 文件。:

import pygame as pg
import random
import time
import pygame.time
from pygame import mixer

pg.init()

pg.display.set_caption('Space Invaders')
icon = pg.image.load('001-ufo.png')
pg.display.set_icon(icon)
mixer.music.load('Tetris.mp3')
mixer.music.play(-1)
kills = 0
font = pg.font.Font('freesansbold.ttf', 34)


def Score():
    global kills
    score = font.render(f'Score: {kills}', True, (255, 000, 000))
    screen.blit(score, (10, 10))


class Vector:
    def __init__(self):
        self.x = None
        self.y = None


class Bullet(pg.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pg.image.load('bullet.png')
        self.rect = self.image.get_rect()
        self.position = Vector()
        self.velocity = Vector()
        self.position.x = x
        self.position.y = y
        self.velocity.x = 0
        self.velocity.y = 5
        self.rect.center = [self.position.x, self.position.y]
        self.fire_sound = mixer.Sound('rocket_sound.wav')
        self.explosion_sound = mixer.Sound('explosion.wav')

    def update(self):
        self.position.y = self.position.y - self.velocity.y
        if self.position.y < -16:
            self.kill()
        self.rect.center = [self.position.x, self.position.y]
        new = pg.sprite.groupcollide(bullet_group, enemy_group, True, True)
        if new != {}:
            enemy = Enemy()
            enemy_group.add(enemy)
            self.explosion_sound.play()


class Player(pg.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pg.image.load('player.png')
        self.rect = self.image.get_rect()
        self.position = Vector()
        self.velocity = Vector()
        self.position.x = 650
        self.position.y = 600
        self.velocity.x = 0
        self.velocity.y = 0
        self.rect.center = [self.position.x, self.position.y]

    def update(self):
        self.position.x = self.position.x + self.velocity.x
        if self.position.x+32 > 1300:
            self.velocity.x = 0
            self.position.x = 1300-32
        if self.position.x-32 < 0:
            self.velocity.x = 0
            self.position.x = 0+32
        self.rect.center = [self.position.x, self.position.y]

    def create_bullet(self):
        return Bullet(self.position.x, self.position.y)


class Enemy(pg.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.position = Vector()
        self.velocity = Vector()
        self.image = pg.image.load('001-ufo.png')
        self.rect = self.image.get_rect()
        self.position.y = random.randint(32, 368)
        self.position.x = random.randint(32, 1284)
        self.velocity.x = 5
        self.velocity.y = 0
        self.rect.center = [self.position.x, self.position.y]

    def update(self):
        self.position.x = self.position.x + self.velocity.x
        if self.position.x > 1284:
            self.position.x = 1284
            self.velocity.x = -5
            self.position.y += 32
        if self.position.x < 16:
            self.position.x = 16
            self.velocity.x = 5
            self.position.y += 32
        if self.position.y > 600-16:
            quit(0)
        self.rect.center = [self.position.x, self.position.y]


p1 = Player()
p_g = pg.sprite.Group()
p_g.add(p1)

bullet_group = pg.sprite.Group()

enemy_group = pg.sprite.Group()
for i in range(20):
    enemy = Enemy()
    enemy_group.add(enemy)

img = pg.image.load('player.png')
screen = pg.display.set_mode((1300, 700))
clock = pygame.time.Clock()
prev_time = time.time()
running = True
while running:
    screen.fill((0, 255, 0))
    for event in pg.event.get():
        if event.type == pg.QUIT:
            quit(0)
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_LEFT:
                p1.velocity.x = -10
            if event.key == pg.K_RIGHT:
                p1.velocity.x = 10
            if event.key == pg.K_SPACE:
                now_time = time.time()
                if now_time - prev_time > 0.5:
                    bullet = p1.create_bullet()
                    bullet_group.add(bullet)
                    bullet.fire_sound.play()
                    prev_time = now_time
        if event.type == pg.KEYUP:
            if event.key == pg.K_LEFT or event.key == pg.K_RIGHT:
                p1.velocity.x = 0

    p_g.update()
    p_g.draw(screen)

    bullet_group.update()
    bullet_group.draw(screen)
    enemy_group.update()
    enemy_group.draw(screen)
    pg.display.update()
    clock.tick(60)

这些是我的图像和声音文件。 (忽略 mainbak.py 它只是一个备份文件) enter image description here

附言我的游戏在 python 中运行良好。问题仅在于可执行文件。

最佳答案

这是 pygame 中的一个错误。

报告于 https://github.com/pygame/pygame/issues/2603 , 固定在 https://github.com/pygame/pygame/pull/2604

这将在 pygame 2.0.2 中修复,无论何时出现。

编辑:onefile 可执行文件仍然损坏,应由 https://github.com/pygame/pygame/pull/2911 修复, 在 pygame 2.1.3.dev2 中可用。但是,由于完全不同的原因 ( https://github.com/pygame/pygame/issues/3070 ),它在同一个版本中再次崩溃。因此,在撰写本文时,没有当前版本适用于此。

为了缓解这种情况,您必须在您的项目中包含一个字体文件,而不是依赖于 pygame 默认字体。许多 ttf 文件可从 Google Fonts 下载。

关于python - 在使用 pyinstaller 制作的可执行文件中获取运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68737639/

相关文章:

python - 使用 XlsxWriter 将文本框或文本框中的文本定向为垂直?

python - 使用 PyInstaller got pyodbc 编译后如何修复启动时的错误?

Python类找不到成员

python - pygame 屏幕仅在我将窗口移出屏幕时才起作用

python - pygame.mixer.Sound - 无法打开文件

python - 橙色仅随机出现在屏幕 python 的右侧

python - 从 pyinstaller 制作的外部 exe 文件调用时出现未知命令 "syncdb"

python - 如何将 pyinstaller 与隐藏导入一起用于 scipy.optimize leastsq

python - 软标签上的 scikit-learn 分类

python - (unittest) 测试 Flask-Security : Cannot get past login page