Lab 4: Breakout, Part II

Objectives

Before you begin programming, it's important that you play a game of Breakout to get some idea of what we're aiming toward. At the Unix prompt, type the following.

% lbreakout
Our final program will lack all the frills of this program, but it gives you some idea of where we're headed.


Figure 4.1: Screen shot of lab program.
By the time you finish this lab, you should have a window like that of Figure 4.1. The only missing piece of the game is the user's paddle, which will have to wait for additional concepts later in the semester. For now, the ball will simply bounce around the window until all the blocks are gone.

In this lab you'll create a new class, Breakout, whose purpose is solely to contain the main() method that will use the Ball and Block classes from Lab 2 to accomplish the goal. The BouncingBallWindow class of Lab 2 should give you some idea of the general structure.

You'll want to import the Lab2 package in Breakout.java, so that it can use the Ball and Block classes you wrote in that lab. (So Breakout.java should contain the line ``import Lab2.*;''.)

To initialize the board, read from the level1 file. To access a file in your program, you can use the InputFile class in the csbsju.cs160 package. When you open the file, you'll want to refer to it as ``CS160/Lab4/level1'', since the InputFile constructor works relative to your home directory.

95 180 2.0 -1.1
2
30 50 20 30
80 50 20 30

Figure 4.2: An example level1 file.

Figure 4.2 contains an example file, illustrating the file format. The first line of this file says to begin the ball's top left corner at point (95,180), at a velocity of 2.0 and a direction of -1.1 radians. The next line says that the board should contain 2 blocks. (The actual level1 file has more blocks than this.) The first block should have its top left corner at (30,50) and should be 20 pixels wide and 30 pixels tall. The second block should have its top left corner at (80,50) and should be 20 pixels wide and 30 pixels tall.

To store the blocks, you will want to use an array. When the ball bounces off a block, the block should disappear. The easiest way to do this is to set that array element to null, essentially indicating that the block no longer exists, and then to decline to draw null blocks when you redraw the blocks.

Your program should track the user's score. The user gets 10 points for each block hit. This score should be displayed in the lower left corner of the screen.

When all blocks are gone, the program should automatically exit. Use the dispose() method of DrawableFrame to close the window and the program.