You will need to create three classes:
A ball knows its x and y coordinates (doubles) and the previous x and y coordinates. The is where the ball was at the previous tick. These are all public.
A ball has an x and y velocity (doubles). These are public.
A ball knows its color.
Every ball is 5 pixels across and the x, y coordinate defines the center of the ball.
A ball supports the following methods:
- void move(): the ball moves, as defined by the velocity. Reflect off the top (y = 0), the left side (x = 0), and the right side (x = 400). Make the bounces "true" - don't let the x or y coordinate go "through" a wall.
- void paint(Graphics g): draw the ball.
The constructor sets the x and y coordinate, the x and y velocity, and the color. The initial value of the previous x and y is the same as the initial x and y.
The paddle knows its x and y coordinates (integers).
The paddle supports two methods:
- void paint(Graphics g): draw the paddle (assume the paddle is black)
- void bounce(Ball b): if the ball crosses the face of the paddle (the previous y coordinate is above the face and the current one is below) and the x coordinate is within the paddle, bounce the ball by changing its y coordinate and velocity. The new y velocity is just the negative of the old y velocity. The x velocity depends on where the ball strikes the paddle: it is 0 in the center and increases by 1 for every 2 pixels from the center. If the ball hits the left side it goes left and if it hits the right side it goes right.
- void move(int x): move the paddle to a new x location.
The constructor sets the x and y location of the paddle.
This class represents the bricks that the ball hits. Every brick knows:
- Its location on the screen: integers x and y. This is the upper left corner
- Its color
- The i and j coordinates within the bricks array in the main program
- The array of bricks in which the brick lives
You'll need to pass all of these to the constructor.
The main program contains constants defining the height and width of the brick. The width should be 40 and the height is 10.
A brick supports the following methods:
- void paint(Graphics g): draw the brick
- void hit(Ball b): react to a ball hitting the brick. The ball can hit from any of the four sides. You can determine which side it hits by looking at the previous position of the ball. For example, if the ball was above the brick and is now inside it, it must be bounced up. When a ball hits the brick, the brick changes the position and velocity of the ball as well as places a null into the brick array to make the brick disappear.
You'll need the following code in your main program.
- Painting code: this calls the paint methods in the ball, the paddle, and all of the bricks in the array. Remember that some of the array elements will be null.
- The tick method: move the paddle to the current mousex position, move the ball, bounce the ball off the paddle, and then interact with the bricks as described below. If the ball goes off the bottom of the screen, stop running the game.
If the game is not running, no nothing.
- Initialization code: create the objects for the paddle and the ball. Place the ball at (200, 300). Place the paddle at (200, 580). Create a two dimensional array of bricks that is 10 across and 5 high. Draw the topmost brick at 50 to leave space above the brick array. Set the colors of the bricks to create a checkerboard pattern.
- Start button code: place the ball at (200, 300) with velocity (0, 8). Create an array of bricks in which the x coordinates of the bricks start at 0 and the y coordinates start at 50 (that is, there is a 50 px empty area at the top). Alternate colors of the bricks in a checkerboard pattern using any colors you want.
The key to the tick routine is to react to bricks. A method in main, brickAt, returns the brick at a given coordinate or null if there is no brick there. Use this in tick to determine which brick (if any) the ball hits.