-
Notifications
You must be signed in to change notification settings - Fork 0
/
rogue.py
executable file
·196 lines (149 loc) · 5.41 KB
/
rogue.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
#!/usr/bin/env python3
# encoding: utf-8
import logging
import curses
import actions
from tiles import TileFactory
from colors import Colors
import random
class Map:
def __init__(self, filename):
with open(filename) as f:
self.tiles = []
for y, r in enumerate(f):
row = []
for x, c in enumerate(r.strip()):
row.append(TileFactory.make_tile(c, y, x))
self.tiles.append(row)
def show(self, screen):
for y, row in enumerate(self.tiles):
for x, tile in enumerate(row):
character, color_pair = tile.glyph()
screen.addstr(y, x, character, color_pair)
class Player:
def __init__(self, y, x):
self.y = y
self.x = x
self.items = []
def show(self, screen):
character, color_pair = self.glyph()
screen.addstr(self.y, self.x, character, color_pair)
def glyph(self):
return ('@', Colors.WHITE)
def move(self, tile):
if not tile.passable():
raise actions.ActionException("a %s blocks your way" % tile)
self.y = tile.y
self.x = tile.x
def drop(self, tile, item):
if item is None:
raise actions.ActionException("you don't have such an item")
if tile.item is not None:
raise actions.ActionException("there's no space on the floor")
tile.item = item
self.items.remove(item)
return tile.item
def pickup(self, tile):
if not tile.item:
raise actions.ActionException("there's nothing here")
self.items.append(tile.item)
tile.item = None
return self.items[-1]
def open(self, tile):
tile.open()
def close(self, tile):
tile.close()
def use(self, tile, item):
return tile.use(item)
class Game:
DIRECTIONS = [
(curses.KEY_LEFT, 0, -1),
(curses.KEY_UP, -1, 0),
(curses.KEY_RIGHT, 0, 1),
(curses.KEY_DOWN, 1, 0)
]
def __init__(self, screen, map):
self.screen = screen
self.status = curses.newwin(1, 80, 26, 0)
self.map = Map(map)
self.player = Player(1, 1)
def _get_direction(self, message=None, pressed=None):
if message:
self.message("%s [<arrows>]?" % message)
pressed = pressed or self.screen.getch()
for key, dy, dx in Game.DIRECTIONS:
if pressed == key:
return self.player.y + dy, self.player.x + dx
return None, None
def _get_item(self, message):
selection = ", ".join(["%i: %s" % i for i in enumerate(self.player.items, 1)])
self.message("%s [%s]?" % (message, selection))
pressed = self.screen.getch()
try:
return self.player.items[pressed - ord('1')]
except IndexError:
return None
def action(self):
pressed = self.screen.getch()
y, x = self._get_direction(pressed=pressed)
if y and x:
return actions.Move(self.player, self.map.tiles[y][x])
# open
if pressed == ord('o'):
y, x = self._get_direction("Open what")
if y and x:
if str(self.map.tiles[y][x]) == 'teleport':
random_tile = random.choice(random.choice(
[[inner for inner in tiles if inner.passable()] for tiles in self.map.tiles]))
return actions.Move(self.player, random_tile)
return actions.Open(self.player, self.map.tiles[y][x])
# close
if pressed == ord('c'):
y, x = self._get_direction("Close what")
if y and x:
return actions.Close(self.player, self.map.tiles[y][x])
y, x = self.player.y, self.player.x
# pick up
if pressed == ord('p'):
return actions.PickUp(self.player, self.map.tiles[y][x])
# drop
if pressed == ord('d'):
item = self._get_item("Drop what")
return actions.Drop(self.player, self.map.tiles[y][x], item)
# use
if pressed == ord('u'):
item = self._get_item("Use what")
if item:
y, x = self._get_direction("Use %s on what" % item)
if y and x:
return actions.Use(self.player, self.map.tiles[y][x], item)
if pressed == ord('?'):
return actions.Wait("<arrows>: walk, O: open, C: close, P: pick up, D: drop, U: use")
return actions.Wait()
def message(self, message, color=None):
self.status.clear()
self.status.addstr(0, 0, message, color or Colors.LIGHT_GRAY)
self.status.addstr(0, 72, "?: help")
self.status.refresh()
def run(self):
self.screen.refresh()
self.message("You stand in a corridor.")
while True:
self.map.show(self.screen)
self.player.show(self.screen)
self.screen.refresh()
action = self.action()
if action:
message, color = action.perform()
#print(message, color) # trzeba by wypisywac do pliku, tj. robic log
self.message(message, color)
def main(screen):
curses.curs_set(0)
colors = Colors()
game = Game(screen, 'map.txt')
game.run()
if __name__ == '__main__':
try:
curses.wrapper(main)
except KeyboardInterrupt:
print("Thanks for playing!")