Making Games with Python and Pygame
Al Sweigart
Making Games with Python and Pygame
Free
Description
Contents
Reviews

Making Games with Python & Pygame covers the Pygame library with the source code for 11 games. Making Games was written as a sequel for the same age range as Invent with Python. Once you have an understanding of the basics of Python programming, you can now expand your abilities using the Pygame library to make games with graphics, animation, and sound.



The book features the source code to 11 games. The games are clones of classics such as Nibbles, Tetris, Simon, Bejeweled, Othello, Connect Four, Flood It, and others.



Buy the book via Inventwithpython.com

Language
English
ISBN
978-1469901732
Who is this book for?
About This Book
Chapter 1 – Installing Python and Pygame
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
Chapter 2 – Pygame Basics
GUI vs. CLI
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
A Reminder About Functions, Methods, Constructor Functions, and Functions in Modules (and the Difference Between Them)
Surface Objects and The Window
Colors
Transparent Colors
pygame.Color Objects
Rect Objects
Primitive Drawing Functions
pygame.PixelArray Objects
The pygame.display.update() Function
Animation
Frames Per Second and pygame.time.Clock Objects
Drawing Images with pygame.image.load() and blit()
Fonts
Anti-Aliasing
Playing Sounds
Summary
Chapter 3 – Memory Puzzle
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
The “Start Game” Animation
Revealing and Covering the Groups of Boxes
The “Game Won” Animation
Telling if the Player Has Won
Why Bother Having a main() Function?
Why Bother With Readability?
Summary, and a Hacking Suggestion
Chapter 4 – Slide Puzzle
How to Play Slide Puzzle
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
Summary
Chapter 5 – Simulate
How to Play Simulate
Source Code to Simulate
The Usual Starting Stuff
Setting Up the Buttons
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
Drawing the Buttons
Animating the Background Change
The Game Over Animation
Converting from Pixel Coordinates to Buttons
Explicit is Better Than Implicit
Chapter 6 – Wormy
How to Play Wormy
Source Code to Wormy
The Grid
The Setup Code
The main() Function
A Separate runGame() Function
The Event Handling Loop
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
Chapter 7 - Tetromino
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
Splitting a “Line of Code” Across Multiple Lines
The main() Function
The Start of a New Game
The Game Loop
The Event Handling Loop
Pausing the Game
Using Movement Variables to Handle User Input
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
Drawing the “Next” Piece
Summary
Chapter 8 – Squirrel Eat Squirrel
How to Play Squirrel Eat Squirrel
The Design of Squirrel Eat Squirrel
Source Code to Squirrel Eat Squirrel
The Usual Setup Code
Describing the Data Structures
The main() Function
The pygame.transform.flip() Function
A More Detailed Game State than Usual
The Usual Text Creation Code
Cameras
The “Active Area”
Keeping Track of the Location of Things in the Game World
Starting Off with Some Grass
The Game Loop
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
The Event Handling Loop
Moving the Player, and Accounting for Bounce
Collision Detection: Eat or Be Eaten
The Game Over Screen
Winning
Drawing a Graphical Health Meter
The Same Old terminate() Function
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
Summary
Chapter 9 – Star Pusher
How to Play Star Pusher
Source Code to Star Pusher
The Initial Setup
Data Structures in Star Pusher
The “Game State” Data Structure
The “Map” 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
Recursive Functions
Stack Overflows
Preventing Stack Overflows with a Base Case
The Flood Fill Algorithm
Drawing the Map
Checking if the Level is Finished
Summary
Chapter 10 – Four Extra Games
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
Summary
Glossary
About the Author
The book hasn't received reviews yet.
You May Also Like