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

Proj_Pong

 

This is the CLASSIC intro programming project. 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. (note this is not a great version, in fact, I expect much better from you).

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. Keep track of the score.

 

Rubric - 20 points:

  • You have 2 paddles and a ball (3 points)
  • The paddles move up or down approp (2 point)
  • The ball starts at the center and moves in a random direction.
  • 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)

Advanced - must do at least 2 of the following (2 points each):

  • Ball moves faster as game goes on.
  • Limit game to 5 balls and it says who won
  • ball moves faster if hit on edges or when paddle is moving
  • sound
  • better game play in any way you figure out
  • sound
  • 2 controllers at same time

 

Starting point:

 

Goal 1 - Get paddles to show up.

  1. Use your code from class (Sams) or dog/kennel as reference (Paddle,Ball is Dog and Pong is Kennel).
  2. In Pong create 2 paddles and draw them (just like having a couple of Balls)
    1. Create a new class for Paddle (ie public class Paddle) . See if you can:
  3. 2. create a method drawPaddle (Graphics oG) that will draw on bufferGraphics.
    3. the class Pong will call the method drawPaddle by saying: paddle1.drawPaddle(bufferGraphics);

 

Goal 2 - Get paddles to move up and down.

  1. In pong, if key pressed is q then paddle1.moveUp(); if a then paddle1.moveDown(). For this to work, you will need to
  2. add moveUp and moveDown methods in Paddle.

Goal 3. Create a new class for Ball (ie public class Ball), that will have a lot of the stuff from HWJ14.

 

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.

 

 

Extra:

1. Fix the game so both users can control their paddles at the same time. Note when you hold down up and then press the a key, the paddle no longer goes up. To do this:

  • Create a variable String movingDirection in Paddle that will be either up,down, or nothing.
  • In keyPressed, if the key is the up key, we would say paddleRight.movingDirection="up"
  • In drawBall, we would say if movingDirection is up, move up
  • In keyReleased, if the key released is the up key, we would say paddleRight.movingDirection=""
  • Do this for all keys.