PyInstaller的.exe文件在打开后立即关闭

时间:2020-03-21 20:07:34

标签: python pygame pyinstaller

我使用python的pygame库创建了一个基本的反应游戏。有一个指向我的github的链接,因此您可以查看代码。正如标题指出的那样,每当我单击.exe文件时,它只会创建一个窗口,然后将其关闭。根据我的检查,没有什么可以阻止程序在代码中的流动,并且问题一定出在pygame中。我重新安装了pygame,但是结果是一样的。启动.exe文件时,控制台屏幕上不会弹出错误消息。我寻找了类似的问题,但找不到答案。我使用以下命令创建.exe文件: pyinstaller game.py --onefile

存储库链接-https://github.com/Viktor-stefanov/Reaction-Game

1 个答案:

答案 0 :(得分:2)

所以我仔细检查并使用print语句来查找错误。

该错误发生在第103行: font = pygame.font.SysFont('comicsans', 38, True)

这是由于'comicsans'字体不是典型字体。

'comicsans'的三个实例更改为'comicsansms'并将其固定在我的计算机上,我使用pyinstaller.exe --onefile "path/to/script.py"构建了它

您可能需要考虑使用pyinstaller打包字体,或者明确包含pygame的字体文件以提高兼容性。

正如您在该软件包的未解决问题中所看到的,引用的pyinstaller不包括pygame的字体文件“ PyInstaller不会使用找到的导入来拉入数据文件”: https://github.com/lordmauve/pgzero/issues/59

不是解决方案:

如果您对我的工作感兴趣,请在.exe中构建它并通过命令提示符运行它:

(您将看到它在“ here13”处停止)

import pygame
from random import choice, randint

pygame.init()

s_width, s_height = 600, 700
win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Reaction Speed Game')

print("here")

class square():
    def __init__(self, x, y, width, height, color):
        print("here2")
        super().__init__()
        self.x = x
        self.y = y
        self.color = color
        self.width = width
        self.height = height

        self.image = pygame.Surface([width, height])
        self.image.fill((0, 0, 0))

        self.rect = pygame.Rect(x, y, width, height)

    def clicked(self):
        print("here3")
        self.width = randint(50, 80)
        self.height = self.width
        self.x, self.y = randint(1, s_width-self.width), randint(1, s_height - self.height)
        self.color = choice(square_colors)
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def draw(self, surface):
        print("here4")
        pygame.draw.rect(surface, self.color, (self.x, self.y, self.width, self.height))



def renderText(time):
    print("here5")
    global dest
    font = pygame.font.SysFont('comicsans', 32)
    if time < 325:
        text = font.render('Legendary!', 0, (255, 255, 255))
    elif time < 450:
        text = font.render('Epic!', 0, (255, 255, 255))
    elif time < 600:
        text = font.render('Great!', 0, (255, 255 ,255))
    else:
        text = font.render('Good!', 0, (255, 255, 255))
    dest = s.x, s.y + s.height // 2
    return text


def redrawWindow(surface, clickMessage=False):
    print("here6")
    surface.fill((0, 0, 0))
    s.draw(surface)
    if clickMessage:
        surface.blit(text, dest)
    pygame.display.update()


def main():
    print("here7")
    global s, square_colors, text
    square_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
    sx, sy = s_width // 2 - 32, s_height // 2 - 32
    sw, sh = 50, 50
    s = square(sx, sy, sw, sh, choice(square_colors))
    start_time = pygame.time.get_ticks()
    time = 0
    message = False
    flag = True
    while flag:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                flag = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                if s.rect.collidepoint(pos):
                    time = pygame.time.get_ticks() - start_time
                    text = renderText(time)
                    start_time = pygame.time.get_ticks()
                    s.clicked()
                    message = True

        if pygame.time.get_ticks() - start_time > 800:
            message = False
        redrawWindow(win, message)


    pygame.display.quit()


def menu():
    print("here8")
    flag = True
    while flag:
        win.fill((0, 0, 0))

        for event in pygame.event.get():
            print("here10")
            if event.type == pygame.QUIT:
                print("here11")
                flag = False

        print("here12")
        start_time = pygame.time.get_ticks()
        if start_time <= 5000:
            print("here13")
            font = pygame.font.SysFont('comicsans', 38, True)
            print("here17")
            text = font.render('Click on the squares as fast as you can!', 0, (255, 255, 255))
            print("here18")
            win.blit(text, (12, 250))
            print("here19")
        else:
            print("here14")
            font = pygame.font.SysFont('comicsans', 46, True)
            if start_time <= 6000:
                text = font.render('3', 0, (255, 255, 255))
            elif start_time <= 7000:
                text = font.render('2', 0, (255, 255, 255))
            elif start_time <= 8000:
                text = font.render('1', 0, (255, 255, 255))
            else:
                flag = False
                main()

            print("here15")
            win.blit(text, (s_width // 2 - 10, 250))
            print("here16")

        pygame.display.update()

    pygame.display.quit()



if __name__ == '__main__':
    print("here9")
    menu()