-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiles.py
145 lines (111 loc) · 3.8 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
import csv, util, os
from drawable import Drawable
class Tile(Drawable):
level = []
tile_map = {
# 0: Empty,
# 1: Brick,
# 2: Ladder,
# 3: Rope,
# 4: Gold
}
_hidden_tiles = []
@staticmethod
def load_level(num):
with open(os.path.join('levels', 'level{}.csv').format(num)) as file_data:
Tile.level = []
row_num = 0
for row in csv.reader(file_data):
Tile.level.extend([Tile.tile_map[elem]((index, row_num)) if elem in Tile.tile_map else Empty((index, row_num)) for index, elem in enumerate(row) ])
row_num += 1
@staticmethod
def query(coord, property):
tile = Tile.level[util.index(*coord)]
return tile.properties[property]
@staticmethod
def tile_at(coord):
return Tile.level[util.index(*coord)]
@staticmethod
def clear(coord):
Tile.level[util.index(*coord)].undraw()
Tile.level[util.index(*coord)] = Empty(coord)
def __init__(self, coord, img_path=None, properties={}, hidden=False):
super(Tile, self).__init__(coord, img_path)
if not hidden:
self.draw()
# Set up tile properties
self.properties = {'passable': True,
'takable': False,
'standable': False,
'climbable': False,
'grabbable': False,
'diggable': False}
for key in properties:
if key in self.properties:
self.properties[key] = properties[key]
self.coord = coord
if hidden:
self.hide()
def hide(self):
self.hidden_properties = self.properties
self.properties = {'passable': True,
'takable': False,
'standable': False,
'climbable': False,
'grabbable': False,
'diggable': False}
self.undraw()
def show(self):
self.draw()
self.properties = self.hidden_properties
def take(self):
pass
class Empty(Tile):
def __init__(self, coord):
super(Empty, self).__init__(coord)
class Brick(Tile):
def __init__(self, coord):
properties = {'passable': False,
'standable': True,
'diggable': True}
super(Brick, self).__init__(coord, 'brick.gif', properties)
class Ladder(Tile):
def __init__(self, coord, hidden=False):
properties = {'standable': True,
'climbable': True,
'grabbable': True}
super(Ladder, self).__init__(coord, 'ladder.gif', properties, hidden)
class Rope(Tile):
def __init__(self, coord):
properties = {'grabbable': True}
super(Rope, self).__init__(coord, 'rope.gif', properties)
class Gold(Tile):
_num_gold = 0
@staticmethod
def all_taken():
return Gold._num_gold <= 0
def __init__(self, coord):
Gold._num_gold += 1
properties = {'takable': True}
super(Gold, self).__init__(coord, 'gold.gif', properties)
def take(self):
Gold._num_gold -= 1
Tile.clear(self.coord)
class HiddenLadder(Ladder):
_hidden = []
@staticmethod
def showAll():
for ladder in HiddenLadder._hidden:
ladder.show()
HiddenLadder._hidden = []
def __init__(self, coord):
super(HiddenLadder, self).__init__(coord, hidden=True)
HiddenLadder._hidden.append(self)
Tile.tile_map = {'0': Empty,
'1': Brick,
'2': Ladder,
'3': Rope,
'4': Gold,
'5': HiddenLadder}
if __name__ == "__main__":
Tile.load_level(1)