Hey @adamchan ! This is a fun problem to talk through. Here are some tips:
Essentially, you’re trying to add an “event” to your code - when you press the space bar, go to a new screen. This is really tricky in Game Lab, which isn’t setup to use Event Based programming. It may seem like a while loop is what you need, but in reality we just need to use the existing draw()
loop in a creative way.
To get this kind of functionality, you need to plan your program around different states. Right now, your program is designed to have two states - a start screen state and a game state. To transition between the two, you press the space bar. I find it helpful to think of states as a diagram:
We can use a variable to keep track of which state we’re in (similar to your start variable above), and then change that variable every time we want our program to behave differently. This means our draw loop is basically a bunch of if-statements to test which state we’re in, then we can use functions to perform the correct behaviors based on the result. Using my diagram above, here’s what that code could look like:
var state = 1;
function draw() {
if(state == 1) {
text(“Hit Space to Begin”,200,200);
if(keyDown(“space”)){
state = 2;
}
} else if (state == 2) {
playGame();
}
}
If you wanted, you could get even more complicated with how your game is designed:
This would mean adding more to your if-statements in the draw loop so you could transition between the different states.
This all might seem a little overwhelming at first. If it helps, here’s an example of a 4-state game that uses this same pattern: Help Elvis Get To His Concert.
One kinda cool thing about this way of thinking is that using states to represent your game is a really common practice in other computer science areas. Most robotics operate on a state-based system, and systems for analyzing text also use a similar system - for example, taking an address and parsing it into house number, street, city, zip code can be represented using states.
Hope this helps!
-Dan
Code.org Curriculum Writer