Вы находитесь на странице: 1из 4

1/2/2019 Python Programming Tutorials

search Home +=1 Support the Content Community Log in Sign up

Converting PyGame to an executable

Game Development in Python 3 With PyGame - 16 - Convert to executable

In this PyGame with Python 3 programming tutorial, we cover how we can convert our PyGame into an executable, or an installer, which we
can then share with people who do not have Python installed, or pygame... etc. We will do this using cx_Freeze, which is a very similar module
to py2exe for those who are familiar.

The reason why you may want to convert your game into an executable is so that you can share it with people who may not have PyGame or
Python installed.

In order to use cx_Freeze, you will need to download and install it. The official source is: cx_Freeze website/

Some people have had problems with the official source, so you can use a patched version, found here instead:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#cx_freeze

Make sure you match the bit version (32 or 64 bit) with your bit version of Python.

Once you have cx_Freeze installed, you're ready to convert your game to an executable or an installer.

First, you will want to make a setup.py file with the following contents:

import cx_Freeze

executables = [cx_Freeze.Executable("pygameVideo15.py")]

https://pythonprogramming.net/converting-pygame-executable-cx_freeze/ 1/4
1/2/2019 Python Programming Tutorials

cx_Freeze.setup(
search Home +=1 Support the Content Community Log in Sign up

name="A bit Racey",


options={"build_exe": {"packages":["pygame"],
"include_files":["racecar.png"]}},
executables = executables

The above is converting the python script contained under the file name: pygameVideo15.py

From there, we specify the name of the application, then we explicitly notify cx_Freeze that we are using the pygame module, and we
explicitly tell it that we have an included file, which is racecar.png.

cx_Freeze can sometimes figure out modules that you're using, but sometimes not. Following with typical Pythonic precepts, explicit is better
than implicit, so let's be clear!

Once you've done that, you'll want to navigate to your terminal and make your way to the directory containing your Python game script and
the setup.py script.

Then, type:

python setup.py build.

If you get an error stating that Python is not in your path, then give the full path. For example, on Windows, with Python 3.4, you would do:

C:/Python34/python setup.py build

From there, you should get a build directory, within it will be an executable that will run your script.

You can also do something like:

python setup.py bdist_msi

If you're on a mac, then you would do:

python setup.py bdist_dmg

That will generate a Windows installer.

There are various errors and issues that you might run in to while using cx_Freeze. Check out the video for some of the possible issues.
Google can also be a massive help, and some times... just sometimes ... the documents can help a ton!

There exists 2 quiz/question(s) for this tutorial. Sign Up To +=1 for access to these, video downloads, and no ads.

The next tutorial: Adding A Pause Function To Our Game And Game Over

https://pythonprogramming.net/converting-pygame-executable-cx_freeze/ 2/4
1/2/2019 Python Programming Tutorials

search Home +=1 Support the Content Community Log in Sign up

Introduction to PyGame

Displaying images with PyGame

Moving an image around in PyGame

Adding boundaries

Displaying text to PyGame screen

Drawing objects with PyGame

Crashing

PyGame Score

Drawing Objects and Shapes in PyGame

Creating a start menu

PyGame Buttons, part 1, drawing the rectangle

PyGame Buttons, part 2, making the buttons interactive

PyGame Buttons, part 3, adding text to the button

PyGame Buttons, part 4, creating a general PyGame button function

PyGame Buttons, part 5, running functions on a button click

Converting PyGame to an executable

Adding a pause function to our game and Game Over

PyGame Icon

Sounds and Music with PyGame

Apigee API Management LEARN MORE


Named a leader in the 2018 Gartner Magic Quadrant. Download the report today.

https://pythonprogramming.net/converting-pygame-executable-cx_freeze/ 3/4
1/2/2019 Python Programming Tutorials

You've reached the end!


search
 Home +=1 Support the Content Community Log in Sign up

Contact: Harrison@pythonprogramming.net.

Support this Website!


Hire me
Facebook
Twitter
Google+

Legal stuff:

Terms and Conditions


Privacy Policy

© OVER 9000! PythonProgramming.net Programming is a superpower.

https://pythonprogramming.net/converting-pygame-executable-cx_freeze/ 4/4

Вам также может понравиться