CIS191-S09/Homework/Homework 4
From MCIS Wiki
Due Wednesday, Feb 18, 10pm
Start with the following project: media:breakout-s09.zip
You will need to create three classes:
- Ball
- Paddle
- Brick
Contents |
Ball
A ball knows its x and y coordinates (integers) 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 (integers). 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.
Paddle
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.
Brick
This class represents the bricks that the ball hits. Every brick knows:
- Its location on the screen (x, y, width, height)
- Its color
- The array in which it lives
- The i and j coordinates within this array
You'll need to pass all of these to the constructor.
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.
Main Program
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).
- 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. Write a method which determines what brick (if any) is at an (x, y) location. You need to divide the x and y coordinates to get the index into the brick array. If these numbers are outside the array or there is no brick at that point return null.
Brick getBrick(int x, int y)
To react to bricks, first determine which brick the ball has hit using getBrick. If this is not null, call the hit method in the brick. If you hit a brick you'll need to see if the the new location is in another brick - keep calling the different hit methods until the ball is no longer at a location occupied by a brick.
Discussion
Question?
- To paint the bricks like you described in class, I've tried this code (in for loops using "i" and "j"):
(i+j)%2==0?g.setColor(bricks[i][j].c):g.setColor(Color.RED);
where "bricks[i][j].c" is a variable color in the Brick class. It doesn't work, so is anything missing/wrong with it?
You need yo put the ? : stuff inside the call to the Brick constructor.
bricks[i][j] = new Brick( ..... , (i+j)%2 == 0 ? Color.BLACK : Color.RED, )
- What do you put in the for loops in tick to make the Brick hit() method work?
You need to figure out which brick is being hit by computing the location in the array based on the x,y of the ball. There's no loop in tick.
