python - NameError:没有为pygame定义名称“退出”
问题描述
我尝试制作一个小蛇游戏(使用 pygame),并尝试使用 cx_Freeze 将其导出为 .exe。游戏运行良好,直到我关闭窗口。当我这样做时,会出现以下错误消息:
但我只是用它来关闭游戏循环:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
if event.key == pygame.K_SPACE:
Game()
如果我打开 .py 文件,我不会收到任何错误消息。我不知道如何解决这个问题。
我的 setup.py:
import cx_Freeze
executables = [cx_Freeze.Executable("snake.py", base="Win32GUI", icon="links/icon.ico")]
cx_Freeze.setup(
name="Snake",
options={"build_exe": {"packages": ["pygame"], "include_files": ["links/"]}},
executables=executables
)
这是我的代码:Codeshare.io
解决方案
sys.exit()
所以我通过打字而不是打字解决了这个问题。quit()
我以前试过这个,但我不知道我必须这样做import sys
。
(对不起,我是初学者)
推荐阅读
- c# - 使用自定义 get/set 时出现 NullReferenceException
- python - 站点无法通过自动化正确响应(python3 selenium)
- docker - 构建失败时的 Docker 挂载类型绑定
- java - 如何从 tflite 模型输出形状 [1, 28, 28,1] 的数组作为 android 中的图像
- android - Android Room 如何复制现有 POJO 实体并使用 DAO 将副本保存在数据库中
- flutter - Flutter- 从 Firestore 获取数据并在 FormBuilderDropdown 中使用它
- javascript - 通过给定的索引,角度将选定的对象从数组中移动到数组中的位置
- typescript - Formik 中的“onSubmit”函数有什么问题?
- java - 如何在java中打印带两位小数和不带小数的浮点值
- amazon-web-services - 使用 cloudwatch 代理自动扩展内存利用率