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.
- 1The game loopEvery pygame program is one loop that handles events, updates the game and draws a frame, about sixty times a second.
- 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.
- 3Moving with the keyboardKeep where things are in variables, change those variables every frame, and let the keys decide by how much.
- 4Rectangles and collisionsA
pygame.Rectholds a position and a size, and it can tell you when two things touch. - 5Score, lives and game statesA program becomes a game when it keeps score, can be lost and can start again.
- 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.
- 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.