Building Pong in Zig with Raylib – Part 3: Edge Collisions, Scoring & Player Input
In this part, we round out the core gameplay of our Pong clone in Zig using raylib. By the end, we’ve got a working game - with a bouncing ball, paddle controls, and basic scoring.
🧱 Edge Collisions
We already had paddle collision working, but we needed the ball to bounce off the top and bottom edges of the screen. That meant checking if the ball’s y-position was outside the visible range and inverting its vertical velocity accordingly:
| |
There was a small detour where I realised screen_height wasn’t giving the
expected value - using GetScreenHeight() directly from raylib helped resolve
that.
🏁 Scoring System
Once edge collisions were in place, it was time to detect goals. If the ball
passed the left or right edge of the screen, the opposing player got a point. We
also need a reset() method to the Ball struct to return it to the centre
after each goal.
| |
For now, let’s print the scores via std.debug.print, but this sets the stage
for UI integration.
| |
⌨️ Player Input
Finally, we wired up keyboard input to allow paddle movement. We used
IsKeyDown from raylib, and made sure movement was frame-rate independent by
scaling it with dt:
| |
Left paddle uses W/S, right paddle uses E/D. Simple and responsive.
| |
✅ What’s Working
By the end of this episode, we’ve got:
- Ball bounces off top and bottom edges
- Scoring when the ball passes a paddle
- Ball reset after each goal
- Both paddles are fully controllable
All that’s missing now is a visible score, a win condition, and maybe a simple menu.