I just need your help for a simple question. I’m about on doing a sort of mini-game in which you can choose between playing against the CPU or another player… but I don’t know how!
In fact, I need to have a “menu screen” to show before the actual game starts in which you can have a text like “Choose if you want to play single-player mode or two-player mode” and then you have two buttons to press, pressing one you’re gonna choose single-player mode and with the other one you’re gonna add a friend to the game; and this should be shown forever until you don’t press one of the buttons.
The problem is, if I do something like a while(); or do {} while(); cycle that runs until I do something, it does show nothing on the window; it just keeps showing a blank window forever (even if I try to press the buttons, guessing where the hell they should be, it doesn’t load them and I can’t interact with the program in any way).
So, I need someone’s help to do this, I just need this to be alive!! Plz… Or at least say me if you’re sure that this is impossible or there’s a way of doing this…
The idea is that draw() has different states it runs in.
E.g.:
state 0 means we ask for number of players
state 1 we are in a game human against PC
state 2 we are in a game of two humans
Thus no while() needed just let draw() run again and again:
void draw() {
if(state==0) {
background(0);
text ("please click the number of players", 112,112);
}
else if (state==1) {
background(0);
//game...
}
else if (state==2) {
background(0);
//game...
}
}
That’s right. The function draw() updates the screen only once at its end. Hence you don’t see anything.
Therefore use my method / technique above where you use the fact that draw() in itself loops automatically. It runs 60 times per second. But it runs only the lines that belong to the current state due to the if-clause. That’s the trick.
(Nothing outside the if-clauses is allowed. This is a rule of thumb.)
When you change the state to 1 or to 2 respectively, the next screen for the game is shown.
A Help Screen or a High Score Screen would be other possible states.
In mousePressed() or keyPressed() you need the same if..else if..-clause to distinguish the inputs depending on the state:
E.g. when you enter the number of players, that’s possible in state 0, but not in state 1 or 2.