Making games with Python & pyGame Chinese Translation


Making games with Python & pyGame playing games with pyGame

Chapter 1 – installing Python and pyGame

Original author: Al sweigart
Translation: bigbigbli / Li Chao
Reprint prohibited

What should you know before you start playing games?

Before reading this book, it may be helpful if you have a little understanding of Python Programming (or how to program in a language other than Python); However, even if you haven’t read the book, you can still read it.

Programming is not as difficult as people think.

If you have some trouble, you can Read this book for free – invent your own computer games with
Python, or in Find a related problem on.

Before reading this book, it doesn’t matter if you haven’t learned about the pyGame library. The next chapter is a short tutorial on all the main features and functions of pyGame.

If you haven’t read anything about Python and pyGame, and haven’t installed Python and pyGame, I’ll explain how to install it in detail later. If you already have both installed, you can skip this chapter.

Download and install Python

Before we start programming, you need to install the Python interpreter on your computer. An interpreter is a program that understands the instructions you will write in Python. Without an interpreter, your computer will not be able to run Python programs. From now on, we will call the Python interpreter python.

The Python interpreter can be downloaded from the official website of the python programming language,。 You may need help from others to download and install Python software. What Python interpreter to install depends on whether your computer’s operating system is windows, Mac OS X, or Linux, such as Ubuntu. You can also find videos of installing Python software on your computer on the Internet

Installing Python on Windows system

When you visit You should see the list of links on the left (for example, – about, – news, – documentation, – download, and so on). Click the download link to enter the download page, then find the file named Python 3.2 Windows Installer, and then click its link to download Python for Windows system.

Double click the python-3.2.msi file you just downloaded to start the python installer( If not, try right clicking on the file and selecting install.) Once the installer starts, just continue clicking the next button and accept the installer’s selection at the beginning (no changes are required). When the installation is complete, click finish.

Mac OS X system

Python 2.5.1 comes with Mac OS x10.5 and is pre installed by apple. At present, pyGame only supports Python 2, not Python 3 (it has been supported in 2020). However, the program in this book uses both Python 2 and python 3.

Python website( is also some additional information about using Python on the Mac.

Start Python

We will use idle software to type in our programs and run them. Idle stands for interactive development environment. The development environment is software that makes it easy to write Python programs, just as word processor software makes it easy to write books.

If your operating system is Windows XP, you can run Python by clicking the start button, and then select program – Python 3.1 – idle (Python GUI). For Windows Vista or windows 7, just click the windows button in the lower left corner, find idle or search for idle and select.

If your operating system is Max OS X, open the finder window and click applications to start idle, then click Python 3.2, and then click the idle icon.

Install pyGame

After installing python, the pyGame library is not included. Like python, pyGame is free. You also need to download and install pyGame, which is as easy as downloading and installing the Python interpreter. In the browser, go to , and then click the downloads download link on the left side of the website. You need to download the pyGame installer of the operating system and the python version you installed. This book assumes that you downloaded the Windows version.

On windows, double-click the downloaded file to install pyGame. To check that pyGame is installed correctly, type the following in the interactive shell:

import pygame

If nothing appears after you click enter, you know that pyGame has been successfully installed. If an import error occurs: no module named pyGame appears, try installing pyGame again (and make sure you enter the correct pyGame).

This chapter has five applets that demonstrate how to use the different functions provided by pyGame. In the last chapter, you will use these features to complete a complete game with pyGame written in Python.

A video tutorial on how to install pyGame is available on the book’s website Consult.

How to use this book

Making games with Python & pyGame is different from other programming books because it focuses on the complete source code of several game programs. This book does not teach you programming concepts, but let you figure out how to make programs with these concepts. Here we show you some programs and explain how they are combined.

Generally speaking, you should read these chapters in order. Many concepts are used repeatedly in these games, and they are explained in detail only in the first game they appear. But if there is a game that you think is interesting, skip to that chapter and read it first. If you feel you can master it, you can read it at any time.

Characteristic program

Each chapter is a single game program and explains how different parts of the code work. It is helpful to enter code line by line from this book, but you can also download the source code file from the book’s website. In a web browser, go to the URL And follow the instructions to download the source code file. But entering the code yourself can really help you learn the code better.

Download graphics and sound files

Although you can enter the code you read from this book, you need to Download the graphics and sound files used in the game in this book and ensure that these image and sound files are located in the same folder as the. Py Python files, otherwise the python program will not be able to find these files.

Number of rows and space

When entering the source code yourself, do not enter the line number that appears at the beginning of each line. For example, if you see this in a Book:

You do not need to enter 1,2,3,4 on the left

It should be entered as follows:

number = random.randint(1, 20)
spam = 42
print('Hello world!')

These numbers are only used in this book. You can refer to specific lines in the code. They are not part of the actual program.

In addition to the line number, make sure that the code entered is exactly the same as displayed. Note that some lines do not start at the far left of the page, but are indented with four or eight or more spaces. Be sure to add the correct number of spaces at the beginning of each line( Since each character in idle is the same width, you can calculate the number of spaces by counting the number of characters above or below the line you are viewing.)

For example, in the following code, you can see that the second line is indented by four spaces because the four characters (while) of the upper line are above the indented space. The third line is indented by the other four spaces (four characters if n is above the indented space of the third line):

while spam < 10:
if number == 42:

The text packaging of this book

Some lines of code are too long for one line on the page of this book. The text of the code will be wrapped to the next line. When you enter these lines in the file editor, enter the code in one line without pressing enter.

You can determine when a new line starts by looking at the line number to the left of the code. For example, the following code has only two lines of code, although the first line wraps around the next line:

View your code online

Some of the programs in this book are a little long. Although it is helpful to learn Python by entering the source code of these programs, you may accidentally enter errors that cause the program to crash. Where the error is may not be obvious.

You can copy and paste the text of the source code into the online diff tool on the book website. The diff tool displays any differences between the source code in the book and the source code you type. This is a simple way to find any errors in the program.

Copying and pasting text is a very useful computer skill, especially for computer programming. stay There is a video tutorial on copying and pasting on the book’s website.

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]