import objectdraw.*; // Play the vintage pong-like video game "breakout" // Display walls of bricks on the screen, a moving // ball and a paddle. When the ball hits a brick // the brick disappears while the ball bounces off. public class Breakout extends FrameWindowController { // dimensions of the grid in which bricks are placed private static final int COLS = 7; private static final int ROWS = 30; // locations and dimensions of brick walls private static final int WALL1_START_ROW = 3; private static final int WALL2_START_ROW = 10; // ratio between size of paddle and bricks private static final double PADDLERATIO = 1.5; // dimensions of the paddle private double paddleWidth, paddleHeight; // dimensions of bricks private double brickWidth, brickHeight; // the paddle private FilledRect paddle; // the collection of bricks placed in the playing area private BreakoutBricks bricks; // create the boundary and the paddle public void begin() { brickWidth = canvas.getWidth() / COLS; brickHeight = canvas.getHeight() / ROWS; paddleWidth = PADDLERATIO * brickWidth; paddleHeight = PADDLERATIO * brickHeight; // create the bricks bricks = new BreakoutBricks(ROWS * COLS); paddle = new FilledRect(canvas.getWidth()/2 - paddleWidth/2, canvas.getHeight() - (paddleHeight + 2), paddleWidth, paddleHeight, canvas); // add two groups of bricks addWall(WALL1_START_ROW); addWall(WALL2_START_ROW); } private static final int BRICK_SPACING = 2; private static final int WALL_ROWS = 4; private void addWall(int startingRow) { for (int row = startingRow; row < startingRow + WALL_ROWS; row++) { for (int col = 0; col < COLS; col++) { bricks.add(new FilledRect(col * brickWidth + BRICK_SPACING, brickHeight * row, brickWidth - BRICK_SPACING, brickHeight - BRICK_SPACING, canvas)); } } } // Make a new ball whenever the user clicks public void onMouseClick(Location point){ new BreakoutBall(bricks, paddle, canvas); } // make the paddle follow the mouse back and forth public void onMouseMove(Location point){ if (point.getX() < 0) { paddle.moveTo(0, paddle.getY()); } else if (point.getX() + paddleWidth > canvas.getWidth()) { paddle.moveTo(canvas.getWidth() - paddleWidth, paddle.getY()); } else { paddle.moveTo(point.getX(), paddle.getY()); } } }