This topic is READ ONLY
dracobook (2) [Avatar] Offline
#1
Hi I do not really understand how the "done" variable works in chapter 18 and chapter 19. Could you further explain? Does this have to do with the "While True" loop? What does this "While True" loop mean...I mean what is the True referring to? is it assigned to some variable? Any help would be good. Thanks
warren.sande (211) [Avatar] Offline
#2
Re: "done" variable
The "while True" loop is just a way to make an infinite loop - one that runs forever (or until the user does something to end the program).

Remember that while loops keep looping while some condition is True. So, this basically says, "while True is True, keep looping". Since True is always True, it loops forever.

A while loop that doesn't loop forever might look like:
while i < 10:

As long as the comparison i < 10 is True, it will keep looping, but if i reaches 10, then that comparison will be False, and the loop will stop.

In the code in Chapters 18 and 19, you will also see "while 1:" That's just another way of making an infinite loop. It really means "while 1 != 0". Since 1 is always "not equal" to 0, the expression 1 != 0 is always true, so it loops forever.

To break out of the infinite while loop, we either need to use "break" or "continue", or end the program. You can end a Pygame program by closing the Pygame window, or by using the sys.exit() function, which is what we do.
So the while loop keeps running as long as the Pygame window is open.

The "done" variable tells the program whether the player is still playing the game, or if they are done (out of lives). Being done doesn't mean the program stops. The pygame window can still stay open after you lose you last life - you're just not playing any more. Done gets set to True when there are no more lives left (lives == 0).

So, when done is True, it means keep the Pygame window open (keep refreshing the screen, etc.) but don't start a new life.

When the player is done, (done == True), the program also displays the final score.

I hope this helps make it clearer.

Regards,
Warren Sande
dracobook (2) [Avatar] Offline
#3
Re: "done" variable
Ah...thanks a lot for your quick response and also your help... you really cleared things up !