Lab: Pong

In today’s lab, you’ll be recreating pieces of Pong, one of the earliest arcade games. To get started, create a fork of the starter code repository at https://git.cs.grinnell.edu/csc161/pong and add your lab partner(s) as collaborators on the repository.

As you’ll see in a moment, the starter code is the same as the demo code from the second SDL reading. This obviously isn’t the same as pong, but the code does include several useful elements you may be able to adapt to build pong.

This lab is less structured than many of our earlier labs, but the next section walks through the important pieces your pong implementation should have.

Implementing Pong

The following steps should help guide you through an implementation of pong, or at least some of the major pieces of the game.

Step 1: Paddles

The first thing you’ll need to do is implement the player paddles. You can draw these as basic rectangles inside the outlined play area. The players should be able to move the paddles up and down with the keyboard, but not outside of the outlined “field”.

Hint: when a user types the w key, event.key.keysym.sym will have the value 'w' instead of the defined constants we used to detect the arrow keys.

This is optional, but if you want to make paddle movement work more like the classic pong game you’ll need to handle SDL_KEYUP events. Holding a key should continue to move a paddle, and then releasing the key should stop it. You can even use the FRICTION method from the demo to slow the paddle down gradually when the user releases a key.

Step 2: Ball Movement

Next, you’ll need to update the ball so it starts moving after a brief pause at the start of the game. You can wait for a key press to start, or just begin the game after a certain amount of time has elapsed. The ball should begin moving at an angle so you’re able to test collisions with the play area in the next step.

We’ll be turning off friction for the ball, so you can remove the code that slows the ball down each frame. You may also want to make the ball smaller at this point.

Step 3: Play Area

Now that you have a moving ball and paddles, it’s time to constrain the ball to the outlined play area. Update the collision code so the ball bounces off the edges of the field instead of the edges of the window.

Step 4: Paddle Collision Detection

When the ball collides with a paddle, it should bounce back toward the other player. You can check for collisions with basic conditionals, or you can use the SDL_HasIntersection function.

Simple rectanguler collision detection should be enough for this game. There will be a small gap between the ball and the paddle when the ball hits near a corner, but if you make the ball smaller you likely won’t notice this. You’re welcome to come up with a more accurate way of detecting collisions if you have time.

One important detail from pong is that the angle of the ball bounce depends on where it hits the paddle. Hitting the ball with the upper part of the paddle sends the ball upwards, whereas hitting it with the lower part of the paddle sends the ball downwards. You can add this to your implementation now, or save it for an extra challenge later in the process.

Step 5: Scoring

Add code to detect when a player has scored (the ball hit the boundary behind the opposing player’s paddle). After scoring a point, the game should start the ball back in the middle for another round. For now, just print the score in the terminal, since displaying text in SDL requires some additional library support.

If you want an extra challenge, you can add goals to the two ends of the field to match with the soccer-like appearance of the game. Points are scored by hitting the ball into the opponents goal rather than just against the baseline.