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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM