【问题标题】:NameError: name 'quit' is not defined for pygameNameError:没有为pygame定义名称“退出”
【发布时间】:2020-10-05 13:13:35
【问题描述】:

我尝试制作一个小蛇游戏(使用 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

【问题讨论】:

  • 不要发布指向您的代码的链接。 (Codeshare.io 似乎没有自动启动)。贴出调用quit()的相关代码。
  • 您在程序中未定义的代码笔上的第 42 行调用 pygame.quit() 下的函数 quit()。创建函数或删除行。
  • quit 在冻结时对我来说也总是失败。请改用sys.exit()
  • quit() 已经是一个定义的函数,pygame.quit() 也是。如果我删除其中一个,只会产生更多错误或错误...
  • for 循环之前是否有一个while 循环运行?

标签: python pygame cx-freeze


【解决方案1】:

所以我通过输入 sys.exit() 而不是输入 quit() 解决了这个问题 我之前试过这个,但我不知道我必须import sys

(对不起,我是初学者)

【讨论】:

    猜你喜欢
    • 2017-12-17
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多