如何将pygame转换为exe? [英] How can I convert pygame to exe?

查看:132
本文介绍了如何将pygame转换为exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我做了一个 pygame 游戏.我想把它展示给我的朋友们.但不幸的是,我无法制作可执行文件,只能制作 .py 文件.我曾尝试使用诸如 py2exe 之类的工具,这些工具可能不支持 Python 3.6 和 pyinstaller.我认为 pyinstaller 可能很有用,我也可以成功制作一个 exe,但它根本不起作用.它只是显示它无法打开已经在同一个文件中的声音文件.

Recently, I made a pygame game. I want to show it to my friends. But unfortunately, I cant make an executable file, only a .py file. I have tried to use tools such as py2exe which might not support Python 3.6 and pyinstaller. I think pyinstaller may be useful, and I can also make an exe successfully but it didn't work at all. It just showed it couldn't open the sound file which is already in the same file.

我尝试了两种加载文件的方法,先加载路径,然后exe转换后显示无法打开与exe在同一路径下的文件.

I have tried two ways to load files firstly, just load the path then the exe converted shows it can't open the file which is already in the same path with the exe.

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

Secondly, I use the os.path which the exe showed it cant open ...../temp/... which is a long path but my computer doesn't have this folder at all.

我是一个学习python的新手.我已经搜索了一天,但无法使其工作.你能帮我吗?

I am a new guy to learn python. I have searched for a day and can't make it work. Can you help me?

推荐答案

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

I'm just a scripting hack, and an idiot when it comes to command line tools. But I REALLY wanted to package up my python game into a Windows executable. I wrestled with pyinstaller for days, and read all of the threads of others who are also trying to figure it out, and I also read all the pyinstaller docs several times. No one solution worked for me, but after much trial and error I finally stumbled onto a recipe for success and want to share it in case there are other pygame developers out there banging their heads against this. The solution contains bits and pieces of several threads.

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

How to create a single file exe of a multi-file python pygame with several asset directories using pyinstaller on Windows:

首先,安装pyinstaller.

First, install pyinstaller.

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

Open Windows Command Prompt, type:

pip install pyinstaller

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

Make a python game. Say the main game file is called main_game_script.py, located in

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

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

Add this function to main_game_script.py. Make sure to import os and sys. (I never would have figured out on my own that I needed to add this function to my game, but it works, so thanks to whoever posted it in some other thread)

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)

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

To load images into game, call that function, like such:

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

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

When your game is ready to be wrapped in an EXE, open Windows Command Prompt. Go to the main game directory and type:

pyinstaller --onefile main_game_script.py

看起来像这样:

C:\Users\USERNAME\Documents\python\game_dir>pyinstaller --onefile main_game_script.py

这将创建四个重要项目:

This will create four important items:

- 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 中)以将所需的游戏资产路径添加到 exe:

Running the command spits out a ton of warnings and logs. I don't know what it all means. But as long as the final output line says success, you're probably good. Your exe will not work yet. First, you must modify the .spec file (which now exists in the game_dir) to add the needed game asset paths to the exe:

用记事本或其他方式打开 main_game_script.spec 文件.用这样的资产目录路径替换空的 datas[] 列表(使用元组!):

Open the main_game_script.spec file in notepad or whatever. Replace the empty datas[] list with asset directory paths like such (using tuples!):

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

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

The first value of each tuple is the actual file names you want to import. The second value is the relative path from you main_game_script.py The syntax and paths must be perfect, or it won't work and may not throw an error

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

Save and close the 'main_game_script.spec' file. Return to your windows command prompt. Type:

pyinstaller main_game_script.spec

像这样:

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

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

This somehow embeds the asset dir paths into the exe that you've already built

做完这一切(完成 100 次后只需要几分钟),您终于拥有了包含所有游戏资产的 Python 游戏的单个 EXE 文件.如果一切顺利,请双击 EXE 来玩您的 Python 游戏.

After all this (which only takes a couple minutes after you've done it 100 times), you finally have a single file EXE of your python game that includes all of the game assets. If you've done everything right, double click on the EXE to play your python game.

注意:第一次把它做好花了我几天的时间.我从一个带有最少代码的简单 test_game_script.py 开始,并有条不紊地进行迭代,在每次迭代中添加更多资产路径和复杂性,以便我可以隔离出问题所在以及原因.如果您的游戏无法正确转换为 EXE,请从一个简单的 5 行游戏脚本开始,并在其上慢慢构建,直到您拥有一个可以工作的管道,然后将该功能正常的资产管道应用到您的实际游戏中.否则将无法隔离问题.

Note: getting it right the first time took me a couple days. I started with a simple test_game_script.py with minimal code and iterated methodically, adding more asset paths and complexity with each iteration so I could isolate what was broken and why. If your game isn't converting to EXE properly, start with a simple 5 line game script with one asset and slowly build on it until you have a pipeline that works, THEN apply that functioning asset pipeline your ACTUAL game. Otherwise isolating the problem will be impossible.

这篇关于如何将pygame转换为exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆