logo资料库

More Python Programming for the Absolute Beginner 无水印原版pdf.pdf

第1页 / 共349页
第2页 / 共349页
第3页 / 共349页
第4页 / 共349页
第5页 / 共349页
第6页 / 共349页
第7页 / 共349页
第8页 / 共349页
资料共349页,剩余部分请下载后查看
Cover
Copyright
About the Author
Table of Contents
1 Python Has Class
Examining the Geometry Program
Getting Started with Python
Python Tools
Python Language
Objects in Python
What Came Before?
What's Coming Next?
OOP: The Python Way
Single Inheritance
Multiple Inheritance
Summary
2 Getting Started with Pygame: The Pie Game
Examining The Pie Game
Using Pygame
Printing Text
Looping
Drawing Circles
Drawing Rectangles
Drawing Lines
Drawing Arcs
The Pie Game
Summary
3 File I/O, Data, and Fonts: The Trivia Game
Examining The Trivia Game
Python Data Types
More Printing
Getting User Input
Handling Exceptions
The Mad Lib Game
File Input/Output
Working with Text Files
Working with Binary Files
The Trivia Game
Printing Text with Pygame
The Trivia Class
Loading the Trivia Data
Displaying the Question and Answers
Responding to User Input
Going to the Next Question
Main Code
Summary
4 User Input: The Bomb Catcher Game
Examining The Bomb Catcher Game
Pygame Events
Real-Time Event Loop
Keyboard Events
Mouse Events
Device Polling
Polling the Keyboard
Polling the Mouse
The Bomb Catcher Game
Summary
5 Math and Graphics: The Analog Clock Demo
Examining The Analog Clock Demo
Basic Trigonometry
Circle Theory
Traversing a Circle's Perimeter
Circle Demo
The Analog Clock Demo
Getting the Time
Drawing the Clock
Summary
6 Bitmap Graphics: The Orbiting Spaceship Demo
Examining The Orbiting Spaceship Demo
Using Bitmaps
Loading a Bitmap
Drawing the Background
Drawing the Planet
Drawing the Spaceship
Orbiting the Planet
Orbiting
Rotating
Summary
7 Animation with Sprites: The Escape the Dragon Game
Examining The Escape The Dragon Game
Using Pygame Sprites
Custom Animation
Loading a Sprite Sheet
Changing the Frame
Drawing One Frame
Sprite Groups
MySprite Class
Sprite Animation to the Test
The Escape the Dragon Game
Jumping
Colliding
Source Code
Summary
8 Sprite Collision Detection: The Zombie Mob Game
Examining The Zombie Mob Game
Collision Detection Techniques
Rectangle Collision Between Two Sprites
Circle Collision Between Two Sprites
Pixel-Perfect Masked Collision Between Two Sprites
Rectangle Collision Between a Sprite and a Group
Rectangle Collision Between Two Groups
The Zombie Mob Game
Creating Your Own Module
Advanced Directional Animation
Colliding with Zombies
Getting Health
Game Source Code
Summary
9 Arrays, Lists, and Tuples: The Block Breaker Game
Examining The Block Breaker Game
Arrays and Lists
Lists with One Dimension
Creating a Stack-like List
Creating a Queue-like List
Lists with More Dimensions
Tuples
Packing a Tuple
Unpacking a Tuple
Searching for Elements
Counting Elements
Tuples as Constant Arrays
The Block Breaker Game
Block Breaker Levels
Loading and Changing Levels
Initializing the Game
Moving the Paddle
Moving the Ball
Hitting the Paddle
Hitting the Blocks
Main Code
MySprite Update
Summary
10 Timing and Sound: The Oil Spill Game
Examining The Oil Spill Game
Sound
Loading an Audio File
Playing an Audio Clip
Building The Oil Spill Game
Gameplay
Source Code
Summary
11 Program Logic: The Snake Game
Examining The Snake Game
Building The Snake Game
Hatching a Snake—the SnakeSegment Class
Raising a Snake—the Snake Class
Feeding the Snake—the Food Class
Initializing the Game
Program Main
Growth by Eating Food
Biting One's Self Is Not Advisable
Falling off the World
Teaching the Snake to Move Itself
Moving Automatically
Getting the Current Direction
Moving Toward the Food
Other Code Changes
Summary
12 Trigonometry: The Tank Battle Game
Examining The Tank Battle Game
Angular Velocity
Calculating Angular Velocity
Pygame's Goofy Rotation
Moving Forward and Backward at Any Angle
Improved Angle Wrapping
Building The Tank Battle Game
The Tanks
The Bullets
Main Code
Summary
13 Random Terrain: The Artillery Gunner Game
Examining The Artillery Gunner Game
Creating the Terrain
Defining the Height Map
Smoothing the Terrain
Locating Grid Points
Artillery Cannons
Placing the Cannons
Drawing the Turrets
Firing the Cannons
Shots Are A'Flyin
Computer Firing
Scoring a Hit
The Complete Game
Summary
14 More of Everything: The Dungeon Role-Playing Game
Examining The Dungeon Game
Review of Classic Dungeon RPGs
Rogue
NetHack
AngBand
Sword of Fargoal
Kingdom of Kroz
ZZT
Creating a Dungeon Level
Understanding ASCII Characters
Simulating a Text Console Display
Generating Random Rooms
Generating Random Hallways
Populating the Dungeon
Adding the Entrance and Exit Portals
Adding Gold
Adding Weapons, Armor, and Health Potions
Adding Monsters
Complete Dungeon Class
Adding the Player's Character
Advanced Gameplay
Picking Up Items
Fighting Monsters
Moving Monsters
Visibility Range
Exiting the Level
Wrapping Up the Gameplay
Summary
Appendix A: Installing Python and Pygame
Installing Python
Installing Pygame
Appendix B: Pygame Key Codes
Index
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 Absolute Beginner: Jonathan S. Harbour Publisher and General Manager, Course Technology PTR: Stacy L. Hiquet Associate Director of Marketing: Sarah Panella Manager of Editorial Services: Heather Talbot Marketing Manager: Mark Hughes Senior Acquisitions Editor: Mitzi Koontz Project Editor: Jenny Davidson Technical Reviewer: Keith Davenport © 2012 Course Technology, a part of Cengage Learning. ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced, transmitted, stored, or used in any form or by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, in- formation networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the publisher. For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706 For permission to use material from this text or product, submit all requests online at cengage.com/permissions Further permissions questions can be emailed to permissionrequest@cengage.com Interior Layout Tech: Value Chain Software Python is a registered trademark of the PSF. 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!
ACKNOWLEDGMENTS 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!
ABOUT THE AUTHOR 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
分享到:
收藏