Montana Tech of The University of Montana
Computer Science & Software Engineering

CSCI 135
Fundamentals of Computer Science I
Fall 2014



ASSIGNMENT #8

In this assignment, you will be building a video game called Bombs Away. In the process, you will learn how to use the Java ArrayList collection.

Bombs Away
The object of the game is to avoid any bombs hitting the ground and destroying your civilization. Luckily you have a powerful laser that you can fire at the falling bombs. You aim your laser with the mouse and fire it by pressing the button. Bombs are different sizes and require varying amounts of lasering to destroy. But watch out, some of the bombs are splitters. When a splitter is destroyed, it splits into two bomblets that also need to be destroyed. After destroying 10 bombs, you get a single shot nuke. The nuke destroys all currently falling bombs. The nuke is fired by pressing the spacebar. After using the nuke, you have to destroy another 10 bombs with your laser before you get another one. Good luck!

Classes: You should start by downloading the file bombs.zip. The zip file also contains stub versions for the two classes you will be developing as well as the background image file. You will need to add instances variables, the bodies of instance methods, and the body of the main() method. Here is a description of the two classes: Canvas size and coordinates. In this assignment, we'll leave the canvas size at the default size of 512 x 512 pixels. We will also use the default coordinate system which has the lower-left corner being (0.0, 0.0) and the upper-right corner being (1.0, 1.0). For a background image, draw mcommand.jpg at the center of the window.

Creating new bombs. At every time step of your animation loop, there is a 1% chance that a new bomb will be created. You should pause between each animation cycle for 10ms (i.e. by calling StdDraw.show(10)). When a new bomb is constructed, it should be assigned values as follows: Bomb dynamics and appearance. A Bomb updates its position in the updatePos() method by adding its y-velocity to its y-position. A bomb starts out as completely blue (RGB 0.0, 0.0, 1.0). As it gets lasered, it starts to turn red (RGB 1.0, 0.0, 0.0). We'll use a linear interpolation between blue and red based on how much of a bomb's initial strength is left. If a bomb started with an initial strength of T and currently has L strength left, the current color RGB is (1.0 - L/T, 0.0, L/T).

Firing the laser. You can detect when the user presses the mouse button using StdDraw.mousePressed(). Your game should draw a red line from the lower-left corner to the current mouse coordinate when the laser is fire. You can obtain the coordinate of the mouse by calling StdDraw.mouseX() and StdDraw.mouseY(). The laser hits a falling bomb if the mouse x- and y-position intersects the circle representing the bomb. The laser can hit multiple bombs simultaneously if they are overlapping. Your intersects() instance method should determine the Euclidean distance between the mouse coordinate and the center of a bomb. If this distance is less than or equal to the radius of the bomb, it counts as a hit. Each hit reduces the remaining strength of a bomb by one.

Creating bomblets. Should a bomb be destroyed that is a splitter, two new bombs should be created. The two new bombs should be assigned values as follows: Game over. The game is over if the y-position of the center of any bomb reaches the line y = 0.

Scoring. You should display a score in the upper-left corner. You get one point for every bomb you destroy. In the case of a splitter, you get a point for destroying the big bomb and another two if you successfully destroy both bomblets.

Nukes. After you score ten points, you gain the power of activating a nuke that destroys all falling bombs. You should indicate the nuke is available by putting an asterisk after the number in the scoreboard. The nuke is activated by pressing the spacebar. The screen should flash red for 100 milliseconds when the nuke is fired and then all bombs should be destroyed. Be sure to count the destroyed bombs in your score. After using the nuke, the user has to laser 10 more bombs in the normal way before the nuke can be used again. You can only have one nuke in your stockpile.
Is there an easy way to generate random numbers in a certain integer or double range? You can use the static methods in StdRandom.java to help you do this.

How do I draw a line? StdDraw has a method for drawing a straight line between two points: StdDraw.line(double x1, double y1, double x2, double y2).

How do I detect when the user hits the spacebar? First check if a key has been pressed by calling StdDraw.hasNextKeyTyped(). Then check which character was typed using StdDraw.nextKeyTyped(). The spacebar character is simply a space between two single apostrophes.

How do I create an ArrayList? An ArrayList is a special type of thing in Java called a generic. It can contain a collection of any reference type. You need to specify the type it is going to hold when you create the ArrayList. You do this by placing the reference type inside angle brackets. You also need to specify arguments to the constructor of the ArrayList class. Since typically we don't specify any constructor arguments, this is done by placing empty ()'s at the end. For example to create a new collection that can hold Ball objects, you would do: ArrayList<Ball> balls = new ArrayList<Ball>();

What methods are available for an ArrayList? A summary of all methods is available here. For the assignment, you'll probably want to use the add(E e), remove(int index), and clear() methods.

How do I draw in a specific RGB color? You need to create a new Color object, constructing it with the values for the red, green, and blue components. For example, to create a new color halfway between red and blue you would do: Color c = new Color(0.5f, 0.0f, 0.5f); Note that the constructor to Color does not take double parameters, so you may need to cast things to a float. To be able to use the Color class, you'll need to import java.awt.*;

What mouse related methods do I need? You'll need StdDraw.mousePressed() to decide if the user is currently holding down the mouse button. You'll also need StdDraw.mouseX() and StdDraw.mouseY() to determine the mouse pointer's location. See MouseDraw for an example program. Note that the Bomb class is exhibiting good data encapsulation, it doesn't actually know or care how clients are determining when and where user actions occur. This is the job of BombsAway and so all mouse and keyboard input should be handled in this client class and not inside the Bomb class.

Does the game have to continue to play during the 100ms flash of the nuke? Since a tenth of a second is a very short time in human terms, it is fine for the game to "pause" while the red flash is being displayed. You can draw a big red rectangle over the whole screen, then call StdDraw.show(100) to both display the rectangle and pause for 100ms. After this your game loop can continue as normal.

My score text doesn't look like yours. How do I make it match? While not required, you can use the same font we did by calling StdDraw.setFont(new Font("SansSerif", Font.BOLD, 18)). Your can call this once before your animation loop in your main() method and all subsequent text drawing will use this font.

How do I compute the Euclidean distance between two points? You need to use the formula: In this formula p1 and p2 are the coordinates of one point and q1 and q2 are the coordinates of another. You can take a square root in Java using Math.sqrt(). While there is a math method for taking powers, if you are just squaring a value, it is much more efficient just to use the normal multiplication operator.
Extra credit. There are plenty of possibilities to improve the game. For example:
Submission. Submit your programs BombsAway.java, Bomb.java and any extra credit files using Moodle. Be sure each submitted source file has the required header with your name, username, and a description of the program.

Page last updated: December 18, 2014