-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiles.py
162 lines (120 loc) · 3.34 KB
/
tiles.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
# to do:
# patrzenie na sciany
# spacerujace kozy :D
# pijane kozy
# i cokolwiek tylko wymy�limy
# (zachowajmy to dla siebie, bedziemy mergowac razem za tydzien)
import items
from actions import ActionException
from colors import Colors
from actions import ActionException
from items import Key
class Tile:
def __init__(self, y, x):
self.y = y
self.x = x
self.item = None
def passable(self):
raise NotImplemented
def glyph(self):
raise NotImplemented
# powinno byc jeszcze use(...), walk() etc.
class Floor(Tile):
def passable(self):
return True
def glyph(self):
return self.item.glyph() if self.item else ('.', Colors.DARK_GRAY)
class KeyFloor(Floor):
def __init__(self, y, x):
super().__init__(y,x)
self.item = Key()
# new comment
class KnifeFloor(Floor):
def __init__(self, y, x):
self.y = y
self.x = x
self.item = items.Knife()
class Wall(Tile):
def passable(self):
return False
def glyph(self):
return ('#', Colors.WHITE)
def __str__(self):
return "wall"
class Teleport(Tile):
def passable(self):
return True
def glyph(self):
return ('T', Colors.DARK_RED)
def __str__(self):
return "teleport"
def open(self):
return "you used the teleport"
# new comment
class Door(Tile):
OPEN = 1
CLOSED = 2
def __init__(self, y, x):
super().__init__(y, x)
self.item = False
self.state = Door.CLOSED
def passable(self):
return self.state == Door.OPEN
def glyph(self):
return ('/' if self.passable() else '+', Colors.BROWN)
def open(self):
if self.state == Door.CLOSED:
self.state = Door.OPEN
else:
raise ActionException("it's already open")
def close(self):
if self.state == Door.OPEN:
self.state = Door.CLOSED
else:
raise ActionException("it's already closed")
def __str__(self):
return "door"
def use(self, item):
if isinstance(item, items.Knife):
return "you stab at the door, leaving a mark"
class KeyDoor(Door):
LOCKED = 3
glyphs = {
1: '/',
2: '+',
3: '|',
}
def __init__(self, y, x):
super().__init__(y, x)
self.state = KeyDoor.LOCKED
def glyph(self):
return (self.glyphs[self.state], Colors.DARK_RED)
def open(self):
if self.state == KeyDoor.LOCKED:
raise ActionException("this door is locked")
super().open()
def use(self, item):
if isinstance(item, items.Key) and self.state == KeyDoor.LOCKED:
self.state = KeyDoor.CLOSED
return "you have unlocked the door"
if isinstance(item, items.Key) and self.state == KeyDoor.CLOSED:
self.state = KeyDoor.LOCKED
return "you have locked the door"
else:
raise ActionException("Close the door, please.")
super().use(item)
# new comment
class TileFactory:
TILES = {
'.': Floor,
'#': Wall,
'+': Door,
'1': KeyDoor,
'2': KeyFloor,
'3': Teleport,
'(': KnifeFloor
}
@staticmethod
def make_tile(character, y, x):
klass = TileFactory.TILES[character]
return klass(y, x)