email me at borlaj@portlandschools.org
notes previous (09/07/06) submit the dump links  
 

Proj_Pong

 

This is the CLASSIC intro programming project. I think you can do this, but it will be a challenge. The idea here is you are going to make a simple pong game, where a ball will bounce back and forth when hit by two paddles from each side as shown here (it seems to only work in explorer). I dont expect you to make something so excellent to play (it would be great, but more than required).

Required items:

2 paddles, a ball that either hits a paddle and returns, or doesnt. If it doesnt I want the game to reset. Up and down is controlled w/ q and a and up and down.

Extras:

* Score
* Ball moves faster if it when paddle is moving (nice)
* I'm sure you can think of more

Rubric - 20 points:

* You have 2 paddles and a ball (3 points)
* The paddles move up or down approp (2 point)
* The ball hits a paddle and bounces off at the appropriate angle (2)
* If it does not hit a paddle it goes off screen (2 point)
* New ball appears if old ball goes off screen (1 point)
* You use 3 classes - one for paddle, ball and Pong (3 points)
* Code in all 3 classes is properly indented, commented, and have approp variable names. (3 points)
* You keep score, which works properly (2 points)
* You use buffergraphics to limit the flickering (2 points)
* Extra credit - anything beyond the code required (see above.)

 

 

Starting point:

 

Goal 1 - I would try to get each paddles to move up and down see below:

1. I might start with the BufferGraphics class that I would rename Pong. You will need to implement KeyListener, Runnable, so include that and all necessary methods that support that.
2. Create a new class for Paddle (ie public class Paddle). See if you can:

1. create a constructor that takes an x,y coordinate.
2. create a method drawPaddle (graphics bufferGraphics) that will draw on bufferGraphics.
3. the class Pong will call the method drawPaddle by saying: paddle1.drawPaddle(bufferGraphics);

4. once you get that working: get keyListener so that if key pressed is q then paddle1.moveUp(); if a then paddle1.moveDown(). For this to work, you will need to:

add moveUp and moveDown methods in Paddle.

So you have the ball moving around the screen and the paddles moving up and down. Great hot shot. Well, now you need to have the ball hit the paddle. This is a little tough.
There are a couple of ways you could go about doing it. The easiest to do, probably is to implement it in the Pong class. Maybe in draw animation, you will check to see if the ball is in range of the left paddle something like:

if (ball.x>=paddle1.x && ball.x<=paddle1.x+10 && ball.y>=paddle1.y && ball.y<=paddle1.y+50)
	ball.changeInX=ball.changeInX*-1;
In ball, I might only bounce off the top and bottom, not left and right sides. In pong, I would detect if the ball is off the screen, at which point I would place the ball in the middle again.


Many people are asking about why when they move the paddle, it increases the speed of the animation - this is because you are calling repaint in keypressed. Remember with runnable, keypressed is happening constantly; so you dont need to call repaint again.