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:
- 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.
- 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.
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 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
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
- Start by making the window object. You can call it whatever you
like. The size can also be whatever you want. If you make the size
a slightly unusual number (like 413 instead of 400), the ball will be
less likely to fall into repeating patterns.
- Next make the ball object, and set its color. You should start it in the
middle of the screen (the size and colors are up to you).
- Set the
dy variables for the ball.
You can start them at something like .001 for both. You may need to
test these to get a reasonable speed.
- Next make the paddle. It should be at or near the bottom of the screen.
The size and colors are up to you as well.
- Call the draw method on the ball and paddle so they appear on the window.
You should test it now to make sure that they show up!
- The rest of the program should be in a while loop. You can make it
and infinite loop by using "while True".
- Check for user input. To do this, call the "checkKey" method.
If it is equal to "Left" or "Right", then call the paddles .move
method with values that make it move in the correct direction.
- Move the ball. To do this, call the "move" method with your dx and dy
- Check for the wall collisions. Use logic described above to check if
the ball has hit the top, left, or right sides of the screen. If so, reverse
the balls direction so it bounces back.
- Check for collisions with the bottom of the screen the same way. If there
is a collision here, the player lost. We can just reset the ball in this
case. To do so, you can call .move on the ball to move it up the screen
again. If your window height is 600, moving it up 500 pixels will put it
back near the top.
- Check for collisions with the paddle. To do this, we need to check
If both things are true, then the ball hit the paddle. Set the ball's dy to be
negative so it goes back up.
- If the ball's y coordinate is bigger than the point of the top of
the paddle. For instance, if your window height is 600, and the paddle
is 20 pixels tall, then the y coordinate would have to be at least
580 to hit the paddle.
- If the ball's x coordinate is lined up with the paddle.
Rectangles also have a "getCenter" method we can use to find the center point
of the paddle. If the ball's x is more than the left side of the paddle,
and the ball's x is less than the right side of the paddle, it is lined up.
Extra Credit Features
There are some chances for extra credit in this program:
- Make the ball accelerate some amount each time the paddle hits it. This
will make the game get harder as the user plays.
- Keep track of the score. You can count the number of times the player hits
the ball with the paddle, and display this number. Reset it to 0 when they
- Extra credit will also be given for games that are especially fancy or
When writing your program, also be sure to:
- Put a comment at the top of your program with your name, the name of
the program, the purpose of the program and the honor pledge.
- Use good variable names (like "speed" and "time" instead of "x" and "y").
- Test your program before turning it in.
To submit your program, email the program file to firstname.lastname@example.org.
Copyright © 2019 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.