I’m a bit new to processing, and I’m trying to make a game using multiple levels. Levels are either locked or unlocked, and are represented by either a button or a lock. When a button is pressed, the background does indeed change to the right background, but the buttons and lock icons do not disappear.
I’ve tried multiple things, such as using a screen variable and a game_in_progress boolean, but nothing seems to work. If it helps, both the buttons and lock icons are made using the class function.
(sorry for the whole mess of code, I’m not the best organizer)
Lock myLock2;
Lock myLock3;
Lock myLock4;
Lock myLock5;
Lock myLock6;
Lock myLock7;
void setup() {
screen=select_screen;
myLock2 = new Lock(0,60,100+20,50);
myLock3 = new Lock(0,60,160+20,50);
myLock4 = new Lock(0,60,220+20,50);
myLock5 = new Lock(0,60,280+20,50);
myLock6 = new Lock(0,60,340+20,50);
myLock7 = new Lock(0,60,420,50);
mybutton1 = new button(c,1,35,35,43,50);
mybutton2 = new button(c,2,35,35,43+60,50);
mybutton3 = new button(c,3,35,35,43+60*2,50);
mybutton4 = new button(c,4,35,35,43+60*3,50);
mybutton5 = new button(c,5,35,35,43+60*4,50);
mybutton6 = new button(c,6,35,35,43+60*5,50);
mybutton7 = new button(c,7,35,35,43+60*6,50);
//
l[2]=1; // '0' in this case means locked; '1' means unlocked
l[3]=0;
l[4]=0;
l[5]=0;
l[6]=0;
l[7]=0;
}
void draw() {
if(screen==select_screen) {
displayLocks();
testButtons();}
//displayMainScreen(); //working on other screens right now
}
void displayLocks() {
if(screen==select_screen) {
if(select==false) {
//locks being displayed if l[x] = 0;locked if l[x] = 1; unlocked and level select buttons
if(screen==select_screen) {
mybutton1.display();}
if(screen==select_screen) {
if(l[2]==0) {
myLock2.display();
}
else {
mybutton2.display();
}}
if(screen==select_screen) {
if(l[3]==0) {
myLock3.display();
}
else {
mybutton3.display();}
}
if(screen==select_screen) {
if(l[4]==0) {
myLock4.display();
}
else {
mybutton4.display();}
}
if(screen==select_screen) {
if(l[5]==0) {
myLock5.display();
}
else {
mybutton5.display();}
}
if(screen==select_screen) {
if(l[6]==0) {
myLock6.display();
}
else {
mybutton6.display();}
}
if(screen==select_screen) {
if(l[7]==0) {
myLock7.display();
}
else {
mybutton7.display();}
}
}
}}
boolean select=false;
void testButtons() {
if(mousePressed) {
if(((mouseX > 43) && (mouseX < 78))
&& ((mouseY > 50) && (mouseY < 85))) {
level1();
select==true;
} else
if(((mouseX > 103) && (mouseX < 138))
&& ((mouseY > 50) && (mouseY < 85))) {
if(l[2]!=0) {
level2();select==true;}} else
if(((mouseX > 163) && (mouseX < 198))
&& ((mouseY > 50) && (mouseY < 85))) {
if(l[3]!=0) {
level3();select==true;}} else
if(((mouseX > 223) && (mouseX < 258))
&& ((mouseY > 50) && (mouseY < 85))) {
if(l[4]!=0) {
level4();select==true;}} else
if(((mouseX > 283) && (mouseX < 318))
&& ((mouseY > 50) && (mouseY < 85))) {
if(l[5]!=0) {
level5();select==true;}} else
if(((mouseX > 343) && (mouseX < 378))
&& ((mouseY > 50) && (mouseY < 85))) {
if(l[6]!=0) {
level6();select==true;}} else
if(((mouseX > 403) && (mouseX < 438))
&& ((mouseY > 50) && (mouseY < 85))) {
if(l[7]!=0) {
level7();select==true;}}}}
button mybutton1;
button mybutton2;
button mybutton3;
button mybutton4;
button mybutton5;
button mybutton6;
button mybutton7;
button mybuttonstart;
button mybuttonprog;
button mybuttonbackprog;
//class button
//
class button {
color c;
int w;
float xpos;
float ypos;
int size;
color p;
int size2;
boolean func;
//arguments
button(color tempc,int tempw, int tempsize, int tempheight, float tempxpos, float tempypos) {
c = tempc;
w = tempw;
p = c;
xpos = tempxpos;
ypos = tempypos;
size = tempsize;
size2 = tempheight;
}
void display() {
noStroke();
rectMode(CORNER);
if(((mouseX > xpos) && (mouseX < xpos + size)) &&
((mouseY > ypos) && (mouseY < ypos + size2))) {
fill(51);
} else {
fill(c);}
stroke(0);
strokeWeight(1.5);
rect(xpos,ypos,size,size2);
fill(255);
text(w,xpos+size/1.6,ypos+size2/2);
}}
//// levels themselves
////
////
float player_paddleX=width-45;
float ai_paddleX=45;
float player_offset=-15; //how much to add/subtract for collision detection
float ai_offset=15; //as paddles will be drawn using rectMode(CENTER);
float player_paddleY=mouseY;
float ai_paddleY=width/2-paddle_height/2;
int paddle_height=85;
float ai_distance_from_ball;
float ballX=width/2;
int ballsize=20;
int x;
int y;
//
float dx=-1; //x direction of ball -1 = left +1 = right
float dy=1; //y direction of ball -1 = up +1 = down
boolean ai_win=false; //did the ai win by scoring 2?
boolean player_win=false; //did the payer win by scoring 2?
int player_score=0;
int ai_score=0;
int max_score=2;
int[] l ={
2,3,4,5,6,7
}; //which levels are locked/unlocked?
///
int[] level = { //used for displaying different levels w/different difficulties
1,2,3,4,5,6,7
};
int Xlevel=0;
int current_level = { //used for testing whether the next level is unlocked(current BEST level,
1,2,3,4,5,6,7 //not level currently playing on
};
int Ylevel;
boolean game_in_progress=false;
float[] ball_speed = {
0.4,0.55,0.625,0.675,0.725,0.775,0.85 //speed of ball per level
};
float t;
float[] ai_easing_spd = { //speed of computer player paddle per level
0.045,0.0525,0.0575,0.0635,0.075,0.0775,0.08
};
float ease;
color[] ball_color = { //dif. ball colors per level
#d71c1c,#227dd5,#d4d71c,#ef30c2,#d7881a,#1eb816,#b3a019
};
///
void level1() {
gameStart();
screen=game_screen;
Xlevel=1;
t = ball_speed[1];screen!=select_screen;
ballcolor=ball_color[1];
ease = ai_easing_spd[1];}
void level2() {
gameStart();
screen=game_screen;screen!=select_screen;
t = ball_speed[2];
ease = ai_easing_spd[2];
ballcolor=ball_color[2];
Xlevel=2;}
void level3() {
gameStart();
screen=game_screen;screen!=select_screen;
float t = ball_speed[3];
float ease = ai_easing_spd[3];
ballcolor=ball_color[3];
Xlevel=3;}
void level4() {
gameStart();
screen=game_screen;
screen!=select_screen;
float t = ball_speed[4];
float ease = ai_easing_spd[4];
ballcolor=ball_color[4];
Xlevel=4;}
void level5() {
gameStart();
screen=game_screen;screen!=select_screen;
float t = ball_speed[5];
float ease = ai_easing_spd[5];
ballcolor=ball_color[5];
Xlevel=5;}
void level6() {
gameStart();
screen=game_screen;screen!=select_screen;
float t = ball_speed[6];
float ease = ai_easing_spd[6];
ballcolor=ball_color[6];
Xlevel=6;}
void level7() {
gameStart();
screen=game_screen;
select_screen!=screen;
t = ball_speed[7];
ease = ai_easing_spd[1];
ballcolor=ball_color[7];
Xlevel=7;}
void gameStart() {
game_in_progress ==true;
background(0);
float s=0;
float w=0.0152;
//x=x+dx*(5*t);
//y=y+dy*(8*t);
if(Xlevel=1) {
ballcolor=ball_color[1];}
if(Xlevel=2) {
ballcolor=ball_color[2];}
if(Xlevel=3) {
ballcolor=ball_color[3];}
if(Xlevel=4) {
ballcolor=ball_color[4];}
if(Xlevel=5) {
ballcolor=ball_color[5];}
if(Xlevel=6) {
ballcolor=ball_color[6];}
if(Xlevel=7) {
ballcolor=ball_color[7];}
fill(ballcolor);
ellipse(x,y,ballsize,ballsize);
text(""+ease,200,100);
}