Pac-Man
Note
Requires: session 9 finished.
A maze of blue walls. White dots everywhere. Four coloured ghosts coming at you. Eat all the dots to win. Touch a ghost and it’s game over.
Getting started
Download the starter code and unpack the ZIP. Open main.py in Thonny and click Run. The maze appears and Pac-Man is ready to go. You add the movement, the collisions and the ghosts.
How does it work?
Pac-Man moves in pixels, not in tiles. Every frame he moves snelheid × dt pixels in his direction. dt is the time (in seconds) the previous frame took. That way the game moves just as fast on slow and fast computers.
nieuw_x = self.rect.x + self.dx * PACMAN_SNL * dtYou check a wall collision by making a test rect at the new position and seeing whether it overlaps with a wall block:
test = self.rect.copy()
test.x = nieuw_x
test.y = nieuw_y
botsing = any(test.colliderect(m.rect) for m in muren)✅ Basic
- Let Pac-Man move in his current direction (the arrow keys are already there).
- Add wall collision: stop when the new position touches a wall.
- Make the direction change work: only switch to the new direction when it’s free.
⭐ Stretch
- Eat white dots: when Pac-Man touches a dot, remove it and count 10 points.
- Eat yellow power dots: remove, count 50 points and set
self.powered_up = True. - Win when all the dots are gone: set
self.staat = GEWONNEN.
🔥 Expert
- Let the ghosts move: try directions and pick a free one (no wall collision).
- Let the ghosts hunt Pac-Man: pick the direction that makes the distance to Pac-Man smaller.
- If a ghost touches Pac-Man and
powered_upis False: game over. - If a ghost touches Pac-Man and
powered_upis True: eat the ghost (+200 points).
Downloads
- Starter code:
main.py+ sound files; you fill in the movement and collisions.