Opensource.
com
A guide to building
a video game in Python
OPENSOURCE.COM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ABOUT OPENSOURCE.COM
What is Opensource.com?
publishes stories about creating,
OPENSOURCE.COM adopting, and sharing open source
solutions. Visit Opensource.com to learn more about how the open source
way is improving technologies, education, business, government, health, law,
entertainment, humanitarian efforts, and more.
Submit a story idea: opensource.com/story
2 A GUIDE TO BUILDING A VIDEO GAME IN PYTHON . CC BY-SA 4.0 . OPENSOURCE.COM
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ABOUT THE AUTHORS
SETH KENLON
is an independent multimedia artist, free culture
SETH KENLON advocate, and UNIX geek. He has worked in the film
and computing industry, often at the same
time. He is one of the maintainers of the
Slackware-based multimedia production
project, http://slackermedia.info.
JESS WEICHLER
Jess Weichler is a digital artist using open
JESS WEICHLER source software and hardware to create
works digitally and in the physical world at CyanideCupcake.com.
She is also an award-winning educator
for (and founder of) MakerBox.org.nz
an organization that teaches kids of
all ages how to use technology, from
sewing needles to Arduinos, to make
their ideas a reality.
Follow Jess at @jlweich
A GUIDE TO BUILDING A VIDEO GAME IN PYTHON . CC BY-SA 4.0 . OPENSOURCE.COM 3
CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The code for this booklet can be found here:
https://gitlab.com/makerbox/scratch2python
CHAPTERS
Learn how to program in Python by building a simple 5
dice game
Build a game framework with Python using the 10
Pygame module
How to add a player to your Python game 15
Using Pygame to move your game character around 19
What’s a hero without a villain? How to add one to your 24
Python game
Put platforms in a Python game with Pygame 29
Simulate gravity in your Python game 37
Add jumping to your Python platformer game 41
Enable your Python game player to run forward 47
and backward
Put some loot in your Python platformer game 52
Add scorekeeping to your Python game 57
Add throwing mechanics to your Python game 65
Add sound to your Python game 72
APPENDICES
How to install Python on Windows 74
Managing Python packages the right way 77
Easily set image transparency using GIMP 80
4 A GUIDE TO BUILDING A VIDEO GAME IN PYTHON . CC BY-SA 4.0 . OPENSOURCE.COM
. . . . . . . . . . . . . . . . . . . . LEARN HOW TO PROGRAM IN PYTHON BY BUILDING A SIMPLE DICE GAME
Learn how to program in Python
by building a simple dice game
Python is a good language for young and old, with or without any programming experience.
[1] is an all-purpose programming lan- Windows: Microsoft Windows doesn’t currently ship with
PYTHON guage that can be used to create desktop
applications, 3D graphics, video games, and even websites.
Python. Install it from python.org/downloads/windows [3]. Be
sure to select Add Python to PATH in the install wizard.
It’s a great first programming language because it can be Read my article How to Install Python on Windows [4] for
easy to learn and it’s simpler than complex languages like instructions specific to Microsoft Windows.
C, C++, or Java. Even so, Python is powerful and robust
enough to create advanced applications, and it’s used in just Running an IDE
about every industry that uses computers. This makes Py- To write programs in Python, all you really need is a text editor,
thon a good language for young and old, with or without any but it’s convenient to have an integrated development environ-
programming experience. ment (IDE). An IDE integrates a text editor with some friendly
and helpful Python features. IDLE 3 and PyCharm (Communi-
Installing Python ty Edition) are two options among many [5] to consider.
Before learning Python, you may need to install it.
Linux: If you use Linux, Python is already included, IDLE 3
but make sure that you have Python 3 specifically. To Python comes with a basic IDE called IDLE.
check which version is installed, open a terminal window
and type:
python --version
Should that reveal that you have version 2 installed, or no
version at all, try specifying Python 3 instead:
python3 --version
If that command is not found, then you must install Python 3
from your package manager or software center. Which pack-
age manager your Linux distribution uses depends on the
distribution. The most common are dnf on Fedora and apt
on Ubuntu. For instance, on Fedora, you type this:
sudo dnf install python3
MacOS: If you’re on a Mac, follow the instructions for Li-
nux to see if you have Python 3 installed. MacOS does not
have a built-in package manager, so if Python 3 is not found,
install it from python.org/downloads/mac-osx [2]. Although
your version of macOS may already have Python 2 installed,
you should learn Python 3. IDLE
A GUIDE TO BUILDING A VIDEO GAME IN PYTHON . CC BY-SA 4.0 . OPENSOURCE.COM 5
Click here to download full PDF material