O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Invent Your Own Computer Games with Python , 4th Edition

Book Description

Invent Your Own Computer Games with Python teaches you how to make your own versions of classic computer games.

Table of Contents

  1. Cover
  2. Title Page
  3. Copyright Page
  4. About the Author
  5. About the Technical Reviewer
  6. Brief Contents
  7. Contents in Detail
  8. Acknowledgments
  9. Introduction
    1. Who Is This Book For?
    2. About This Book
    3. How to Use This Book
      1. Line Numbers and Indentation
      2. Long Code Lines
    4. Downloading and Installing Python
    5. Starting IDLE
    6. Finding Help Online
  10. Chapter 1: The Interactive Shell
    1. Some Simple Math
      1. Integers and Floating-Point Numbers
      2. Expressions
    2. Evaluating Expressions
    3. Syntax Errors
    4. Storing Values in Variables
    5. Summary
  11. Chapter 2: Writing Programs
    1. String Values
    2. String Concatenation
    3. Writing Programs in IDLE’s File Editor
      1. Creating the Hello World Program
      2. Saving Your Program
      3. Running Your Program
    4. How the Hello World Program Works
      1. Comments for the Programmer
      2. Functions: Mini-Programs Inside Programs
      3. The End of the Program
    5. Naming Variables
    6. Summary
  12. Chapter 3: Guess the Number
    1. Sample Run of Guess the Number
    2. Source Code for Guess the Number
    3. Importing the random Module
    4. Generating Random Numbers with the random.randint() Function
    5. Welcoming the Player
    6. Flow Control Statements
      1. Using Loops to Repeat Code
      2. Grouping with Blocks
      3. Looping with for Statements
    7. Getting the Player’s Guess
    8. Converting Values with the int(), float(), and str() Functions
    9. The Boolean Data Type
      1. Comparison Operators
      2. Checking for True or False with Conditions
      3. Experimenting with Booleans, Comparison Operators, and Conditions
      4. The Difference Between = and ==
    10. if Statements
    11. Leaving Loops Early with the break Statement
    12. Checking Whether the Player Won
    13. Checking Whether the Player Lost
    14. Summary
  13. Chapter 4: A Joke-Telling Program
    1. Sample Run of Jokes
    2. Source Code for Jokes
    3. How the Code Works
    4. Escape Characters
    5. Single and Double Quotes
    6. The print() Function’s end Keyword Parameter
    7. Summary
  14. Chapter 5: Dragon Realm
    1. How to Play Dragon Realm
    2. Sample Run of Dragon Realm
    3. Flowchart for Dragon Realm
    4. Source Code for Dragon Realm
    5. Importing the random and time Modules
    6. Functions in Dragon Realm
      1. def Statements
      2. Calling a Function
      3. Where to Put Function Definitions
    7. Multiline Strings
    8. How to Loop with while Statements
    9. Boolean Operators
      1. The and Operator
      2. The or Operator
      3. The not Operator
      4. Evaluating Boolean Operators
    10. Return Values
    11. Global Scope and Local Scope
    12. Function Parameters
    13. Displaying the Game Results
    14. Deciding Which Cave Has the Friendly Dragon
    15. The Game Loop
      1. Calling the Functions in the Program
      2. Asking the Player to Play Again
    16. Summary
  15. Chapter 6: Using the Debugger
    1. Types of Bugs
    2. The Debugger
      1. Starting the Debugger
      2. Stepping Through the Program with the Debugger
    3. Finding the Bug
    4. Setting Breakpoints
    5. Using Breakpoints
    6. Summary
  16. Chapter 7: Designing Hangman with Flowcharts
    1. How to Play Hangman
    2. Sample Run of Hangman
    3. ASCII Art
    4. Designing a Program with a Flowchart
      1. Creating the Flowchart
      2. Branching from a Flowchart Box
      3. Ending or Restarting the Game
      4. Guessing Again
      5. Offering Feedback to the Player
    5. Summary
  17. Chapter 8: Writing the Hangman Code
    1. Source Code for Hangman
    2. Importing the random Module
    3. Constant Variables
    4. The Lists Data Type
      1. Accessing Items with Indexes
      2. List Concatenation
      3. The in Operator
    5. Calling Methods
      1. The reverse() and append() List Methods
      2. The split() String Method
    6. Getting a Secret Word from the Word List
    7. Displaying the Board to the Player
      1. The list() and range() Functions
      2. List and String Slicing
      3. Displaying the Secret Word with Blanks
    8. Getting the Player’s Guess
      1. The lower() and upper() String Methods
      2. Leaving the while Loop
    9. elif Statements
    10. Making Sure the Player Entered a Valid Guess
    11. Asking the Player to Play Again
    12. Review of the Hangman Functions
    13. The Game Loop
      1. Calling the displayBoard() Function
      2. Letting the Player Enter Their Guess
      3. Checking Whether the Letter Is in the Secret Word
      4. Checking Whether the Player Won
      5. Handling an Incorrect Guess
      6. Checking Whether the Player Lost
      7. Ending or Resetting the Game
    14. Summary
  18. Chapter 9: Extending Hangman
    1. Adding More Guesses
    2. The Dictionary Data Type
      1. Getting the Size of Dictionaries with len()
      2. The Difference Between Dictionaries and Lists
      3. The keys() and values() Dictionary Methods
      4. Using Dictionaries of Words in Hangman
    3. Randomly Choosing from a List
    4. Deleting Items from Lists
    5. Multiple Assignment
    6. Printing the Word Category for the Player
    7. Summary
  19. Chapter 10: Tic-Tac-Toe
    1. Sample Run of Tic-Tac-Toe
    2. Source Code for Tic-Tac-Toe
    3. Designing the Program
      1. Representing the Board as Data
      2. Strategizing with the Game AI
    4. Importing the random Module
    5. Printing the Board on the Screen
    6. Letting the Player Choose X or O
    7. Deciding Who Goes First
    8. Placing a Mark on the Board
      1. List References
      2. Using List References in makeMove()
    9. Checking Whether the Player Won
    10. Duplicating the Board Data
    11. Checking Whether a Space on the Board Is Free
    12. Letting the Player Enter a Move
    13. Short-Circuit Evaluation
    14. Choosing a Move from a List of Moves
    15. The None Value
    16. Creating the Computer’s AI
      1. Checking Whether the Computer Can Win in One Move
      2. Checking Whether the Player Can Win in One Move
      3. Checking the Corner, Center, and Side Spaces (in That Order)
      4. Checking Whether the Board Is Full
    17. The Game Loop
      1. Choosing the Player’s Mark and Who Goes First
      2. Running the Player’s Turn
      3. Running the Computer’s Turn
      4. Asking the Player to Play Again
    18. Summary
  20. Chapter 11: The Bagels Deduction Game
    1. Sample Run of Bagels
    2. Source Code for Bagels
    3. Flowchart for Bagels
    4. Importing random and Defining getSecretNum()
    5. Shuffling a Unique Set of Digits
      1. Changing List Item Order with the random.shuffle() Function
      2. Getting the Secret Number from the Shuffled Digits
    6. Augmented Assignment Operators
    7. Calculating the Clues to Give
    8. The sort() List Method
    9. The join() String Method
    10. Checking Whether a String Has Only Numbers
    11. Starting the Game
    12. String Interpolation
    13. The Game Loop
      1. Getting the Player’s Guess
      2. Getting the Clues for the Player’s Guess
      3. Checking Whether the Player Won or Lost
      4. Asking the Player to Play Again
    14. Summary
  21. Chapter 12: The Cartesian Coordinate System
    1. Grids and Cartesian Coordinates
    2. Negative Numbers
    3. The Coordinate System of a Computer Screen
    4. Math Tricks
      1. Trick 1: A Minus Eats the Plus Sign on Its Left
      2. Trick 2: Two Minuses Combine into a Plus
      3. Trick 3: Two Numbers Being Added Can Swap Places
    5. Absolute Values and the abs() Function
    6. Summary
  22. Chapter 13: Sonar Treasure Hunt
    1. Sample Run of Sonar Treasure Hunt
    2. Source Code for Sonar Treasure Hunt
    3. Designing the Program
    4. Importing the random, sys, and math Modules
    5. Creating a New Game Board
    6. Drawing the Game Board
      1. Drawing the X-Coordinates Along the Top of the Board
      2. Drawing the Ocean
      3. Printing a Row in the Ocean
      4. Drawing the X-Coordinates Along the Bottom of the Board
    7. Creating the Random Treasure Chests
    8. Determining Whether a Move Is Valid
    9. Placing a Move on the Board
      1. Finding the Closest Treasure Chest
      2. Removing Values with the remove() List Method
      3. Getting the Player’s Move
    10. Printing the Game Instructions for the Player
    11. The Game Loop
      1. Displaying the Game Status for the Player
      2. Handling the Player’s Move
      3. Finding a Sunken Treasure Chest
      4. Checking Whether the Player Won
      5. Checking Whether the Player Lost
      6. Terminating the Program with the sys.exit() Function
    12. Summary
  23. Chapter 14: Caesar Cipher
    1. Cryptography and Encryption
    2. How the Caesar Cipher Works
    3. Sample Run of Caesar Cipher
    4. Source Code for Caesar Cipher
    5. Setting the Maximum Key Length
    6. Deciding to Encrypt or Decrypt the Message
    7. Getting the Message from the Player
    8. Getting the Key from the Player
    9. Encrypting or Decrypting the Message
      1. Finding Passed Strings with the find() String Method
      2. Encrypting or Decrypting Each Letter
    10. Starting the Program
    11. The Brute-Force Technique
    12. Adding the Brute-Force Mode
    13. Summary
  24. Chapter 15: The Reversegam Game
    1. How to Play Reversegam
    2. Sample Run of Reversegam
    3. Source Code for Reversegam
    4. Importing Modules and Setting Up Constants
    5. The Game Board Data Structure
      1. Drawing the Board Data Structure on the Screen
      2. Creating a Fresh Board Data Structure
    6. Checking Whether a Move Is Valid
      1. Checking Each of the Eight Directions
      2. Finding Out Whether There Are Tiles to Flip Over
    7. Checking for Valid Coordinates
      1. Getting a List with All Valid Moves
      2. Calling the bool() Function
    8. Getting the Score of the Game Board
    9. Getting the Player’s Tile Choice
    10. Determining Who Goes First
    11. Placing a Tile on the Board
    12. Copying the Board Data Structure
    13. Determining Whether a Space Is on a Corner
    14. Getting the Player’s Move
    15. Getting the Computer’s Move
      1. Strategizing with Corner Moves
      2. Getting a List of the Highest-Scoring Moves
    16. Printing the Scores to the Screen
    17. Starting the Game
      1. Checking for a Stalemate
      2. Running the Player’s Turn
      3. Running the Computer’s Turn
    18. The Game Loop
    19. Asking the Player to Play Again
    20. Summary
  25. Chapter 16: Reversegam AI Simulation
    1. Making the Computer Play Against Itself
      1. Sample Run of Simulation 1
      2. Source Code for Simulation 1
      3. Removing the Player Prompts and Adding a Computer Player
    2. Making the Computer Play Itself Several Times
      1. Sample Run of Simulation 2
      2. Source Code for Simulation 2
      3. Keeping Track of Multiple Games
      4. Commenting Out print() Function Calls
      5. Using Percentages to Grade the AIs
    3. Comparing Different AI Algorithms
      1. Source Code for Simulation 3
      2. How the AIs Work in Simulation 3
      3. Comparing the AIs
    4. Summary
  26. Chapter 17: Creating Graphics
    1. Installing pygame
    2. Hello World in pygame
    3. Sample Run of pygame Hello World
    4. Source Code for pygame Hello World
    5. Importing the pygame Module
    6. Initializing pygame
    7. Setting Up the pygame Window
      1. Tuples
      2. Surface Objects
    8. Setting Up Color Variables
    9. Writing Text on the pygame Window
      1. Using Fonts to Style Text
      2. Rendering a Font Object
      3. Setting the Text Location with Rect Attributes
    10. Filling a Surface Object with a Color
    11. pygame’s Drawing Functions
      1. Drawing a Polygon
      2. Drawing a Line
      3. Drawing a Circle
      4. Drawing an Ellipse
      5. Drawing a Rectangle
      6. Coloring Pixels
    12. The blit() Method for Surface Objects
    13. Drawing the Surface Object to the Screen
    14. Events and the Game Loop
      1. Getting Event Objects
      2. Exiting the Program
    15. Summary
  27. Chapter 18: Animating Graphics
    1. Sample Run of the Animation Program
    2. Source Code for the Animation Program
    3. Moving and Bouncing the Boxes
    4. Setting Up the Constant Variables
      1. Constant Variables for Direction
      2. Constant Variables for Color
    5. Setting Up the Box Data Structures
    6. The Game Loop
      1. Handling When the Player Quits
      2. Moving Each Box
      3. Bouncing a Box
      4. Drawing the Boxes on the Window in Their New Positions
      5. Drawing the Window on the Screen
    7. Summary
  28. Chapter 19: Collision Detection
    1. Sample Run of the Collision Detection Program
    2. Source Code for the Collision Detection Program
    3. Importing the Modules
    4. Using a Clock to Pace the Program
    5. Setting Up the Window and Data Structures
    6. Setting Up Variables to Track Movement
    7. Handling Events
      1. Handling the KEYDOWN Event
      2. Handling the KEYUP Event
    8. Teleporting the Player
    9. Adding New Food Squares
    10. Moving the Player Around the Window
      1. Drawing the Player on the Window
      2. Checking for Collisions
    11. Drawing the Food Squares on the Window
    12. Summary
  29. Chapter 20: Using Sounds and Images
    1. Adding Images with Sprites
    2. Sound and Image Files
    3. Sample Run of the Sprites and Sounds Program
    4. Source Code for the Sprites and Sounds Program
    5. Setting Up the Window and the Data Structure
      1. Adding a Sprite
      2. Changing the Size of a Sprite
    6. Setting Up the Music and Sounds
      1. Adding Sound Files
      2. Toggling the Sound On and Off
    7. Drawing the Player on the Window
    8. Checking for Collisions
    9. Drawing the Cherries on the Window
    10. Summary
  30. Chapter 21: A Dodger Game with Sounds and Images
    1. Review of the Basic pygame Data Types
    2. Sample Run of Dodger
    3. Source Code for Dodger
    4. Importing the Modules
    5. Setting Up the Constant Variables
    6. Defining Functions
      1. Ending and Pausing the Game
      2. Keeping Track of Baddie Collisions
      3. Drawing Text to the Window
    7. Initializing pygame and Setting Up the Window
    8. Setting Up Font, Sound, and Image Objects
    9. Displaying the Start Screen
    10. Starting the Game
    11. The Game Loop
      1. Handling Keyboard Events
      2. Handling Mouse Movement
    12. Adding New Baddies
    13. Moving the Player’s Character and the Baddies
    14. Implementing the Cheat Codes
    15. Removing the Baddies
    16. Drawing the Window
      1. Drawing the Player’s Score
      2. Drawing the Player’s Character and Baddies
    17. Checking for Collisions
    18. The Game Over Screen
    19. Modifying the Dodger Game
    20. Summary
  31. Index
  32. Resources
  33. The Electronic Frontier Foundation (EFF)
  34. Don’t Just Play Games—Make Them!