-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
230 lines (182 loc) · 7.55 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""The game engine and the function for parsing user inputs."""
import os
from sys import exit
import enemies
from player import Player
from map import rooms
from util import divider
from util import delay
def play():
"""The engine that runs the game."""
# Clear the command prompt and show the game introduction.
os.system('cls')
print """
______ _ _______ _______ _______ _
( __ \ |\ /|( ( /|( ____ \( ____ \( ___ )( ( /|
| ( \ )| ) ( || \ ( || ( \/| ( \/| ( ) || \ ( |
| | ) || | | || \ | || | | (__ | | | || \ | |
| | | || | | || (\ \) || | ____ | __) | | | || (\ \) |
| | ) || | | || | \ || | \_ )| ( | | | || | \ |
| (__/ )| (___) || ) \ || (___) || (____/\| (___) || ) \ |
(______/ (_______)|/ )_)(_______)(_______/(_______)|/ )_)
_______ _______ ______ _______ _______ _______
( ___ )( ____ \ ( __ \ ( ___ )( ___ )( )
| ( ) || ( \/ | ( \ )| ( ) || ( ) || () () |
| | | || (__ | | ) || | | || | | || || || |
| | | || __) | | | || | | || | | || |(_)| |
| | | || ( | | ) || | | || | | || | | |
| (___) || ) | (__/ )| (___) || (___) || ) ( |
(_______)|/ (______/ (_______)(_______)|/ \|
Type help for instructions, q to quit.
"""
divider()
print "Your quest is to defeat the evil spider in the Dungeon of Doom."
divider()
print rooms[player.room].description()
# Run game engine while the player is alive.
while player.is_alive():
try:
# Provide prompt to user.
divider()
prompt = raw_input("\nWhat do you want to do?\n> ")
os.system('cls')
# Determine what action to take based on user input.
parse(prompt)
divider()
# Update the toom conditions based on the user's action.
rooms[player.room].update_room_conditions()
# End game if victory condition is met.
if victory():
break
# Print the updated room conditions.
print rooms[player.room].description()
except KeyboardInterrupt:
quit()
# Final text printed when the game ends.
print "The End."
def quit():
"""Quit the game and exit back to the command line."""
os.system('cls')
print "Quitting the game."
exit(0)
def victory():
"""Victory conditions of the game."""
# Victory condition: The spider in the spider room is dead.
return not rooms['spider_room'].contents['spider'].is_alive()
def parse(prompt):
"""Determine what action to take based on the user input, prompt."""
# List of acceptable first words in user input.
movements = ["move", "go", "travel", "head"]
directions = ["north", "south", "east", "west", "n", "s", "e", "w"]
fighting = ["kill", "destroy", "fight", "slay"]
looking = ["look", "see", "inspect", "view", "search"]
taking = ["take", "get", "steal"]
using = ["use", "put", "combine", "pull", "push", \
"close", "open", "lock", "unlock"]
using_connecters = ["on","with","in"]
# Split user input into list of words.
input = prompt.lower().split()
# Parse the list of words in the user input.
# No user input.
if input == []:
print "You didn't enter anything!"
return
# View inventory.
elif len(input) == 1 and get_first_word(input) == "inventory":
return player.look("inventory")
# View help.
elif len(input) == 1 and get_first_word(input) == "help":
return help()
# Quit the game.
elif len(input) == 1 and get_first_word(input) == "quit":
return quit()
# Move to a new location (north, south, east, or west).
elif get_first_word(input) in movements or \
get_first_word(input) in directions:
# Delete the move prompt, leaving only the direction.
if len(input) > 1 and get_first_word(input) in movements: del input[0]
# Format the direction for the player's move method.
# For example, "north" becomes "N"
direction = input[-1][0].upper()
player.move(direction)
# Fight an enemy.
elif get_first_word(input) in fighting:
fight_prompt = input[0]
enemy = input[-1]
# User supplied a fight prompt but no enemy to fight.
if enemy == fight_prompt:
print "What do you want to %s?" % fight_prompt
else:
try:
# If the specified enemy is in the room, fight it.
enemy_to_fight = rooms[player.room].contents[enemy]
player.fight(enemy_to_fight)
except KeyError:
print "You can't %s that!" % fight_prompt
# Look at something.
elif get_first_word(input) in looking:
look_prompt = input[0]
thing = input[-1]
# User supplied a look prompt but nothing to look at, view, see, etc.
if thing == look_prompt:
if look_prompt == "look":
look_prompt += " at"
print "What do you want to %s?" % look_prompt
else:
player.look(thing)
# Take something.
elif get_first_word(input) in taking:
take_prompt = input[0]
item = input[-1]
player.take(take_prompt, item)
# Use something.
elif get_first_word(input) in using:
input = [word for word in input if word != "the"]
use_prompt = input[0]
# Input contains only a user prompt.
if len(input) == 1:
print "What do you want to %s?" % use_prompt
# Input is in 'use X' format.
elif len(input) == 2:
object = input[1]
player.use(use_prompt, object)
# Input is in 'use X on' or 'use X Y' format.
elif len(input) == 3:
object = input[1]
# 'use X on'
if input[2] in using_connecters:
use_connecter = input[2]
print "What do you want to %s the %s %s?" % (
use_prompt, object, use_connecter)
# 'use X Y'
else:
other_object = input[2]
print "How do you want to %s %s and %s together?" % (
use_prompt, object, other_object)
# Input is in 'use X on Y' format.
else:
object = input[1]
use_connecter = input[2]
other_object = input[-1]
player.use(use_prompt, object, use_connecter, other_object)
# User input cannot be parsed.
else:
print "I don't understand."
def get_first_word(input):
"""Return the first word of the user input."""
return input[0]
def help():
"""Print the game's documentation for the user."""
print"""
quit - Quit the game.
help - Get help.
inventory - See your inventory.
take <item> - Add something to your inventory.
look <thing> - Get the description of something.
use <item> - Use an item from your inventory.
fight <enemy> - Fight an enemy.
move <direction> - Move north, south, east, or west.
"""
# Create the player and start the game.
player = Player("hero", 20, 8, "entrance_room")
play()