parts of the code work so you can customize it or build your own games based on my game code You can also download all the code you need If you get stuck or just want to jump straight into playing the game and seeing it work you can do so All the software you need is free and I’ve provided instructions for Windows PCs and the Raspberry Pi I recommend you use the Raspberry Pi 3 or Raspberry Pi 2 The game may run too slowly to enjoy on the Pi Zero original Model B and older models There are several different ways you can use the book and the game Download the game play it first and then use the book to understand how it works This way you eliminate the risk of seeing any spoilers in the book before you play the game Although I’ve kept them to a minimum minimum you might notice a few clues in the code as you read the book If you get really stuck on a problem in the game you can try reading the code to work out the solution In any case I recommend you run the game at least once to see what you’ll be building and learn how to run your programs Build the game and then play it This book guides you through creating creating the game from start to finish As you work your way through the chapters you’ll add new sections to the game and see how they work If you can’t get the code working at any point you can just use my version of the code listing and continue building from there If you choose this route avoid making any custom changes to the game until you’ve built it played it and finished it Otherwise you might accidentally make the game impossible to complete It’s okay to make any changes I suggest in the exercises Customize the game When you understand how the program works you can change it by using your own maps graphics objects and puzzles The Escape game is set on a space station but yours could be in the jungle under the sea or almost anywhere You could use the book to build your own version of Escape first or use my version of the final game and customize that I’d love to see what you make using the program as a starting point You can find me on Twitter at musicandwords or visit my website at www sean co uk What’s in this Book Here’s a briefing on what’s in store for you as you embark on your mission Chapter 1 shows you how to go on a spacewalk You’ll learn how to use graphics in your Python programs using Pygame Zero and discover some of the basics of making Python programs Chapter 2 introduces lists which store much of the information in the Escape game You’ll see how to use lists to make a map Chapter 3 shows you how to get parts of a program to repeat and how to use that knowledge to display a map You’ll also design a room layout for the space station using wall pillars and floor tiles 2 Introduction
In Chapter 4 you’ll start to build the Escape game laying down the blueprints for the station You’ll see how the program understands the station layout and uses it to create the fabric for the rooms putting the walls and floor in place In Chapter 5 you’ll learn how to use dictionaries in Python which are another important way of storing information You’ll add information information for all the objects the game uses and you’ll see how to create a preview of your own room design When you extend the program in Chapter 6 you’ll see all the scenery in place and will be able to look at all the rooms After building the space station you can move in In Chapter 7 you’ll add your astronaut character and discover how to move around the rooms and animate movements Chapter 8 shows you how to polish the game’s graphics with shadows fading walls and a new function to draw the rooms that fixes the remaining graphical glitches When the space station is operational you can unpack your personal effects In Chapter 9 you’ll position items the player can examine pick up and drop In Chapter 10 you’ll see how to use and combine items so you can solve puzzles in the game The space station is nearly complete Chapter 11 adds safety doors that restrict access to certain zones Just as you’re putting your feet up and celebrating a job well done there’s danger around the corner as you’ll add moving hazards in Chapter 12 As you work through the book you’ll complete training missions that give you an opportunity to test your programs and your coding skills The answers if you need them are at the end of each chapter The appendixes at the back of the book will help you too Appendix A contains the listing for the whole game If you’re not sure where to add a new chunk of code you can check here Appendix B contains a table of the most important variables lists and dictionaries if you can’t remember what’s stored where and Appendix C has some debugging tips if a program doesn’t work for you For more information and supporting resources for the book visit the book’s website at www sean co uk books mission-python You can also find information and resources at https nostarch com missionpython Installing the Software The game uses the Python programming language and Pygame Zero which is software that makes it easier to handle graphics and sound You need to install both of these before you begin Note For updated installation instructions visit the book’s web page at https nostarch com missionpython Introduction 3
Installing the Software on raspberry Pi If you’re using a Raspberry Pi Python and Pygame Zero are already installed You can skip ahead to Downloading the Game Files on page 7 Installing Python on Windows To install the software on a Windows PC follow these steps 1 Open your web browser and visit https www python org downloads 2 At the time of this writing 3 7 is the latest version of Python but Pygame isn’t available for easy installation on it yet I recommend you use the latest version of Python 3 6 instead 3 6 6 at the time of writing writing You can find old versions of Python farther down the screen on the downloads page see Figure 1 Save the file on your desktop or somewhere else you can easily find it Pygame Zero works only with Python 3 so if you usually use Python 2 you’ll need to switch to Python 3 for this book Figure 1 The Python downloads page 3 When the file has downloaded double-click it to run it 4 In the window that opens select the checkbox to Add Python 3 6 to PATH see Figure 2 5 Click Install Now 4 Introduction
Figure 2 The Python installer 6 If you’re asked whether you want to allow this application to make changes to your device click Yes 7 Python will take a few minutes to install When it finishes click Close to complete the installation Installing Pygame Zero on Windows Now that you have Python installed on your computer you can install Pygame Zero Follow these steps 1 Hold down the Windows Start key and press R The Run window should open see Figure 3 2 Enter cmd see Figure 3 Press enter or click OK Figure 3 The Windows Run dialog box 3 The command line window should open as shown in Figure 4 Here you can enter instructions for managing files or starting programs Enter pip install pgzero and press enter at the end of the line Introduction 5