PyGame

Let's build games!

Introduction

Developing software is a rewarding and enjoyable activity. Developing games, even more so.

PyGame is an easy way to learn about game development, programming and Python (which is one of the most popular programming languages out there). In this tutorial we will work through the basics needed to build a variety of games.

The other great thing about both Python and Pygame is that they are completely free and available for most major platforms (Windows, Mac, Linux, Raspberry Pi, etc) and because of how popular they are there is plenty of great (and also free) resources available on the Internet for learning (and supporting your learning) with these great tools.

Python is also not a toy language but a serious tool which is used by many people around the world. It is, in fact, the most popular programming language used in industry and is used by the likes of :

  • Google
  • Facebook
  • Netflix
  • Dropbox
  • Amazon
  • NASA

PyGame Logoand many more.

On the following pages we will learn about how PyGame works and tinker with a variety of concepts which will form the building blocks for a variety of simple games. Building upon these will lead into more complex games.

I am going to assume you already have a basic understanding of Python and programming.

Outline

I've tried to structure this tutorial so that skills build logically but also so that you can get in and play with things as quick as possible.

Once you have a hang of the basics, you can then put it into action building some simple games.

  • Pong - A simple version of the classic game Pong
  • Pong - An enhanced version of Pong utilising functions and multiple screens

Expected knowledge

In this tutorial I will assume that you already have a basic knowledge of coding in Python. You don't need to be great at programming but you should have a basic understanding of the following concepts :

  • Variables
  • Lists, tuples and dictionaries
  • If statements
  • Loops (while and for)
  • Functions

If you're a bit weak in some of these areas, don't worry as I'll do my best to keep things simple and explain things as we go.

Going Further

This tutorial will introduce you to a lot of the basics of PyGame in an easy to follow way. It is only an introduction however and doesn't cover the more involved and powerful features of PyGame. Whilst the knowledge contained here will be sufficient to create quite a lot of games, you may want to delve deaper. If that is you then the official PyGame Documentation is an excellent place to start.

There are also many other great tutorials out there that run you through building more complex games with PyGame. They can be a little daunting at first but if you run through this tutorial first so you understand the basics and have a solid foundation then you should have a much easier time working through and understanding those other tutorials.

Obtaining Python and PyGame

PyGame can be a bit tricky to get set up and working on your machine. Thankfully there are many good resources out there to help you. The general process is :

  • Install Python
  • Use Pip (Pythons' library manager) to install PyGame

I won't go through how to install Python and PyGame here but many students have told me that they have found Videos on YouTube to be the most helpful way to figure out getting it installed. Of course, a general search for how to install PyGame in your favourite search engine would also be pretty useful.

Working through this tutorial

I've tried to structure this tutorial so that instead of just being a knowledge dump it guides you though building the code and understanding the concepts. You should have working programs throughout the tutorial (not just at the end) so you can see how things work as you go.

The tutorial builds progressively so try and work though the sections in order. Many sections assume knowledge that was covered in previous sections.

The sections also won't provide you with completed code files which you can just copy and paste as whole. I've found that when this is provided students just skip to the end and copy the final complete code then assure themselves (and the teacher) that they understand it and it's all good. Some students can do this but usually what happens is that they don't really understand the underlying principles so whilst they may be able to get the gist of what is going on in that particular file, they haven't built up the skills to apply the techniques to build other programs.

Instead I will guide you though building up the completed code. As you copy in the code I encourage you to tinker with and experiment with values to help you get a deeper understanding of what is going on. Yes, this is a bit more work but nothing of value was ever easy and in the long run this will make you much more powerful with PyGame enabling you to build more complex and engaging games (which really is the whole point isnt it?).

On the next page you will learn about the basic pygame template and get a window operating. From there we can start to put elements and interactivity into the window. Let's get started.