Repository navigation
Expand file tree
/
Copy pathadv.py
More file actions
153 lines (116 loc) · 4.42 KB
/
Copy pathadv.py
File metadata and controls
153 lines (116 loc) · 4.42 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
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
from room import Room
from player import Player
from item import Item, Weapon
from monster import Monster
from colorama import Fore, Style
from os import system, name
# Declare all the rooms
sword = Weapon("sword", "you can slay your enemy with this")
mana = Item("mana", "you can cast spells with this")
heal = Item("heal", "you can heal your health with this")
roshan = Monster('Roshan')
creep = Monster('Creep')
room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons", [sword], [roshan, creep]),
'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east.""", [mana]),
'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling
into the darkness. Ahead to the north, a light flickers in
the distance, but there is no way across the chasm.""", [heal]),
'narrow': Room("Narrow Passage", """The narrow passage bends here from west
to north. The smell of gold permeates the air.""", [mana, sword, heal]),
'treasure': Room("Treasure Chamber", """You've found the long-lost treasure
chamber! Sadly, it has already been completely emptied by
earlier adventurers. The only exit is to the south."""),
}
# Link rooms together
room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
room['foyer'].n_to = room['overlook']
room['foyer'].e_to = room['narrow']
room['overlook'].s_to = room['foyer']
room['narrow'].w_to = room['foyer']
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']
#
# Main
#
def clear_screen():
if name == 'nt':
system('cls')
else:
system('clear')
player_name = ""
clear_screen()
while len(player_name) < 1:
player_name = input('Please enter a nickname: ')
# Make a new player object that is currently in the 'outside' room.
new_player = Player(player_name, room['outside'])
clear_screen()
print('Welcome', new_player)
# Write a loop that:
#
# * Prints the current room name
# * Prints the current description (the textwrap module might be useful here).
# * Waits for user input and decides what to do.
#
# If the user enters a cardinal direction, attempt to move to the room there.
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.
is_playing = True
print(new_player.current_room)
while is_playing:
command = input('What do you want to do? ')
if(command == 'q'):
is_playing = False
break
clear_screen()
if(command == 'help' or command == 'h'):
print('[s] for south')
print('[n] for north')
print('[e] for east')
print('[w] for west')
print('get [item_name] for picking up items')
print('take [item_name] for picking up items')
print('drop [item_name] for dropping items')
print('attack [unit_name] for attacking monsters')
print(new_player.current_room)
elif command == 'i' or command == 'inventory':
new_player.show_inventory()
print(new_player.current_room)
else:
command = command.split(' ')
if len(command) == 1:
print(new_player)
command = command[0]
new_player.move_to(command)
elif len(command) == 2:
if command[0] == 'get' or command[0] == 'take':
requested_item = command[1]
for item in new_player.current_room.items:
if item.name == requested_item:
requested_item = item
break
if type(requested_item) != str:
requested_item.add_to_player(new_player)
else:
print('Item not here')
elif command[0] == 'drop':
dropping_item = command[1]
for item in new_player.inventory:
if item.name == dropping_item:
dropping_item = item
break
else:
requested_item = None
if isinstance(dropping_item, Item) or isinstance(dropping_item, Weapon):
dropping_item.remove_from_player(new_player)
else:
print("You can't drop what you don't have!")
elif command[0] == 'attack':
unit = command[1]
new_player.attack(unit)
print(new_player)
print(new_player.current_room)