Game Development Reference
In-Depth Information
function lets the player type in the cave they choose to go into. When the return cave line
in this function executes, the program execution jumps back down here, and the local
variable cave's value is the return value of this function. The return value is stored in a
new variable named caveNumber . Then the execution moves to the next line.
42. checkCave(caveNumber)
This line calls our checkCave() function with the argument of caveNumber's value.
Not only does execution jump to line 20, but the value stored in caveNumber is copied to
the parameter chosenCave inside the checkCave() function. This is the function that
will display either 'Gives you his treasure!' or 'Gobbles you down in
one bite!' , depending on the cave the player chose to go in.
Asking the Player to Play Again
44. print('Do you want to play again? (yes or no)')
45. playAgain = input()
After the game has been played, the player is asked if they would like to play again. The
variable playAgain stores the string that the user typed in. Then we reach the end of the
while-block, so the program rechecks the while statement's condition: while
playAgain == 'yes' or playAgain == 'y'
The difference is, now the value of playAgain is equal to whatever string the player
typed in. If the player typed in the string 'yes' or 'y' , then we would enter the loop
again at line 38.
If the player typed in 'no' or 'n' or something silly like 'Abraham Lincoln' ,
then the while statement's condition would be False , and we would go to the next line
after the while-block. But since there are no more lines after the while-block, the program
terminates.
But remember, the string 'YES' is different from the string 'yes' . If the player typed
in the string 'YES' , then the while statement's condition would evaluate to False and
the program would still terminate.
We've just completed our second game! In our Dragon Realm game, we used a lot of
what we learned in the "Guess the Number" game and picked up a few new tricks as well.
If you didn't understand some of the concepts in this program, then read the summary at the
end of this chapter, or go over each line of the source code again, or try changing the source
code and see how the program changes. In the next chapter we won't create a game, but a
computer program that will create secret codes out of ordinary messages and also decode
the secret code back to the original message.
Search WWH ::




Custom Search