Contents in Detail
MISSION PYTHON
CODE A SPACE ADVENTURE GAME!
BY SEAN MCMANUS
San Francisco
MISSION PYTHON. Copyright 2018 by Sean McManus.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN-10: 1-59327-857-8
ISBN-13: 978-1-59327-857-1
Publisher: William Pollock
Production Editor: Riley Hoffman
Cover Illustration: Josh Ellingson
Game Illustrations: Rafael Pimenta
Developmental Editor: Liz Chadwick
Technical Reviewer: Daniel Aldred
Copyeditor: Anne Marie Walker
Compositor: Riley Hoffman
Proofreader: Emelie Burnette
The following images are reproduced with permission:
courtesy of Johnson Space Center, NASA
courtesy of NASA/JPL-Caltech/UCLA
image of Mars courtesy of NASA
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900;
www.nostarch.com
Library of Congress Control Number: 2018950581
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
To my wife, Karen, with thanks for all her support throughout this mission; and to Leo, our wonderful son, who is taking us on the most amazing journey.
ABOUT THE AUTHOR
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 worlds largest technology companies. His novel for adults, Earworm, goes undercover in the music industry, exposing a conspiracy to replace bands with computer-generated 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.
BRIEF CONTENTS
ACKNOWLEDGMENTS
Many thanks to everyone at No Starch Press who worked hard to bring you this book, including developmental editor Liz Chadwick, production editor Riley Hoffman, copyeditor Anne Marie Walker, proofreaders Emelie Burnette and Meg Sneeringer, and production manager Serena Yang. Thank you to Tyler Ortman, who commissioned the book, and Bill Pollock, for his support on this project. Josh Ellingson created the stunning cover artwork. Thank you to Amanda Hariri, Anna Morrow, and Rachel Barry for their support with marketing.
Rafael Pimenta designed the awesome graphics for the game. Daniel Aldred was the technical editor, testing the code and providing feedback on the text. Thanks to them both.
We wouldnt have been able to create this book without the dedicated work of the open source community. Daniel Pope created Pygame Zero and helped with research queries. You can learn about some more cool features of Pygame Zero that werent required for our mission at http://pygame-zero.readthedocs.io/en/latest/. Pygame Zero extends Pygame, so thanks also to the Pygame development team and to the wider Python community who contribute to its success.
NASA allows us to use many of its images to tell our story, for which we are grateful. Its work is hugely inspiring.
Thank you to Russell Barnes, Sam Alder, Eben Upton, and Carrie Anne Philbin at the Raspberry Pi Foundation who helped to get this project off the ground.
Finally, thank you for reading the book! If you enjoy it, please consider sharing a review, tweet, or blog post to help others to discover it. In any event, I hope you enjoy it.
INTRODUCTION
Air is running out. Theres a leak in the space station, so youve got to act fast. Can you find your way to safety? Youll 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?
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. Its 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, Ill 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 Ive 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 Ive 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 youll be building and learn how to run your programs.