-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation.py
More file actions
125 lines (102 loc) · 3.8 KB
/
location.py
File metadata and controls
125 lines (102 loc) · 3.8 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
import curses
import printing, utils
import myevent
import object
class Location:
def __init__(self, name, description):
self.name = name
self.description = description
self.objects = {}
self.navigation = {}
self.events = {}
def add_nav(self, dirn, dest):
self.navigation[dirn] = dest
def add_events(self, events):
for event in events:
self.events[event] = myevent.Event(events[event]['prob'], events[event]['description'])
def add_objects(self, objs):
for obj in objs:
self.objects[obj] = object.Object(objs[obj]['name'], objs[obj]['description'])
try:
if objs[obj]['carryable'] == 'yes':
self.objects[obj].make_carryable()
except KeyError:
pass
try:
self.objects[obj].add_image(objs[obj]['image'])
except KeyError:
pass
def enter(self, this, stdscr, last_locn):
next_locn = self
desc = curses.newwin(int(0.5*curses.LINES), curses.COLS, 0, 0)
menu = curses.newwin(curses.LINES, curses.COLS, int(0.5*curses.LINES), 0)
menu.keypad(True)
menu.clear()
menu.refresh()
selection = 0
command = ""
operand = ""
if self != last_locn:
printing.print_at_speed(self.description + "\n", 100, desc)
for event in self.events:
self.events[event].trigger(desc)
for dirn, dest in self.navigation.items():
printing.print_at_speed("To the " + dirn + " there is " + dest.name + ".", 100, desc)
desc.refresh()
while(True):
count, command, operand = self.print_menu(menu, selection)
key = menu.getch()
if key in [curses.KEY_ENTER, ord('\n')]:
break
elif key == curses.KEY_UP:
selection = (selection - 1) % count
elif key == curses.KEY_DOWN:
selection = (selection + 1) % count
if command == "go":
next_locn = self.navigation[operand]
if command == "pickup":
self.objects[operand].pickup(this, operand, menu)
if command == "inventory":
self.print_inventory(this, menu)
return next_locn
def print_menu(self, stdscr, selection):
count = 0
command = ""
operand = ""
stdscr.clear()
# for event in self.events:
# self.events[event].trigger(stdscr)
for dirn, dest in self.navigation.items():
menu_string = "-> Go " + dirn + "\n"
if count == selection:
stdscr.addstr(menu_string, curses.A_REVERSE)
command = "go"
operand = dirn
else:
stdscr.addstr(menu_string)
count += 1
for obj_id, obj in self.objects.items():
menu_string = "-> Pickup " + obj_id + "\n"
if count == selection:
stdscr.addstr(menu_string, curses.A_REVERSE)
command = "pickup"
operand = obj_id
else:
stdscr.addstr(menu_string)
count += 1
menu_string = "-> Inventory\n"
if count == selection:
stdscr.addstr(menu_string, curses.A_REVERSE)
command = "inventory"
else:
stdscr.addstr(menu_string)
count += 1
stdscr.refresh()
return (count, command, operand)
def print_inventory(self, this, stdscr):
stdscr.clear()
stdscr.addstr("Inventory:\n\n")
for item in this.inventory:
stdscr.addstr(this.inventory[item].name + ": " + this.inventory[item].description + "\n")
stdscr.refresh()
stdscr.getch()