CIT020 Index > Objects

Arrays

In this assignment, you will create a game that lets the player “pop” balloons by moving the mouse over them.

Part 1:

Modify the Balloon class from the previous assignment as follows:

Balloon

You do not need to add any further instance variables. You already have all the ones you need (center x and y, width and height, color, and x- and y-speed).

Add the following methods to the Balloon class:

void draw()
Modify this function so that you no longer draw the string on the balloon.
boolean hit(int pX, int pY)
This function returns true if the point (pXpY) is inside the balloon, false otherwise. This requires a bit of math, so I am giving you the code:
boolean hit(int pX, int pY)
{
  float a = w / 2.0;
  float b = h / 2.0;
  float ex = pX - x; // calculate relative to center of ellipse
  float ey = pY - y;
  float r = (ex * ex) / (a * a) + (ey * ey) / (b * b);
  return (r < 1);
}
void pop()
This function “pops” the balloon by setting its center x and y coordinates to -1000 (thus moving it off-screen) and its x- and y- speed to zero (so it won’t move any more).

Putting it Together

Your main program makes a sketch that is 400 by 400 dots, and sets the frame rate to 20. The program will create an array of 30 Balloon objects of different sizes and colors and speeds. For each balloon:

Your draw() function will have a loop that does the following for each of the balloons in the array:

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

When You Finish

Zip up the sketch into a zip file named game.zip and upload it.