Table Of ContentC
books for professionals by professionals® r
a
v
e
n
Program Arcade Games: With Python and PyGame
P
r
This fun and handy book by Dr. Paul Vincent Craven shows you how to learn and use Python o
and PyGame to design and build cool arcade games. In Program Arcade Games: With Python
g
and PyGame, Second Edition, learn how to create fun and simple quiz games; integrate and
r
start using graphics; animate graphics; integrate and use game controllers; add sound and bit- a
mapped graphics; and build grid-based games.
m
After reading and using this book, you’ll be able to program and build simple arcade game
applications using one of today’s most popular programming languages, Python. You can even
A
deploy onto Steam and other Linux-based game systems as well as Android, one of today’s
most popular mobile and tablet platforms. r
program
c
• How to create quiz games a
• How to integrate and start using graphics d
• How to animate graphics
e
• How to integrate and use game controllers
• How to add sound and bit-mapped graphics G
arcade Games
• How to build grid-based games
a
m
e
s
related Titles
With Python and Pygame
—
Fourth Edition
—
Dr. Paul Vincent Craven
$ 44.99
Shelve in:
ISBN 978-1-4842-1789-4
Graphics/Game Programming
54499
User level:
Beginning–Advanced
9781484217894 SOURCE CODE ONLINE
www.apress.com
www.it-ebooks.info
Program Arcade
Games
With Python and Pygame
Fourth Edition
Paul Vincent Craven
www.it-ebooks.info
Program Arcade Games: With Python and Pygame, Fourth Edition
Copyright © 2016 by Paul Vincent Craven
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with
reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed
on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or
parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its
current version, and permission for use must always be obtained from Springer. Permissions for use may be
obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under
the respective Copyright Law.
ISBN-13 (pbk): 978-1-4842-1789-4
ISBN-13 (electronic): 978-1-4842-1790-0
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol
with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only
in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director: Welmoed Spahr
Lead Editor: Steve Anglin
Editorial Board: Steve Anglin, Pramila Balan, Louise Corrigan, Jonathan Gennick, Robert Hutchinson,
Celestin Suresh John, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie,
Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing
Coordinating Editor: Mark Powers
Copy Editor: Karen Jameson
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC
and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM
Finance Inc is a Delaware corporation
For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our Special
Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to
readers at www.apress.com/9781484217894. For detailed information about how to locate your book’s source
code, go to www.apress.com/source-code/. Readers can also access source code at SpringerLink in the
Supplementary Material section for each chapter.
www.it-ebooks.info
This book is dedicated to everyone who loves to learn.
www.it-ebooks.info
Contents at a Glance
About the Author ����������������������������������������������������������������������������������������������������xix
Introduction ������������������������������������������������������������������������������������������������������������xxi
■ Chapter 1: Before Getting Started… ����������������������������������������������������������������������1
■ Chapter 2: Create a Custom Calculator ���������������������������������������������������������������11
■ Chapter 3: What Is a Computer Language? ���������������������������������������������������������33
■ Chapter 4: Quiz Games and If Statements �����������������������������������������������������������41
■ Chapter 5: Guessing Games with Random Numbers and Loops ��������������������������59
■ Chapter 6: Introduction to Graphics ��������������������������������������������������������������������81
■ Chapter 7: Back to Looping �������������������������������������������������������������������������������107
■ Chapter 8: Introduction to Lists �������������������������������������������������������������������������121
■ Chapter 9: Introduction to Animation ����������������������������������������������������������������137
■ Chapter 10: Functions ���������������������������������������������������������������������������������������153
■ Chapter 11: Controllers and Graphics ���������������������������������������������������������������179
■ Chapter 12: Bitmapped Graphics and Sound �����������������������������������������������������205
■ Chapter 13: Introduction to Classes ������������������������������������������������������������������217
■ Chapter 14: Introduction to Sprites �������������������������������������������������������������������247
■ Chapter 15: Libraries and Modules �������������������������������������������������������������������267
■ Chapter 16: Searching ���������������������������������������������������������������������������������������273
■ Chapter 17: Array-Backed Grids ������������������������������������������������������������������������287
v
www.it-ebooks.info
■ Contents at a GlanCe
■ Chapter 18: Sorting �������������������������������������������������������������������������������������������301
■ Chapter 19: Exceptions ��������������������������������������������������������������������������������������311
■ Chapter 20: Recursion ���������������������������������������������������������������������������������������319
■ Chapter 21: Formatting �������������������������������������������������������������������������������������335
■ Chapter 22: Exercises ����������������������������������������������������������������������������������������347
Index ���������������������������������������������������������������������������������������������������������������������389
vi
www.it-ebooks.info
Contents
About the Author ����������������������������������������������������������������������������������������������������xix
Introduction ������������������������������������������������������������������������������������������������������������xxi
■ Chapter 1: Before Getting Started… ����������������������������������������������������������������������1
Installing and Starting Python ������������������������������������������������������������������������������������������1
Windows Installation ������������������������������������������������������������������������������������������������������������������������������1
Mac Installation ��������������������������������������������������������������������������������������������������������������������������������������3
Unix Installation ��������������������������������������������������������������������������������������������������������������������������������������6
Optional Wing IDE �����������������������������������������������������������������������������������������������������������������������������������7
Viewing File Extensions ����������������������������������������������������������������������������������������������������9
Learn to Make Games and Get Paid ���������������������������������������������������������������������������������9
Get the Most from This Book ����������������������������������������������������������������������������������������������������������������10
Send Feedback �������������������������������������������������������������������������������������������������������������������������������������10
■ Chapter 2: Create a Custom Calculator ���������������������������������������������������������������11
Printing ���������������������������������������������������������������������������������������������������������������������������12
Printing Text ������������������������������������������������������������������������������������������������������������������������������������������12
Printing Results of Expressions ������������������������������������������������������������������������������������������������������������13
Printing Multiple Items �������������������������������������������������������������������������������������������������������������������������13
Escape Codes �����������������������������������������������������������������������������������������������������������������14
Comments ����������������������������������������������������������������������������������������������������������������������15
Assignment Operators ����������������������������������������������������������������������������������������������������16
Variables �������������������������������������������������������������������������������������������������������������������������18
Operators ������������������������������������������������������������������������������������������������������������������������19
Operator Spacing ����������������������������������������������������������������������������������������������������������������������������������20
vii
www.it-ebooks.info
■ Contents
Order of Operations ��������������������������������������������������������������������������������������������������������20
Trig Functions �����������������������������������������������������������������������������������������������������������������20
Custom Equation Calculators �����������������������������������������������������������������������������������������21
Review ����������������������������������������������������������������������������������������������������������������������������25
Multiple Choice Quiz �����������������������������������������������������������������������������������������������������������������������������25
Short Answer Worksheet ����������������������������������������������������������������������������������������������������������������������28
Exercise ������������������������������������������������������������������������������������������������������������������������������������������������31
■ Chapter 3: What Is a Computer Language? ���������������������������������������������������������33
Short History of Programming ����������������������������������������������������������������������������������������33
Review ����������������������������������������������������������������������������������������������������������������������������38
Multiple Choice Quiz �����������������������������������������������������������������������������������������������������������������������������38
Short Answer Worksheet ����������������������������������������������������������������������������������������������������������������������40
Exercise ������������������������������������������������������������������������������������������������������������������������������������������������40
■ Chapter 4: Quiz Games and If Statements �����������������������������������������������������������41
Basic Comparisons ���������������������������������������������������������������������������������������������������������41
Indentation ���������������������������������������������������������������������������������������������������������������������43
Using And/Or ������������������������������������������������������������������������������������������������������������������44
Boolean Variables �����������������������������������������������������������������������������������������������������������44
Else and Else If ���������������������������������������������������������������������������������������������������������������46
Text Comparisons �����������������������������������������������������������������������������������������������������������47
Multiple Text Possibilities ���������������������������������������������������������������������������������������������������������������������48
Case Insensitive Comparisons ��������������������������������������������������������������������������������������������������������������48
Example if Statements ���������������������������������������������������������������������������������������������������48
Review ����������������������������������������������������������������������������������������������������������������������������51
Multiple Choice Quiz �����������������������������������������������������������������������������������������������������������������������������51
Short Answer Worksheet ����������������������������������������������������������������������������������������������������������������������55
Exercise ������������������������������������������������������������������������������������������������������������������������������������������������57
viii
www.it-ebooks.info
■ Contents
■ Chapter 5: Guessing Games with Random Numbers and Loops ��������������������������59
for Loops ������������������������������������������������������������������������������������������������������������������������63
Counting by Numbers Other Than One �������������������������������������������������������������������������������������������������65
Nesting Loops ���������������������������������������������������������������������������������������������������������������������������������������66
Keeping a Running Total �����������������������������������������������������������������������������������������������������������������������66
Example for Loops ����������������������������������������������������������������������������������������������������������68
while Loops ��������������������������������������������������������������������������������������������������������������������70
Using Increment Operators �������������������������������������������������������������������������������������������������������������������70
Looping Until User Wants to Quit ����������������������������������������������������������������������������������������������������������71
Common Problems with while Loops ���������������������������������������������������������������������������������������������������72
Example while Loops �����������������������������������������������������������������������������������������������������72
Random Numbers �����������������������������������������������������������������������������������������������������������74
The randrange Function �����������������������������������������������������������������������������������������������������������������������74
The random Function ���������������������������������������������������������������������������������������������������������������������������74
Review ����������������������������������������������������������������������������������������������������������������������������75
Multiple Choice Quiz �����������������������������������������������������������������������������������������������������������������������������75
Short Answer Worksheet ����������������������������������������������������������������������������������������������������������������������78
Exercise ������������������������������������������������������������������������������������������������������������������������������������������������79
■ Chapter 6: Introduction to Graphics ��������������������������������������������������������������������81
Computer Coordinate Systems ���������������������������������������������������������������������������������������82
Pygame Library ��������������������������������������������������������������������������������������������������������������85
Colors �����������������������������������������������������������������������������������������������������������������������������85
Open a Window ��������������������������������������������������������������������������������������������������������������87
Interacting with the User ������������������������������������������������������������������������������������������������87
The Event Processing Loop�������������������������������������������������������������������������������������������������������������������88
Processing Each Frame ������������������������������������������������������������������������������������������������������������������������89
Ending the Program ��������������������������������������������������������������������������������������������������������90
Clearing the Screen ��������������������������������������������������������������������������������������������������������90
ix
www.it-ebooks.info
■ Contents
Flipping the Screen ��������������������������������������������������������������������������������������������������������90
Open a Blank Window ����������������������������������������������������������������������������������������������������90
Drawing Introduction ������������������������������������������������������������������������������������������������������92
Drawing Lines ����������������������������������������������������������������������������������������������������������������93
Drawing Lines with Loops and Offsets ���������������������������������������������������������������������������93
Drawing a Rectangle ������������������������������������������������������������������������������������������������������95
Drawing an Ellipse ���������������������������������������������������������������������������������������������������������95
Drawing an Arc ���������������������������������������������������������������������������������������������������������������96
Drawing a Polygon ���������������������������������������������������������������������������������������������������������96
Drawing Text �������������������������������������������������������������������������������������������������������������������97
Full Program Listing �������������������������������������������������������������������������������������������������������99
Review ��������������������������������������������������������������������������������������������������������������������������102
Multiple Choice Quiz ���������������������������������������������������������������������������������������������������������������������������102
Short Answer Worksheet ��������������������������������������������������������������������������������������������������������������������105
Exercise ����������������������������������������������������������������������������������������������������������������������������������������������106
■ Chapter 7: Back to Looping �������������������������������������������������������������������������������107
Print Statement End Characters �����������������������������������������������������������������������������������107
Advanced Looping Problems ����������������������������������������������������������������������������������������108
Review ��������������������������������������������������������������������������������������������������������������������������115
Multiple Choice Quiz ���������������������������������������������������������������������������������������������������������������������������115
Short Answer Worksheet ��������������������������������������������������������������������������������������������������������������������117
Exercise ����������������������������������������������������������������������������������������������������������������������������������������������119
■ Chapter 8: Introduction to Lists �������������������������������������������������������������������������121
Working with Lists ��������������������������������������������������������������������������������������������������������122
Iterating through a List �������������������������������������������������������������������������������������������������124
Adding to a List ������������������������������������������������������������������������������������������������������������125
Summing or Modifying a List ���������������������������������������������������������������������������������������126
Slicing Strings ��������������������������������������������������������������������������������������������������������������127
x
www.it-ebooks.info
Description:program arcade Games. With Python and Pygame. —. Fourth Edition. —. Dr. Paul Vincent Craven. Program Arcade Games www.it-ebooks.info