© Mark Cunningham 2020
M. CunninghamGame Programming with Code Angelhttps://doi.org/10.1007/978-1-4842-5305-2_1

1. Introduction

Mark Cunningham1 
(1)
Edinburgh, Scotland
 

Welcome to Python game programming with Code Angel. In this book, you will write the Python program code required to build four amazing games, learning how to code as you go along.

The four games you will make are shown in the following.

Figure 1-1 shows the Forest Bomber game.
../images/482396_1_En_1_Chapter/482396_1_En_1_Fig1_HTML.jpg
Figure 1-1

Forest Bomber

Figure 1-2 shows the Snapper game.
../images/482396_1_En_1_Chapter/482396_1_En_1_Fig2_HTML.jpg
Figure 1-2

Snapper

Figure 1-3 shows the Alien Invasion game.
../images/482396_1_En_1_Chapter/482396_1_En_1_Fig3_HTML.jpg
Figure 1-3

Alien Invasion

Figure 1-4 shows the Golf game.
../images/482396_1_En_1_Chapter/482396_1_En_1_Fig4_HTML.jpg
Figure 1-4

Golf

Coding a game

Each game is built up steadily over several chapters. Take your time working through each chapter, adding the program code as instructed. New concepts will be explained as they are introduced.

As you finish each game, you should run your program to check that it works as expected. If it doesn't, don't worry. It's quite common for a program to contain errors which are known as bugs . If your program has a bug, carefully compare your code with the code from this book and try to spot any differences. Missing out just one single character or typing a character in the wrong place can prevent a program from running.

Have fun playing your finished game or impress your friends by letting them play the game that you have programmed!

Python and Pygame

In order to run any of the code in this book, you will need to install the Python programming language on your computer. This is a fairly straightforward process. Follow the installation instructions on the Python web site: www.python.org.

Once you have installed Python, you need to install the Pygame library which extends Python and provides many of the functions required to make games. Visit the web site for information on how to download and install Pygame: www.pygame.org.

Both Python and Pygame are open source and completely free.

Choosing an IDE

Now that you have installed Python and Pygame, you are ready to begin coding. You are probably wondering where to actually type your Python code.

The best way to do this is by using an IDE. IDE stands for integrated development environment. It's what programmers use to enter, run, and debug their code.

There are lots of different IDEs to choose from. At Code Angel, we recommend one of the following IDEs to get you started. They are all free and available for Windows, MacOS, and Linux.

Python IDLE

Pros: Installed with Python, fine for small projects

Cons: Basic, limited debugging, not great for larger projects

Rating 3/5

Thonny

Pros: Easy to use

Cons: Not as polished as PyCharm

Rating 4/5

Windows, Mac, or Linux

PyCharm Edu

Pros: Works well for large projects

Cons: More complex interface and menu options, steep learning curve

Installation: Needs Pygame to be added under Settings ➤ Project Interpreter

Rating 5/5

Once you install whichever IDE you think best meets your needs, you are ready to start coding.

Bugs and debugging

A bug is simply an error in a computer program. Bugs are so called because early computers were very large and insects would get inside the system and cause a short circuit. Nowadays, a bug refers to program code which contains an error.

A bug may cause a program to crash or prevent it from working in the way that was expected. Debugging means finding and fixing bugs in program code.

When a program crashes, it may display an error message. Sometimes the message can be difficult to understand. The most important thing to note is the line number at which the error occurred. Find the line number in your program, and compare it very closely with the same line of code from this book. Look for any differences. Your program code must be exactly the same as the code from the book. Also check that the lines above and below the reported line number are correct.

A logic error is an error which causes a program to run incorrectly but does not necessarily cause the program to crash. Because there is no error message, logic errors can be difficult to find.

With logic errors, you need to work out where in the program code the error might be. For example, imagine a game where the objective is to shoot a spaceship. Each time that the player hits the spaceship, they should have ten points added to their score. However, you find when running the game that ten points are subtracted each time a spaceship is hit. This would be an example of a logic error.

This logic error must have occurred when the program updated the game score. It is likely that a minus sign has been used instead of a plus sign, and that is what has caused the error. The game will not actually crash, but the player will soon get fed up losing points every time they hit a spaceship!

Common mistakes

While bugs can be caused in many different ways, there are some common mistakes you should look out for.

Indentation

In Python, indentation levels are very important. Indentation errors can cause a program to crash or not work correctly. Each level of indentation should be either four spaces or a single tab.
while True:
⎵⎵⎵⎵for event in pygame.event.get():
⎵⎵⎵⎵⎵⎵⎵⎵key_pressed = pygame.key.get_pressed()

Make sure that you carefully check indentation levels as you enter new lines of code.

Variable and function names

Make sure variable names and function names are spelled correctly.
score  ✓
csore  ×
Variable names and function names use underscores between words.
plane_exploded  ✓
Variable names and function names cannot contain spaces.
plane exploded  ×

Case

Python is case sensitive. This applies to Python commands, as well as function and variable names.

So in a Python program, score, Score, and SCORE are three completely different things.

Brackets and quotes

Brackets and quotes come in pairs.

If there is an opening bracket, there must also be a closing bracket

( )

The same applies to quotes

“ ” or ‘ ’

Missing colons

if statements , for statements, def function declarations, and class definitions all end in a colon. It’s common for beginners to miss the colon out by mistake – if you are getting an error, make sure that the colon is not missing.
if bomb.y > SCREEN_HEIGHT:

Comments

A comment is a note or explanation included in the program code. In Python, comments begin with the # hash symbol and will often be displayed in a different color by the IDE.

Comments are ignored by the computer when the program is being run. All of the programs in this book include comments to help explain what the program code is doing. These comments don’t affect the program and so do not actually have to be entered, but it is good practice to include them.

Time to get started…

You are now ready to start programming the games in this book, beginning with Forest Bomber. You might find coding tricky at times, you might get frustrated, but to become a programmer, the key is to stick with it and not give up. And of course…have fun!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.98.71