python - 简单的 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``
解决方案
我已经设法让它发挥作用。问题出在字体上。改变你的线路
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
希望这有效。
推荐阅读
- ruby-on-rails - Rails 5.2:是否可以从这些嵌套路由中删除第一个资源名称?
- django - Django FormView在success_url中传递pk
- airflow - Airflow 为什么调度程序不启动我的 DAG?
- android - AlarmManager 重复报警并不总是调用 BroadcastReceiver
- python - 如何使用熊猫从 Excel 文件中删除重复项
- reactjs - 值未更新或添加到 Redux 状态
- .net - 如何在 .NET Core 中生成对 CSR 的响应(即编写 CSR 签名服务器)?
- ios - 在没有导航控制器的情况下关闭情节提要
- python-3.x - 使用 python 在 AWS 上删除 DB GLUE
- javascript - 正则表达式将所有最外面的括号对匹配到数组中