Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
Look up keyword or section
Like this

Table Of Contents

Chapter 1 – Installing Python and Pygame 1
What You Should Know Before You Begin
Downloading and Installing Python
Windows Instructions
Mac OS X Instructions
Ubuntu and Linux Instructions
Starting Python
Installing Pygame
How to Use This Book
The Featured Programs
Downloading Graphics and Sound Files
Line Numbers and Spaces
Text Wrapping in This Book
Checking Your Code Online
More Info Links on http://invpy.com
Source Code for Hello World with Pygame
Setting Up a Pygame Program
Game Loops and Game States
pygame.event.Event Objects
The QUIT Event and pygame.quit() Function
Pixel Coordinates
Surface Objects and The Window
Transparent Colors
pygame.Color Objects
Rect Objects
Primitive Drawing Functions
pygame.PixelArray Objects
The pygame.display.update() Function
Frames Per Second and pygame.time.Clock Objects
Drawing Images with pygame.image.load() and blit()
Playing Sounds
How to Play Memory Puzzle
Nested for Loops
Source Code of Memory Puzzle
Credits and Imports
Magic Numbers are Bad
Sanity Checks with assert Statements
Telling If a Number is Even or Odd
Crash Early and Crash Often!
Making the Source Code Look Pretty
Using Constant Variables Instead of Strings
Making Sure We Have Enough Icons
Tuples vs. Lists, Immutable vs. Mutable
One Item Tuples Need a Trailing Comma
Converting Between Lists and Tuples
The global statement, and Why Global Variables are Evil
Data Structures and 2D Lists
The “Start Game” Animation
The Game Loop
The Event Handling Loop
Checking Which Box The Mouse Cursor is Over
Handling the First Clicked Box
Handling a Mismatched Pair of Icons
Handling If the Player Won
Drawing the Game State to the Screen
Creating the “Revealed Boxes” Data Structure
Creating the Board Data Structure: Step 1 – Get All Possible Icons
Step 2 – Shuffling and Truncating the List of All Icons
Step 3 – Placing the Icons on the Board
Splitting a List into a List of Lists
Different Coordinate Systems
Converting from Pixel Coordinates to Box Coordinates
Drawing the Icon, and Syntactic Sugar
Syntactic Sugar with Getting a Board Space’s Icon’s Shape and Color
Drawing the Box Cover
Handling the Revealing and Covering Animation
Drawing the Entire Board
Drawing the Highlight
Revealing and Covering the Groups of Boxes
Telling if the Player Has Won
Why Bother Having a main() Function?
Why Bother With Readability?
Source Code to Slide Puzzle
Second Verse, Same as the First
Setting Up the Buttons
Being Smart By Using Stupid Code
The Main Game Loop
Clicking on the Buttons
Sliding Tiles with the Mouse
Sliding Tiles with the Keyboard
“Equal To One Of” Trick with the in Operator
WASD and Arrow Keys
Actually Performing the Tile Slide
IDLE and Terminating Pygame Programs
Checking for a Specific Event, and Posting Events to Pygame’s Event Queue
Creating the Board Data Structure
Not Tracking the Blank Position
Making a Move by Updating the Board Data Structure
When NOT to Use an Assertion
Getting a Not-So-Random Move
Converting Tile Coordinates to Pixel Coordinates
Converting from Pixel Coordinates to Board Coordinates
Drawing a Tile
The Making Text Appear on the Screen
Drawing the Board
Drawing the Border of the Board
Drawing the Buttons
Animating the Tile Slides
The copy() Surface Method
Creating a New Puzzle
Animating the Board Reset
Time vs. Memory Tradeoffs
Nobody Cares About a Few Bytes
Nobody Cares About a Few Million Nanoseconds
How to Play Simulate
Source Code to Simulate
The Usual Starting Stuff
The main() Function
Some Local Variables Used in This Program
Drawing the Board and Handling Input
Checking for Mouse Clicks
Checking for Keyboard Presses
The Two States of the Game Loop
Figuring Out if the Player Pressed the Right Buttons
Epoch Time
Drawing the Board to the Screen
Same Old terminate() Function
Reusing The Constant Variables
Animating the Button Flash
Animating the Background Change
The Game Over Animation
Converting from Pixel Coordinates to Buttons
Explicit is Better Than Implicit
How to Play Wormy
Source Code to Wormy
The Grid
The Setup Code
A Separate runGame() Function
Collision Detection
Detecting Collisions with the Apple
Moving the Worm
The insert() List Method
Drawing the Screen
Drawing “Press a key” Text to the Screen
The checkForKeyPress() Function
The Start Screen
Rotating the Start Screen Text
Rotations Are Not Perfect
Deciding Where the Apple Appears
Game Over Screens
Drawing Functions
Don’t Reuse Variable Names
How to Play Tetromino
Some Tetromino Nomenclature
Source Code to Tetromino
The Usual Setup Code
Setting up Timing Constants for Holding Down Keys
More Setup Code
Setting Up the Piece Templates
Checking if a Slide or Rotation is Valid
Finding the Bottom
Moving by Holding Down the Key
Letting the Piece “Naturally” Fall
Drawing Everything on the Screen
makeTextObjs(), A Shortcut Function for Making Text
The Same Old terminate() Function
Waiting for a Key Press Event with the checkForKeyPress() Function
showTextScreen(), A Generic Text Screen Function
The checkForQuit() Function
The calculateLevelAndFallFreq() Function
Generating Pieces with the getNewPiece() Function
Adding Pieces to the Board Data Structure
Creating a New Board Data Structure
The isOnBoard() and isValidPosition() Functions
Checking for, and Removing, Complete Lines
Convert from Board Coordinates to Pixel Coordinates
Drawing a Box on the Board or Elsewhere on the Screen
Drawing Everything to the Screen
Drawing the Score and Level Text
Drawing a Piece on the Board or Elsewhere on the Screen
How to Play Squirrel Eat Squirrel
The Design of Squirrel Eat Squirrel
Source Code to Squirrel Eat Squirrel
Describing the Data Structures
The pygame.transform.flip() Function
A More Detailed Game State than Usual
The Usual Text Creation Code
The “Active Area”
Keeping Track of the Location of Things in the Game World
Starting Off with Some Grass
Checking to Disable Invulnerability
Moving the Enemy Squirrels
Removing the Far Away Grass and Squirrel Objects
When Deleting Items in a List, Iterate Over the List in Reverse
Adding New Grass and Squirrel Objects
Camera Slack, and Moving the Camera View
Drawing the Background, Grass, Squirrels, and Health Meter
Moving the Player, and Accounting for Bounce
Collision Detection: Eat or Be Eaten
The Game Over Screen
Drawing a Graphical Health Meter
The Mathematics of the Sine Function
Backwards Compatibility with Python Version 2
The getRandomVelocity() Function
Finding a Place to Add New Squirrels and Grass
Creating Enemy Squirrel Data Structures
Flipping the Squirrel Image
Creating Grass Data Structures
Checking if Outside the Active Area
How to Play Star Pusher
Source Code to Star Pusher
The Initial Setup
Data Structures in Star Pusher
The “Game State” Data Structure
The “Levels” Data Structure
Reading and Writing Text Files
Text Files and Binary Files
Writing to Files
Reading from Files
About the Star Pusher Map File Format
Drawing the Map
Checking if the Level is Finished
Flippy, an “Othello” Clone
Source Code for Flippy
Ink Spill, a “Flood It” Clone
Source Code for Ink Spill
Four-In-A-Row, a “Connect Four” Clone
Source Code for Four-In-A-Row
Gemgem, a “Bejeweled” Clone
Source Code for Gemgem
0 of .
Results for:
No results containing your search query
P. 1
Making Games with Python & Pygame

Making Games with Python & Pygame

|Views: 34|Likes:
Published by stumari
By Al Sweigart
Copyright © 2012 by Albert Sweigart
Some Rights Reserved. ―Making Games with Python & Pygame‖) is licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
By Al Sweigart
Copyright © 2012 by Albert Sweigart
Some Rights Reserved. ―Making Games with Python & Pygame‖) is licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

More info:

Published by: stumari on Jan 24, 2013
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





You're Reading a Free Preview
Pages 8 to 89 are not shown in this preview.
You're Reading a Free Preview
Pages 97 to 187 are not shown in this preview.
You're Reading a Free Preview
Pages 195 to 296 are not shown in this preview.
You're Reading a Free Preview
Pages 304 to 354 are not shown in this preview.
You're Reading a Free Preview
Pages 362 to 365 are not shown in this preview.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->