Level Change controlled by Buttons

Hi,
I’m trying to create a very simple game where the player is given a choice between two buttons in every level, get it right and you move on get it wrong and the word “WRONG” flashes on the screen, but every time I click Start it immediately goes to the End screen. Does anyone know why this is happening?

Here’s what I have so far:

boolean Start = true;
boolean Level1 = false;
boolean Level2 = false;
boolean Level3 = false;
boolean End = false;

void setup()
{
  size(900,900);
  background(0);
  
  fill(150,240,156);
  textSize(150);
  text("ESCAPE",195,125);
  
  fill(173,165,165);
  rect(280,150,260,80,10);
  fill(150,240,156);
  textSize(100);
  text("Start",300,225);
}
 
  void mousePressed()
  {
    if (mouseX>=280 && mouseX<=540 && mouseY>=150 && mouseY<=230);
  {
    Level1=!Level1;
  }
  
  if (mouseX>=80 && mouseX<=180 && mouseY>=720 && mouseY<=800);
  {
    if (Level1);
    {
      Level2=!Level2;
    }
    if (Level2)
    {
      End=!End;
    }
  }
  if (mouseX>=500 && mouseX<=810 && mouseY>=720 && mouseY<=800);
  {
    if(Level1)
    {
      fill(150,240,156);
      textSize(125);
      text("WRONG",300,680);
    }
    if(Level2)
    {
      fill(150,240,156);
      textSize(125);
      text("WRONG",300,680);
    }
  }
}

void draw()
{
  if (Level1)
  {
    background(0);
    
    fill(150,240,156);
    textSize(40);
    text("You wake up in a room with a locked door.  The lock on the door is RUSTY and one wall is made of loose BRICKS.",25,80,850,240);
    
    fill(171,176,174);
    rect(80,720,150,80,10);
    fill(150,240,156);
    textSize(50);
    text("Lock",80,780); 
    
    fill(171,176,174);
    rect(500,720,150,80,10);
    fill(150,240,156);
    textSize(50);
    text("Bricks",500,765); 
  }
  
  if(Level2)
  {
    background(0);
    
    fill(150,240,156);
    textSize(40);
    text("There is no new doors, only ivy covering the walls, there are a pair of scissors on the floor, you cannot see the ceiling.",25,80,850,240);
    
    fill(171,176,174);
    rect(80,745,150,80,10);
    fill(150,240,156);
    textSize(50);
    text("Climb",84,787); 
  
    fill(171,176,174);
    rect(500,745,150,80,10);
    fill(150,240,156);
    textSize(50);
    text("Clip",500,787);
  }
  
  if(End)
  {
    background(0);
    
    fill(150,240,156);
    textSize(150);
    text("YOU WON!",95,340);
  }
}
1 Like

If you had added some System.out.out.println() or just println() to print out the state of those variables at the beginning and end of mousePressed you’d have noticed that after a single click you end up with ‘End’ being true.Close observation also reveals that “Wrong” gets drawn on the screen just before it switches to drawing You Won!

Thus clearly something is going wrong and ‘End’ is being toggled to True on the first click. I suspect that it happening because you have semicolons at the end of several If statements. So the If is terminating without doing anything and the stuff that should happen if the condition is true is being treated as a static block and being done every time.

Also, I think you want every check after the first if to be an else if so you don’t test multiple conditions that clearly are contradictory every click regardless of whether any of them were true. If you don’t do that, then what will happen is that if the first check is true and the first if block statements cause the second check to be true, then the second if block statements will also be executed and so on…

1 Like