Game Development Reference
In-Depth Information
6 - Dragon Realm
NameError: name 'sayGoodBye' is not defined
To fix this, put the function definition before the function call:
def sayGoodBye():
print('Good bye!')
sayGoodBye()
Displaying the Game Results
Back to the game's source code:
20. print('You approach the cave...')
21. time.sleep(2)
We display some text to the player, and then call the
time.sleep()
function.
Remember how in our call to
randint()
, the function
randint()
is inside the
random
module? In the Dragon Realm game, we also imported the
time
module. The
time
module has a function called
sleep()
that will pause the program for a few
seconds. We pass the integer value
2
as an argument to the
time.sleep()
function to
tell it to pause for exactly 2 seconds.
22. print('It is dark and spooky...')
23. time.sleep(2)
Here we print some more text and wait again for another 2 seconds. These short pauses
add suspense to the game, instead of displaying all the text all at once. In our jokes
program, we called the
input()
function to wait until the player pressed the Enter key.
Here, the player doesn't have to do anything at all except wait.
24. print('A large dragon jumps out in front of you! He
opens his jaws and...')
25. print()
26. time.sleep(2)
What happens next? And how does the program decide what happens?