Game Development Reference
In-Depth Information
6 - Dragon Realm
39.
40. caveNumber = chooseCave()
41.
42. checkCave(caveNumber)
43.
44. print('Do you want to play again? (yes or no)')
45. playAgain = input()
How the Code Works
Let's look at the source code in more detail.
1. import random
2. import time
Here we have two
import
statements. We import the
random
module like we did in the
Guess the Number game. In Dragon Realm, we will also want some time-related functions
that the
time
module includes, so we will import that as well.
Defining the
displayIntro()
Function
4. def displayIntro():
5. print('You are on a planet full of dragons. In front
of you,')
6. print('you see two caves. In one cave, the dragon is
friendly')
7. print('and will share his treasure with you. The other
dragon')
8. print('is greedy and hungry, and will eat you on
sight.')
9. print()
Figure 6-1 shows a new type of
statement, the
def statement
. The
def
statement is made up of the def
keyword, followed by a function name
with parentheses, and then a colon
(the
:
sign). There is a block after the
statement called the def-block.
Figure 6-1: Parts of a
def
statement.