【问题标题】:when I want to use pyinstaller to package my python script, what should I do if I want to use "pygame.font.SysFont" in my script?当我想使用pyinstaller打包我的python脚本时,如果我想在我的脚本中使用“pygame.font.SysFont”该怎么办?
【发布时间】:2020-02-19 19:37:05
【问题描述】:

我用 pygame 包制作了我的简单游戏,它在我的 python 编辑器上运行良好
所以我尝试通过pyinstaller
打包这个文件 结果,EXE文件构建成功。但是当我执行这个文件时,这个文件在打开后立即关闭。

于是我尝试调试我的脚本,最后发现问题是pygame.font.SysFont
当我没有使用pygame.font.SysFont并通过pyinstaller打包这个脚本时,我可以成功玩我的游戏
但是如果我尝试在我的脚本中至少使用一次pygame.font.SysFont 并通过 pyinstaller 打包此脚本,我将无法玩我的游戏,因为此文件(pyinstaller 的 exe 输出文件)在之后立即关闭开幕。

如果有人知道这个问题,请告诉我
谢谢

import sys
import os
import random
import pygame as pg
from pygame.locals import QUIT, Rect, KEYDOWN, K_SPACE, K_r


def main():

    game_over = False
    ship_y = 250
    velocity = 0
    score = 0
    slope = random.randint(1, 6)
    walls = 160
    holes = []
    for xpos in range(walls):
        holes.append(Rect(xpos * 5, 100, 5, 400)) 

    parent_path = os.getcwd()
    ship_img = pg.image.load(os.path.join(parent_path, "img\\ship.png"))
    bang_img = pg.image.load(os.path.join(parent_path, "img\\bang.png"))


    while True:
        space_down = False
        for event in pg.event.get():
            if event.type == QUIT:
                pg.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    space_down = True

        if not game_over:
            score += 10
            velocity += -3 if space_down else 3
            ship_y += velocity

            # Scroll map automatically
            edge = holes[-1].copy()
            test = edge.move(0, slope)
            if test.top <= 0 or test.bottom >= 600:
                slope = random.randint(1, 6) * (-1 if slope > 0 else 1)
                if edge.height >= 160:
                    edge.inflate_ip(0, -20)

            edge.move_ip(5, slope)
            holes.append(edge)
            del holes[0]
            holes = [x.move(-5, 0) for x in holes]

            # Check crash
            if holes[0].top > ship_y or holes[0].bottom < ship_y + 50:
                game_over = True

        SURFACE.fill((0, 255, 0))
        for hole in holes:
            pg.draw.rect(SURFACE, (0, 0, 0), hole)
        SURFACE.blit(ship_img, (0, ship_y))

        score_board = SYSFONT.render(f"score : {score}", True, (0, 0, 225)) # I have to comment out this line
        SURFACE.blit(score_board, (600, 20))

        if game_over:

            end_msg = SYSFONT.render(f"press R to restart", True, (255, 255, 255)) # I have to comment out this line
            cal_level = int((400 - holes[0].height) / 20)
            level = SYSFONT.render(f"level : {cal_level}", True, (255, 0, 0)) # I have to comment out this line
            SURFACE.blit(end_msg, (320, 200))
            SURFACE.blit(level, (370, 240))
            SURFACE.blit(bang_img, (0, ship_y - 40))

            pg.display.update()

            while True:
                for event in pg.event.get():
                    if event.type == KEYDOWN:
                        if event.key == K_r:
                            game_over = False

                if not game_over:
                    ship_y = 250
                    velocity = 0
                    score = 0
                    slope = random.randint(1, 6)
                    holes = []
                    for xpos in range(walls):
                        holes.append(Rect(xpos * 5, 100, 5, 400))
                    break


        pg.display.update()
        FPS.tick(30)


if __name__ == "__main__":
    pg.init()
    pg.key.set_repeat(5, 5)
    SURFACE = pg.display.set_mode((800, 600))
    FPS = pg.time.Clock()
    SYSFONT = pg.font.SysFont(None, 36)   # I have to comment out this line

    main()

【问题讨论】:

  • 编译时不要执行 --windowed 并查看控制台中的错误消息。如果它说它有任何丢失的文件或模块,您将它们复制为 --data-file 或 --hidden-import

标签: python pygame pyinstaller


【解决方案1】:

我自己找到了解决方案
pygame.font.SysFont(None, 36, bold=True)
中的问题是 pyinstaller 在 pygame 中找不到默认系统字体
所以我指定了我的字体类型,比如 Calibri,之后效果很好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多