Editor

Pygame fundamentals

Pygame Online · 7 lessons

Learn what every game is made of, one short lesson at a time, with each example running beside the text.

  1. 1The game loopEvery pygame program is one loop that handles events, updates the game and draws a frame, about sixty times a second.
  2. 2Drawing shapes, colours and textA handful of drawing functions, a colour for each shape and positions measured from the top-left corner are enough to draw any game.
  3. 3Moving with the keyboardKeep where things are in variables, change those variables every frame, and let the keys decide by how much.
  4. 4Rectangles and collisionsA pygame.Rect holds a position and a size, and it can tell you when two things touch.
  5. 5Score, lives and game statesA program becomes a game when it keeps score, can be lost and can start again.
  6. 6Lists of things: spawning, timers and randomnessMost games have many of something: keep them in a list, add new ones on a timer and drop the ones you no longer need.
  7. 7Sprites and groupsIn a bigger game, a class for each kind of thing keeps the code tidy, and sprite groups update, draw and collide them all at once.

You need a little Python first: variables, if, loops and functions. The Python tutorials cover them. After the lessons, build whole games step by step in the pygame challenges.