Using a While Loop in Game Lab

I’m trying to create a “start button” mechanic by using a while loop as an “until” function above the main body of the code, where the loop executes until a key is pressed, something like this:

var start = false;
while(start==false){
text(“Hit Space to Begin”,200,200);
if(keyDown(“space”)){
start=true;
}
}

However, it doesn’t seem to work. I create a watcher for “start”, and the value never seems to change no matter what I try. I tried changing it from a keyDown to a mouseclick, but the loop doesn’t seem to register any key presses or button presses to change the value of “start” and so it never exits.

Any thoughts are appreciated!

I recreated your code and ran into the same problem. I’m not 100% sure I know what’s happening here, but when you change it to an “if” statement and put it inside the draw loop, it works and I think functionally, it’s the same thing as using while.

While is usually used to create a recurring (looping) condition whereas if is typically a one-time decision. However, putting an if statement inside the draw loop turns it into a looping condition so it functions like the while loop.

Now, why the while isn’t working? I’m not sure. Maybe someone with a little more experience can help explain that, but I can confirm I can’t get it to work inside or outside of my draw loop.

Mike

Thank you for your response, that makes total sense! I suppose I’m also simply frustrated that the while loop doesn’t seem to be working. I want to teach my students about while loops but if they don’t work with keyDowns or mouseDowns then I’ll be rather disappointed. I’m glad to see that I’m not crazy and that the issue was reproducible. I wonder if it’s an issue in Game Lab getting overloaded from the infinite loop and being unable to register the key inputs (if that’s even possible), because I do notice Game Lab begins to slow down quite a bit when trying to run the code in this way.

Yeah, I saw the same thing. I’ll ask around to see if I can find an answer.

Mike

If you use “mouseDidMove,” that does work. I’m guessing something about the while loop is slowing things down and you have to hit the space at the very millisecond that it is checking for that. (Something that is very difficult to do…)

Mike

Strange… I tried pausing the program in the debug window and stepping through one line at a time while holding down “space” and it doesn’t register. Something about the while loop doesn’t play nice with the key inputs. :thinking:

1 Like

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

1 Like