Boolean issue in my dodge the wall game.
in
Programming Questions
•
2 years ago
In this game, you control the ellipse and make it move. You try and dodge the incoming walls. You have three lives which I used booleans to measure. When the 4th boolean is true, then it is game over. But I can't get the third boolean to work.(I'm only a beginner so please don't do anything too complex. Thanks) This is my program:
/*week three project
dodge block game
*/
int x;
float blockY=50;
float block2Y=470;
PFont font;
int xpos=200;
int ypos=250;
boolean life1=false;
boolean life2=false;
boolean life3=false;
boolean life4=false;
int blockX=1550;
int block2X=blockX+750;
void setup(){
size(1500,500);
font=loadFont ("AgencyFB-Reg-48.vlw");
textSize (30);
smooth();
}
void draw(){
background (0);
block();
gameCharacter();
instruct();
if(life4==true){
x=x+10;
background(x);
delay (100);
textSize(100);
fill(255,0,0);
text("Game Over");
}
}
void instruct(){
fill(175);
text ("Avoid moving blocks. AWSD keys are faster, arrow keys are slower.",250,40);
text("You have three lives",600,80);
}
void gameCharacter(){
fill(56,139,36);
ellipse(xpos,ypos,25,25);
if (keyPressed){
if(keyCode==UP){
ypos-=5;
}
if(keyCode==DOWN){
ypos+=5;
}
if(keyCode==LEFT){
xpos-=5;
}
if(keyCode==RIGHT){
xpos+=5;
}
if(key=='w'){
ypos-=10;
}
if(key=='s'){
ypos+=10;
}
if(key=='a'){
xpos-=10;
}
if(key=='d'){
xpos+=10;
}
}
if (xpos<0){
xpos=0;
}
if(xpos>500){
xpos=500;
}
if(ypos>height){
ypos=height;
}
if (ypos<0){
ypos=0;
}
if(life1==false){
if(xpos+25>=blockX-25&&xpos+25<=blockX+25){
if(ypos+25<=blockY+225&&ypos-25>=blockY-225){
life1=true;
blockX=1550;
block2X=2300;
}
else if(ypos-25<=blockY+225&&ypos-25>=blockY-225){
life1=true;
blockX=1550;
block2X=2300;
}
}
}
if(life1==false){
if(xpos-25>=blockX-25&&xpos-25<=blockX+25){
if(ypos+25<=blockY+225&&ypos+25>=blockY-225){
life1=true;
blockX=1550;
block2X=2300;
}
else if(ypos-25<=blockY+225&&ypos-25>=blockY-225){
life1=true;
blockX=1550;
block2X=2300;
}
}
}
if(life1==true){
if(xpos+25>=blockX-25&&xpos+25<=blockX+25){
if(ypos+25<=blockY+225&&ypos-25>=blockY-225){
life2=true;
blockX=1550;
block2X=2300;
}
else if(ypos-25<=blockY+225&&ypos-25>=blockY-225){
life2=true;
blockX=1550;
block2X=2300;
}
}
}
if(life1==true){
if(xpos-25>=blockX-25&&xpos-25<=blockX+25){
if(ypos+25<=blockY+225&&ypos+25>=blockY-225){
life2=true;
blockX=1550;
block2X=2300;
}
else if(ypos-25<=blockY+225&&ypos-25>=blockY-225){
life2=true;
blockX=1550;
block2X=2300;
}
}
}
if(life2==true){
if(xpos+25>=blockX-25&&xpos+25<=blockX+25){
if(ypos+25<=blockY+225&&ypos-25>=blockY-225){
life3=true;
blockX=1550;
block2X=2300;
}
else if(ypos-25<=blockY+225&&ypos-25>=blockY-225){
life3=true;
blockX=1550;
block2X=2300;
}
}
}
if(life2==true){
if(xpos-25>=blockX-25&&xpos-25<=blockX+25){
if(ypos+25<=blockY+225&&ypos+25>=blockY-225){
life3=true;
blockX=1550;
block2X=2300;
}
else if(ypos-25<=blockY+225&&ypos-25>=blockY-225){
life3=true;
blockX=1550;
block2X=2300;
}
}
}
if(life3){
if(xpos+25>=blockX-25&&xpos+25<=blockX+25){
if(ypos+25<=blockY+225&&ypos-25>=blockY-225){
life4=true;
blockX=1550;
block2X=2300;
}
else if(ypos-25<=blockY+225&&ypos-25>=blockY-225){
life4=true;
blockX=1550;
block2X=2300;
}
}
}
if(life3){
if(xpos-25>=blockX-25&&xpos-25<=blockX+25){
if(ypos+25<=blockY+225&&ypos+25>=blockY-225){
life4=true;
blockX=1550;
block2X=2300;
}
else if(ypos-25<=blockY+225&&ypos-25>=blockY-225){
life4=true;
blockX=1550;
block2X=2300;
}
}
}
if(life1&&life2&&life3){
println("life1,2,and 3 are true");
}
else if(life1&&life2){
println("life1and2 are true");
}
else if(life1){
println("life1 is true");
}
if (life3==false){
println("life3 is false");
}
//||xpos-25==blockX+750+25||xpos+25==blockX+750-25)
}
void block(){
fill(255);
rectMode(CENTER);
rect(blockX,blockY,50,450);
rect(blockX,blockY+510,50,450);
rect(block2X,blockY-510,50,450);
rect(block2X,block2Y,50,450);
rect(block2X,block2Y+510,50,450);
rect(block2X,block2Y-510,50,450);
blockX-=8;
block2X-=8;
if(blockX<=0){
blockY=random(500);
blockX=1550;
}
if(block2X<=0){
block2Y=random(500);
block2X=1550;
}
}
1