python+pygame游戏开发之使用Py2exe打包游戏

最近在用python+pygame 开发游戏,写完以后在分享给朋友玩的时候遇到了很大的问题,只有搭建了环境才能运行python脚本。

这会吓退99%以上的人……所以把我们的游戏打包(注意是打包而不是编译,python毕竟是脚本程序)成一个可执行文件势在必行。

在网上搜了几款关于python打包的程序,使用以后发现Py2exe是比较实用和方便的(py2exe是免费的)。

 

1.简介

Py2exe可以将一个Python程序打包成exe可执行文件,方便没有Python环境的电脑上使用程序。使用Py2exe打包需要编写一个打包脚本,执行后可以得到打包文件。对于32位版本,Py2exe可以将程序打包成单文件;对于64位版本,暂时还不支持打包成单文件。不过,无论如何压缩,目前Py2exe打包出来的程序都还是偏大的。

 

2.软件准备

首先到py2exe的官网http://www.py2exe.org/ 去下载安装包,注意需要对应自己的python版本,要不然会出现问题。

双击exe文件,一路next下去即可。需要注意的是,32位版本和64位版本必须和Python使用的版本配套。

 

3.打包流程

py2exe是需要写一个脚本进行打包的操作,使用下面这个专为pygame写就的脚本(参考py2exe官方),可以极大的方便打包操作,注意在使用前修改BuildExe里的各个参数。

在这里非常感谢“xishui”大神提供的脚本。这个脚本实在是太棒了!!!

#!python
# -*- coding: gb2312 -*-

# 这个脚本专为pygame优化,使用py2exe打包代码和资源至dist目录
#
# 使用中若有问题,可以留言至:
#  //eyehere.net/2011/python-pygame-novice-professional-py2exe/
#
# 安装需求:
#         python, pygame, py2exe 都应该装上

# 使用方法:
#         1: 修改此文件,指定需要打包的.py和对应数据
#         2: python pygame2exe.py
#         3: 在dist文件夹中,enjoy it~

try:
    from distutils.core import setup
    import py2exe, pygame
    from modulefinder import Module
    import glob, fnmatch
    import sys, os, shutil
except ImportError, message:
    raise SystemExit,  "Sorry, you must install py2exe, pygame. %s" % message

# 这个函数是用来判断DLL是否是系统提供的(是的话就不用打包)
origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
    # 需要hack一下,freetype和ogg的dll并不是系统DLL
    if os.path.basename(pathname).lower() in ("libfreetype-6.dll", "libogg-0.dll", "sdl_ttf.dll"):
        return 0
    return origIsSystemDLL(pathname)
# 把Hack过的函数重新写回去
py2exe.build_exe.isSystemDLL = isSystemDLL

# 这个新的类也是一个Hack,使得pygame的默认字体会被拷贝
class pygame2exe(py2exe.build_exe.py2exe):
    def copy_extensions(self, extensions):
        # 获得pygame默认字体
        pygamedir = os.path.split(pygame.base.__file__)[0]
        pygame_default_font = os.path.join(pygamedir, pygame.font.get_default_font())
        # 加入拷贝文件列表
        extensions.append(Module("pygame.font", pygame_default_font))
        py2exe.build_exe.py2exe.copy_extensions(self, extensions)

# 这个类是我们真正做事情的部分
class BuildExe:
    def __init__(self):
        #------------------------------------------------------#
        ##### 对于一个新的游戏程序,需要修改这里的各个参数 #####
        #------------------------------------------------------#

        # 起始py文件
        self.script = "MyGames.py"
        # 游戏名
        self.project_name = "MyGames"
        # 游戏site
        self.project_url = "about:none"
        # 游戏版本
        self.project_version = "0.0"
        # 游戏许可
        self.license = "MyGames License"
        # 游戏作者
        self.author_name = "xishui"
        # 联系电邮
        self.author_email = "blog@eyehere.net"
        # 游戏版权
        self.copyright = "Copyright (c) 3000 xishui."
        # 游戏描述
        self.project_description = "MyGames Description"
        # 游戏图标(None的话使用pygame的默认图标)
        self.icon_file = None
        # 额外需要拷贝的文件、文件夹(图片,音频等)
        self.extra_datas = []
        # 额外需要的python库名
        self.extra_modules = []
        # 需要排除的python库
        self.exclude_modules = []
        # 额外需要排除的dll
        self.exclude_dll = ['']
        # 需要加入的py文件
        self.extra_scripts = []
        # 打包Zip文件名(None的话,打包到exe文件中)
        self.zipfile_name = None
        # 生成文件夹
        self.dist_dir ='dist'

    def opj(self, *args):
        path = os.path.join(*args)
        return os.path.normpath(path)

    def find_data_files(self, srcdir, *wildcards, **kw):
        # 从源文件夹内获取文件
        def walk_helper(arg, dirname, files):
            # 当然你使用其他的版本控制工具什么的,也可以加进来
            if '.svn' in dirname:
                return
            names = []
            lst, wildcards = arg
            for wc in wildcards:
                wc_name = self.opj(dirname, wc)
                for f in files:
                    filename = self.opj(dirname, f)

                    if fnmatch.fnmatch(filename, wc_name) and not os.path.isdir(filename):
                        names.append(filename)
            if names:
                lst.append( (dirname, names ) )

        file_list = []
        recursive = kw.get('recursive', True)
        if recursive:
            os.path.walk(srcdir, walk_helper, (file_list, wildcards))
        else:
            walk_helper((file_list, wildcards),
                        srcdir,
                        [os.path.basename(f) for f in glob.glob(self.opj(srcdir, '*'))])
        return file_list

    def run(self):
        if os.path.isdir(self.dist_dir): # 删除上次的生成结果
            shutil.rmtree(self.dist_dir)

        # 获得默认图标
        if self.icon_file == None:
            path = os.path.split(pygame.__file__)[0]
            self.icon_file = os.path.join(path, 'pygame.ico')

        # 获得需要打包的数据文件
        extra_datas = []
        for data in self.extra_datas:
            if os.path.isdir(data):
                extra_datas.extend(self.find_data_files(data, '*'))
            else:
                extra_datas.append(('.', [data]))

        # 开始打包exe
        setup(
            cmdclass = {'py2exe': pygame2exe},
            version = self.project_version,
            description = self.project_description,
            name = self.project_name,
            url = self.project_url,
            author = self.author_name,
            author_email = self.author_email,
            license = self.license,

            # 默认生成窗口程序,如果需要生成终端程序(debug阶段),使用:
            # console = [{
            windows = [{
                'script': self.script,
                'icon_resources': [(0, self.icon_file)],
                'copyright': self.copyright
            }],
            options = {'py2exe': {'optimize': 2, 'bundle_files': 1,
                                  'compressed': True,
                                  'excludes': self.exclude_modules,
                                  'packages': self.extra_modules,
                                  'dist_dir': self.dist_dir,
                                  'dll_excludes': self.exclude_dll,
                                  'includes': self.extra_scripts} },
            zipfile = self.zipfile_name,
            data_files = extra_datas,
            )

        if os.path.isdir('build'): # 清除build文件夹
            shutil.rmtree('build')

if __name__ == '__main__':
    if len(sys.argv) < 2:
        sys.argv.append('py2exe')
    BuildExe().run()
    raw_input("Finished! Press any key to exit.")

可以先从简单的程序开始,有了一点经验再尝试打包复杂的游戏。
一些提示:

  • 如果执行出错,会生成一个xxx.exe.log,参考这里的log信息看是不是少打包了东西。
  • 一开始可以使用console来打包,这样可以在命令行里看到更多的信息。
  • 对于每一个游戏,基本都需要拷贝上面的原始代码修改为独一无二的打包执行文件。
  • 即使一个很小的py文件,最终生成的exe文件也很大(看安装的库而定,我这里最小4.7M左右),事实上py2exe在打包的时候会把无数的不需要的库都打进来导致最终文件臃肿,如果你安装了很繁杂的库(wxPython等)更是如此。使用zip打包以后查看里面的库文件,把不需要的逐一加入到self.exclude_modules中,最后可以把文件尺寸控制在一个可以接受的范围内。

补充:
很多人在打包使用Font模块时出现问题,这里需要把sdl_ttf.dll声明为非系统文件,我已经修改了脚本默认就加入了。而且建议,如果将来是确定要打包为exe的,那么就不要使用系统字体,即”pygame.font.SysFont(xxx)”,而是使用字体文件,然后打包时将文件当作图片等一起打包,这样出问题的概率会大大降低。

“dist_dir”应该是属于py2exe的特有options而不是setup的。

 

参考博客:http://eyehere.net/2011/python-pygame-novice-professional-py2exe/

posted @ 2015-10-16 22:53  马三小伙儿  阅读(13126)  评论(5编辑  收藏  举报