Professional Documents
Culture Documents
Source Code
Source Code
Source Code
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
600;
APPLICATION_WIDTH;
10;
30;
4;
/**Ball velocity*/
private double vx, vy;
/* Method: run() */
setUpGame();
playGame();
if(brickCounter == 0) {
ball.setVisible(false);
printWinner();
break;
if(brickCounter > 0) {
removeAll();
if(brickCounter > 0) {
printGameOver();
}}
private void setUpGame() {
drawBricks( getWidth()/2,
BRICK_Y_OFFSET); drawPaddle();
drawBall();
* one for loop for the rows and one for loop for the columns.
*/
in the row,
* so for the starting point of the next bricks in the column, you
need to:
* add a brick width
*/
double x = cx - (NBRICKS_PER_ROW*BRICK_WIDTH)/2 -
((NBRICKS_PER_ROW-1)*BRICK_SEP)/2 + column*BRICK_WIDTH
column*BRICK_SEP;
* start at the given length from the top for the first row,
*/
); add (brick);
brick.setFilled(true);
if (row < 2) {
brick.setColor(Color.RED);
if (row == 2 || row == 3) {
brick.setColor(Color.ORANGE);
if (row == 4 || row == 5) {
brick.setColor(Color.YELLOW);
if (row == 6 || row == 7) {
brick.setColor(Color.GREEN);
if (row == 8 || row == 9) {
brick.setColor(Color.CYAN);
}}}}
//paddle set-up
double x = getWidth()/2 -
PADDLE_WIDTH/2;
//since the rectangle gets drawn from the top left corner
PADDLE_HEIGHT); paddle.setFilled(true);
add (paddle);
addMouseListeners();
}
//making the mouse track the paddle
* If the middle point of the paddle is between half paddle width of the screen
* the x location of the paddle is set at where the mouse is minus half a
paddle's width,
*/
> PADDLE_WIDTH/2)) {
- PADDLE_Y_OFFSET - PADDLE_HEIGHT);
}}
//ball set-up
BALL_RADIUS); ball.setFilled(true);
add(ball);
}
waitForClick();
getBallVelocity();
while (true) {
moveBall();
break;
if(brickCounter == 0) {
break;
}}}
{ vy = 4.0;
vx = rgen.nextDouble(1.0, 3.0);
if (rgen.nextBoolean(0.5)) {
vx = -vx;
}}
ball.move(vx, vy);
vx = -vx;
//We don't need to check for the bottom wall, since the ball can fall through the
vy = -vy;
if (collider == paddle) {
/* We need to make sure that the ball only bounces off the top part of
the paddle
* and also that it doesn't "stick" to it if different sides of the ball hit
the paddle quickly and get the ball "stuck" on the paddle.
* I ran "println ("vx: " + vx + ", vy: " + vy + ", ballX: " + ball.getX() + ",
* so it never reaches exactly the the height at which the ball hits the
* but less than the height where the ball hits the paddle minus 4.
*/
- PADDLE_Y_OFFSET
- PADDLE_HEIGHT - BALL_RADIUS*2 + 4) {
vy = -vy;
}}
//since we lay down a row of bricks, the last brick in the brick wall is assigned the
value brick.
//so we narrow it down by saying that the collier does not equal to a paddle
or null,
remove(collider);
brickCounter--;
vy = -vy;
pause (DELAY);
}
else if (getElementAt( (ball.getX() + BALL_RADIUS*2), ball.getY()) != null ){
BALL_RADIUS*2)) != null ){
BALL_RADIUS*2);
else{
return null;
getHeight()/2); gameOver.move(-gameOver.getWidth()/2, -
gameOver.getHeight()); gameOver.setColor(Color.RED);
add (gameOver);
getHeight()/2); Winner.move(-Winner.getWidth()/2, -
Winner.getHeight()); Winner.setColor(Color.RED);
add (Winner);
}}