Pygame之Tutorials - Windows Executables

Pygame Tutorials
Windows Executables

打包成Windows可执行文件

by Pete Shinners
pete@shinners.org

Revision 1.2, January 28th, 2002



Introduction
介绍

One drawback to creating your game with pygame is the large amount of dependencies that your game requires. In order for people to play your game, they need a lot of libraries installed. For unix users, this isn't too bad, since most unix distributions come with their own package and dependency management system. On Windows there is nothing like this, and it is more difficult for a Windows user to just download your python source and run your game.
使用pygame来构建你自己的游戏会有一个问题必须解决,游戏有许多依赖。玩家要开始玩游戏,必须先安装一些库文件。这对unix用户来说并不是问题,因为大多数unix发行版本都附带有包和依赖管理系统。但在windows平台上什么都没有。这样让玩家仅仅载python源码再执行游戏都是个复杂过程。
 
The best solution is to create a collection of all the needed files for your game to run. With python this can mean a lot of files. This document will show you the tools you need to create a standalone version of your game. It is not difficult.
最好的解决之道是打包,把所有需要的文件都放入一个集合里面。对python来说这以为着不少文件。这篇文档将向你展示一个工具,可以用它来创建独立的游戏程序。这很简单的。
 

Download The Tools
下载工具

The first thing you need to do is download the tools to build the executable. We will use the excellent PY2EXE tool. This package extends the distutils to turn your python code into an executable. You can grab the latest version from the PY2EXE page. It uses a simple windows installer to manage the installation.
首先你要去下载一个工具才能创建包。我们将使用PY2EXE这个工具。这个扩展模块在distutils机制的基础上,将你的python代码转换成一个可执行文件。你可以访问 PY2EXE页面去缺德最新版本。它使用一个windows安装程序来进行安装操作。
 
To use PY2EXE you'll need to create a simple distutils script to run it. I've created a version of this script that I suggest you use. pygame2exe.py.
要使用PY2EXE的话,你需要编写一个distutils脚本。我已经编写了一个版本,可以看看 pygame2exe.py.。
 
 

Build the Script

There's a small block of variables you will want to change in the pygame2exe.py script to make it work for your specific game.
你只需要修改一点pygame2exe.py中的变量就可以用来给你的游戏打包了。
project_name = "aliens"         #name for exe
project_script = "aliens.py"    #name of base .PY
icon_file = "aliens.ico"        #name of .ICO
optimize = 2                    #0, 1, or 2; like -O and -OO
dos_console = 0                 #set to 1 to run in a DOS box
data_directories = ['data']     #used data directories
extra_modules = []              #any extra modules that are missing

Most of the options are pretty self descriptive. When first testing your game, you'll want to set the dos_console value to 1. This will print any errors into the dos console. When you release your game, you'll want to set it back to 0, so your game can run without opening a dos prompt.
大多数选项都是自解释的。当测试你的游戏时,你会把dos_console值设置为1。这将把所有错误信息输出到dos命令行。当你发行正式版本时,就可以把这个值设置回0了,这样游戏执行时就不会出现dos命令行了。
 
If you notice your game doesn't run, and get an error about missing modules, you can add the names of specific missing modules to the list named extra_modules. For example, if your game uses the surfarray module, surfarray depends on the Numeric modules, but sometimes it can be difficult for PY2EXE to detect Numeric as a dependency. You can add the missing Numeric modules to this list and everything will be ok.
如果提示游戏不能运行,而且显示缺失模块的信息时,你可以把缺失模块的名字加入到名为extra_modules的列表里去解决。例如,游戏使用了surfarray模块,它依赖于Numeric模块,但有时PY2EXE不能检查出Numeric是必须的。如果你 手工将缺失的模块添加到列表中,问题就迎刃而解了。
 
Once you are all setup, simply run this script and it will do all the work. The final executable and DLLs will be placed inside a project directory, which will be inside a directory named "dist".
当一切就绪时,只需要执行脚本,所有工作就会启动执行。最终的可执行文件以及DLL都会放在项目目录之下的dist子目录中。
 
You'll notice there aren't any .PY source files in the final directory. That is because all the needed python source has been compressed as bytecode into the actual executable file.
你会看到在目录下没有任何.py文件。因为所有必须的python源文件都被压缩成字节码放到了可执行文件当中了。
 
The icon_file variable is an optional name of a .ICO file to use for the executable. You can leave it empty ("") and it will be ignored. Also be warned the icon_file doesn't work too well, especially on windows9x.
变量icon_file可以为可执行文件设置图标。你也可以让它留空,这样它也会被自动忽略。但不得不告诉你,在win9x系统下,图标文件可能不会正常工作。
 
The data_directories is a list of paths that will be copies into the final game directory. Since most games out there will require some sort of graphics/sound/font resources, this will help copy them into the correct place.
data_directories是一系列路径名,表示那些路径会被拷贝到最终目录之下。因为大多数游戏需要图像、音效、字体资源,这样会利于将它们拷贝到正确位置。
 
You'll need to doublecheck that all the game resources and files got copied into the final directory in the "dist" folder. Once that is done and the game is working, you can compress the entire directory into a .ZIP file and send it to all your friends. They do not need any versions of python or SDL installed on their machines. Even if they have older versions of python (like version 1.5.2), it will not interfere with your game. 
你需要确认所有游戏资源以及程序文件都被拷贝到最终目录“dist”下面了。如果游戏也能顺利执行,你就可以把整个目录打包为ZIP文件发给你的朋友了。他们并不需要在机器上面安装任何python或者SDL。即使他们带有老版本的python(比如1.5.2版),也不会干扰到你的游戏。
 
You can also safely pick through the final game directory, and remove any of the "PYD" files that you don't expect your game to use. for example, if you don't use joysticks in your game, you can remove the "joystick.pyd" to make your game size a little smaller.
你也可以整理最终目录去移除任何你不需要的.PYD文件。例如,如果你的游戏不支持手柄,你就可以把joystick.pyd文件删除了,这样就能减小游戏的容量。
 

Creating an Actual Installer
创建一个安装程序

There's nothing wrong with distributing your game in a .ZIP file. But it is usually a lot nicer to create an installer executable so the game can automatically decompress itself, as well as setup Start Menu icons and adding an option to uninstall. For this you'll need to use one of the many available installing tools for windows. There are many free ones to choose from, and there are several high quality ones.
以ZIP格式发行游戏并没有什么问题。但更好的方式是创建一个安装程序,让游戏自动解压、配置开始菜单、配置反安装项目。你可以使用windows平台上面的若干种工具来完成这一工作。其中一些是免费的,这里我们推荐几种。
 
I can recommend INNO Setup for creating the install tool, and ISTool for configuring the package. These are both of the utilities I use when creating the installable pygame package for windows.  
我推荐使用 INNO Setup 来创建安装工具,用 ISTool 来配置包。这也是我用来创建windows版pygame包的工具。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值