首页 > 解决方案 > 简单的 Pong 游戏不会作为 .exe 运行

问题描述

我是这个论坛的新手,python 经验有限。自从这次隔离以来,我决定重新开始学习 python。在过去的几天里,我使用 curses 和 pygame 制作了一些工作游戏。这些游戏在我的电脑上正常运行,但我想将它们转换为 .exe 格式。我已经使用 pyinstaller 来创建 .exe,但是一旦您尝试打开 .exe 文件,我的乒乓球游戏(使用 pygame)就会崩溃。我假设它可能与 pygame 的导入有关。任何帮助将非常感激。

这是我在 GitHub 上的代码的链接: https ://github.com/nick-cheshire/PythonGames-/blob/master/Pong.py

感谢@TheBigKahuna,我能够创建一个 .bat 文件并运行它,这是我收到的错误:

``Traceback (most recent call last):
  File "Pong.py", line 137, in <module>
    pong()
  File "Pong.py", line 90, in pong
    game_font = pygame.font.Font("freesansbold.ttf", 32)
  File "site-packages\pygame\pkgdata.py", line 50, in getResource
  File "site-packages\pkg_resources\__init__.py", line 1134, in resource_exists
  File "site-packages\pkg_resources\__init__.py", line 1404, in has_resource
  File "site-packages\pkg_resources\__init__.py", line 1472, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
[18912] Failed to execute script Pong``

标签: pythonpygamepyinstaller

解决方案


我已经设法让它发挥作用。问题出在字体上。改变你的线路

game_font = pygame.font.Font("freesansbold.ttf", 32)

game_font = pygame.font.SysFont("Arial", 20)

我认为问题在于定位文件。如果我错了,请纠正。现在,当使用 pyinstaller 制作 .exe 时,请确保您的文件已命名main.py,并且由于您正在构建 GUI,因此您将需要 -w(不带控制台)参数。因此,我建议以管理员身份运行 powershell,导航到您的 main.py 并:

pyinstaller --onefile -w main.py

希望这有效。


推荐阅读