Learn Python by creating a video game | Opensource.com

Learn Python by creating a video game

Get to know Python the fun way using the step-by-step instructions in our new eBook.

Arcade games
Image credits : 
Cicada Strange on Flickr, CC BY-SA 2.0

Subscribe now

Get the highlights in your inbox every week.

Python is one of the most popular programming languages out there. Whether you want to learn it for work or for fun, it's a powerful and useful language for any purpose. You can create applications to help you with daily tasks, fun games you and your friends can play, scripts to process data, applications to generate or parse information, and much more.

No matter what you intend to do with a programming language, we think it's more fun to learn by creating a game than by crunching numbers or transforming strings. And if you're completely new to programming, it's a lot easier to understand why you have to do something in code when you can see the code working in a familiar setting like a video game.

You may not select Python as the best programming language available (everyone has their own answer for that), but it is a relatively non-intimidating one. Python can use a lot of words (such as is and is not) instead of symbols (such as = and !=). It also manages many low-level tasks so that you usually don't have to worry about things like data types and garbage collection. This generally means you can get started coding without the upfront frustration that a complex language like C or Java presents.

To help you learn Python, we wrote an eBook showing how to create a platformer video game with it. Step through the lessons to learn Python while building a video game. As an additional benefit, you'll also learn about programming logic, syntax, operators, and more. You can see immediate results as you learn, so everything you learn is promptly reinforced.

Python in a minute

Python is a general-purpose language, meaning it (like most languages) provides functions to do simple "tricks" with numbers and letters. Linux users already have Python installed. Mac users have an old version of Python, but you can install the latest version from the Python.org website. Windows users can learn to install Python from this install Python on Windows article.

Once installed, you can start an interactive Python shell and do math:

$ python3
>>> 5+6
>>> 11/2
>>> 11//2
>>> 11%2

As you can see from this sample, there's some special notation required, but it's mostly familiar to anyone comfortable with math. Maybe you're not a numbers person and prefer letters:

$ python3
>>> string = "hello world"
>>> print(string)
hello world
>>> print(string.upper())
>>> print(string[0])
>>> print(string[1])
>>> print(string[2])
>>> print(string[3])
>>> print(string[4])

Again, there's special notation for relatively basic tasks, but even without an explanation, you probably already detected that the [0] and [1] notation enables you to "slice" data and print presents data on your screen.

Pygame in five minutes

If you want to create a video game or anything beyond a basic calculator with Python alone, it could take a lot of study, work, and time. Luckily, Python's been around for a couple of decades, so people have developed libraries of code to help you perform typical programming feats with (comparatively) very little effort. Pygame is a set of code modules designed for creating video games. It's not the only such library, but it's the oldest (for better and for worse), so there's a lot of documentation and examples online.

Start by learning the recommended Python virtual environment workflow:

$ python3 -m venv mycode/venv
$ cd mycode
$ source ./venv/bin/activate

Once you're in a virtual environment, you can safely install Pygame into your project folder:

(venv)$ echo "pygame" >> requirements.txt
(venv)$ python -m pip install -r requirements.txt
[...] Installing collected packages: pygame
Successfully installed pygame-x.y.z

Now that you have Pygame installed, you can create a simple demo application. It's easier than you think. Python can do what's called object-oriented programming (OOP), which is a fancy computer science term to describe when code is structured as if you were creating physical objects with code. Programmers aren't deluded, though. They know they're not really making physical objects when they code, but it can help to imagine it that way because then you understand the limits of your programmed world.

For instance, if you were stranded on a desert island and wanted a coffee cup to appear, you'd have to harvest some clay, fashion a cup, and bake it. If you were very clever, you'll create a mold first so that whenever you want another cup, you can quickly create a new one from your template. Even though each cup came from the same template, they would be physically independent: should one break, you still have others. And you could make each coffee cup appear unique by adding color or etchings.

In Pygame, and in many programming tasks, you use similar logic. A thing can't appear in your programmed world until you define it. Here's how to make a coffee cup appear in a Python and Pygame program.

Object-oriented programming with Pygame

Create a new file called main.py and enter this code to initiate the Pygame module and to use a Pygame template to create a window:

import pygame


screen = pygame.display.set_mode((960,720))

Just as you might use a template to create an object in real life, you use a template provided by Pygame to create a sprite (which is Pygame's term for a visual game object). In object-oriented programming, a class represents a template for an object. Type this code into your document:

class Cup(pygame.sprite.Sprite):
    def __init__(self):

        # image
        img = pygame.image.load('coffeecup.png').convert()
        self.image = img

        # volume
        self.rect = self.image.get_rect()
        self.rect.x = 10
        self.rect.y = 10

This code block uses Pygame's sprite template to design a coffee cup sprite. Your coffee cup sprite has an image because of self.image, while self.rect gives it volume (width and height). These are attributes Pygame expects a sprite to possess, but if you were creating a playable video game, you could give it any other attribute you want, such as health points and a score.

All you've done so far is create a window and the template for a coffee cup. Your game doesn't actually make a cup yet.

The last part of your code must use your template to generate a cup and add it to your game world. As you may know, computers are very fast, so technically, the code you've created so far would only run for a millisecond or so. When writing a graphical computer application, you must force the computer to remain open whether or not it thinks it has completed the prescribed tasks. Programmers do this with an infinite loop, which in Python is represented by the instruction while True (True is always true, so the loop never ends).

An infinite loop ensures your application stays open long enough for the computer user to see and use the application:

cup = Cup()

while True:
    screen.blit(cup.image, cup.rect)

This code sample creates a cup from your template Cup and then updates the display using a Pygame function. Finally, it draws the cup's image within its bounding box using Pygame's blit function.

Get a graphic

Before you can run the code successfully, you need a graphic for your coffee cup. You can find lots of Creative Commons coffee cup graphics on FreeSVG.org. I used this one. Save the graphic in your project directory, and call it coffeecup.png.

Run the game

Launch your application:

(venv)$ python ./main.py


Coffee cup in Pygame

(Seth Kenlon, CC BY-SA 4.0)

Download the Python gaming eBook

Pygame is a powerful framework, and there's a lot more you can do with it than just draw coffee cups on your screen. Download our free eBook to get to know Pygame—and Python itself—better.

Programming keyboard.

Python is a good language for young and old, with or without any programming experience.
Python game screenshot

The first part of this series explored Python by creating a simple dice game. Now it's time to make your own game from scratch.
Python game screenshot

Part three of a series on building a game from scratch with Python.
Python game screenshot

In the fourth part of this series, learn how to code the controls needed to move a game character.
Dogs playing chess

In part five of this series on building a Python game from scratch, add a bad guy for your good guy to battle.

In part six of this series on building a Python game from scratch, create some platforms for your characters to travel.
Cosmic stars in outer space

Learn how to program video games with Python's Pygame module and start manipulating gravity.
Arcade games

Learn how to fight gravity with jumping in this installment on programming video games with Python's Pygame module.
Gaming artifacts with joystick, GameBoy, paddle

Let your player run free by enabling the side-scroller effect in your Python platformer using the Pygame module.
Hearts, stars, and dollar signs

Give your players some treasures to collect and boost their score in this installment on programming video games with Python's Pygame module.
connecting yellow dots in a maze

In the eleventh and final article in this series on programming with Python's Pygame module, display your game player's score when they collect loot or take damage.
Gaming on a grid with penguin pawns

Running around avoiding enemies is one thing. Fighting back is another. Learn how in the 12th article in this series on creating a platformer in Pygame.
Colorful sound wave graph

Hear what happens when your hero fights, jumps, collects loot, and more by adding sounds to your game. Learn how in the 13th article in this series on creating a platformer in Pygame.


About the author

Seth Kenlon
Seth Kenlon - Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. 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 Slackermedia.