Create a Game Loop
I want to change this now by introducing a game loop, and the game loop can often be an infinite loop with a certain
break condition. You’ve done those before, so let’s try that out. I want to continue asking for input from the user until—let’s see the conditions—the game continues until the health of either the player or the monster reaches
0 or until the player decides to run away.
00:54 Now I have an infinite game loop here that keeps asking for an action from the user, prints out the action, and then does things, like either it reduces the health of the monster, or it heals the player’s health, or it allows us to run away.
But now we have the task that if the player runs away, the game should end. So this is an option for us to break out of the loop. So here I’m going to add the
break keyword, which means we now also have a way to get out of this infinite loop.
01:30 Save. Now we’re in here. Okay, now I’m going to start by healing. Doesn’t do anything for me. I’ll attack the monster. I can reduce the monster’s health, and it keeps asking me for new input. So that’s great.
Become a Member to join the conversation.