invent your own computer games with python I N V E N T Y O U R O W N C O M P U T E R G A M E S W I T H P Y T H O N 4TH EdITION by 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. 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 Figure 20-1 on page 302, from left to right, were created by fsvieira, przemek.sz, LordNeo, and Suppercut. The grass sprite image in Figure 20-2 on page 302 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; [email protected] 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. Brief Contents Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Chapter 1: The Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 2: Writing Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 3: Guess the Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Chapter 4: A Joke-Telling Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Chapter 5: Dragon Realm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Chapter 6: Using the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Chapter 7: Designing Hangman with Flowcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Chapter 8: Writing the Hangman Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Chapter 9: Extending Hangman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Chapter 10: Tic-Tac-Toe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Chapter 11: The Bagels Deduction Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Chapter 12: The Cartesian Coordinate System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Chapter 13: Sonar Treasure Hunt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Chapter 14: Caesar Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Chapter 15: The Reversegam Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Chapter 16: Reversegam AI Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Chapter 17: Creating Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Chapter 18: Animating Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Chapter 19: Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Chapter 20: Using Sounds and Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Chapter 21: A Dodger Game with Sounds and Images . . . . . . . . . . . . . . . . . . . . . . 311 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 viii Brief Contents