Assignment: Pong Game

In this lab assignment you will be implementing a Pong game. Here is a story about the game from Prof. Balkcom’s childhood days, in his own words:

“When I was 10, I went to visit my grandmother. She had a funky little box hooked to her black-and-white television, and we were able to play a simple video game involving a ball and two rectangles, which represented paddles. That game was called Pong, and it is not to be confused with the subterranean version played around here.”

Here’s a video of what it looked like back then: Atari Pong.

The game has three important behaviors:

  1. If the ball hits a vertical wall, the game is over.
  2. If the ball hits a horizontal wall, it bounces off that wall.
  3. If the ball hits the inner face of a paddle, it bounces off the paddle.

What to implement for the checkpoint

In this assignment, you will implement all functionalities related to paddle drawing and paddle movement. Each paddle is independently controllable. Six keys on the keyboard control the game; here are the recommended keys:

The paddles should never leave the playing surface. While a paddle is touching the top wall, it cannot go up, and while a paddle is touching the bottom wall, it cannot go down.

You do not have to produce something that looks identical to my version; it is meant only to provide a demonstration of basic functionality.

Moving the paddles

You will start by creating a square graphics window with just the paddles. Each paddle is a rectangular block, 80 pixels high and 20 pixels wide, initially in the upper-left and lower-right corners of a 400 x 400 window. Like so:

You may choose whatever colors you like.

The paddles may move up and down as follows:

But no paddle may move outside the window. When the left paddle hits the top window boundary, pressing a has no effect, and when it hits the bottom window boundary, pressing z has no effect. Likewise, when the right paddle hits the top window boundary, pressing k has no effect, and when it hits the bottom window boundary, pressing m has no effect. Remember that paddles move only vertically

Here’s what my window looked like after I pressed k a few times:

And then, after pressing z a few times:

Also, your paddles should be able to move simultaneously. Your section leader will run your code to verify that you got the behavior right.

Hint: Use boolean variables that remember which is being pressed down at any given point.

Coding style

Make sure to define constants for things that never change: - The height and width of the window. - The height and width of the paddles. - The four keys of interest. - The amount that a paddle moves when it moves.

You need to break down your code into functions where each function has a fixed purpose. For example, in my code, I have the following functions:

  1. a function that draws the paddles
  2. a function that updates the position of the paddles based on which key is being pressed

You can break down your code into a different set of functions.

Special note

  1. You must use callback functions to handle key press and release events.
  2. You cannot call any cs1lib drawing functions in your key press and release functions.

What to submit for the checkpoint

Submit a zip file containing the following: 1. Three screenshots of your program: one of its initial state and two with the paddles having moved. 2. Your .py source code.

How to proceed after the checkpoint

Remember the game has three important behaviors:

  1. If the ball hits a vertical wall, the game is over.
  2. If the ball hits a horizontal wall, it bounces off that wall.
  3. If the ball hits the inner face of a paddle, it bounces off the paddle.

What to implement

In this assignment, you will implement all functionalities related to the movement of the ball and drawing the ball. You will also implement some features that relate to the complete game.

Two keys on the keyboard control when the game starts and when the game window closes. Update your callback functions to handle the following keys:

Ball movement

At the start of the game, the ball is not moving and is at the center of the screen. When the player presses the space bar, the ball starts moving towards one of the four corners. You can make it move to the corner of your choice.

Without any interference, the ball continues to move in the direction it is moving. There are three events that can change the direction of the ball or stop it:

  1. When the ball collides with the top or bottom walls (ceiling and floor), it should bounce back.
  2. When the ball collides with the inner long edge of the paddle, it should bounce back.
  3. When the ball collides with the left or right vertical walls, the game should end and reset to the start screen to let the player start a new game by pressing the space bar.

The velocity of a ball is the number of pixels the ball moves in a specific direction every time you update the position of the ball. To control the movement of the ball correctly, you need to control the vertical and horizontal velocity of the ball independently.

Hint: You will also need to know when the game is in progress. Use a boolean to remember that. To detect the collision with the walls or paddles, it is important to note that the ball may not be moving by one pixel at a time. In that case, to detect a collision, you need to check if the surface of the ball has moved past the edge or wall it is colliding with. If the ball collides with the shorter edges of the paddles, we do not treat it as a collision.

Coding style

Make sure to add new constants for things that never change. You need constants for the following::

You need to break down your code into functions where each function has a fixed purpose. For example, in my code, I have the following functions:

  1. A function that draws the ball
  2. A function that updates the ball position
  3. A function that checks if the ball has collided with the inner face of either paddle and updates the ball velocity
  4. A function that checks if the ball has collided with the ceiling or the floor and updates the ball velocity
  5. A function that checks if the ball has collided with the vertical walls and resets the game if collision occurs
  6. A function to reset the state to the starting state

You can break down your code into a different set of functions.

Special note

  1. You must use callback functions to handle key press and release events.
  2. You cannot call any cs1lib drawing functions in your callback functions.

What to submit

Submit a zip file containing the following: 1. Two screenshots of your program: one of its initial state with the ball in center and one with ball in action. 2. Your .py source code.

Honor Code

The consequences of violating the Honor Code can be severe. Please always keep in mind the word and spirit of the Honor Code.