Phu Xuan University Library
Making Games with Python and Pygame

External

Making Games with Python and Pygame

This book will teach you how to make graphical computer games in the Python programming language using the Pygame library.This book assumes you know a little bit about Python or programming in general. If you don’t know how to program, you can learn by downloading the free book "Invent Your Own Computer Games with Python" from http://inventwithpython.com. Or you can jump right into this book and mostly pick it up along the way. This book is for the intermediate programmer who has learned what va

Author
Al Sweigart
Publisher
LibreTexts
Source
LibreTexts
License
CC BY-NC-SA
Language
EN
Category
Information Technology
Contents· 272 sections
  1. 01: Introduction
  2. 1.01: Who Is This Book For
  3. 1.02: About This Book
  4. 02: Installing Python and Pygame
  5. 2.01: What You Should Know Before You Begin
  6. 2.02: Downloading and Installing Python
  7. 2.03: Windows Instructions
  8. 2.04: Mac OS X Instructions
  9. 2.05: Ubuntu and Linux Instructions
  10. 2.06: Starting Python
  11. 2.07: Installing Pygame
  12. 2.08: How to Use This Book
  13. 2.09: The Featured Programs
  14. 2.10: Downloading Graphics and Sound Files
  15. 2.11: Line Numbers and Spaces
  16. 2.12: Text Wrapping in This Book
  17. 2.13: Checking Your Code Online
  18. 2.14: More Info Links on invpy.com
  19. 03: Pygame Basics
  20. 3.01: GUI vs. CLI
  21. 3.02: Source Code for Hello World with Pygame
  22. 3.03: Setting Up a Pygame Program
  23. 3.04: Game Loops and Game States
  24. 3.05: pygame.event.Event Objects
  25. 3.06: The QUIT Event and pygame.quit() Function
  26. 3.07: Pixel Coordinates
  27. 3.08: A Reminder About Functions Methods Constructor Functions and Functions in Modules (and the Difference Between Them)
  28. 3.09: Surface Objects and The Window
  29. 3.10: Colors
  30. 3.11: Transparent Colors
  31. 3.12: pygame.Color Objects
  32. 3.13: Rect Objects
  33. 3.14: Primitive Drawing Functions
  34. 3.15: pygame.PixelArray Objects
  35. 3.16: The pygame.display.update() Function
  36. 3.17: Animation
  37. 3.18: Frames Per Second and pygame.time.Clock Objects
  38. 3.19: Drawing Images with pygame.image.load() and blit()
  39. 3.20: Fonts
  40. 3.21: Anti-Aliasing
  41. 3.22: Playing Sounds
  42. 3.23: Summary
  43. 04: Memory Puzzle
  44. 4.01: How to Play Memory Puzzle
  45. 4.02: Nested for Loops
  46. 4.03: Source Code of Memory Puzzle
  47. 4.04: Credits and Imports
  48. 4.05: Magic Numbers are Bad
  49. 4.06: Sanity Checks with assert Statements
  50. 4.07: Telling If a Number is Even or Odd
  51. 4.08: Crash Early and Crash Often
  52. 4.09: Making the Source Code Look Pretty
  53. 4.10: Using Constant Variables Instead of Strings
  54. 4.11: Making Sure We Have Enough Icons
  55. 4.12: Tuples vs. Lists, Immutable vs. Mutable
  56. 4.13: One Item Tuples Need a Trailing Comma
  57. 4.14: Converting Between Lists and Tuples
  58. 4.15: The global Statement, and Why Global Variables are Evil
  59. 4.16: Data Structures and 2D Lists
  60. 4.17: The “Start Game” Animation
  61. 4.18: The Game Loop
  62. 4.19: The Event Handling Loop
  63. 4.20: Checking Which Box The Mouse Cursor is Over
  64. 4.21: Handling the First Clicked Box
  65. 4.22: Handling a Mismatched Pair of Icons
  66. 4.23: Handling If the Player Won
  67. 4.24: Drawing the Game State to the Screen
  68. 4.25: Creating the “Revealed Boxes” Data Structure
  69. 4.26: Creating the Board Data Structure- Step 1 – Get All Possible Icons
  70. 4.27: Step 2 – Shuffling and Truncating the List of All Icons
  71. 4.28: Step 3 – Placing the Icons on the Board
  72. 4.29: Splitting a List into a List of Lists
  73. 4.30: Different Coordinate Systems
  74. 4.31: Converting from Pixel Coordinates to Box Coordinates
  75. 4.32: Drawing the Icon, and Syntactic Sugar
  76. 4.33: Syntactic Sugar with Getting a Board Space’s Icon’s Shape and Color
  77. 4.34: Drawing the Box Cover
  78. 4.35: Handling the Revealing and Covering Animation
  79. 4.36: Drawing the Entire Board
  80. 4.37: Drawing the Highlight
  81. 4.38: The "Start Game" Animation
  82. 4.39: Revealing and Covering the Groups of Boxes
  83. 4.40: The “Game Won” Animation
  84. 4.41: Telling if the Player Has Won
  85. 4.42: Why Bother Having a main() Function?
  86. 4.43: Why Bother With Readability?
  87. 4.44: Summary, and a Hacking Suggestion
  88. 05: Slide Puzzle
  89. 5.01: How to Play Slide Puzzle
  90. 5.02: Source Code to Slide Puzzle
  91. 5.03: Second Verse, Same as the First
  92. 5.04: Setting Up the Buttons
  93. 5.05: Being Smart By Using Stupid Code
  94. 5.06: The Main Game Loop
  95. 5.07: Clicking on the Buttons
  96. 5.08: Sliding Tiles with the Mouse
  97. 5.09: Sliding Tiles with the Keyboard
  98. 5.10: “Equal To One Of” Trick with the in Operator
  99. 5.11: WASD and Arrow Keys
  100. 5.12: Actually Performing the Tile Slide
  101. 5.13: IDLE and Terminating Pygame Programs
  102. 5.14: Checking for a Specific Event, and Posting Events to Pygame’s Event Queue
  103. 5.15: Creating the Board Data Structure
  104. 5.16: Not Tracking the Blank Position
  105. 5.17: Making a Move by Updating the Board Data Structure
  106. 5.18: When NOT to Use an Assertion
  107. 5.19: Getting a Not-So-Random Move
  108. 5.20: Converting Tile Coordinates to Pixel Coordinates
  109. 5.21: Converting from Pixel Coordinates to Board Coordinates
  110. 5.22: Drawing a Tile
  111. 5.23: The Making Text Appear on the Screen
  112. 5.24: Drawing the Board
  113. 5.25: Drawing the Border of the Board
  114. 5.26: Drawing the Buttons
  115. 5.27: Animating the Tile Slides
  116. 5.28: The copy() Surface Method
  117. 5.29: Creating a New Puzzle
  118. 5.30: Animating the Board Reset
  119. 5.31: Time vs. Memory Tradeoffs
  120. 5.32: Nobody Cares About a Few Bytes
  121. 5.33: Nobody Cares About a Few Million Nanoseconds
  122. 5.34: Summary
  123. 06: Simulate
  124. 6.01: How to Play Simulate
  125. 6.02: Source Code to Simulate
  126. 6.03: The Usual Starting Stuff
  127. 6.04: Setting Up the Buttons
  128. 6.05: The main() Function
  129. 6.06: Some Local Variables Used in This Program
  130. 6.07: Drawing the Board and Handling Input
  131. 6.08: Checking for Mouse Clicks
  132. 6.09: Checking for Keyboard Presses
  133. 6.10: The Two States of the Game Loop
  134. 6.11: Figuring Out if the Player Pressed the Right Buttons
  135. 6.12: Epoch Time
  136. 6.13: Drawing the Board to the Screen
  137. 6.14: Same Old terminate() Function
  138. 6.15: Reusing The Constant Variables
  139. 6.16: Animating the Button Flash
  140. 6.17: Drawing the Buttons
  141. 6.18: Animating the Background Change
  142. 6.19: The Game Over Animation
  143. 6.20: Converting from Pixel Coordinates to Buttons
  144. 6.21: Explicit is Better Than Implicit
  145. 07: Wormy
  146. 7.01: How to Play Wormy
  147. 7.02: Source Code to Wormy
  148. 7.03: The Grid
  149. 7.04: The Setup Code
  150. 7.05: The main() Function
  151. 7.06: A Separate runGame() Function
  152. 7.07: The Event Handling Loop
  153. 7.08: Collision Detection
  154. 7.09: Detecting Collisions with the Apple
  155. 7.10: Moving the Worm
  156. 7.11: The insert() List Method
  157. 7.12: Drawing the Screen
  158. 7.13: Drawing “Press a key” Text to the Screen
  159. 7.14: The checkForKeyPress() Function
  160. 7.15: The Start Screen
  161. 7.16: Rotating the Start Screen Text
  162. 7.17: Rotations Are Not Perfect
  163. 7.18: Deciding Where the Apple Appears
  164. 7.19: Game Over Screens
  165. 7.20: Drawing Functions
  166. 7.21: Don’t Reuse Variable Names
  167. 08: Tetromino
  168. 8.01: How to Play Tetromino
  169. 8.02: Some Tetromino Nomenclature
  170. 8.03: Source Code to Tetromino
  171. 8.04: The Usual Setup Code
  172. 8.05: Setting up Timing Constants for Holding Down Keys
  173. 8.06: More Setup Code
  174. 8.07: Setting Up the Piece Templates
  175. 8.08: Splitting a “Line of Code” Across Multiple Lines
  176. 8.09: The main() Function
  177. 8.10: The Start of a New Game
  178. 8.11: The Game Loop
  179. 8.12: The Event Handling Loop
  180. 8.13: Pausing the Game
  181. 8.14: Using Movement Variables to Handle User Input
  182. 8.15: Checking if a Slide or Rotation is Valid
  183. 8.16: Finding the Bottom
  184. 8.17: Moving by Holding Down the Key
  185. 8.18: Letting the Piece “Naturally” Fall
  186. 8.19: Drawing Everything on the Screen
  187. 8.20: makeTextObjs(), A Shortcut Function for Making Text
  188. 8.21: The Same Old terminate() Function
  189. 8.22: Waiting for a Key Press Event with the checkForKeyPress() Function
  190. 8.23: showTextScreen(), A Generic Text Screen Function
  191. 8.24: The checkForQuit() Function
  192. 8.25: The calculateLevelAndFallFreq() Function
  193. 8.26: Generating Pieces with the getNewPiece() Function
  194. 8.27: Adding Pieces to the Board Data Structure
  195. 8.28: Creating a New Board Data Structure
  196. 8.29: The isOnBoard() and isValidPosition() Functions
  197. 8.30: Checking for, and Removing, Complete Lines
  198. 8.31: Convert from Board Coordinates to Pixel Coordinates
  199. 8.32: Drawing a Box on the Board or Elsewhere on the Screen
  200. 8.33: Drawing Everything to the Screen
  201. 8.34: Drawing the Score and Level Text
  202. 8.35: Drawing a Piece on the Board or Elsewhere on the Screen
  203. 8.36: Drawing the “Next” Piece
  204. 8.37: Summary
  205. 09: Squirrel Eat Squirrel
  206. 9.01: How to Play Squirrel Eat Squirrel
  207. 9.02: The Design of Squirrel Eat Squirrel
  208. 9.03: Source Code to Squirrel Eat Squirrel
  209. 9.04: The Usual Setup Code
  210. 9.05: Describing the Data Structures
  211. 9.06: The main() Function
  212. 9.07: The pygame.transform.flip() Function
  213. 9.08: A More Detailed Game State than Usual
  214. 9.09: The Usual Text Creation Code
  215. 9.10: Cameras
  216. 9.11: The “Active Area”
  217. 9.12: Keeping Track of the Location of Things in the Game World
  218. 9.13: Starting Off with Some Grass
  219. 9.14: The Game Loop
  220. 9.15: Checking to Disable Invulnerability
  221. 9.16: Moving the Enemy Squirrels
  222. 9.17: Removing the Far Away Grass and Squirrel Objects
  223. 9.18: When Deleting Items in a List, Iterate Over the List in Reverse
  224. 9.19: Adding New Grass and Squirrel Objects
  225. 9.20: Camera Slack, and Moving the Camera View
  226. 9.21: Drawing the Background, Grass, Squirrels, and Health Meter
  227. 9.22: The Event Handling Loop
  228. 9.23: Moving the Player, and Accounting for Bounce
  229. 9.24: Collision Detection- Eat or Be Eaten
  230. 9.25: The Game Over Screen
  231. 9.26: Winning
  232. 9.27: Drawing a Graphical Health Meter
  233. 9.28: The Same Old terminate() Function
  234. 9.29: The Mathematics of the Sine Function
  235. 9.30: Backwards Compatibility with Python Version 2
  236. 9.31: The getRandomVelocity() Function
  237. 9.32: Finding a Place to Add New Squirrels and Grass
  238. 9.33: Creating Enemy Squirrel Data Structures
  239. 9.34: Flipping the Squirrel Image
  240. 9.35: Creating Grass Data Structures
  241. 9.36: Checking if Outside the Active Area
  242. 9.37: Summary
  243. 10: Star Pusher
  244. 10.01: How to Play Star Pusher
  245. 10.02: Source Code to Star Pusher
  246. 10.03: The Initial Setup
  247. 10.04: Data Structures in Star Pusher
  248. 10.05: The “Game State” Data Structure
  249. 10.06: The “Map” Data Structure
  250. 10.07: The “Levels” Data Structure
  251. 10.08: Reading and Writing Text Files
  252. 10.09: Text Files and Binary Files
  253. 10.10: Writing to Files
  254. 10.11: Reading from Files
  255. 10.12: About the Star Pusher Map File Format
  256. 10.13: Recursive Functions
  257. 10.14: Stack Overflows
  258. 10.15: Preventing Stack Overflows with a Base Case
  259. 10.16: The Flood Fill Algorithm
  260. 10.17: Drawing the Map
  261. 10.18: Checking if the Level is Finished
  262. 10.19: Summary
  263. 11: Four Extra Games
  264. 11.01: Flippy, an “Othello” Clone
  265. 11.02: Source Code for Flippy
  266. 11.03: Ink Spill, a “Flood It” Clone
  267. 11.04: Source Code for Ink Spill
  268. 11.05: Four-In-A-Row, a “Connect Four” Clone
  269. 11.06: Source Code for Four-In-A-Row
  270. 11.07: Gemgem, a “Bejeweled” Clone
  271. 11.08: Source Code for Gemgem
  272. 11.09: Summary