Hello!
I’m to trying to make a game, but I want to create a game start menu. I’ve looked at other forums, and I just feel confused??
Can someone ELI5 for this?
i am not sure what you need,
as a menu might be lots of code…
possibly you talk about a easy structure like
mode …
and a operation concept to change that mode
( like hit a key [n] to go to next mode )
so do you talk about that?
// https://discourse.processing.org/t/how-to-make-a-game-start-menu-in-processing/6019
int mode = 0;
void setup() {
println("click on canvas and use key [n] for next level");
println("mode: "+mode);
}
void draw() {
switch(mode) {
case 0:
// here your code for this mode
break;
case 1:
// here your code for this mode
break;
case 2:
// here your code for this mode
break;
default:
println("you should never see this");
break;
}
}
void keyPressed() {
if ( key == 'n' ) mode++;
if ( mode > 2 ) {
println("reset");
mode = 0;
}
println("mode: "+mode);
}
now a start menu would be much more code
while in “mode 0”
I suggesting making two functions: drawMenu()
and drawGame()
, and a variable int screenState
.
In drawMenu()
you create your “play”, “load”, “exit” buttons, and in drawGame()
you write what you would in draw()
wen you didn’t have the menu.
Whit those functions finished, you call them in draw()
based on screenState
, something like this:
final int MENUSCREEN = 0;
final int GAMESCREEN = 1;
// setup() and other global variables
void draw() {
if (screenState == MENUSCREEN) {
drawMenu();
} else if (screenState == GAMESCREEN) {
drawGame();
} else {
printlin("Something went wrong!");
}
}
void drawGame() {
// Pew, pew, I'm a game!
}
void drawMenu() {
// Don't forget to save :D
}
Remember to set screenShtate
to 0
in setup()
!
Given the datatype of the field screenState is int
, its default value is already 0
.
Which btW, corresponds to the same value as the constant MENUSCREEN.
Also, we can explicitly give an an initial value to the field screenState at the same time we declare it.
No need to wait till setup() in order to do so.
static final int MENUSCREEN = 0, GAMESCREEN = 1;
int screenState = MENUSCREEN;
void setup() {
I agree with the initialization, I’m used to C++ so some things bleed to my Processing, and it helps me debug some NullPointerExceptions
Although It might be better to use setup for all initialization, because you can then restart the sketch just by calling setup();
.
Both setup() & settings() should only be run once, called back automatically by the “Animation” Thread.
If a sketch restarting is desired, we can always make our own restart() function.
hi, how would i make it go from the menu to the game, thanks
In this function you can say if(keyPressed)
screenState = GAMESCREEN;
Or mousePressed