python - 如何将pygame转换为exe?

标签 python pygame

最近做了一个pygame游戏。我想把它展示给我的 friend 们。但不幸的是,我不能制作一个可执行文件,只有一个 .py 文件。我曾尝试使用可能不支持 Python 3.6 和 pyinstaller 的工具,例如 py2exe。我觉得pyinstaller可能会有用,我也可以成功制作一个exe,但根本没有用。它只是显示它无法打开已经在同一个文件中的声音文件。

我试过两种加载文件的方式,先加载路径,然后exe转换后显示打不开已经和exe同路径的文件。

其次,我使用 os.path exe 显示它无法打开 ...../temp/... 这是一条很长的路径,但我的电脑根本没有这个文件夹。

我是学习python的新手。我已经搜索了一天,但无法正常工作。你能帮帮我吗?

最佳答案

我只是一个脚本黑客,在命令行工具方面是个白痴。但我真的很想将我的 python 游戏打包成 Windows 可执行文件。我与 pyinstaller 搏斗了好几天,并阅读了其他也在尝试弄清楚的所有线程,我还多次阅读了所有 pyinstaller 文档。没有一个解决方案适合我,但经过多次试验和错误后,我终于偶然发现了成功的秘诀,并想分享它,以防其他 pygame 开发人员反对这一点。该解决方案包含多个线程的点点滴滴。

如何在 Windows 上使用 pyinstaller 创建具有多个 Assets 目录的多文件 python pygame 的单个文件 exe:

首先,安装pyinstaller。

打开 Windows 命令提示符,键入:

pip install pyinstaller

制作一个 python 游戏。假设主游戏文件名为 main_game_script.py,位于

C:\Users\USERNAME\Documents\python\game_dir

将此函数添加到 main_game_script.py。确保导入 os 和 sys。 (我永远不会自己想出我需要将此功能添加到我的游戏中,但它有效,所以感谢在其他线程中发布它的人)

import sys
import os

def resource_path(relative_path):
    try:
    # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

要将图像加载到游戏中,请调用该函数,如下所示:

asset_url = resource_path('assets/chars/hero.png')
hero_asset = pygame.image.load(asset_url)

当您的游戏准备好打包到 EXE 中时,打开 Windows 命令提示符。转到主游戏目录并输入:

pyinstaller --onefile main_game_script.py --collect-data assets/chars --collect-data assets/tiles --collect-data assets/fonts

它看起来像这样:

C:\Users\USERNAME\Documents\python\game_dir>pyinstaller --onefile main_game_script.py  --collect-data assets/chars --collect-data assets/tiles --collect-data assets/fonts

这将创建四个重要项目:

- a main_game_script.spec file in the game_dir
- a 'build' dir in the game_dir
- a 'dist' directory in the game_dir
- a main_game_script.exe will be created in the 'dist' directory.

运行该命令会吐出大量警告和日志。我不知道这一切意味着什么。但只要最终输出行显示成功,您就可能做得很好。

如果您的 exe 仍然无法运行:首先,您必须修改 .spec 文件(现在存在于 game_dir 中)以将所需的游戏 Assets 路径添加到 exe:

在记事本或其他工具中打开 main_game_script.spec 文件。 将空的 datas[] 列表替换为这样的 Assets 目录路径(使用元组!):

datas=[('assets/chars/*','assets/chars'),('assets/tiles/*.png','assets/tiles'),('assets/fonts/*','assets/fonts')],

每个元组的第一个值是您要导入的实际文件名。 第二个值是你 main_game_script.py 的相对路径 语法和路径必须是完美的,否则将无法运行并且可能不会抛出错误

保存并关闭“main_game_script.spec”文件。返回到您的 Windows 命令提示符。类型:

pyinstaller main_game_script.spec

像这样:

C:\Users\USERNAME\Documents\python\game_dir>pyinstaller main_game_script.spec

这以某种方式将 Assets 目录路径嵌入到您已经构建的 exe 中

完成这一切之后(完成 100 次后只需要几分钟),您终于拥有了包含所有游戏 Assets 的 Python 游戏的单个 EXE 文件。如果一切正常,请双击 EXE 来玩您的 Python 游戏。

注意:第一次做对花了我几天的时间。我从一个简单的 test_game_script.py 开始,使用最少的代码并有条不紊地进行迭代,在每次迭代中添加更多的 Assets 路径和复杂性,这样我就可以找出损坏的地方和原因。如果您的游戏没有正确转换为 EXE,请从一个简单的 5 行游戏脚本和一个 Assets 开始,然后慢慢构建它,直到您拥有一个可用的管道,然后将该功能 Assets 管道应用到您的实际游戏中。否则将无法隔离问题。

关于python - 如何将pygame转换为exe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54210392/

相关文章:

python - 在 Pygame 中绘制箭头

python - pygame中的重力

python - Pygame 和 Sprite 的旋转

python - pygame.draw.rect 和 screen_surface.blit() 有什么区别?

python - 当移动物体偏离路径特定幅度时如何发出警告?

python - 是否可以在 Tkinter 中呈现 HTML?

python - 嵌套列表理解的奇怪行为

python - 为什么小写 'p' 大于大写 'P' ?

python - 贪婪算法将数字列表的列表分成两个分区,每个分区在 Python 中的数量相同

python - 使用 ComputedProperty 安全吗?