首页 > 解决方案 > Pygame 与 cx_Freeze 一起使用,但当我运行可执行文件时,窗口中没有显示任何内容

问题描述

游戏.py:

import pygame

pygame.init()
if pygame.get_init():
  print("pygame successfully initialized")
else:
  print("pygame failed to initialize")

background_color = (255,0,0)
(width, height) = (800, 800)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Game')

x = int(width / 2)
y = int(height / 2)
speed = 10


running = True
while running:
  screen.fill(background_color)

  # drawing
  pygame.draw.circle(screen, (0,0,255), (x, y), 50)

  #input
  keys = pygame.key.get_pressed()
  if keys[97]:
    x -= speed
  elif keys[100]:
    x += speed
  if keys[119]:
    y -= speed
  elif keys[115]:
    y += speed

  pygame.display.flip()

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

设置.py:

from cx_Freeze import setup, Executable

includefiles = ['PNG']
includes = []
excludes = []
packages = []

setup(
    name = 'game',
    version = '0.1',
    description = 'IDk',
    options = {'build_exe': {'includes':includes,'excludes':excludes,'packages':packages,'include_files':includefiles}}, 
    executables = [Executable("game.py")]
)

当我运行 setup.py 脚本时,它会生成一个可以运行的可执行文件,但是,它只会创建一个空的 pygame 窗口。如果我在终端中使用“python game.py”运行脚本,它可以正常工作。我见过很多人无法使用 cx_Freeze 运行 pygame 程序,解决方法是添加import re,但这并不能解决我的问题。我也没有收到任何错误,终端弹出并显示通常的“欢迎使用 pygame”消息。

标签: pythonpygamecx-freeze

解决方案


推荐阅读