Step 1 of 5 · Reading · ~4 min
Read
Production
Capstone: Mini Pong
Every lesson so far built one part of an engine. This one runs them together. Your program will take real frames of uneven length and key presses, turn them into fixed simulation steps, move paddles from held keys, integrate a ball, resolve collisions, keep score, and log what happened. Pong is the smallest game that needs all of those at once.
The pieces and how they connect
real frame times --> accumulator --> fixed steps --> systems in a fixed order --> world + event log
key events ---------------------------------------> held-key state --^
- Game loop: a frame of any length adds to an accumulator (clamped, so a stall cannot cause a spiral), and each 20 ms in it runs one simulation step. Left-over time stays banked.
- Input: key events change a table of held keys that the step reads, because moving a paddle is a level-triggered action.
- Physics: the ball is integrated with a constant velocity every step.
- Collision: the ball is a 2x2 box tested against walls and paddles.
- Events: the step reports what happened (wall, paddle, score) with the simulated time it happened at. Simulated time is the step number times 20 ms, never the wall clock, so replays are identical.
- Snapshot: a text dump of the whole world. Real engines use the same trick to test simulation without a screen.
The world
The field is 100 wide and 50 tall with y growing downward. The ball is a 2x2 box whose top-left corner is stored. Paddles are 2 wide and 10 tall: the left one covers x from 2 to 4, the right one x from 96 to 98, and each moves 2 per step while its key is held (W/S for the left, UP/DOWN for the right; holding both cancels), staying between y = 0 and y = 40.
The order inside one step
Order is part of the design, as in the systems lesson:
- Paddles move from the held keys.
- The ball moves by its velocity.
- Walls. A ball above the top or below the bottom is reflected: its position is mirrored about the wall and its vertical speed flips. Clamping would swallow the distance it travelled past the wall.
- Paddles. Only a ball moving toward a paddle is tested against it; real engines make the same check on the approach velocity, so a contact that is already separating is not resolved again. A hit flips the horizontal speed and places the ball flush against the paddle face. It also adds spin: if the ball's centre is 3 or more above the paddle's centre the vertical speed becomes -2, if 3 or more below it becomes +2.
- Scoring. A ball completely outside the field scores for the opposite side and is served again from the centre towards the player who just conceded.
Worked trace
With no keys pressed, the ball starts at (49,24) moving (2,1). Its y grows by 1 each step. At step 24 it is at (97,48) and has passed the right paddle's reach (the paddle sits at y 20 to 30), at step 25 the ball is at (99,49) and its bottom edge is below the field, so it is mirrored back to y = 47 and flips vy (t=500, a wall event). At step 26 it reaches x = 101, outside the field, so the left player scores (t=520). The ball is served again from the centre.
Common mistakes
- Clamping the ball at a wall instead of reflecting it.
- Not moving the ball flush against the paddle face after a bounce, which shifts every later position by up to two units.
- Resolving the paddles before the walls, or scoring before the paddles.
- Stamping events with the number of frames or the wall clock instead of simulated time.
- Running one step per frame, or forgetting to clamp the frame time.
- Dropping the accumulator's remainder between frames.
Your exercise
Implement step and advance as described in the exercise. The starter parses the commands, holds the world and prints snapshots. The most likely mistake is the order of walls, paddles and scoring.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…