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 { // paddle dimensions private static final double PADDLE_WIDTH = 70; private static final double PADDLE_HEIGHT = 10; // dimensions of the grid in which bricks are placed private static final int NUM_COLS = 7; private static final int NUM_ROWS = 30; // 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() { paddle = new FilledRect(canvas.getWidth()/2 - PADDLE_WIDTH/2, canvas.getHeight() - (PADDLE_HEIGHT + 2), PADDLE_WIDTH, PADDLE_HEIGHT, canvas); // create the bricks bricks = new BreakoutBricks(NUM_ROWS * NUM_COLS); // add two walls of bricks addWall(3, 4); addWall(10, 7); } private static final int BRICK_SPACING = 2; public void addWall(int startingRow, int wallRows) { double brickWidth = canvas.getWidth() / NUM_COLS; double brickHeight = canvas.getHeight() / NUM_ROWS; for (int row = startingRow; row < startingRow + wallRows; row++) { for (int col = 0; col < NUM_COLS; col++) { bricks.add(new FilledRect(col * brickWidth + BRICK_SPACING, row * brickHeight + BRICK_SPACING, 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() < PADDLE_WIDTH/2) { paddle.moveTo(0, paddle.getY()); } else if (point.getX() + PADDLE_WIDTH > canvas.getWidth()) { paddle.moveTo(canvas.getWidth() - PADDLE_WIDTH, paddle.getY()); } else { paddle.moveTo(point.getX() - PADDLE_WIDTH/2, paddle.getY()); } } }