Unformatted text preview:

Math 121: Introduction to Computing Handout #11Assignment #3—BreakoutDue: Monday, October 2, 5:00P.M.Your job in this assignment is to write the classic arcade game ofBreakout. It is a large assignment, but well within yourcapabilities, as long as you break the problem up intomanageable pieces. The decomposition is discussed in thishandout, and there are several suggestions for staying on top ofthe project in a section entitled “Strategy and tactics” later in thishandout.The Breakout gameIn Breakout, the initial configuration of the world appears asshown on the right. The colored rectangles in the top part of thescreen are bricks, and the slightly larger rectangle at the bottomis the paddle. The paddle is in a fixed position in the verticaldimension, but moves back and forth across the screen alongwith the mouse until it reaches the edge of its space.A complete game consists of three turns. On each turn, a ball islaunched from the center of the window toward the bottom of thescreen at a random angle. That ball bounces off the paddle andthe walls of the world, in accordance with the physical principlegenerally expressed as “the angle of incidence equals the angle ofreflection” (which turns out to be very easy to implement asdiscussed later in this handout). Thus, after two bounces—oneoff the paddle and one off the right wall—the ball might have thetrajectory shown in the second diagram. (Note that the dottedline is there only to show the ball’s path and won’t appear on thescreen.)As you can see from the second diagram, the ball is about tocollide with one of the bricks on the bottom row. When thathappens, the ball bounces just as it does on any other collision,but the brick disappears. The third diagram shows what the– 2 –game looks like after that collision and after the player has moved the paddle to put it inline with the oncoming ball.The play on a turn continues in this way until one of two conditions occurs:1. The ball hits the lower wall, which means that the player must have missed it with thepaddle. In this case, the turn ends and the next ball is served if the player has anyturns left. If not, the game ends in a loss for the player.2. The last brick is eliminated. In this case, the player wins, and the game endsimmediately.After all the bricks in a particular column have been cleared, a path will open to the topwall. When this delightful situation occurs, the ball will often bounce back and forthseveral times between the top wall and the upper line of bricks without theuser ever having to worry about hitting the ball with the paddle.This condition is a reward for “breaking out” and gives meaningto the name of the game. The diagram on the right shows thesituation shortly after the first ball has broken through the wall.That ball will go on to clear several more bricks before it comesback down the open channel.It is important to note that, even though breaking out is a veryexciting part of the player’s experience, you don’t have to doanything special in your program to make it happen. The gameis simply operating by the same rules it always applies: bouncingoff walls, clearing bricks, and otherwise obeying the laws ofphysics.The starter fileThe starter project for this assignment has a little more in it than it has in the past, butnone of the important parts of the program. The contents of the initial Breakout.javafile appear in Figure 1. This file takes care of the following details:• It includes the imports you will need in writing the game.• It defines the named constants that control the game parameters, such as thedimensions of the various objects. Your code should use these constants internally sothat changing them in your file changes the behavior of your program accordingly.– 3 –Success in this assignment will depend on breaking up the problem into manageablepieces and getting each one working before you move on to the next. The next fewsections describe a reasonable staged approach to the problem.Set up the bricksBefore you start playing the game, you have to set up the various pieces. Thus, itprobably makes sense to implement the run method as two method calls: one that sets upthe game and one that plays it. An important part of the setup consists of creating therows of bricks at the top of the game, which look like this:The number, dimensions, and spacing of the bricks are specified using named constantsin the starter file, as is the distance from the top of the window to the first line of bricks.The only value you need to compute is the x coordinate of the first column, which shouldbe chosen so that the bricks are centered in the window, with the leftover space dividedequally on the left and right sides. The color of the bricks remain constant for two rowsand run in the following rainbow-like sequence: RED, ORANGE, YELLOW, GREEN, CYAN.– 4 –Figure 1. The Breakout.java starter file– 5 –/* * File: Breakout.java * ------------------- * This file will eventually implement the game of Breakout. */import acm.graphics.*;import acm.program.*;import acm.util.*;import java.applet.*;import java.awt.*;import java.awt.event.*;public class Breakout extends GraphicsProgram {/** Width and height of application window in pixels */public static final int APPLICATION_WIDTH = 400;public static final int APPLICATION_HEIGHT = 600;/** Dimensions of game board (usually the same) */private static final int WIDTH = APPLICATION_WIDTH;private static final int HEIGHT = APPLICATION_HEIGHT;/** Dimensions of the paddle */private static final int PADDLE_WIDTH = 60;private static final int PADDLE_HEIGHT = 10;/** Offset of the paddle up from the bottom */private static final int PADDLE_Y_OFFSET = 30;/** Number of bricks per row */private static final int NBRICKS_PER_ROW = 10;/** Number of rows of bricks */private static final int NBRICK_ROWS = 10;/** Separation between bricks */private static final int BRICK_SEP = 4;/** Width of a brick */private static final int BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;/** Height of a brick */private static final int BRICK_HEIGHT = 8;/** Radius of the ball in pixels */private static final int BALL_RADIUS = 10;/** Offset of the


View Full Document

REED MATH 121 - Study Guide

Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?