PyInstaller将Pygame库编写的游戏程序打包为exe要点

PyInstaller将Pygame库编写的游戏程序打包为exe要点引子最近带领我的学生们制作一款打字练习游戏,这样他们可以一边学习Python一边用自己制作游戏锻炼指法。一箭双雕!为了方便分享给没有Python

大家好,欢迎来到IT知识分享网。

引子

最近带领我的学生们制作一款打字练习游戏,这样他们可以一边学习Python一边用自己制作游戏锻炼指法。一箭双雕!为了方便分享给没有Python运行环境的同学,最好还是把它打包为可独立执行的exe文件。

What is PyInstaller?

PyInstaller是将python文件打包为可执行文件的一套技术,主要作用就是将python脚本,python运行环境以及所需要动态连接库组装成可执行文件。

安装及使用

pip install pyinstaller pyinstaller /path/to/yourscript.py 具体的使用方法网上可以找到很多教程。本文接下来主要讲一下我遇到的疑难杂症,以及处理方法。

打包成功,遇到Fatal Error怎么办?

运行后,提示Fatal Error。这种情况下,不要方,在打包命令行里加入 –debug all 然后重新打包后启动,可以观察到错误提示,只要知道问题所在,解决问题指日可待。

打包Pygame程序三点坑点

1.Pyinstaller没有那么智能,你使用的库它有可能并没有加入到可执行文件中。当你发现你使用的库没有打包到可执行文件中时,可以在打包命令行添加 –hidden-import 例如,假设缺少pygame库,那么在命令行中加入–hidden-import pygame 即可。

2.使用pygame的music功能播放mp3或者ogg文件可导致程序报fatal error。这个原因是PyInstaller并没有打包处理mp3和ogg文件的两个动态链接库文件,在windows平台它们是libmpg123.dlllibogg.dll。如何解决呢?可以修改PyInstaller/hooks/hook-pygame.py文件扩展打包流程,将这些dll文件打包进去。

#----------------------------------------------------------------------------- # Copyright (c) 2013-2020, PyInstaller Development Team. # # Distributed under the terms of the GNU General Public License (version 2 # or later) with exception for distributing the bootloader. # # The full license is in the file COPYING.txt, distributed with this software. # # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception) #----------------------------------------------------------------------------- """ Hook for pygame._view, required for develop releases between 2011-02-08 and 2011-08-31, including prebuilt-pygame1.9.2a0 """ hiddenimports = ['pygame._view'] """ binaries hook for pygame seems to be required for pygame 2.0. Otherwise some essential DLLs will not be transfered to the exe. """ import platform if platform.system() == "Windows": from PyInstaller.utils.hooks import collect_dynamic_libs pre_binaries = collect_dynamic_libs('pygame') binaries = [] for b in pre_binaries: binary, location = b # settles all the DLLs into the top level folder, which prevents duplication # with the DLLs already being put there. binaries.append((binary, "."))

具体可参考https://github.com/pygame/pygame/issues/1514

3.如果python代码使用了外部资源如图片或者音频,也需要特别注意路径问题。由于使用Pyinstaller打包后资源文件路径会发生变换,需要代码进行处理。

import sys import os # If the code is frozen, use this path: if getattr(sys, 'frozen', False): CurrentPath = sys._MEIPASS # If it's not use the path we're on now else: CurrentPath = os.path.dirname(__file__) # Look for the 'sprites' folder on the path I just gave you: spriteFolderPath = os.path.join(CurrentPath, 'sprites') # From the folder you just opened, load the image file 'some_image.png' some_image = pygame.image.load(path.join(spriteFolderPath, 'some_image.png'))

具体可参考https://stackoverflow.com/questions//pygame-not-loading-png-after-making-exe-with-pyinstaller

视频展示

视频加载中…

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/48663.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信