python - 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”消息。
解决方案
推荐阅读
- animation - IMG 标签中的动画 SVG 精灵在水平方向振动
- azure-devops - 在 Azure DevOps 中,有没有办法隐藏自定义工作项类型中的迭代和区域字段?
- bash - 如何在 /etc/ssh/sshrc 中使用 STDOUT 而不会破坏 SCP
- php - 如何在php pdo中连接到远程数据库
- ansible - 为 Ansible google.cloud.compute_instance 指定子网
- spring-boot - Spring Boot CORS 问题
- ios - 如何在同一个应用中创建多个 IAP?
- javascript - Jquery:未捕获的类型错误:无法读取未定义的属性“解码”
- python - Django - 渲染模板并通过 AJAX 传递它们
- nativescript - 在单个组件中使用 Router for web 和 RouterExtensions for mobile