Skip to main content
main-content
Top

About this book

Gain the basics of Python and use PyGame to create fast-paced video games with great graphics and sounds. You'll also learn about object oriented programming (OOP) as well as design patterns like model-view-controller (MVC) and finite state machines (FSMs).

Python, PyGame and Raspberry Pi Game Development teaches you how to use Python and PyGame on your computer. Whether you use Windows, macOS, Linux, or a Raspberry Pi you can unleash the power of Python and PyGame to create great looking games. Included in the text are complete code listings and explanations for "Bricks," "Snake" and "Invaders"-- three fully-working games. These allow you to get started making your own great games. Modify them or build your own exciting titles.

What You'll Learn

Gain the basics of Python and employ it for game developmentDesign your gameBuild games using game projects as templates like Bricks, Snake, and InvadersWork with user defined functions, inheritance, composition, and aggregationAdd sound to your gamesImplement finite state machines

Who This Book Is For

Experienced coders or game developers new to Python, PyGame and Raspberry Pi. This book is also for makers interested in getting into game development.

Table of Contents

Chapter 1. What Is a Programming Language?

This first part of the book discusses what we mean when we talk about service-oriented architectures in general, and microservices in particular. We will spend some time explaining the benefits, as well as touching on potential pitfalls to be avoided, involved in adopting this architectural style.
Sloan Kelly

Chapter 2. What Is Python?

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. Python is first and foremost a scripting language, but can be compiled into computer-readable binary. 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

In this section 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

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 the 'nano' editor from the command line.
Sloan Kelly

Chapter 5. Making Decisions

Up until now we have seen very linear programs. These programs follow from one statement to the next, never deviating. In essence, they're just a 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

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

Up until now, we've mostly stored a single value in a variable.
Sloan Kelly

Chapter 8. Introduction to IDLE under LXDE

In the second half of the book we will concentrate on creating games using the PyGame framework. This is installed by default on the Raspbian "Wheezy" distribution located on the Raspberry Pi web site. Like Python it is free to use and distribute programs written using it.
Sloan Kelly

Chapter 9. Basic Introduction to PyGame

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

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

Our first game project is called Bricks. For those of you who haven't played this game before, you control a bat at the bottom of the screen. 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

This first part of the book discusses what we mean when we talk about service-oriented architectures in general, and microservices in particular. We will spend some time explaining the benefits, as well as touching on potential pitfalls to be avoided, involved in adopting this architectural style.
Sloan Kelly

Chapter 13. File Input and Output

File input and output allows you to store and read back data from your programs. For example, level data like maps, user save data, or high score tables. Take this program as an example; it reads in itself from disk and displays the contents to the screen.
Sloan Kelly

Chapter 14. Introducing Object-Oriented Programming

Object-Oriented Programming is a programming paradigm where items are abstracted down to their basic elements.
Sloan Kelly

Chapter 15. Inheritance, Composition, and Aggregation

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

Chapter 16. Game Project: Snake

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 have to 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

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. Sound

Sounds are played using PyGame's mixer object. Like, PyGame, you must first initialize the sound mixer before using it..
Sloan Kelly

Chapter 19. Finite State Machines

A state can be described as a particular 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.
Sloan Kelly

Chapter 20. Invaders

Our final 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. Conclusion

This first part of the book discusses what we mean when we talk about service-oriented architectures in general, and microservices in particular. We will spend some time explaining the benefits, as well as touching on potential pitfalls to be avoided, involved in adopting this architectural style.
Sloan Kelly
Additional information