gpt4 book ai didi

python - 当我想使用pyinstaller打包我的python脚本时,如果我想在我的脚本中使用 "pygame.font.SysFont",我该怎么办?

转载 作者:行者123 更新时间:2023-12-01 06:22:14 24 4
gpt4 key购买 nike

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

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

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

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()

最佳答案

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

关于python - 当我想使用pyinstaller打包我的python脚本时,如果我想在我的脚本中使用 "pygame.font.SysFont",我该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60307971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com