Game Development Reference
In-Depth Information
Important Note! Be sure to run this program with Python 3, and not Python 2. The
programs in this topic use Python 3, and you'll get errors if you try to run them with Python
2. You can click on Help and then About IDLE to find out what version of Python you
have.
dragon.py
This code can be downloaded from http://inventwithpython.com/dragon.py
If you get errors after typing this code in, compare it to the topic's code with the online
diff tool at http://inventwithpython.com/diff or email the author at
al@inventwithpython.com
1. import random
2. import time
3.
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()
10.
11. def chooseCave():
12. cave = ''
13. while cave != '1' and cave != '2':
14. print('Which cave will you go into? (1 or 2)')
15. cave = input()
16.
17. return cave
18.
19. def checkCave(chosenCave):
20. print('You approach the cave...')
21. time.sleep(2)
22. print('It is dark and spooky...')
23. time.sleep(2)
24. print('A large dragon jumps out in front of you! He
opens his jaws and...')
25. print()
26. time.sleep(2)
27.
28. friendlyCave = random.randint(1, 2)
29.
30. if chosenCave == str(friendlyCave):
31. print('Gives you his treasure!')
32. else:
33. print('Gobbles you down in one bite!')
34.
35. playAgain = 'yes'
36. while playAgain == 'yes' or playAgain == 'y':
37.
38. displayIntro()
Search WWH ::




Custom Search