// name: Carlos Sosa
// section: O
// e-mail id: carlosso@andrew.cmu.edu
// class Homework9
// A video game based on Homework 8
// User must click the moving figures.
// Each figure is worth 1 to 4 points based on the color of the figure.
// Player has 1 minute to score as many points as possible.
/**
Directions:
Click on the moving targets.
Every one you hit eliminates the target and adds points to your
score.
Every one you miss adds two more targets to the game and you lose
points.
Eliminate all of the targets and you win!
Run out of room for more targets and you lose!.
*/
// Global variables or fields go here
final int MAX = 30;
int elements= MAX/2;
Figure [ ] figArr;// calls the Figure array
int hitCount;
int missCount;
boolean index;
// Methods go here.
void setup( )
{
size ( 600, 600);
smooth ( );
hitCount=0;
missCount=0;
//News the array
figArr= new Figure [MAX];
frameRate(10);
for (int i = 0; i < elements; i++)
{
figArr[i]= new Figure(random(width),random(height),random(25,.15*width),(int)random(1,5));
}
}
void draw( )
{
background( 140, 234, 156 );
for (int i = 0; i < elements; i++)
{
figArr[i].drawFigure( );// draws the figure
figArr[i].move( );// moves the figure
}
displayScores( );
displayFigures( );
arrayEmpty( );
if (frameCount%100 == 0)
{
frameRate(frameRate+5);
}
if (frameCount >= 500)
{
noLoop();
background( 140, 234, 156 );
text("Final Score: " + (hitCount-missCount), .38*width, height/2);
}
}
void displayFigures( )
{
text("Targets Left: "+ elements, .35*width, .85*height);
}
void displayScores( )
{
text("Time", .45*width, height/15);
text(500-frameCount,.46*width, height/8);
text("Hits", width/15, height/15);
text(hitCount,.085*width, height/8);
text("Misses", width/30, .8*height);
text(missCount, .085*width, .86*height);
}
void mousePressed()
{
index=false;
int score=0;
for(int i = 0; i < elements; i++)
{
if (figArr[i].mouseOnFigure() == true)
{
index = true;
score = figArr[i].getValue();
delete(i);
break;
}
}
if (index == true)
{
hitCount += score;
}
else
{
missCount++;
insert (0);
}
}
void arrayEmpty( )//Tells the player when the array is empty.
{
if(elements==0)
{
noLoop();
background( 140, 234, 156 );
text("You Win!!! You have eliminated all of the targets.", .2*width, height/2);
}
}
void delete ( int index )//deletes elements of the arrays
{
for(int i= index;i=index+2 ; i--)
{
if(elements>=MAX-1)//If the array is full the player is given a message.
{
noLoop();
background( 140, 234, 156 );
text("You Lose!!! You have missed too many targets.", .2*width, height/2);
}
else
{
figArr[i]=figArr[i-2];
}
}
figArr[index]=new Figure(random(width),random(height),random(25,.15*width),(int)random(1,5));
figArr[index+1]=new Figure(random(width),random(height),random(25,.15*width),(int)random(1,5));
elements+=2;
}