data/tilemap.py (view raw)
1__author__ = 'justinarmstrong'
2
3import os
4import pygame as pg
5import tools, setup
6from components import person, portal
7import constants as c
8
9
10def create_town_sprite_sheet_dict():
11 """Create a dictionary of sprite sheet tiles"""
12 tile_dict = {}
13 tileset1 = setup.GFX['tileset1']
14 tileset2 = setup.GFX['tileset2']
15 tileset3 = setup.GFX['tileset3']
16 sword = setup.GFX['sword']
17 shield = setup.GFX['shield']
18 potion = setup.GFX['potion']
19 gem = setup.GFX['gem']
20 castle_door = setup.GFX['castledoor']
21 medieval_signs = setup.GFX['medievalsigns']
22
23 tile_dict['pavement'] = get_tile(32, 48, tileset2)
24 tile_dict['house wall'] = get_tile(64, 48, tileset2)
25 tile_dict['house roof'] = get_tile(0, 144, tileset2)
26 tile_dict['house door'] = get_tile(48, 64, tileset2)
27 tile_dict['tree'] = get_tile(80, 48, tileset1, 16, 32)
28 tile_dict['well'] = get_tile(96, 50, tileset1, 16, 32)
29 tile_dict['moat'] = get_tile(16, 16, tileset2)
30 tile_dict['fence'] = get_tile(48, 64, tileset1)
31 tile_dict['grass'] = get_tile(0, 16, tileset1)
32 tile_dict['sword'] = get_tile(0, 0, sword, 32, 32)
33 tile_dict['shield'] = get_tile(0, 0, shield, 32, 32)
34 tile_dict['potion'] = get_tile(0, 0, potion, 32, 32)
35 tile_dict['gem'] = get_tile(0, 0, gem, 32, 32)
36 tile_dict['castle bridge'] = get_tile(48, 27, tileset1, 16, 32 )
37 tile_dict['flower1'] = get_tile(64, 64, tileset2)
38 tile_dict['flower2'] = get_tile(80, 64, tileset2)
39 tile_dict['horiz castle wall'] = get_tile(32, 0, tileset3, 48, 32)
40 tile_dict['vert castle wall'] = get_tile(0, 16, tileset3)
41 tile_dict['castle tower'] = get_tile(116, 16, tileset1, 48, 64)
42 tile_dict['main castle roof'] = get_tile(0, 0, tileset1, 160, 16)
43 tile_dict['left castle roof piece'] = get_tile(0, 0, tileset1, 48, 16)
44 tile_dict['right castle roof piece'] = get_tile(112, 0, tileset1, 48, 16)
45 tile_dict['castle side'] = get_tile(0, 72, tileset3)
46 tile_dict['castle door'] = get_tile(0, 0, castle_door, 64, 96)
47 tile_dict['carpet topleft'] = get_tile(112, 112, tileset2)
48 tile_dict['carpet topright'] = get_tile(144, 112, tileset2)
49 tile_dict['carpet bottomleft'] = get_tile(112, 144, tileset2)
50 tile_dict['carpet bottomright'] = get_tile(144, 144, tileset2)
51 tile_dict['carpet left'] = get_tile(112, 128, tileset2)
52 tile_dict['carpet right'] = get_tile(144, 128, tileset2)
53 tile_dict['castle window'] = get_tile(128, 59, tileset1)
54 tile_dict['inn sign'] = get_tile(0, 96, medieval_signs, 32, 32)
55
56 return tile_dict
57
58
59def get_tile(x, y, tileset, width=16, height=16, scale=1):
60 """Gets the surface and rect for a tile"""
61 surface = get_image(x, y, width, height, tileset)
62 surface = pg.transform.scale(surface, (int(width*scale), int(height*scale)))
63 rect = surface.get_rect()
64
65 tile_dict = {'surface': surface,
66 'rect': rect}
67
68 return tile_dict
69
70
71def create_town_map():
72 """Blits the different layers of the map onto one surface"""
73 map = create_background()
74 map = create_map_layer1(map)
75 map = create_map_layer2(map)
76 map = scale_map(map)
77 map = create_map_layer3(map)
78
79 return map
80
81
82def create_background():
83 """Creates the background surface that the rest of
84 the town map will be blitted on"""
85 size = (25*16, 50*16)
86 surface = pg.Surface(size)
87 grass_tile = get_image(0, 0, 16, 16, setup.GFX['tileset2'])
88 grass_rect = grass_tile.get_rect()
89
90 for row in range(50):
91 for column in range(25):
92 grass_rect.y = row * 16
93 grass_rect.x = column * 16
94 surface.blit(grass_tile, grass_rect)
95
96 surface_rect = surface.get_rect()
97
98 background_dict = {'surface': surface,
99 'rect': surface_rect}
100
101 return background_dict
102
103
104def create_map_layer1(map):
105 """Creates the town from a tile map and creates a
106 surface on top of the background"""
107 tile_map = open(os.path.join('data', 'states', 'town_map.txt'), 'r')
108
109 for row, line in enumerate(tile_map):
110 for column, letter in enumerate(line):
111 if letter == '1':
112 tile = town_map_dict['pavement']
113 blit_tile_to_map(tile, row, column, map)
114
115 elif letter == '2':
116 tile = town_map_dict['house wall']
117 blit_tile_to_map(tile, row, column, map)
118
119 elif letter == '3':
120 tile = town_map_dict['house roof']
121 blit_tile_to_map(tile, row, column, map)
122
123 elif letter == 'T':
124 tile = town_map_dict['tree']
125 blit_tile_to_map(tile, row, column, map)
126
127 elif letter == 'W':
128 tile = town_map_dict['well']
129 blit_tile_to_map(tile, row, column, map)
130
131 elif letter == 'M':
132 tile = town_map_dict['moat']
133 blit_tile_to_map(tile, row, column, map)
134
135 elif letter == 'G':
136 tile = town_map_dict['grass']
137 blit_tile_to_map(tile, row, column, map)
138
139 elif letter == 'B':
140 tile = town_map_dict['castle bridge']
141 blit_tile_to_map(tile, row, column, map)
142
143 elif letter == 'C':
144 tile = town_map_dict['horiz castle wall']
145 blit_tile_to_map(tile, row, column, map)
146
147 elif letter == 'V':
148 tile = town_map_dict['vert castle wall']
149 blit_tile_to_map(tile, row, column, map)
150
151 elif letter == 'S':
152 tile = town_map_dict['castle side']
153 blit_tile_to_map(tile, row, column, map)
154
155 elif letter == 'Q':
156 tile = town_map_dict['carpet topleft']
157 blit_tile_to_map(tile, row, column, map)
158
159 elif letter == 'E':
160 tile = town_map_dict['carpet topright']
161 blit_tile_to_map(tile, row, column, map)
162
163 elif letter == 'R':
164 tile = town_map_dict['carpet bottomleft']
165 blit_tile_to_map(tile, row, column, map)
166
167 elif letter == 'U':
168 tile = town_map_dict['carpet bottomright']
169 blit_tile_to_map(tile, row, column, map)
170
171 elif letter == 'P':
172 tile = town_map_dict['carpet left']
173 blit_tile_to_map(tile, row, column, map)
174
175 elif letter == 'A':
176 tile = town_map_dict['carpet right']
177 blit_tile_to_map(tile, row, column, map)
178
179
180 tile_map.close()
181
182 return map
183
184
185def create_map_layer2(map):
186 """Creates doors and other items on top of the rest of the map"""
187 tile_map = open(os.path.join('data', 'states', 'town_layer2.txt'), 'r')
188
189 for row, line in enumerate(tile_map):
190 for column, letter in enumerate(line):
191 if letter == 'D':
192 tile = town_map_dict['house door']
193 blit_tile_to_map(tile, row, column, map)
194 elif letter == 'F':
195 tile = town_map_dict['fence']
196 blit_tile_to_map(tile, row, column, map)
197 elif letter == '$':
198 tile = town_map_dict['flower1']
199 blit_tile_to_map(tile, row, column, map)
200 elif letter == '*':
201 tile = town_map_dict['flower2']
202 blit_tile_to_map(tile, row, column, map)
203 elif letter == 'T':
204 tile = town_map_dict['castle tower']
205 blit_tile_to_map(tile, row, column, map)
206 elif letter == 'W':
207 tile = town_map_dict['vert castle wall']
208 blit_tile_to_map(tile, row, column, map)
209 elif letter == 'M':
210 tile = town_map_dict['main castle roof']
211 blit_tile_to_map(tile, row, column, map)
212 elif letter == 'L':
213 tile = town_map_dict['left castle roof piece']
214 blit_tile_to_map(tile, row, column, map)
215 elif letter == 'R':
216 tile = town_map_dict['right castle roof piece']
217 blit_tile_to_map(tile, row, column, map)
218 elif letter == '#':
219 tile = town_map_dict['tree']
220 blit_tile_to_map(tile, row, column, map)
221 elif letter == 'O':
222 tile = town_map_dict['castle door']
223 blit_tile_to_map(tile, row, column, map)
224 elif letter == 'Q':
225 tile = town_map_dict['castle window']
226 blit_tile_to_map(tile, row, column, map)
227
228 tile_map.close()
229
230 return map
231
232
233def scale_map(map):
234 """Double resolution of map to 32x32"""
235 map['surface'] = pg.transform.scale2x(map['surface'])
236 map['rect'] = map['surface'].get_rect()
237
238 return map
239
240
241def create_map_layer3(map):
242 """Layers for images that are already 32x32"""
243 tile_map = open(os.path.join('data', 'states', 'town_layer3.txt'), 'r')
244
245 for row, line in enumerate(tile_map):
246 for column, letter in enumerate(line):
247 if letter == 'W':
248 tile = town_map_dict['sword']
249 blit_tile_to_map(tile, row, column, map, 32)
250 elif letter == 'A':
251 tile = town_map_dict['shield']
252 blit_tile_to_map(tile, row, column, map, 32)
253 elif letter == 'P':
254 tile = town_map_dict['potion']
255 blit_tile_to_map(tile, row, column, map, 32)
256 elif letter == 'M':
257 tile = town_map_dict['gem']
258 blit_tile_to_map(tile, row, column, map, 32)
259 elif letter == 'I':
260 tile = town_map_dict['inn sign']
261 blit_tile_to_map(tile, row, column, map, 32)
262
263 tile_map.close()
264
265 return map
266
267
268
269def blit_tile_to_map(tile, row, column, map, side_length=16):
270 """Places tile to map"""
271 tile['rect'].x = column * side_length
272 tile['rect'].y = row * side_length
273
274 map['surface'].blit(tile['surface'], tile['rect'])
275
276
277def create_blockers():
278 """Creates invisible rect objects that will prevent the player from
279 walking into trees, buildings and other solid objects"""
280 tile_map = open(os.path.join('data', 'states', 'town_blocker_layer.txt'), 'r')
281 blockers = []
282
283 for row, line in enumerate(tile_map):
284 for column, letter in enumerate(line):
285 if letter == 'B':
286 blockers.append(pg.Rect(column*32, row*32, 32, 32))
287
288 tile_map.close()
289
290 return blockers
291
292
293def create_level_surface(map):
294 """Creates the surface all images are blitted to"""
295 width = map['rect'].width
296 height = map['rect'].height
297
298 return pg.Surface((width, height)).convert()
299
300
301def create_viewport(map):
302 """Create the viewport to view the level through"""
303 return setup.SCREEN.get_rect(bottom=map['rect'].bottom)
304
305
306def set_sprite_positions(player, level_sprites):
307 """Set the start positions for all the sprites in the level"""
308 tile_map = open(os.path.join('data', 'states', 'sprite_start_pos.txt'), 'r')
309
310 for row, line in enumerate(tile_map):
311 for column, letter in enumerate(line):
312 if letter == 'P':
313 player.rect = pg.Rect(column*32, row*32, 32, 32)
314 elif letter == 'F':
315 fem_villager = person.FemaleVillager(column*32, row*32)
316 level_sprites.add(fem_villager)
317 elif letter == 'S':
318 soldier = person.Soldier(column*32, row*32)
319 level_sprites.add(soldier)
320
321 tile_map.close()
322
323
324def make_level_portals():
325 """Create portals to different levels on doors"""
326 tile_map = open(os.path.join('data', 'states', 'level_change_map.txt'), 'r')
327 portal_group = pg.sprite.Group()
328
329 for row, line in enumerate(tile_map):
330 for column, letter in enumerate(line):
331 if letter == 'A':
332 portal_group.add(portal.Portal(column, row, c.MAIN_MENU))
333
334 return portal_group
335
336
337
338get_image = tools.get_image
339town_map_dict = create_town_sprite_sheet_dict()
340