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