Home CPSC 110

Simple Pong


Due: December 6



To work with a graphical program, and get experience writing interactive programs.



For this assignment, you will write a very simple, 1-player game similar to Pong. In this game, there are two objects:

  1. The ball, which will be a circle. The ball should start in the center of the window and move on its own. It should bounce off the top and sides of the window. If it hits the bottom, the player loses a point.
  2. The paddle, which can be a rectangle. The rectangle should be positioned on the bottom of the screen, and should move based on the arrow keys. If the left arrow is pushed, the paddle should move left. If right is pushed, it should move right.


Ball Movement

When the ball hits the paddle, it should bounce back up. For this to work, you will need to keep track of the ball's speed and direction. To do this, you need to variables, which I will call dx and dy. dx is the change in x position, and dy is the change in y position.

For example, if dx = .1, and dy = -.1, then the ball will move .1 pixels to the right and .1 pixels up. The magnitude of these numbers affect the speed. So if it was .2 and -.2, the ball would be going twice as fast. The signs affect the direction it's going in.

To have the ball "bounce" we can just flip the sign of one of these. For example, if the ball is going down and right, like this:

And then hits the right wall, we should reverse its dx variable:

This will involve multiplying these by -1 to flip the sign. Similar logic can be used for getting it to bounce in other directions.



In order to know whether the ball should bounce or not, we need to detect if it has hit a wall (or the paddle). To do that, we need to find the ball's position. That can be done with the circle's "getCenter" method.

Point c = ball.getCenter()
x = c.getX()
y = c.getY()

Once we have the ball's position, we can use if statements to check for the collisions. This will also depend on the radius of the ball. For example, to check if the ball has hit the left wall, we would check if the x value minus the radius is less than or equal to zero.

If it is, that means the ball has hit the left wall. It should then bounce right by changing the dx value to positive (leave dy alone).




Extra Credit Features

There are some chances for extra credit in this program:


General Requirements

When writing your program, also be sure to:



To submit your program, email the program file to ifinlay@umw.edu.

Copyright © 2019 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.