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