首页 > 解决方案 > 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

标签: pythonpygamecx-freeze

解决方案


sys.exit()所以我通过打字而不是打字解决了这个问题。quit() 我以前试过这个,但我不知道我必须这样做import sys

(对不起,我是初学者)


推荐阅读