data/tilemap.py (view raw)
1__author__ = 'justinarmstrong'
2
3import os
4import pygame as pg
5from . import tools, setup
6from . components import person, portal
7from . import 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 shopsigns = setup.GFX['shopsigns']
17 castle_door = setup.GFX['castledoor']
18 medieval_signs = setup.GFX['medievalsigns']
19 house = setup.GFX['house']
20 houseoverworld = setup.GFX['houseoverworld']
21 oldmanhouse = setup.GFX['house1']
22 evilcastle = setup.GFX['evilcastle']
23
24 tile_dict['pavement'] = get_tile(32, 48, tileset2)
25 tile_dict['house wall'] = get_tile(64, 48, tileset2)
26 tile_dict['house roof'] = get_tile(0, 144, tileset2)
27 tile_dict['house door'] = get_tile(48, 64, tileset2)
28 tile_dict['tree'] = get_tile(80, 48, tileset1, 16, 32)
29 tile_dict['well'] = get_tile(96, 50, tileset1, 16, 32)
30 tile_dict['moat'] = get_tile(16, 16, tileset2)
31 tile_dict['fence'] = get_tile(48, 64, tileset1)
32 tile_dict['grass'] = get_tile(0, 16, tileset1)
33 tile_dict['sword'] = get_tile(96, 0, shopsigns, 32, 32)
34 tile_dict['shield'] = get_tile(64, 0, shopsigns, 32, 32)
35 tile_dict['potion'] = get_tile(32, 0, shopsigns, 32, 32)
36 tile_dict['gem'] = get_tile(0, 0, shopsigns, 32, 32)
37 tile_dict['castle bridge'] = get_tile(48, 27, tileset1, 16, 32 )
38 tile_dict['flower1'] = get_tile(64, 64, tileset2)
39 tile_dict['flower2'] = get_tile(80, 64, tileset2)
40 tile_dict['horiz castle wall'] = get_tile(32, 0, tileset3, 48, 32)
41 tile_dict['left vert castle wall'] = get_tile(0, 16, tileset3)
42 tile_dict['right vert castle wall'] = get_tile(162, 16, tileset3)
43 tile_dict['castle tower'] = get_tile(116, 16, tileset1, 48, 64)
44 tile_dict['main castle roof'] = get_tile(0, 0, tileset1, 160, 16)
45 tile_dict['left castle roof piece'] = get_tile(0, 0, tileset1, 48, 16)
46 tile_dict['right castle roof piece'] = get_tile(112, 0, tileset1, 48, 16)
47 tile_dict['castle side'] = get_tile(0, 72, tileset3)
48 tile_dict['castle door'] = get_tile(0, 0, castle_door, 64, 96)
49 tile_dict['carpet topleft'] = get_tile(112, 112, tileset2)
50 tile_dict['carpet topright'] = get_tile(144, 112, tileset2)
51 tile_dict['carpet bottomleft'] = get_tile(112, 144, tileset2)
52 tile_dict['carpet bottomright'] = get_tile(144, 144, tileset2)
53 tile_dict['carpet bottom'] = get_tile(128, 144, tileset2)
54 tile_dict['carpet top'] = get_tile(128, 112, tileset2)
55 tile_dict['carpet left'] = get_tile(112, 128, tileset2)
56 tile_dict['carpet right'] = get_tile(144, 128, tileset2)
57 tile_dict['carpet center'] = get_tile(128, 128, tileset2)
58 tile_dict['castle window'] = get_tile(128, 59, tileset1)
59 tile_dict['marble floor'] = get_tile(80, 96, tileset3)
60 tile_dict['inn sign'] = get_tile(0, 96, medieval_signs, 32, 32)
61 tile_dict['banner1'] = get_tile(112, 38, tileset3, 16, 22)
62 tile_dict['banner2'] = get_tile(128, 38, tileset3, 16, 22)
63 tile_dict['banner3'] = get_tile(144, 38, tileset3, 16, 22)
64 tile_dict['black tile'] = make_black_surface_tile()
65 tile_dict['bed'] = get_tile(456, 206, house, 48, 82)
66 tile_dict['shelves'] = get_tile(352, 116, house, 160, 70)
67 tile_dict['chair'] = get_tile(323, 256, house, 32, 32)
68 tile_dict['table'] = get_tile(82, 161, tileset3, 46, 32)
69 tile_dict['fancy carpet'] = get_tile(112, 96, tileset3, 64, 64)
70 tile_dict['column'] = get_tile(64, 96, tileset3, 16, 48)
71 tile_dict['bottomwater1'] = get_tile(16, 32, tileset2)
72 tile_dict['bottomwater2'] = get_tile(32, 32, tileset2)
73 tile_dict['bottomwater3'] = get_tile(48, 32, tileset2)
74 tile_dict['topwater1'] = get_tile(16, 0, tileset2)
75 tile_dict['topwater2'] = get_tile(32, 0, tileset2)
76 tile_dict['topwater3'] = get_tile(48, 0, tileset2)
77 tile_dict['leftwater1'] = get_tile(80, 0, tileset2)
78 tile_dict['leftwater2'] = get_tile(80, 16, tileset2)
79 tile_dict['leftwater3'] = get_tile(0, 16, tileset2)
80 tile_dict['rightwater1'] = get_tile(144, 0, tileset2)
81 tile_dict['rightwater2'] = get_tile(144, 16, tileset2)
82 tile_dict['rightwater3'] = get_tile(64, 16, tileset2)
83 tile_dict['houseoverworld'] = get_tile(0, 0, houseoverworld, 128, 128)
84 tile_dict['oldmanhouse'] = get_tile(0, 0, oldmanhouse, 32, 32)
85 tile_dict['evilcastle'] = get_tile(0, 0, evilcastle, 128, 128)
86
87 return tile_dict
88
89def create_layer1_tile_code_dict():
90 """Creates a dictionary of codes to tiles for layer 1"""
91 tile_dict = {'1': 'pavement',
92 '2': 'house wall',
93 '3': 'house roof',
94 'T': 'tree',
95 'W': 'well',
96 'M': 'moat',
97 'G': 'grass',
98 'B': 'castle bridge',
99 'V': 'left vert castle wall',
100 'C': 'horiz castle wall',
101 'X': 'right vert castle wall',
102 'S': 'castle side',
103 'Q': 'carpet topleft',
104 'E': 'carpet topright',
105 'J': 'carpet top',
106 'R': 'carpet bottomleft',
107 'U': 'carpet bottomright',
108 'D': 'carpet bottom',
109 'P': 'carpet left',
110 'A': 'carpet right',
111 'F': 'carpet center',
112 'H': 'marble floor',
113 'K': 'black tile',
114 'L': 'topwater1',
115 'N': 'topwater2',
116 'Y': 'topwater3',
117 'Z': 'leftwater1',
118 '4': 'leftwater2',
119 '5': 'leftwater3',
120 '6': 'rightwater1',
121 '7': 'rightwater2',
122 '8': 'rightwater3',
123 '9': 'bottomwater1',
124 'a': 'bottomwater2',
125 'b': 'bottomwater3'
126 }
127
128 return tile_dict
129
130
131def get_tile(x, y, tileset, width=16, height=16, scale=1):
132 """Gets the surface and rect for a tile"""
133 surface = get_image(x, y, width, height, tileset)
134 surface = pg.transform.scale(surface, (int(width*scale), int(height*scale)))
135 rect = surface.get_rect()
136
137 tile_dict = {'surface': surface,
138 'rect': rect}
139
140 return tile_dict
141
142
143def make_black_surface_tile():
144 """Make a black surface"""
145 surface = pg.Surface((32, 32))
146 surface.fill(c.NEAR_BLACK_BLUE)
147 rect = surface.get_rect()
148 new_dict = {'surface': surface,
149 'rect': rect}
150
151 return new_dict
152
153
154def make_background(state_name, width, height):
155 """Creates the background surface that the rest of
156 the town map will be blitted on"""
157 size = (width*16, height*16)
158 surface = pg.Surface(size)
159 if state_name == c.TOWN or state_name == c.OVERWORLD:
160 tile = get_image(0, 0, 16, 16, setup.GFX['tileset2'])
161 else:
162 tile = pg.Surface((16, 16))
163 tile.fill(c.BLACK_BLUE)
164
165 rect = tile.get_rect()
166
167 for row in range(height):
168 for column in range(width):
169 rect.y = row * 16
170 rect.x = column * 16
171 surface.blit(tile, rect)
172
173 return surface
174
175
176def create_map_layer1(state, width, height):
177 """Creates the town from a tile map and creates a
178 surface on top of the background"""
179 map = make_background(state, width, height)
180
181 tile_map = open(os.path.join('data', 'states', state, 'layer1.txt'), 'r')
182
183 for row, line in enumerate(tile_map):
184 for column, letter in enumerate(line):
185 if letter not in layer1_tile_code_dict:
186 pass
187 else:
188 tile = tile_dict[layer1_tile_code_dict[letter]]
189 blit_tile_to_map(tile, row, column, map)
190
191 tile_map.close()
192
193 layer1_extra = create_map_layer2(state, width, height, 'layer1extra.txt')
194 map.blit(layer1_extra, (0,0))
195
196 map = scale_map(map)
197
198 return map
199
200
201def create_map_layer2(state, width, height, file_name='layer2.txt'):
202 """Creates doors and other items on top of the rest of the map"""
203 map = make_background(None, width, height)
204 map.set_colorkey(c.BLACK_BLUE)
205
206 tile_map = open(os.path.join('data', 'states', state, file_name), 'r')
207
208 for row, line in enumerate(tile_map):
209 for column, letter in enumerate(line):
210 if letter == 'D':
211 tile = tile_dict['house door']
212 blit_tile_to_map(tile, row, column, map)
213 elif letter == 'F':
214 tile = tile_dict['fence']
215 blit_tile_to_map(tile, row, column, map)
216 elif letter == '$':
217 tile = tile_dict['flower1']
218 blit_tile_to_map(tile, row, column, map)
219 elif letter == '*':
220 tile = tile_dict['flower2']
221 blit_tile_to_map(tile, row, column, map)
222 elif letter == 'T':
223 tile = tile_dict['castle tower']
224 blit_tile_to_map(tile, row, column, map)
225 elif letter == 'W':
226 tile = tile_dict['left vert castle wall']
227 blit_tile_to_map(tile, row, column, map)
228 elif letter == 'M':
229 tile = tile_dict['main castle roof']
230 blit_tile_to_map(tile, row, column, map)
231 elif letter == 'L':
232 tile = tile_dict['left castle roof piece']
233 blit_tile_to_map(tile, row, column, map)
234 elif letter == 'R':
235 tile = tile_dict['right castle roof piece']
236 blit_tile_to_map(tile, row, column, map)
237 elif letter == '#':
238 tile = tile_dict['tree']
239 blit_tile_to_map(tile, row, column, map)
240 elif letter == 'O':
241 tile = tile_dict['castle door']
242 blit_tile_to_map(tile, row, column, map)
243 elif letter == 'Q':
244 tile = tile_dict['castle window']
245 blit_tile_to_map(tile, row, column, map)
246 elif letter == 'A':
247 tile = tile_dict['banner1']
248 blit_tile_to_map(tile, row, column, map)
249 elif letter == 'B':
250 tile = tile_dict['banner2']
251 blit_tile_to_map(tile, row, column, map)
252 elif letter == 'C':
253 tile = tile_dict['banner3']
254 blit_tile_to_map(tile, row, column, map)
255 elif letter == 'E':
256 tile = tile_dict['bed']
257 blit_tile_to_map(tile, row, column, map)
258 elif letter == 'G':
259 tile = tile_dict['shelves']
260 blit_tile_to_map(tile, row, column, map)
261 elif letter == 'H':
262 tile = tile_dict['chair']
263 blit_tile_to_map(tile, row, column, map)
264 elif letter == 'I':
265 tile = tile_dict['table']
266 blit_tile_to_map(tile, row, column, map)
267 elif letter == 'J':
268 tile = tile_dict['fancy carpet']
269 blit_tile_to_map(tile, row, column, map)
270 elif letter == 'K':
271 tile = tile_dict['column']
272 blit_tile_to_map(tile, row, column, map)
273 elif letter == 'N':
274 tile = tile_dict['black tile']
275 blit_tile_to_map(tile, row, column, map)
276 elif letter == 'P':
277 tile = tile_dict['houseoverworld']
278 blit_tile_to_map(tile, row, column, map)
279 elif letter == '4':
280 tile = tile_dict['oldmanhouse']
281 blit_tile_to_map(tile, row, column, map)
282 elif letter == '5':
283 tile = tile_dict['evilcastle']
284 blit_tile_to_map(tile, row, column, map)
285
286 tile_map.close()
287
288 if file_name == 'layer2.txt':
289 map = pg.transform.scale2x(map)
290 map = create_map_layer3(map, state)
291
292 return map
293
294
295def scale_map(map):
296 """Double resolution of map to 32x32"""
297 map = pg.transform.scale2x(map)
298
299 return map
300
301
302def create_map_layer3(map, state):
303 """Layers for images that are already 32x32"""
304 tile_map = open(os.path.join('data', 'states', state, 'layer3.txt'), 'r')
305
306 for row, line in enumerate(tile_map):
307 for column, letter in enumerate(line):
308 if letter == 'W':
309 tile = tile_dict['sword']
310 blit_tile_to_map(tile, row, column, map, 32)
311 elif letter == 'A':
312 tile = tile_dict['shield']
313 blit_tile_to_map(tile, row, column, map, 32)
314 elif letter == 'P':
315 tile = tile_dict['potion']
316 blit_tile_to_map(tile, row, column, map, 32)
317 elif letter == 'M':
318 tile = tile_dict['gem']
319 blit_tile_to_map(tile, row, column, map, 32)
320 elif letter == 'I':
321 tile = tile_dict['inn sign']
322 blit_tile_to_map(tile, row, column, map, 32)
323 elif letter == 'B':
324 tile = tile_dict['chair']
325 blit_tile_to_map(tile, row, column, map, 32)
326
327
328 tile_map.close()
329
330 return map
331
332
333
334def blit_tile_to_map(tile, row, column, map, side_length=16):
335 """Places tile to map"""
336 tile['rect'].x = column * side_length
337 tile['rect'].y = row * side_length
338
339 map.blit(tile['surface'], tile['rect'])
340
341
342def create_blockers(state):
343 """Creates invisible rect objects that will prevent the player from
344 walking into trees, buildings and other solid objects"""
345 tile_map = open(os.path.join('data', 'states', state, 'blockers.txt'), 'r')
346 blockers = []
347
348 for row, line in enumerate(tile_map):
349 for column, letter in enumerate(line):
350 if letter == 'B':
351 blockers.append(pg.Rect(column*32, row*32, 32, 32))
352
353 tile_map.close()
354
355 return blockers
356
357
358def make_level_surface(map):
359 """Creates the surface all images are blitted to"""
360 map_rect = map.get_rect()
361 size = map_rect.size
362
363
364 return pg.Surface(size).convert()
365
366
367def create_viewport(map):
368 """Create the viewport to view the level through"""
369 map_rect = map.get_rect()
370 return setup.SCREEN.get_rect(bottom=map_rect.bottom)
371
372
373def set_sprite_positions(player, level_sprites, state, game_data):
374 """Set the start positions for all the sprites in the level"""
375 start_pos_key = state + ' start pos'
376 x = game_data[start_pos_key][0]*32
377 y = game_data[start_pos_key][1]*32
378 player.rect = pg.Rect(x, y, 32, 32)
379
380 tile_map = open(os.path.join('data', 'states', state, 'sprite_start_pos.txt'), 'r')
381
382 for row, line in enumerate(tile_map):
383 for column, letter in enumerate(line):
384 if letter == 'F':
385 fem_villager = person.FemaleVillager(column*32, row*32)
386 level_sprites.add(fem_villager)
387 elif letter == 'S':
388 soldier = person.Soldier(column*32, row*32)
389 level_sprites.add(soldier)
390 elif letter == 'A':
391 soldier = person.Soldier(column*32, row*32, 'right', 'resting')
392 level_sprites.add(soldier)
393 elif letter == 'B':
394 soldier = person.Soldier(column*32, row*32, 'left', 'resting')
395 level_sprites.add(soldier)
396 elif letter == 'C':
397 king = person.King(column*32, row*32, 'down', 'resting')
398 level_sprites.add(king)
399 elif letter == 'D':
400 well = person.Well(column*32, row*32)
401 level_sprites.add(well)
402 elif letter == 'E':
403 fem_villager = person.FemaleVillager2(column*32, row*32)
404 level_sprites.add(fem_villager)
405 elif letter == 'G':
406 devil_villager = person.Devil(column*32, row*32)
407 level_sprites.add(devil_villager)
408 elif letter == 'H':
409 old_villager = person.OldMan(column*32, row*32)
410 level_sprites.add(old_villager)
411
412 tile_map.close()
413
414
415def make_level_portals(state):
416 """Create portals to different levels on doors"""
417 tile_map = open(os.path.join('data', 'states', state, 'portals.txt'), 'r')
418 portal_group = pg.sprite.Group()
419
420 for row, line in enumerate(tile_map):
421 for column, letter in enumerate(line):
422 if letter == 'A':
423 portal_group.add(portal.Portal(column, row, c.CASTLE))
424 elif letter == 'B':
425 portal_group.add(portal.Portal(column, row, c.TOWN))
426 elif letter == 'C':
427 portal_group.add(portal.Portal(column, row, c.INN))
428 elif letter == 'D':
429 portal_group.add(portal.Portal(column, row, c.MAGIC_SHOP))
430 elif letter == 'E':
431 portal_group.add(portal.Portal(column, row, c.POTION_SHOP))
432 elif letter == 'F':
433 portal_group.add(portal.Portal(column, row, c.WEAPON_SHOP))
434 elif letter == 'G':
435 portal_group.add(portal.Portal(column, row, c.ARMOR_SHOP))
436 elif letter == 'H':
437 portal_group.add(portal.Portal(column, row, c.HOUSE))
438 elif letter == 'J':
439 portal_group.add(portal.Portal(column, row, c.OVERWORLD))
440
441 return portal_group
442
443
444
445get_image = tools.get_image
446tile_dict = create_town_sprite_sheet_dict()
447layer1_tile_code_dict = create_layer1_tile_code_dict()
448