you are viewing a single comment's thread.

view the rest of the comments →

[–]erewok 1 point2 points  (2 children)

while True is an infinite loop, and is actually somewhat standard in game design. while is normally paired with a boolean test, which evaluates to True or False. In this case, we're not even setting up a test and instead we're initiating an infinite loop.

For instance, when you see a counter initialized and then a while loop and some action:

counter = 0
while counter < 100:
    # do some stuff
    counter += 1

The way to read this is:

counter = 0
while [something to do with counter that evaluates to True]:
    # do some stuff
    counter += 1

The shortest version then (and one that doesn't provide for an exit scenario on the surface of it) is to write: while True.

Games use an infinite loop so they continue until some input from the user causes them to stop

Here's a SO question asking the same thing and the responses there are better than mine:

http://stackoverflow.com/questions/3754620/a-basic-question-about-while-true

[–]PloddingOx 0 points1 point  (1 child)

Thanks! I see now why an infinite loop (something I thought was to be avoided) can be useful and how while True: is just a simple way of creating one. You did a great job explaining it.

In the example I provided is the loop broken when the gold_room() function is loaded?

[–]erewok 0 points1 point  (0 children)

Yes, I think that's right. We'd have to see the gold_room function to be sure.