Contents
INVENT YOUR OWN COMPUTER GAMES WITH PYTHON
4TH EDITION
Al Sweigart
San Francisco
INVENT YOUR OWN COMPUTER GAMES WITH PYTHON, 4TH EDITION.
Copyright 2017 by Al Sweigart.
Some rights reserved. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
Printed in USA
First printing
20 19 18 17 16 1 2 3 4 5 6 7 8 9
ISBN-10: 1-59327-795-4
ISBN-13: 978-1-59327-795-6
Publisher: William Pollock
Production Editor: Laurel Chun
Cover Illustration: Josh Ellingson
Interior Design: Octopod Studios
Developmental Editor: Jan Cash
Technical Reviewer: Ari Lacenski
Copyeditor: Rachel Monaghan
Compositor: Susan Glinert Stevens
Proofreader: Paula L. Fleming
Indexer: Nancy Guenther
The sprite images in was created by txturs. These images have been dedicated to the public domain with a CC0 1.0 Public Domain Dedication.
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 Cataloging-in-Publication Data
Names: Sweigart, Al, author.
Title: Invent your own computer games with Python / by Al Sweigart.
Description: San Francisco : No Starch Press, Inc., [2017]
Identifiers: LCCN 2016037817 (print) | LCCN 2016044807 (ebook) | ISBN
9781593277956 | ISBN 1593277954 | ISBN 9781593278113 (epub) | ISBN
159327811X (epub) | ISBN 9781593278120 (mobi) | ISBN 1593278128 (mobi)
Subjects: LCSH: Computer games--Programming. | Python (Computer program
language)
Classification: LCC QA76.76.C672 S785 2017 (print) | LCC QA76.76.C672 (ebook)
| DDC 794.8/1526--dc23
LC record available at https://lccn.loc.gov/2016037817
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 Caro
About the Author
Al Sweigart is a software developer, tech book author, and hoopy frood who really knows where his towel is. He has written several programming books for beginners, including Automate the Boring Stuff with Python and Scratch Programming Playground, also from No Starch Press. His books are freely available under a Creative Commons license at his website https://inventwithpython.com/.
About the Technical Reviewer
Ari Lacenski is a developer of Android applications and Python software. She lives in the Bay Area, where she writes about Android programming at http://gradlewhy.ghost.io/ and mentors with Women Who Code.
CONTENTS IN DETAIL
THE INTERACTIVE SHELL
WRITING PROGRAMS
GUESS THE NUMBER
A JOKE-TELLING PROGRAM
DRAGON REALM
USING THE DEBUGGER
DESIGNING HANGMAN WITH FLOWCHARTS
WRITING THE HANGMAN CODE
EXTENDING HANGMAN
TIC-TAC-TOE
THE BAGELS DEDUCTION GAME
THE CARTESIAN COORDINATE SYSTEM
SONAR TREASURE HUNT
CAESAR CIPHER
THE REVERSEGAM GAME
REVERSEGAM AI SIMULATION
CREATING GRAPHICS
ANIMATING GRAPHICS
COLLISION DETECTION
USING SOUNDS AND IMAGES
A DODGER GAME WITH SOUNDS AND IMAGES
ACKNOWLEDGMENTS
This book would not have been possible without the exceptional work of the No Starch Press team. Thanks to my publisher, Bill Pollock; thanks to my editors, Laurel Chun, Jan Cash, and Tyler Ortman, for their incredible help throughout the process; thanks to my technical editor Ari Lacenski for her thorough review; and thanks to Josh Ellingson for yet another great cover.
INTRODUCTION
When I first played video games as a kid, I was hooked. But I didnt just want to play video games, I wanted to make them. I found a book like this one that taught me how to write my first programs and games. It was fun and easy. The first games I made were like the ones in this book. They werent as fancy as the Nintendo games my parents bought for me, but they were games I had made myself.
Now, as an adult, I still have fun programming and I get paid for it. But even if you dont want to become a computer programmer, programming is a useful and fun skill to have. It trains your brain to think logically, make plans, and reconsider your ideas whenever you find mistakes in your code.
Many programming books for beginners fall into two categories. The first category includes books that dont teach programming so much as game creation software or languages that simplify so much that what is taught is no longer programming. The other category consists of books that teach programming like a mathematics textbookall principles and concepts, with few real-life applications for the reader. This book takes a different approach and teaches you how to program by making video games. I show the source code for the games right up front and explain programming principles from the examples. This approach was the key for me when I was learning to program. The more I learned how other peoples programs worked, the more ideas I had for my own programs.
All youll need is a computer, some free software called the Python interpreter, and this book. Once you learn how to create the games in this book, youll be able to develop games on your own.
Computers are incredible machines, and learning to program them isnt as hard as people think. A computer program is a bunch of instructions that the computer can understand, just like a storybook is a bunch of sentences that the reader can understand. To instruct a computer, you write a program in a language the computer understands. This book will teach you a programming language called Python. There are many other programming languages you can learn, like BASIC, Java, JavaScript, PHP, and C++.