Gaming Questions (levels and states/screens)

Hi I was wondering how someone can create levels for a game in processing??
,Thank you!

1 Like

Yes, you can make different levels for games in Processing.

Here’s a game I once made that has different levels, as well as a built-in level editor: Amanda's Birthday Quest

Here’s the source so you can see how it works:
http://www.tfguy44.com/BirthdayQuest/BirthdayQuest.pde

it’s hard to describe without seeing your code and knowing what game it is

show some code

There is no inbuilt function for levels. You have to program it yourself.

Example

for example make an int variable levelNum (int levelNum;) and increase it when your level is finished (in a maze: player reaches target; in breakout: all bricks are gone).

levelNum++;

Check it against new variable maxLevel (final int maxLevel = 4;) and then display a “You won. You finished all levels.” Screen.

in draw() you can draw the maze (or whatever content the levels have) depending on levelNum:

switch (levelNum) {
 case 0:
  // show level 0
  // ... 
  break; 

 case 1:
  // show level 1
  // ...
  break; 

// ...

case maxLevel : 
  //display a "You won. You finished all levels." Screen.
  break; 

default: 
  // Error 
  break; 
} // end of switch

Remark

when your maze is in a data structure you can make an array of it which is more elegant like

displayMaze(maze[levelNum]);

then you don’t need the switch()

Chrisir

Here is my code, so what i want to do is allow the user to click on a like start button and take them to a
different screen where there are game modes this works but the issue that keeps arising is that like the buttons on the various screens overlap so they both like happen at the same time, what can i do so that the screens function separately, thank you! sorry if this is a little confusing.

1 Like

Distinguish between mousePressed as a variable as you have in draw() and mousePressed() as a function that you already have.

Move the mouse functionality from draw into the mousePressed () function. This will help you because a mouse press here gets registered only once and not many times.

Additionally you can remake the if system that distinguishes the screens in mousePressed () function so only the relevant buttons for the screen are evaluated

here is an example of such a screen / state management

Within the game section you can have of course Levels as shown above.

Full Sketch

// screen / state
int state=0; 
final int maxState = 2; 

//-----

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

void draw() {

  background(0);

  switch (state) {
  case 0:  
    // show state 0 / Start Menu for example
    text("screen 0", 120, 120); 
    break; 

  case 1:
    // show state 1 / Game for example
    text("screen 1", 120, 120); 
    break; 

  case maxState: 
    //display a "You won. You finished all levels." Screen.
    text("screen 2", 120, 120);
    break; 

  default: 
    // Error 
    break;
  } // end of switch
}//draw

// ----------

void mousePressed() {

  switch (state) {
  case 0:
    // show 0
    state=1;  
    break; 

  case 1:
    // show 1
    state=2; 
    break; 

  case maxState: 
    //ignore
    break; 

  default: 
    // Error 
    break;
  } // end of switch
}
//