Repository navigation
Expand file tree
/
Copy pathplayer.py
More file actions
84 lines (72 loc) · 2.85 KB
/
Copy pathplayer.py
File metadata and controls
84 lines (72 loc) · 2.85 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
# Write a class to hold player information, e.g. what room they are in
# currently.
from colorama import Fore, Back, Style
from item import Weapon, Item
class Player:
def __init__(self, nickname, current_room=None, inventory=None, dmg=0):
self.nickname = nickname
self.current_room = current_room
if inventory == None:
self.inventory = []
else:
self.inventory = inventory
self.dmg = dmg
def __str__(self):
return f"{Fore.BLACK}{Back.WHITE} {self.nickname} (Attack dmg: {self.dmg}) {Style.RESET_ALL}"
def attack(self, unit):
for monster in self.current_room.monsters:
if monster.name == unit:
unit = monster
break
if type(unit) == str:
print(f'{Back.RED}Wield wisely!{Style.RESET_ALL}')
else:
if unit.health > self.dmg:
unit.health -= self.dmg
else:
unit.dies_at(self.current_room)
def show_inventory(self):
if len(self.inventory) > 0:
item_counts = dict.fromkeys(
[item.name for item in self.inventory], 0)
for item in self.inventory:
if item.name in item_counts.keys():
item_counts.update(
{item.name: item_counts.get(item.name) + 1})
player_inventory = 'Your inventory: ' + \
', '.join(
[f'{item}: {item_counts[item]}' for item in item_counts])
else:
player_inventory = 'No items in inventory'
print(f'{Back.BLUE}{Fore.WHITE}{player_inventory}{Style.RESET_ALL}')
def move_to(self, direction):
def no_room():
print(f'{Fore.RED}There is nothing there!{Style.RESET_ALL}')
if direction.lower() == 'w':
if self.current_room.w_to:
self.current_room = self.current_room.w_to
print(self.current_room)
else:
no_room()
elif direction.lower() == 'e':
if self.current_room.e_to:
self.current_room = self.current_room.e_to
print(self.current_room)
else:
no_room()
elif direction.lower() == "s":
if self.current_room.s_to:
self.current_room = self.current_room.s_to
print(self.current_room)
else:
no_room()
elif direction.lower() == 'n':
if self.current_room.n_to:
self.current_room = self.current_room.n_to
print(self.current_room)
else:
no_room()
else:
print(f'{Fore.RED}Sorry, no idea what that means!{Style.RESET_ALL}')
print(f'{Fore.CYAN}Type "help" for available commands{Style.RESET_ALL}')
print(self.current_room)