More Python ® Programming for the Absolute Beginner Jonathan S. Harbour Course Technology PTR A part of Cengage Learning Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States More Python® Programming for the © 2012 Course Technology, a part of Cengage Learning. Absolute Beginner: Jonathan S. ALL RIGHTS RESERVED. No part of this work covered by the copyright Harbour herein may be reproduced, transmitted, stored, or used in any form or by Publisher and General Manager, Course any means graphic, electronic, or mechanical, including but not limited to Technology PTR: Stacy L. Hiquet photocopying, recording, scanning, digitizing, taping, Web distribution, in- formation networks, or information storage and retrieval systems, except as Associate Director of Marketing: permitted under Section 107 or 108 of the 1976 United States Copyright Act, Sarah Panella without the prior written permission of the publisher. Manager of Editorial Services: Heather Talbot For product information and technology assistance, contact us at Marketing Manager: Mark Hughes Cengage Learning Customer & Sales Support, 1-800-354-9706 Senior Acquisitions Editor: Mitzi Koontz For permission to use material from this text or product, submit all requests online at cengage.com/permissions Further permissions Project Editor: Jenny Davidson questions can be emailed to [email protected] Technical Reviewer: Keith Davenport Python is a registered trademark of the PSF. Interior Layout Tech: Value Chain Software India All other trademarks are the property of their respective owners. Cover Designer: Mike Tanamachi All images © Cengage Learning unless otherwise noted. Indexer: Broccoli Information Services Library of Congress Control Number: 2011933243 Proofreader: Michael Beady ISBN-13: 978-1-4354-5980-9 ISBN-10: 1-4354-5980-6 eISBN-10: 1-4354-5981-4 Course Technology, a part of Cengage Learning 20 Channel Center Street Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit courseptr.com Visit our corporate website at cengage.com Printed in the United States of America 1 2 3 4 5 6 7 13 12 11 This book is dedicated to lone game developers on forums like The Game Programming Wiki (www.gpwiki.org) who put enormous passion into their creative works of interactive fiction, often without recognition. Do what you love and share it with the world! A CKNOWLEDGMENTS I offer my gratitude to those involved in getting this book finished and into print, especially Mitzi Koontz, Jenny Davidson, Keith Davenport, Mike Tanamachi, and Michael Beady. I would like to say, I hope it sells a million copies so I never have to work with any of you again, but that sentiment might be misunderstood and considered rude! So I will just say thanks! A A BOUT THE UTHOR J onathan S. Harbour has been programming since the 1980s. His first game system was an Atari 2600, which he disassembled on the floor of his room as a kid. He has written on C++, C#, Basic, Java, DirectX, Allegro, Lua, Dark- Basic, Game Boy Advance, Pocket PC, and game consoles. His other recent books include Beginning Java SE 6 Game Programming, Third Edition; XNA Game Studio 4.0 for Xbox 360 Developers; Multi-Threaded Game Engine Design; and an earlier book in this series, Visual Basic .NET Programming for the Absolute Beginner (2003). He holds a mas- ter’s degree in information systems. Visit him on the web at www.jharbour.com and www.facebook.com/jharbourcom. This page intentionally left blank Table of Contents Chapter 1 Python Has Class....................................................... 1 Examining the Geometry Program................................................................................... 2 Getting Started with Python............................................................................................... 2 Python Tools................................................................................................................... 3 Python Language........................................................................................................... 7 Objects in Python................................................................................................................... 7 What Came Before?....................................................................................................... 7 What’s Coming Next?................................................................................................. 11 OOP: The Python Way................................................................................................. 13 Single Inheritance....................................................................................................... 16 Multiple Inheritance.................................................................................................. 17 Summary................................................................................................................................ 19 Chapter 2 Getting Started with Pygame: The Pie Game......... 21 Examining The Pie Game................................................................................................... 22 Using Pygame........................................................................................................................ 22 Printing Text................................................................................................................. 23 Looping........................................................................................................................... 24 Drawing Circles............................................................................................................ 25 Drawing Rectangles.................................................................................................... 27 Drawing Lines............................................................................................................... 28 Drawing Arcs................................................................................................................ 30 The Pie Game......................................................................................................................... 31 Summary................................................................................................................................ 34 Chapter 3 File I/O, Data, and Fonts: The Trivia Game.......... 35 Examining The Trivia Game............................................................................................. 35 Python Data Types............................................................................................................... 36 More Printing............................................................................................................... 37 Getting User Input...................................................................................................... 39 Handling Exceptions.................................................................................................. 40 The Mad Lib Game....................................................................................................... 40 File Input/Output................................................................................................................. 43 Working with Text Files............................................................................................ 43 viii More Python Programming for the Absolute Beginner Working with Binary Files........................................................................................ 45 The Trivia Game................................................................................................................... 47 Printing Text with Pygame....................................................................................... 48 The Trivia Class............................................................................................................ 48 Loading the Trivia Data............................................................................................. 49 Displaying the Question and Answers................................................................... 50 Responding to User Input......................................................................................... 52 Going to the Next Question...................................................................................... 53 Main Code...................................................................................................................... 54 Summary................................................................................................................................ 55 Chapter 4 User Input: The Bomb Catcher Game..................... 57 Examining The Bomb Catcher Game............................................................................. 58 Pygame Events...................................................................................................................... 58 Real-Time Event Loop................................................................................................. 59 Keyboard Events........................................................................................................... 60 Mouse Events................................................................................................................ 61 Device Polling....................................................................................................................... 61 Polling the Keyboard.................................................................................................. 61 Polling the Mouse........................................................................................................ 65 The Bomb Catcher Game................................................................................................... 67 Summary................................................................................................................................ 71 Chapter 5 Math and Graphics: The Analog Clock Demo..... 73 Examining The Analog Clock Demo............................................................................... 74 Basic Trigonometry............................................................................................................. 74 Circle Theory................................................................................................................. 75 Traversing a Circle’s Perimeter................................................................................ 79 Circle Demo.................................................................................................................. 81 The Analog Clock Demo..................................................................................................... 83 Getting the Time......................................................................................................... 84 Drawing the Clock...................................................................................................... 84 Summary................................................................................................................................ 92 Chapter 6 Bitmap Graphics: The Orbiting Spaceship Demo.. 93 Examining The Orbiting Spaceship Demo.................................................................... 93 Using Bitmaps....................................................................................................................... 94 Loading a Bitmap......................................................................................................... 94 Drawing the Background.......................................................................................... 95 Drawing the Planet..................................................................................................... 97 Contents ix Drawing the Spaceship.............................................................................................. 98 Orbiting the Planet........................................................................................................... 101 Orbiting........................................................................................................................ 101 Rotating....................................................................................................................... 103 Summary.............................................................................................................................. 107 Chapter 7 Animation with Sprites: The Escape the Dragon Game......................................................................... 109 Examining The Escape The Dragon Game.................................................................. 109 Using Pygame Sprites....................................................................................................... 110 Custom Animation................................................................................................... 110 Loading a Sprite Sheet............................................................................................. 112 Changing the Frame................................................................................................. 112 Drawing One Frame.................................................................................................. 114 Sprite Groups.............................................................................................................. 115 MySprite Class............................................................................................................ 116 Sprite Animation to the Test.................................................................................. 118 The Escape the Dragon Game......................................................................................... 119 Jumping....................................................................................................................... 120 Colliding...................................................................................................................... 121 Source Code................................................................................................................ 122 Summary.............................................................................................................................. 125 Chapter 8 Sprite Collision Detection: The Zombie Mob Game......................................................................... 127 Examining The Zombie Mob Game.............................................................................. 127 Collision Detection Techniques..................................................................................... 128 Rectangle Collision Between Two Sprites........................................................... 128 Circle Collision Between Two Sprites.................................................................. 129 Pixel-Perfect Masked Collision Between Two Sprites....................................... 130 Rectangle Collision Between a Sprite and a Group.......................................... 130 Rectangle Collision Between Two Groups.......................................................... 131 The Zombie Mob Game.................................................................................................... 131 Creating Your Own Module.................................................................................... 132 Advanced Directional Animation......................................................................... 135 Colliding with Zombies........................................................................................... 138 Getting Health............................................................................................................ 139 Game Source Code.................................................................................................... 141 Summary.............................................................................................................................. 146
Description: