[RESOLVED] A game menu with three levels

Alright so first off, you don’t have a void setup() ? I think you should start with that :smiley:

void setup()
 {
  size(600,600);    
 }

Next, where is your levels variable? I see that you are using one. I will declare that at the top of your program:

int levels = 0;

Now, you will be able to pass in your variable through the switch statement.

Next, you have functions that aren’t really created? Like menu() or game() for example.

I went through and made simple backgrounds for each function here:

void menu()
{
 background(0,255,0); 
 textAlign(CENTER); 
 textSize(50); 
 text("MENU", width/2,height/2);  
}

void game()
{
 background(255,255,0); 
 textAlign(CENTER); 
 textSize(50); 
 text("GAME", width/2,height/2);  
}

void level2()
{
 background(0,255,255); 
 textAlign(CENTER); 
 textSize(50); 
 text("LEVEL 2", width/2,height/2);  
}

void level3()
{
 background(255,155,255); 
 textAlign(CENTER); 
 textSize(50); 
 text("LEVEL 3", width/2,height/2);  
}

void info()
{
 background(125,255,255); 
 textAlign(CENTER); 
 textSize(50); 
 text("INFO", width/2,height/2);  
}

Now, depending on the function called, you will see a different screen with whatever state it is on.

Next, I noticed in your void keyPressed() function you were switching the levels variable again? I don’t necessarily know exactly what your aim is with doing that, but what I did was change it so you’re switching the actual key? And changing the state based on that?

Like this:

void keyPressed() {
 switch(key)
  {
   case 'q': 
     levels = 1; //press q to change the levels to 1
    break; 
    
    case 'w': 
     levels = 2; //press w to change the levels to 2
    break; 
   
    case 'e': 
     levels = 3; //press e to change the levels to 3
    break; 
   
    case 'r': 
     levels = 4; //press r to change the levels to 4
     break; 
   
    default: 
     levels = 0; //press anything else to change the levels back to 0
    break;  
  }
}

As of now, you have a very simple state switching program. I kept everything that you had in draw, and just made those changes that I showed you. You can either use this, and expand on it, or you can use @TfGuy44’s code, which is another great example as well.

Feel free to keep us posted, and we would love to help you further,

EnhancedLoop7

2 Likes