Making Games with Python and Pygame

by Al Sweigart

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

Description from LibreTexts.

Subjects
Engineering, Computer Science
Licence
CC BY-NC-SA 3.0, as LibreTexts states it. Non-commercial use only.
Who can read
Tweens profiles and older, judged from its title and subjects

Contents

LibreTexts writes equations in TeX, and here they show as TeX; the PDF has them typeset.

  1. Front Matter
  2. TitlePage
  3. InfoPage
  4. Table of Contents
  5. Licensing
  6. 1: Introduction
  7. Front Matter
  8. TitlePage
  9. InfoPage
  10. 1.1: Who Is This Book For?
  11. 1.2: About This Book
  12. Back Matter
  13. Index
  14. 2: Installing Python and Pygame
  15. Front Matter
  16. TitlePage
  17. InfoPage
  18. 2.1: What You Should Know Before You Begin
  19. 2.2: Downloading and Installing Python
  20. 2.3: Windows Instructions
  21. 2.4: Mac OS X Instructions
  22. 2.5: Ubuntu and Linux Instructions
  23. 2.6: Starting Python
  24. 2.7: Installing Pygame
  25. 2.8: How to Use This Book
  26. 2.9: The Featured Programs
  27. 2.10: Downloading Graphics and Sound Files
  28. 2.11: Line Numbers and Spaces
  29. 2.12: Text Wrapping in This Book
  30. 2.13: Checking Your Code Online
  31. 2.14: More Info Links on invpy.com
  32. Back Matter
  33. Index
  34. 3: Pygame Basics
  35. Front Matter
  36. TitlePage
  37. InfoPage
  38. 3.1: GUI vs. CLI
  39. 3.2: Source Code for Hello World with Pygame
  40. 3.3: Setting Up a Pygame Program
  41. 3.4: Game Loops and Game States
  42. 3.5: pygame.event.Event Objects
  43. 3.6: The QUIT Event and pygame.quit() Function
  44. 3.7: Pixel Coordinates
  45. 3.8: A Reminder About Functions, Methods, Constructor Functions, and Functions in Modules (and the Difference Between Them)
  46. 3.9: Surface Objects and The Window
  47. 3.10: Colors
  48. 3.11: Transparent Colors
  49. 3.12: pygame.Color Objects
  50. 3.13: Rect Objects
  51. 3.14: Primitive Drawing Functions
  52. 3.15: pygame.PixelArray Objects
  53. 3.16: The pygame.display.update() Function
  54. 3.17: Animation
  55. 3.18: Frames Per Second and pygame.time.Clock Objects
  56. 3.19: Drawing Images with pygame.image.load() and blit()
  57. 3.20: Fonts
  58. 3.21: Anti-Aliasing
  59. 3.22: Playing Sounds
  60. 3.23: Summary
  61. Back Matter
  62. Index
  63. 4: Memory Puzzle
  64. Front Matter
  65. TitlePage
  66. InfoPage
  67. 4.1: How to Play Memory Puzzle
  68. 4.2: Nested for Loops
  69. 4.3: Source Code of Memory Puzzle
  70. 4.4: Credits and Imports
  71. 4.5: Magic Numbers are Bad
  72. 4.6: Sanity Checks with assert Statements
  73. 4.7: Telling If a Number is Even or Odd
  74. 4.8: Crash Early and Crash Often!
  75. 4.9: Making the Source Code Look Pretty
  76. 4.10: Using Constant Variables Instead of Strings
  77. 4.11: Making Sure We Have Enough Icons
  78. 4.12: Tuples vs. Lists, Immutable vs. Mutable
  79. 4.13: One Item Tuples Need a Trailing Comma
  80. 4.14: Converting Between Lists and Tuples
  81. 4.15: The global Statement, and Why Global Variables are Evil
  82. 4.16: Data Structures and 2D Lists
  83. 4.17: The “Start Game” Animation
  84. 4.18: The Game Loop
  85. 4.19: The Event Handling Loop
  86. 4.20: Checking Which Box The Mouse Cursor is Over
  87. 4.21: Handling the First Clicked Box
  88. 4.22: Handling a Mismatched Pair of Icons
  89. 4.23: Handling If the Player Won
  90. 4.24: Drawing the Game State to the Screen
  91. 4.25: Creating the “Revealed Boxes” Data Structure
  92. 4.26: Creating the Board Data Structure- Step 1 – Get All Possible Icons
  93. 4.27: Step 2 – Shuffling and Truncating the List of All Icons
  94. 4.28: Step 3 – Placing the Icons on the Board
  95. 4.29: Splitting a List into a List of Lists
  96. 4.30: Different Coordinate Systems
  97. 4.31: Converting from Pixel Coordinates to Box Coordinates
  98. 4.32: Drawing the Icon, and Syntactic Sugar
  99. 4.33: Syntactic Sugar with Getting a Board Space’s Icon’s Shape and Color
  100. 4.34: Drawing the Box Cover
  101. 4.35: Handling the Revealing and Covering Animation
  102. 4.36: Drawing the Entire Board
  103. 4.37: Drawing the Highlight
  104. 4.38: The "Start Game" Animation
  105. 4.39: Revealing and Covering the Groups of Boxes
  106. 4.40: The “Game Won” Animation
  107. 4.41: Telling if the Player Has Won
  108. 4.42: Why Bother Having a main() Function?
  109. 4.43: Why Bother With Readability?
  110. 4.44: Summary, and a Hacking Suggestion
  111. Back Matter
  112. Index
  113. 5: Slide Puzzle
  114. Front Matter
  115. TitlePage
  116. InfoPage
  117. 5.1: How to Play Slide Puzzle
  118. 5.2: Source Code to Slide Puzzle
  119. 5.3: Second Verse, Same as the First
  120. 5.4: Setting Up the Buttons
  121. 5.5: Being Smart By Using Stupid Code
  122. 5.6: The Main Game Loop
  123. 5.7: Clicking on the Buttons
  124. 5.8: Sliding Tiles with the Mouse
  125. 5.9: Sliding Tiles with the Keyboard
  126. 5.10: “Equal To One Of” Trick with the in Operator
  127. 5.11: WASD and Arrow Keys
  128. 5.12: Actually Performing the Tile Slide
  129. 5.13: IDLE and Terminating Pygame Programs
  130. 5.14: Checking for a Specific Event, and Posting Events to Pygame’s Event Queue
  131. 5.15: Creating the Board Data Structure
  132. 5.16: Not Tracking the Blank Position
  133. 5.17: Making a Move by Updating the Board Data Structure
  134. 5.18: When NOT to Use an Assertion
  135. 5.19: Getting a Not-So-Random Move
  136. 5.20: Converting Tile Coordinates to Pixel Coordinates
  137. 5.21: Converting from Pixel Coordinates to Board Coordinates
  138. 5.22: Drawing a Tile
  139. 5.23: The Making Text Appear on the Screen
  140. 5.24: Drawing the Board
  141. 5.25: Drawing the Border of the Board
  142. 5.26: Drawing the Buttons
  143. 5.27: Animating the Tile Slides
  144. 5.28: The copy() Surface Method
  145. 5.29: Creating a New Puzzle
  146. 5.30: Animating the Board Reset
  147. 5.31: Time vs. Memory Tradeoffs
  148. 5.32: Nobody Cares About a Few Bytes
  149. 5.33: Nobody Cares About a Few Million Nanoseconds
  150. 5.34: Summary
  151. Back Matter
  152. Index
  153. 6: Simulate
  154. Front Matter
  155. TitlePage
  156. InfoPage
  157. 6.1: How to Play Simulate
  158. 6.2: Source Code to Simulate
  159. 6.3: The Usual Starting Stuff
  160. 6.4: Setting Up the Buttons
  161. 6.5: The main() Function
  162. 6.6: Some Local Variables Used in This Program
  163. 6.7: Drawing the Board and Handling Input
  164. 6.8: Checking for Mouse Clicks
  165. 6.9: Checking for Keyboard Presses
  166. 6.10: The Two States of the Game Loop
  167. 6.11: Figuring Out if the Player Pressed the Right Buttons
  168. 6.12: Epoch Time
  169. 6.13: Drawing the Board to the Screen
  170. 6.14: Same Old terminate() Function
  171. 6.15: Reusing The Constant Variables
  172. 6.16: Animating the Button Flash
  173. 6.17: Drawing the Buttons
  174. 6.18: Animating the Background Change
  175. 6.19: The Game Over Animation
  176. 6.20: Converting from Pixel Coordinates to Buttons
  177. 6.21: Explicit is Better Than Implicit
  178. Back Matter
  179. Index
  180. 7: Wormy
  181. Front Matter
  182. TitlePage
  183. InfoPage
  184. 7.1: How to Play Wormy
  185. 7.2: Source Code to Wormy
  186. 7.3: The Grid
  187. 7.4: The Setup Code
  188. 7.5: The main() Function
  189. 7.6: A Separate runGame() Function
  190. 7.7: The Event Handling Loop
  191. 7.8: Collision Detection
  192. 7.9: Detecting Collisions with the Apple
  193. 7.10: Moving the Worm
  194. 7.11: The insert() List Method
  195. 7.12: Drawing the Screen
  196. 7.13: Drawing “Press a key” Text to the Screen
  197. 7.14: The checkForKeyPress() Function
  198. 7.15: The Start Screen
  199. 7.16: Rotating the Start Screen Text
  200. 7.17: Rotations Are Not Perfect
  201. 7.18: Deciding Where the Apple Appears
  202. 7.19: Game Over Screens
  203. 7.20: Drawing Functions
  204. 7.21: Don’t Reuse Variable Names
  205. Back Matter
  206. Index
  207. 8: Tetromino
  208. Front Matter
  209. TitlePage
  210. InfoPage
  211. 8.1: How to Play Tetromino
  212. 8.2: Some Tetromino Nomenclature
  213. 8.3: Source Code to Tetromino
  214. 8.4: The Usual Setup Code
  215. 8.5: Setting up Timing Constants for Holding Down Keys
  216. 8.6: More Setup Code
  217. 8.7: Setting Up the Piece Templates
  218. 8.8: Splitting a “Line of Code” Across Multiple Lines
  219. 8.9: The main() Function
  220. 8.10: The Start of a New Game
  221. 8.11: The Game Loop
  222. 8.12: The Event Handling Loop
  223. 8.13: Pausing the Game
  224. 8.14: Using Movement Variables to Handle User Input
  225. 8.15: Checking if a Slide or Rotation is Valid
  226. 8.16: Finding the Bottom
  227. 8.17: Moving by Holding Down the Key
  228. 8.18: Letting the Piece “Naturally” Fall
  229. 8.19: Drawing Everything on the Screen
  230. 8.20: makeTextObjs(), A Shortcut Function for Making Text
  231. 8.21: The Same Old terminate() Function
  232. 8.22: Waiting for a Key Press Event with the checkForKeyPress() Function
  233. 8.23: showTextScreen(), A Generic Text Screen Function
  234. 8.24: The checkForQuit() Function
  235. 8.25: The calculateLevelAndFallFreq() Function
  236. 8.26: Generating Pieces with the getNewPiece() Function
  237. 8.27: Adding Pieces to the Board Data Structure
  238. 8.28: Creating a New Board Data Structure
  239. 8.29: The isOnBoard() and isValidPosition() Functions
  240. 8.30: Checking for, and Removing, Complete Lines
  241. 8.31: Convert from Board Coordinates to Pixel Coordinates
  242. 8.32: Drawing a Box on the Board or Elsewhere on the Screen
  243. 8.33: Drawing Everything to the Screen
  244. 8.34: Drawing the Score and Level Text
  245. 8.35: Drawing a Piece on the Board or Elsewhere on the Screen
  246. 8.36: Drawing the “Next” Piece
  247. 8.37: Summary
  248. Back Matter
  249. Index
  250. 9: Squirrel Eat Squirrel
  251. Front Matter
  252. TitlePage
  253. InfoPage
  254. 9.1: How to Play Squirrel Eat Squirrel
  255. 9.2: The Design of Squirrel Eat Squirrel
  256. 9.3: Source Code to Squirrel Eat Squirrel
  257. 9.4: The Usual Setup Code
  258. 9.5: Describing the Data Structures
  259. 9.6: The main() Function
  260. 9.7: The pygame.transform.flip() Function
  261. 9.8: A More Detailed Game State than Usual
  262. 9.9: The Usual Text Creation Code
  263. 9.10: Cameras
  264. 9.11: The “Active Area”
  265. 9.12: Keeping Track of the Location of Things in the Game World
  266. 9.13: Starting Off with Some Grass
  267. 9.14: The Game Loop
  268. 9.15: Checking to Disable Invulnerability
  269. 9.16: Moving the Enemy Squirrels
  270. 9.17: Removing the Far Away Grass and Squirrel Objects
  271. 9.18: When Deleting Items in a List, Iterate Over the List in Reverse
  272. 9.19: Adding New Grass and Squirrel Objects
  273. 9.20: Camera Slack, and Moving the Camera View
  274. 9.21: Drawing the Background, Grass, Squirrels, and Health Meter
  275. 9.22: The Event Handling Loop
  276. 9.23: Moving the Player, and Accounting for Bounce
  277. 9.24: Collision Detection- Eat or Be Eaten
  278. 9.25: The Game Over Screen
  279. 9.26: Winning
  280. 9.27: Drawing a Graphical Health Meter
  281. 9.28: The Same Old terminate() Function
  282. 9.29: The Mathematics of the Sine Function
  283. 9.30: Backwards Compatibility with Python Version 2
  284. 9.31: The getRandomVelocity() Function
  285. 9.32: Finding a Place to Add New Squirrels and Grass
  286. 9.33: Creating Enemy Squirrel Data Structures
  287. 9.34: Flipping the Squirrel Image
  288. 9.35: Creating Grass Data Structures
  289. 9.36: Checking if Outside the Active Area
  290. 9.37: Summary
  291. Back Matter
  292. Index
  293. 10: Star Pusher
  294. Front Matter
  295. TitlePage
  296. InfoPage
  297. 10.1: How to Play Star Pusher
  298. 10.2: Source Code to Star Pusher
  299. 10.3: The Initial Setup
  300. 10.4: Data Structures in Star Pusher
  301. 10.5: The “Game State” Data Structure
  302. 10.6: The “Map” Data Structure
  303. 10.7: The “Levels” Data Structure
  304. 10.8: Reading and Writing Text Files
  305. 10.9: Text Files and Binary Files
  306. 10.10: Writing to Files
  307. 10.11: Reading from Files
  308. 10.12: About the Star Pusher Map File Format
  309. 10.13: Recursive Functions
  310. 10.14: Stack Overflows
  311. 10.15: Preventing Stack Overflows with a Base Case
  312. 10.16: The Flood Fill Algorithm
  313. 10.17: Drawing the Map
  314. 10.18: Checking if the Level is Finished
  315. 10.19: Summary
  316. Back Matter
  317. Index
  318. 11: Four Extra Games
  319. Front Matter
  320. TitlePage
  321. InfoPage
  322. 11.1: Flippy, an “Othello” Clone
  323. 11.2: Source Code for Flippy
  324. 11.3: Ink Spill, a “Flood It” Clone
  325. 11.4: Source Code for Ink Spill
  326. 11.5: Four-In-A-Row, a “Connect Four” Clone
  327. 11.6: Source Code for Four-In-A-Row
  328. 11.7: Gemgem, a “Bejeweled” Clone
  329. 11.8: Source Code for Gemgem
  330. 11.9: Summary
  331. Back Matter
  332. Index
  333. Back Matter
  334. Front Matter
  335. TitlePage
  336. InfoPage
  337. About the Author
  338. Glossary
  339. Index
  340. Glossary
  341. Detailed Licensing
  342. Back Matter
  343. Index