Repository navigation
Expand file tree
/
Copy pathgame.py
More file actions
92 lines (76 loc) · 2.79 KB
/
Copy pathgame.py
File metadata and controls
92 lines (76 loc) · 2.79 KB
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
from player import Player
class Game:
"""The game"""
def __init__(self, player: Player, rooms: dict):
self.player = player
self.rooms = rooms
self.__playing = False
def start(self):
"""Begin the game"""
self.__playing = True
print("\n\n-- ADVENTURE GAME --\n\n\n")
print("Enter 'n', 's', 'e', or 'w' to move in a cardinal direction.")
print("Enter 'i' or 'inventory' to show your items in hand.")
print("Enter 'q' at any time to quit.")
while self.__playing:
self.__update()
def stop(self):
"""End the game"""
self.__playing = False
@property
def playing(self):
"""True if the game is running, false if not"""
return self.__playing
def __update(self):
print("\n" + self.player.current_room.name)
print(self.player.current_room.description)
move_txt = "Move options: "
move_options: list(str) = []
for d in ["n", "s", "e", "w"]:
room = self.player.current_room.get_room_in_direction(d)
if room is not None:
move_options.append(d)
move_txt = move_txt + ", ".join(move_options)
print(f"\n<{move_txt}>")
txt = input("<Do something>: ")
self.__parse_input(txt)
def __parse_input(self, txt: str):
if txt == "q":
self.__playing = False
print("Quitting...")
elif txt in ("i", "inventory"):
print(f"Inventory: {self.player.inventory}")
elif len(txt) == 1:
self.__parse_move(txt)
else:
self.__parse_action(txt)
def __parse_action(self, txt: str):
split_txt = txt.split()
verb = split_txt.pop(0)
if verb in ("get", "take"):
self.__parse_get(" ".join(split_txt))
elif verb == "drop":
self.__parse_drop(" ".join(split_txt))
else:
self.__warn_inv_input()
def __parse_move(self, txt: str):
possible_room = self.player.current_room.get_room_in_direction(txt)
if possible_room is None:
self.__warn_inv_input()
else:
self.player.enter_room(possible_room)
def __parse_get(self, item_name: str):
for item in self.player.current_room.items_list:
if item.name == item_name:
self.player.take_item(item)
return
print(f"<There's no '{item_name}' in the room>")
def __parse_drop(self, item_name: str):
for item in self.player.items_list:
if item.name == item_name:
self.player.drop_item(item)
return
print(f"<There's no '{item_name}' in your inventory>")
@staticmethod
def __warn_inv_input():
print("<Invalid input; please try again>")