please format code with </> button * homework policy * asking questions
int []xspeed;
int []yspeed;
int []xcor;
int []ycor;
int []direction;
int []steps;
boolean movin[];
boolean gamemode;
float r[];
float b[];
float g[];
boolean flashmode[];
//all the arrays and variables
void setup(){
size(400,400);
xcor= new int[8];
ycor= new int[8];
xspeed= new int[8];
yspeed= new int[8];
direction= new int[8];
steps= new int[8];
movin= new boolean[8];
r= new float[8];
b= new float[8];
g= new float[8];
gamemode=false;
flashmode = new boolean[8];
setvalues();
//all arrays are defined
}
void draw(){
if(!gamemode&&!allstop()){
begin();
}
else if(gamemode&&!allstop()){
background(255);
boxes();
Move();
Bounce();
}
else if(allstop()&&gamemode){
end();
}
//executes all functions needed
}
void boxes(){
flash();
strokeWeight(4);
for(int i = 0; i < 8; i++){
if(movin[i]){
fill(r[i],b[i],g[i]);
rect(xcor[i],ycor[i],30,30);
}
}
//displays are squares
}
void Move(){
for(int i = 0; i < 8; i++){
if(movin[i]){
if(direction[i]==1){
xcor[i]+=xspeed[i];
steps[i]++;
}
else if(direction[i]==2){
xcor[i]-=xspeed[i];
steps[i]++;
}
else if(direction[i]==2){
ycor[i]+=yspeed[i];
steps[i]++;
}
else{
ycor[i]-=yspeed[i];
steps[i]++;
}
if(steps[i]%10==0){
direction[i] = direction_change();
}
}
}
//makes all squares move and move in a random pattern
}
void Bounce(){
for(int i = 0; i < 8; i++){
if(xcor[i]<0||xcor[i]>367){
xspeed[i]*=-1;
}
if(ycor[i]<0||ycor[i]>367){
yspeed[i]*=-1;
}
}
//bounces squares off the wall
}
int direction_change(){
return int(random(1,5));
//changes the direction once steps reach a certain point
}
boolean allstop(){
for(int i = 0; i < 8; i++){
if(movin[i]){
return false;
}
}
return true;
//checks to see if all squares have stopped
}
void begin(){
background(185);
fill(40,220,20);
rect(150,300,100,50);
fill(0,0,255);
textSize(25);
textAlign(CENTER);
text("Crazy Squares",200,150);
text("EPILEPSY WARNING!",200,100);
textSize(11);
text("Click inside the square twice. This increases the speed and changes color.",200,175);
text("Once CRAZY, press any key while mouse inside the square to destroy it.",200,200);
text("Win by destroying all of them!",200,225);
text("Press play to begin!",200,250);
fill(0);
textSize(30);
text("Play",200,335);
//Begining screen once you run the code
}
void end(){
background(185);
fill(40,20,220);
rect(50,300,100,50);
fill(40,220,20);
rect(250,300,100,50);
fill(0,0,255);
textSize(35);
textAlign(CENTER);
text("Nice! You won!",200,200);
fill(0);
textSize(15);
text("Play Again",300,330);
textSize(30);
text("Menu",100,335);
//end screen once you freeze all squares
}
void setvalues(){
for(int i = 0; i < 8; i++){
xcor[i] = 0 + 50 * i;
ycor[i] = 0 + 50 * i;
xspeed[i] = 1;
yspeed[i] = 1;
direction[i] = direction_change();
steps[i] = 0;
movin[i] = true;
flashmode[i] = false;
r[i] = 0;
b[i] = 0;
g[i] = 0;
}
}
void flash(){
for(int i = 0; i < 8; i++){
if((xspeed[i]==3||xspeed[i]==-3)&&(yspeed[i]==3||yspeed[i]==-3)){
if(r[i]==255){
r[i]=0;
b[i]=0;
g[i]=255;
}
else if(b[i]==255){
r[i]=255;
b[i]=0;
g[i]=0;
}
else if(g[i]==255){
r[i]=0;
b[i]=255;
g[i]=0;
}
flashmode[i]=true;
}
else if((xspeed[i]==2||xspeed[i]==-2)&&(yspeed[i]==2||yspeed[i]==-2)){
r[i]=255;
b[i]=255;
g[i]=255;
}
else if(!movin[i]||((xspeed[i]==1||xspeed[i]==-1)&&(yspeed[i]==1||yspeed[i]==-1))){
r[i]=0;
b[i]=0;
g[i]=0;
}
}
//sets the color of the squares
}
void mousePressed(){
for(int i = 0; i < 8; i++){
if(mouseX>=xcor[i]&&mouseX<=xcor[i]+30&&mouseY>=ycor[i]&&mouseY<=ycor[i]+30){
if(xspeed[i]>0&&xspeed[i]<3){
xspeed[i]+=1;
}
else if(xspeed[i]<0&&xspeed[i]>-3){
xspeed[i]-=1;
}
if(yspeed[i]>0&&yspeed[i]<3){
yspeed[i]+=1;
}
else if(yspeed[i]<0&&yspeed[i]>-3){
yspeed[i]-=1;
}
}
//speeds up squares once you press them and changes their colors
}
if(gamemode&&allstop()&&mouseX>=50&&mouseX<=150&&mouseY>=300&&mouseY<=350){
setvalues();
gamemode=false;
}
if(gamemode&&allstop()&&mouseX>=250&&mouseX<=350&&mouseY>=300&&mouseY<=350){
setvalues();
}
if(!gamemode&&!allstop()&&mouseX>=150&&mouseX<=250&&mouseY>=300&&mouseY<=350){
gamemode=true;
}
//resets all the values and brings player to home screen
}
void keyPressed(){
for(int i = 0; i < 8; i++){
if(mouseX>=xcor[i]&&mouseX<=xcor[i]+30&&mouseY>=ycor[i]&&mouseY<=ycor[i]+30&&flashmode[i]&&keyPressed){
movin[i]=false;
}
//stops an individual square once you meet certain requirements
}
}