Skip to main content
main-content
Top

About this book

Expand your basic knowledge of Python and use PyGame to create fast-paced video games with great graphics and sounds. This second edition shows how you can integrate electronic components with your games using the build-in general purpose input/output (GPIO) pins and some Python code to create two new games.

You'll learn about object-oriented programming (OOP) as well as design patterns, such as model-view-controller (MVC) and finite-state machines (FSMs). Whether using Windows, macOS, Linux, or a Raspberry Pi, you can unleash the power of Python and PyGame to create great looking games.

The book also includes complete code listings and explanations for "Bricks," "Snake," and "Invaders"—three fully working games. These allow you to get started in making your own great games and then modify them or build your own exciting titles. The concepts are further explained using games such as “Copycat,” where the player must concentrate and repeat the sequence of lights and sounds, and “Couch Quiz,” in which PyGame and electronic components create a quiz game for 2–4 players.

What You’ll Learn

Gain basic knowledge of Python and employ it for game development

Study game projects you can use as templates, such as Bricks, Snake, and Invaders

Work with user-defined functions, inheritance, composition, and aggregation

Implement finite state machines

Integrate your game with electronics using the GPIO pins

Who This Book Is For

Experienced coders or game developers new to Python, PyGame and Raspberry Pi would find this book helpful. It is also for beginners interested in getting into game development.

Table of Contents

Chapter 1. What Is a Programming Language?

Abstract
A computer program is a list of statements that a computer must carry out in order to complete a task. Usually a repetitive task that would take a human a long time to calculate. A computer language describes the arrangement or syntax of those statements. There are various computer languages out there, each suitable to one or more tasks.
Sloan Kelly

Chapter 2. What Is Python?

Abstract
Python is a modern programming language that supports object-oriented, functional, and imperative programming styles. It is ideal for the beginner because of its readability and ease of use. The upside to all of this is that you can write programs in less lines of code than an equivalent C/C++ or Java program.
Sloan Kelly

Chapter 3. Introducing Python

Abstract
In this chapter we will introduce the Python language. At this stage we’re only interested in understanding the format or syntax of the Python language and its keywords. Python is an interpreted language, meaning that it requires another program called an interpreter to run any code that we write.
Sloan Kelly

Chapter 4. Breaking Free from the Interpreter

Abstract
Up until now we have used the interpreter to write our code. As each line is entered, the Python program interprets it and the processed line's output is displayed onscreen. From now on we will use IDLE.
Sloan Kelly

Chapter 5. Making Decisions

Abstract
Up until now we have seen very linear programs. These programs follow from one statement to the next, never deviating. They’re just a linear shopping list; you get the vegetables first, then bread, then canned vegetables, and finally cat food. Why? Because that’s the order that those items typically appear in a supermarket.
Sloan Kelly

Chapter 6. Making the Raspberry Pi Repeat Itself

Abstract
A video game repeats the action until all the players’ lives have gone, or the end of the game has been reached. So far, we have only written programs that run through a sequence of commands and then terminate. With the use of certain Python keywords, we can get the computer to repeat a block of code when required, either using conditions or for a set number of times.
Sloan Kelly

Chapter 7. Containers

Abstract
Up until now, we’ve mostly stored a single value in a variable like this: playerName = 'Sloan'
Sloan Kelly

Chapter 8. Putting It Together: Tic-Tac-Toe

Abstract
Before we start looking at PyGame and how to create arcade-style games we should take a step back and put what we’ve covered in the first few chapters into a simple ASCII console game of Tic-Tac-Toe – a game for two players.
Sloan Kelly

Chapter 9. Basic Introduction to PyGame

Abstract
PyGame is a free framework for Python that provides modules designed to write video games. It is built on top of the Simple DirectMedia Layer Library (SDL) that provides easy access to sound and visual elements.
Sloan Kelly

Chapter 10. Designing Your Game

Abstract
Before we launch into programming our first game, we’re going to slow things down a little. Before starting any project, whether it is home improvement, taking a trip, or programming a game, you should sit down and plan what you want to do.
Sloan Kelly

Chapter 11. Game Project: Bricks

Abstract
In this chapter we’ll review Bricks, our first game project. For those of you who haven’t played this game before, you control a bat at the bottom of the screen (Figure 11-1). There is a collection of bricks above you and using the ball you must destroy all the bricks by hitting them with the ball.
Sloan Kelly

Chapter 12. User-Defined Functions

Abstract
A user-defined function allows you to package and name several lines of code and reuse those lines of code throughout your program. All you must do is call the name you’ve given your function.
Sloan Kelly

Chapter 13. File Input and Output

Abstract
Being able to save and load files from disk is an important part of game development. Assets such as levels, player sprites, etc. are loaded from files stored on disk. Progress is saved to disk to allow players to resume their game from when they last played.
Sloan Kelly

Chapter 14. Introducing Object-Oriented Programming

Abstract
Until now we have been using Python as a structured language. Each line is executed one after the other. If we want to reuse code, we create functions. There is another way to program called object-oriented programming. In object-oriented programming we create little objects that not old hold our data but group the operations – the things we want to do with that data – with the data itself. The main features of object-oriented programming, or OOP for short, are
Sloan Kelly

Chapter 15. Inheritance, Composition, and Aggregation

Abstract
When most people learn about object-oriented programming, they learn three things:
Sloan Kelly

Chapter 16. Game Project: Snake

Abstract
For our second game we are going to re-create the classic Snake game. Snake has been with us since the late 1970s and, if you had a Nokia phone, you probably had a version of the game on it. You control a snake, and you move around the screen using the cursor keys. You must eat fruit to grow. You are not allowed to touch the outside walls or yourself. Did I mention that you are growing? See Figure 16-1.
Sloan Kelly

Chapter 17. Model View Controller

Abstract
Model View Controller was mentioned before in the “Designing Your Game” section to describe how the interactions between different objects can be used to simplify the problem: breaking down a bigger problem into smaller easier-to-manage chunks. See Figure 17-1.
Sloan Kelly

Chapter 18. Audio

Abstract
Audio is an important part of making a game. You can have the best visuals in the world, the best mechanics but something is missing – it’s audio! In this chapter we take a look at playing one-off sounds such as explosions or effects as well as music.
Sloan Kelly

Chapter 19. Finite State Machines

Abstract
A state can be described as a condition of a program or entity. Finite defines that there is only a set number of states that the program or entity can be defined by. The entity is controlled by a series of rules that determine what the next state of the program or entity is to be placed in.
Sloan Kelly

Chapter 20. Game Project: Invaders

Abstract
Our final arcade-style game project is Invaders and it brings together everything that we’ve done up until this point. We’ve got sounds, animation, MVC, and FSM all wrapped in one game. See Figure 20-1.
Sloan Kelly

Chapter 21. Simple Electronics with the GPIO Pins

Abstract
Up until now we have seen the Raspberry Pi communicate with the keyboard and mouse as input devices and the display as an output. The Raspberry Pi can communicate with a wide variety of peripherals – a fancy name for things you can add on – as well as electronic components like Light Emitting Diodes (LEDs) or switches. This is achieved by attaching devices through the pins on the top of the Raspberry Pi. These pins are called the General Purpose Input/ Output pins or GPIOs for short.
Sloan Kelly

Chapter 22. Game Project: Memory

Abstract
The game ‘Memory’ is the first GPIO project. The board is set up with two rows: one with four LEDs and the other with four buttons as shown in the finished board in Figure 22-1.
Sloan Kelly

Chapter 23. Game Project: Quiz

Abstract
The last project of this book is a couch quiz game for two players. Players are presented with a series of multiple-choice questions and must decide the correct answer. The game uses a mix of PyGame and electronics; the questions are displayed on the monitor and all the input comes from two pairs of three tact switches. Some of the game screens are shown in Figure 23-1.
Sloan Kelly

Chapter 24. Conclusion

Abstract
By now, you should have a good understanding of the Python language as well as the PyGame library. With the games included in this text you should have a good understanding of what goes into creating a video game. Indeed, armed with a good idea, you should have enough knowledge to make a game on your own! In this book we’ve covered player input, displaying graphics, playing sounds, and moving characters about the screen as well as alternative forms of input and output in the form of reading and writing to the GPIO pins.
Sloan Kelly
Additional information