A game menu with three levels (solved)


#1

I’m looking for someone who can help me organize and intervene the code for a school assignment. I have to make a game with three levels + the main menu that contains options to play the full game (three levels of followed) and 3 options to play the individual levels + the possibility of going back and starting the game again (with the use of the keyboard to control the interface)
the issue is like that, it’s been three weeks since I’ve been working on this and I’m not finished. It’s all messy. I have the sketch of what is the main menu and the code of the game that I want to intervene for the three levels. The truth is that I am very tired already, I made thousands of sketches and I could not achieve my goal, I am sure it is something so foolish that I’m not seeing what could be. I repeat, I need someone who can correct and order the code, I am very desperate

If you are a kind soul and you are interested in helping me, write meand I will send you the code and the idea that I have in mind (the codeof the ships I already have it, you just have to intervene so that it is as Ithought)


#2

Sorry, but that’s not really how this works. We can’t just do your homework for you.

The best advice I can give you is to break your problem down into smaller steps and take those steps on one at a time. Don’t try to do everything all at once. Which small step are you stuck on?

You’ll also need to get into the habit of debugging your code. Which line of code is behaving differently from what you expected?

If all else fails, then you can post a small example program here, along with a more specific technical question. Good luck.

Please read the following guidelines and then post a more specific question if you want some help.


#3

As @Kevin said, you’re going to have to give us something we can help you with!

I understand your frustration though, and I’m sure many of us have been there many times. What I would recommend is fully planning out your code. You already know that you have to make a game with three levels, and a main menu. You already know that you need to be able to start a game again and such. You mention that you’re not able to achieve your goal… what goal, specifically? Any section of code that you’re stuck on? We would surely love to help you out! :smiley:

If you do already have a plan, and your code is just not working out? Then again, please send where exactly the problem is, and we would love to answer it. Also, it would be best if you posted your code, so that if someone else has a similar problem to you they can see the answer on the forum and not have to ask another question themselves, if that makes sense.

Please feel free to keep us updated on your project! And feel even more free to ask as many questions as you need to, there are plenty of users here on the forum that would love to answer them!

EnhancedLoop7


#4

As has been said already, we can’t help with code we can’t see. Even then, we’re not going to edit it for you.

You have, however, described your goal sufficiently enough to warrant at least a little help when it comes to structuring your code. I suggest the following format:

int state = 0;
int mode = 0;

void setup() {
  fullScreen();
  reset();
}

void reset() {
  state = 0;
  mode = 1;
}

void draw() {
  background(255, 0, 255);
  switch(state) {
  case 0:
    draw_menu();
    break;
  case 1:
    draw_stage_one();
    break;
  case 2:
    draw_stage_two();
    break;
  case 3:
    draw_stage_three();
    break;
  }
}

void mousePressed() {
  switch(state) {
  case 0:
    if ( over_play_all() ) {
      mode = 1;
      begin_stage(1);
      return;
    }
    if ( over_play_one() ) {
      mode = 0;
      begin_stage(1);
      return;
    }
    if ( over_play_two() ) {
      mode = 0;
      begin_stage(2);
      return;
    }
    if ( over_play_three() ) {
      mode = 0;
      begin_stage(3);
      return;
    }
    break;
  case 1:
    mousePressed_stage_one();
    break;
  case 2:
    mousePressed_stage_two();
    break;
  case 3:
    mousePressed_stage_three();
    break;
  }
}

void begin_stage(int stage) {
  switch(stage) {
  case 1:
    setup_stage_one();
    state = 1;
    return;
  case 2:
    setup_stage_two();
    state = 1;
    return;
  case 3:
    setup_stage_three();
    state = 1;
    return;
  }
}

void setup_stage_one() {
}
void setup_stage_two() {
}
void setup_stage_three() {
}
void draw_menu() {
}
void draw_stage_one() {
}
void draw_stage_two() {
}
void draw_stage_three() {
}
void mousePressed_stage_one() {
}
void mousePressed_stage_two() {
}
void mousePressed_stage_three() {
}
boolean over_play_all() {
  return(false);
}
boolean over_play_one() {
  return(false);
}
boolean over_play_two() {
  return(false);
}
boolean over_play_three() {
  return(false);
}

void on_stage_finished(boolean won) {
  if ( won && mode == 1 ) {
    state++;
    state%=4;
  } else {
    state = 0;
  }
}

If you try running this, you will see that it kicks out nothing more than a full screen, blank purple sketch. As far as being a game goes, that isn’t helpful. Your goal instead is to understand the structure this code is providing, and fill in its gaps with your own game logic.


#7

Please format your code :blush:

It consist on these two steps:

  1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
  2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.

Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.

Kf


#9

thaks for the info :smile:


#10

Thank you very much. At least I know that using the Switch case was a good option. Part of my bad mood was cause I used " if, else if …) to make the main menu, but it did not work well. Using a switch case was the best option to change the screens, but still thinking about how to turn back and reset :relaxed:


#13

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


#15

@blueesky No problem, if you have any more questions we will be glad to help out! Keep us updated on your progress :smiley:

EnhancedLoop7


#17

I’m glad to hear that, and it is not a problem :smiley:

EnhancedLoop7


#19

Please do not delete your posts like that. This is a public forum, and it gets very confusing if you edit your posts after people have replied.

Please go back and edit your posts again so they contain their original content.