Creating Game Using Python Within Ten Minutes


Have you ever wondered how video games are created? It is not as complicated as you might think. Presented here is a simple game called Bunnies and Moles, where the hero, the bunny, has to defend a castle against a horde of moles.

To write this game, we used Python. It is a popular embedded programming language used in Arduino, Raspberry Pi and others. This article aims to familiarise beginners with some Python libraries that can be implemented in embedded projects as well. Python is simple to start out with, fun and easy to learn.

Installing Python

To try this tutorial on a Windows PC, install Python 2.7.3 version (not 3.3.0). Run the installer, and you will get IDLE in All Programs folder in Start menu. Launch IDLE.

If you are using a Mac, you will already have Python installed. Open Terminal; once at Python prompt, to test if it is correctly working, type ‘print 1+1’ and hit Return. It should print 2. You have just written your first Python program!

Now that Python is working correctly, install PyGame to write a game using Python. PyGame is a Python library that makes writing games easy. It provides functionalities such as image handling and sound playback that you can incorporate into the game.

Download PyGame library for Python 2.7 version, PyGame installer appropriate for your system, from

Running Python code from file

If you want to work on a bigger program (like a game), you should save your code to a file so that you do not have to type it repeatedly.

There are several ways to run a Python program as a file. One way is to use a plain text editor like Notepad in Windows or TextEdit in Mac. Open a new text file, type in your Python code like print 1+1 and save it as (xxx can be any descriptive file name).

On Windows, double-click this file to run it. On Mac, open Terminal and type Python, then drag the file that you saved in Terminal window and click Enter.

The other way is to type in your code using IDLE editor, which is what we do here. To run IDLE, type ‘idle’ on Terminal. Choose File\New Window from IDLE menu and you will get a text editor window where you can type in your Python code. Save your code via File\Save and run it via Run\Run Module (F5). Do note that, Run menu is only available if you have a file open in an editor window.

Adding resources

You are almost ready to create your game. But what is a game without some great graphics and sound effects? The graphics and sound effects required for this game have been included in the design.

Create a folder for your game on your hard disk and extract the resources folder into that folder so that your game folder has a sub-folder named Resources, with various resources grouped in the folders inside.

Software development

Hello, bunny. Create a bunny image on the screen. Run IDLE, open a new text editor window and type the following code:

# 1-Import library
import pygame
from pygame.locals import *

# 2 – Initialize the game
width, height = 640, 480

# 3 – Load images
player = pygame.image.load(“resources/

# 4 – Keep looping through
while 1:

# 5 – Clear the screen before
drawing it again

# 6 – Draw the screen elements
screen.blit(player, (100,100))

# 7 – Update the screen

# 8 – Loop through the events
for event in pygame.event.get():
# Check if the event is the X
if event.type==pygame.QUIT:
# If it is quit the game

Save the file in the game folder (the one where the resources sub-folder is) and name it

Now, let us go through the code section.

Import library(#1) imports PyGame library and allows you to use functions from the library in your program.

Initialize the game(#2) initialises PyGame and sets up the display window.

Load images(#3) loads the image that you would use for the bunny.

Keep looping through(#4) keeps looping over the following indented code.

Clear the screen(#5) clears the screen. Fill the screen with black before you draw anything.

Draw the screen element(#6) adds the bunny image that you loaded to the screen at x=100 and y=100.

Update the screen(#7) updates the screen.

Loop through the event (#8) checks for any new events; if there is one, and if it is quit command, exit the program.

If you run the code now (via Run\Run Module in Idle menu), you would see a screen as shown in Fig. 1. W00t, the bunny, is on the scene and ready for action.



Please enter your comment!
Please enter your name here