MiniGame help
in
Programming Questions
•
11 months ago
Hey guys. I'm trying to get this minigame setup. The game works where a bunch of moving green squares are moving around on the screen. When you click on one it turns red and stops moving. When a green moving square touches a red one, the red one turns green and moves around again. I just need the last part where when a square that is red gets hit by a green one, it turns green and moves. Here is my Code:
//Tab 1
Movingrect [] theArray;
int numrect = 20;
void setup ()
{
size (600,600);
theArray = new Movingrect [numrect];
for ( int i=0; i<numrect; i++)
{
theArray[i] = new Movingrect (300, 300);
}
}
void draw ()
{
background (200);
for ( int i = 0; i < numrect; i++)
{
theArray[i].drawrect();
theArray[i].updateLocation();
}
}
void mouseClicked ()
{
for (int i=0; i<numrect; i++)
{
if ((mouseX >= theArray[i].getX()) && (mouseX <= theArray[i].getX()+50) &&
(mouseY >= theArray[i].getY()) && (mouseY <= theArray[i].getY()+50))
{
theArray[i].clicked();
}
}
}
//Tab 2
class Movingrect
{
float myX;
float myY;
float myXspeed;
float myYspeed;
boolean contains;
int Myred;
int Mygreen;
Movingrect ( float someX, float someY)
{
myX=someX;
myY=someY;
myXspeed=random (-2,2);
myYspeed=random (-2,2);
Myred=(0);
Mygreen=(255);
}
void clicked ()
{
Myred = 255;
Mygreen = 0;
myXspeed = 0;
myYspeed = 0;
stoppedX=myX;
stoppedY=myY;
}
void drawrect ()
{
fill(Myred,Mygreen,0);
rect (myX,myY,50,50);
}
void updateLocation ()
{
myX=myX+myXspeed;
myY=myY+myYspeed;
if ((myX <= 0) || (myX > width))
{
myXspeed = myXspeed*-1;
}
if ((myY <= 0) || (myY>height))
{
myYspeed = myYspeed * -1;
}
}
float getX ()
{
return myX;
}
float getY ()
{
return myY;
}
float getspeed ()
{
return myXspeed;
}
}
1