Skip to content

Commit 97d6189

Browse files
authored
adventure game with comments
1 parent ac015a0 commit 97d6189

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

adventure_game.py

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# function to ask play again or not
2+
def play_again():
3+
print("\nDo you want to play again? (y or n)")
4+
5+
# convert the player's input to lower_case
6+
answer = input(">").lower()
7+
8+
if "y" in answer:
9+
# if player typed "yes" or "y" start the game from the beginning
10+
start()
11+
else:
12+
# if user types anything besides "yes" or "y", exit() the program
13+
exit()
14+
15+
16+
# game_over function accepts an argument called "reason"
17+
def game_over(reason):
18+
# print the "reason" in a new line (\n)
19+
print("\n" + reason)
20+
print("Game Over!")
21+
# ask player to play again or not by activating play_again() function
22+
play_again()
23+
24+
25+
# diamond room
26+
def diamond_room():
27+
# some prompts
28+
print("\nYou are now in a room filled with diamonds!")
29+
print("And there is a door too!")
30+
print("What would you do? (1 or 2)")
31+
print("1). Take some diamonds and go through the door.")
32+
print("2). Just go through the door.")
33+
34+
# take input()
35+
answer = input(">")
36+
37+
if answer == "1":
38+
# the player is dead, call game_over() function with the "reason"
39+
game_over("They were cursed diamonds! The moment you touched it, the building collapsed, and you die!")
40+
elif answer == "2":
41+
# the player won the game
42+
print("\nNice, you're are an honest man! Congrats you win the game!")
43+
# activate play_again() function
44+
play_again()
45+
else:
46+
# call game_over() with "reason"
47+
game_over("Go and learn how to type a number.")
48+
49+
50+
# monster room
51+
def monster_room():
52+
# some prompts
53+
# '\n' is to print the line in a new line
54+
print("\nNow you entered the room of a monster!")
55+
print("The monster is sleeping.\nBehind the monster, there is another door. What would you do? (1 or 2)")
56+
print("1). Go through the door silently.")
57+
print("2). Kill the monster and show your courage!")
58+
59+
# take input()
60+
answer = input(">")
61+
62+
if answer == "1":
63+
# lead him to the diamond_room()
64+
diamond_room()
65+
elif answer == "2":
66+
# the player is dead, call game_over() with "reason"
67+
game_over("The monster was hungry, he/it ate you.")
68+
else:
69+
# game_over() with "reason"
70+
game_over("Go and learn how to type a number.")
71+
72+
73+
# bear room
74+
def bear_room():
75+
# give some prompts
76+
# '\n' is to print the line in a new line
77+
print("\nThere is a bear here.")
78+
print("Behind the bear is another door.")
79+
print("The bear is eating tasty honey!")
80+
print("What would you do? (1 or 2)")
81+
print("1). Take the honey.")
82+
print("2). Taunt the bear.")
83+
84+
# take input()
85+
answer = input(">")
86+
87+
if answer == "1":
88+
# the player is dead!
89+
game_over("The bear killed you.")
90+
elif answer == "2":
91+
# lead him to the diamond_room()
92+
print("\nYour Good time, the bear moved from the door. You can go through it now!")
93+
diamond_room()
94+
else:
95+
# else call game_over() function with the "reason" argument
96+
game_over("Don't you know how to type a number?")
97+
98+
99+
def start():
100+
# give some prompts.
101+
print("\nYou are standing in a dark room.")
102+
print("There is a door to your left and right, which one do you take? (l or r)")
103+
104+
# convert the player's input() to lower_case
105+
answer = input(">").lower()
106+
107+
if "l" in answer:
108+
# if player typed "left" or "l" lead him to bear_room()
109+
bear_room()
110+
elif "r" in answer:
111+
# else if player typed "right" or "r" lead him to monster_room()
112+
monster_room()
113+
else:
114+
# else call game_over() function with the "reason" argument
115+
game_over("Don't you know how to type something properly?")
116+
117+
118+
# start the game
119+
start()

0 commit comments

Comments
 (0)