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