- Code a Space Adventure Game! -
BY SEAN MCMANUS
BRIEF CONTENTS
Chapter 1: Your First Spacewalk
Chapter 2: Lists Can Save Your Life
Chapter 3: Repeat After Me
Chapter 4: Creating the Space Station
Chapter 5: Preparing the Space Station Equipment
Chapter 6: Installing the Space Station Equipment
Chapter 7: Moving into the Space Station
Chapter 8: Repairing the Space Station
Chapter 9: Unpacking Your Personal Items
Chapter 10: Make Yourself Useful
Chapter 11: Activating Safety Doors
Chapter 12: Danger! Danger! Adding Hazards
Appendix A: Escape: The Complete Game Listing
Appendix B: Table of Variables, Lists, and Dictionaries
Appendix C: Debugging Your Listings
Book Details
Price
|
3.00 |
---|---|
Pages
| 345 p |
File Size
|
7,856 KB |
File Type
|
PDF format |
ISBN-10 ISBN-13
| 1-59327-857-8 978-1-59327-857-1 |
Copyright©
| 2018 by Sean McManus |
Sean McManus is an expert technology and business writer. His other books
include Cool Scratch Projects in Easy Steps, Scratch Programming in Easy Steps,
Coder Academy, and Raspberry Pi For Dummies (co-authored with Mike Cook).
As a freelance copywriter, he writes for many of the world’s largest
technology companies. His novel for adults, Earworm, goes undercover in
the music industry, exposing a conspiracy to replace bands with computergenerated
music. He has been a Code Club volunteer, helping children at a
local school to learn computer programming. Visit his website at
www.sean.co.uk for sample chapters and bonus content from his books.
ABOUT THE TECHNICAL REVIEWER
Daniel Aldred is a passionate and experienced teacher of computer science.
He leads the computing department at a CAS hub school that supports and
develops other schools and organizations in the local area. He frequently
writes for Linux User & Developer and has created resources and projects for
Raspberry Pi, Pimoroni, micro:bit, and Cambridge International
Assessment. In his spare time he curates two websites,
www.canyoucompute.co.uk for the UK Computing examination course and
www.tecoed.co.uk for his own personal hacks. Daniel also led and managed a
team of eight students to win the first Astro Pi competition, where the
astronaut Major Tim Peake ran their program aboard the ISS.
INTRODUCTION
Air is running out. There’s a leak in the space station, so you’ve got to act
fast. Can you find your way to safety? You’ll need to navigate your way
around the space station, find access cards to unlock doors, and fix your
damaged space suit. The adventure has begun!
And it starts here: on Earth, at mission command, also known as your
computer. This book shows you how to use Python to build a space station
on Mars, explore the station, and escape danger in an adventure game
complete with graphics. Can you think like an astronaut to make it to safety?
Table of Contents
ACKNOWLEDGMENTS
INTRODUCTION
How to Use This Book
What’s in This Book?
Installing the Software
Installing the Software on Raspberry Pi
Installing Python on Windows
Installing Pygame Zero on Windows
Installing the Software on Other Machines
Downloading the Game Files
Downloading and Unzipping the Files on a Raspberry Pi
Unzipping the File on a Windows PC
What’s in the ZIP File
Running the Game
Running Pygame Zero Programs on the Raspberry Pi
Running Pygame Zero Programs in Windows
Playing the Game
1
YOUR FIRST SPACEWALK
Starting the Python Editor
Starting IDLE in Windows 10
Starting IDLE in Windows 8
Starting IDLE on the Raspberry Pi
Introducing the Python Shell
Displaying Text
Training Mission #1
Outputting and Using Numbers
Introducing Script Mode
Creating the Starfield
Understanding the Program So Far
Stopping Your Pygame Zero Program
Adding the Planet and Spaceship
Changing Perspective: Flying Behind the Planet
Training Mission #2
Spacewalking!
Training Mission #3
Understanding the Spacewalk Listing
Training Mission #4
Are You Fit to Fly?
Mission Debrief
2
LISTS CAN SAVE YOUR LIFE
Making Your First List: The Take-Off Checklist
Seeing Your List
Adding and Removing Items
Using Index Numbers
Inserting an Item
Accessing an Individual Item
Replacing an Item
Deleting an Item
Training Mission #1
Creating the Spacewalk Checklist
Training Mission #2
A List of Lists: The Flight Manual
Making a List of Lists
Training Mission #3
Finding an Item in the Flight Manual
Combining Lists
Making Maps from Lists: The Emergency Room
Making the Map
Finding an Emergency Item
Training Mission #4
Swapping Items in the Room
Training Mission #5
Are You Fit to Fly?
Mission Debrief
3
REPEAT AFTER ME
Displaying Maps with Loops
Making the Room Map
Displaying the Map with a Loop
Training Mission #1
Loop the Loop
Nesting Loops to Get Room Coordinates
Cleaning Up the Map
Training Mission #2
Displaying a 3D Room Image
Understanding How the Room Is Drawn
Working Out Where to Draw Each Item
Training Mission #3
Are You Fit to Fly?
Mission Debrief
4
CREATING THE SPACE STATION
Automating the Map Making Process
How the Automatic Map Maker Works
Creating the Map Data
Writing the GAME_MAP Code
Testing and Debugging the Code
Generating Rooms from the Data
How the Room Generating Code Works
Creating the Basic Room Shape
Adding Exits
Testing the Program
Training Mission #1
Exploring the Space Station in 3D
Training Mission #2
Making Your Own Maps
Are You Fit to Fly?
Mission Debrief
5
PREPARING THE SPACE STATION EQUIPMENT
Creating a Simple Planets Dictionary
Understanding the Difference Between a List and a Dictionary
Making an Astronomy Cheat Sheet Dictionary
Error-Proofing the Dictionary
Training Mission #1
Putting Lists Inside Dictionaries
Extracting Information from a List Inside a Dictionary
Training Mission #2
Making the Space Station Objects Dictionary
Adding the First Objects in Escape
Viewing Objects with the Space Station Explorer
Designing a Room
Training Mission #3
Adding the Rest of the Objects
Training Mission #4
Are You Fit to Fly?
Mission Debrief
6
INSTALLING THE SPACE STATION EQUIPMENT
Understanding the Dictionary for the Scenery Data
Adding the Scenery Data
Adding the Perimeter Fence for the Planet Surface
Loading the Scenery into Each Room
Updating the Explorer to Tour the Space Station
Training Mission #1
Are You Fit to Fly?
Mission Debrief
7
MOVING INTO THE SPACE STATION
Arriving on the Space Station
Disabling the Room Navigation Controls in the EXPLORER
Section
Adding New Variables
Teleporting onto the Space Station
Adding the Movement Code
Understanding the Movement Code
Training Mission #1
Moving Between Rooms
Are You Fit to Fly?
Mission Debrief
8
REPAIRING THE SPACE STATION
Sending Information to a Function
Creating a Function that Receives Information
How It Works
Training Mission #1
Adding Variables for Shadows, Wall Transparency, and Colors
Deleting the EXPLORER Section
Adding the DISPLAY Section
Adding the Functions for Drawing Objects
Drawing the Room
Understanding the New draw() Function
Positioning the Room on Your Screen
Making the Front Wall Fade In and Out
Displaying Hints, Tips, and Warnings
Showing the Room Name When You Enter the Room
Are You Fit to Fly?
Mission Debrief
9
UNPACKING YOUR PERSONAL ITEMS
Adding the Props Information
Adding Props to the Room Map
Finding an Object Number from the Room Map
Picking Up Objects
Picking Up Props
Adding the Keyboard Controls
Adding the Inventory Functionality
Displaying the Inventory
Adding the Tab Keyboard Control
Testing the Inventory
Dropping Objects
Training Mission #1
Examining Objects
Training Mission #2
Are You Fit to Fly?
Mission Debrief
10
MAKE YOURSELF USEFUL
Adding the Keyboard Control for Using Objects
Adding Standard Messages for Using Objects
Adding the Game Progress Variables
Adding the Actions for Specific Objects
Combining Objects
Training Mission #1
Adding the Game Completion Sequence
Exploring the Objects
Are You Fit to Fly?
11
ACTIVATING SAFETY DOORS
Planning Where to Put Safety Doors
Positioning the Doors
Adding Access Controls
Making the Doors Open and Close
Adding the Door Animation
Training Mission #1
Shutting the Timed Door
Adding a Teleporter
Training Mission #2
Activating the Airlock Security Door
Removing Exits for Your Own Game Designs
Mission Accomplished?
Are You Fit to Fly?
12
DANGER! DANGER! ADDING HAZARDS
Adding the Air Countdown
Displaying the Air and Energy Bars
Adding the Air Countdown Functions
Starting the Air Countdown and Sounding the Alarm
Training Mission #1
Adding the Moving Hazards
Adding the Hazard Data
Sapping the Player’s Energy
Starting and Stopping Hazards
Setting Up the Hazard Map
Making the Hazards Move
Displaying Hazards in the Room
Training Mission #2
Stopping the Player from Walking Through Hazards
Adding the Toxic Spills
Making the Finishing Touches
Disabling the Teleporter
Cleaning Up the Data
Your Adventure Begins
Your Next Mission: Customizing the Game
Are You Fit to Fly?
Mission Debrief
A ESCAPE: THE COMPLETE GAME LISTING
B TABLE OF VARIABLES, LISTS, AND DICTIONARIES
C DEBUGGING YOUR LISTINGS
Indentation
Case Sensitivity
Parentheses and Brackets
Colons
Commas
Images and Sounds
Spelling
INDEX
HOW TO USE THIS BOOK
By following the instructions in this book, you can build a game called Escape
with a map to explore and puzzles to solve. It’s written in Python, a popular
programming language that is easy to read. It also uses Pygame Zero, which
adds some instructions for managing images and sounds, among other
things. Bit by bit, I’ll show you how to make the game and how the main
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, 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 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.