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['left vert castle wall'] = get_tile(0, 16, tileset3)
41 tile_dict['right vert castle wall'] = get_tile(162, 16, tileset3)
42 tile_dict['castle tower'] = get_tile(116, 16, tileset1, 48, 64)
43 tile_dict['main castle roof'] = get_tile(0, 0, tileset1, 160, 16)
44 tile_dict['left castle roof piece'] = get_tile(0, 0, tileset1, 48, 16)
45 tile_dict['right castle roof piece'] = get_tile(112, 0, tileset1, 48, 16)
46 tile_dict['castle side'] = get_tile(0, 72, tileset3)
47 tile_dict['castle door'] = get_tile(0, 0, castle_door, 64, 96)
48 tile_dict['carpet topleft'] = get_tile(112, 112, tileset2)
49 tile_dict['carpet topright'] = get_tile(144, 112, tileset2)
50 tile_dict['carpet bottomleft'] = get_tile(112, 144, tileset2)
51 tile_dict['carpet bottomright'] = get_tile(144, 144, tileset2)
52 tile_dict['carpet bottom'] = get_tile(128, 144, tileset2)
53 tile_dict['carpet top'] = get_tile(128, 144, tileset2)
54 tile_dict['carpet left'] = get_tile(112, 128, tileset2)
55 tile_dict['carpet right'] = get_tile(144, 128, tileset2)
56 tile_dict['carpet center'] = get_tile(128, 128, tileset2)
57 tile_dict['castle window'] = get_tile(128, 59, tileset1)
58 tile_dict['marble floor'] = get_tile(80, 96, tileset3)
59 tile_dict['inn sign'] = get_tile(0, 96, medieval_signs, 32, 32)
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 create_town_map(state):
77 """Blits the different layers of the map onto one surface"""
78 map = create_background(state)
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):
88 """Creates the background surface that the rest of
89 the town map will be blitted on"""
90 size = (25*16, 50*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 = town_map_dict['pavement']
122 blit_tile_to_map(tile, row, column, map)
123
124 elif letter == '2':
125 tile = town_map_dict['house wall']
126 blit_tile_to_map(tile, row, column, map)
127
128 elif letter == '3':
129 tile = town_map_dict['house roof']
130 blit_tile_to_map(tile, row, column, map)
131
132 elif letter == 'T':
133 tile = town_map_dict['tree']
134 blit_tile_to_map(tile, row, column, map)
135
136 elif letter == 'W':
137 tile = town_map_dict['well']
138 blit_tile_to_map(tile, row, column, map)
139
140 elif letter == 'M':
141 tile = town_map_dict['moat']
142 blit_tile_to_map(tile, row, column, map)
143
144 elif letter == 'G':
145 tile = town_map_dict['grass']
146 blit_tile_to_map(tile, row, column, map)
147
148 elif letter == 'B':
149 tile = town_map_dict['castle bridge']
150 blit_tile_to_map(tile, row, column, map)
151
152 elif letter == 'C':
153 tile = town_map_dict['horiz castle wall']
154 blit_tile_to_map(tile, row, column, map)
155
156 elif letter == 'V':
157 tile = town_map_dict['left vert castle wall']
158 blit_tile_to_map(tile, row, column, map)
159
160 elif letter == 'X':
161 tile = town_map_dict['right vert castle wall']
162 blit_tile_to_map(tile, row, column, map)
163
164 elif letter == 'S':
165 tile = town_map_dict['castle side']
166 blit_tile_to_map(tile, row, column, map)
167
168 elif letter == 'Q':
169 tile = town_map_dict['carpet topleft']
170 blit_tile_to_map(tile, row, column, map)
171
172 elif letter == 'E':
173 tile = town_map_dict['carpet topright']
174 blit_tile_to_map(tile, row, column, map)
175
176 elif letter == 'R':
177 tile = town_map_dict['carpet bottomleft']
178 blit_tile_to_map(tile, row, column, map)
179
180 elif letter == 'U':
181 tile = town_map_dict['carpet bottomright']
182 blit_tile_to_map(tile, row, column, map)
183
184 elif letter == 'D':
185 tile = town_map_dict['carpet bottom']
186 blit_tile_to_map(tile, row, column, map)
187
188 elif letter == 'P':
189 tile = town_map_dict['carpet left']
190 blit_tile_to_map(tile, row, column, map)
191
192 elif letter == 'A':
193 tile = town_map_dict['carpet right']
194 blit_tile_to_map(tile, row, column, map)
195
196 elif letter == 'F':
197 tile = town_map_dict['carpet center']
198 blit_tile_to_map(tile, row, column, map)
199
200 elif letter == 'H':
201 tile = town_map_dict['marble floor']
202 blit_tile_to_map(tile, row, column, map)
203
204
205 tile_map.close()
206
207 return map
208
209
210def create_map_layer2(map, state):
211 """Creates doors and other items on top of the rest of the map"""
212 tile_map = open(os.path.join('data', 'states', state, 'layer2.txt'), 'r')
213
214 for row, line in enumerate(tile_map):
215 for column, letter in enumerate(line):
216 if letter == 'D':
217 tile = town_map_dict['house door']
218 blit_tile_to_map(tile, row, column, map)
219 elif letter == 'F':
220 tile = town_map_dict['fence']
221 blit_tile_to_map(tile, row, column, map)
222 elif letter == '$':
223 tile = town_map_dict['flower1']
224 blit_tile_to_map(tile, row, column, map)
225 elif letter == '*':
226 tile = town_map_dict['flower2']
227 blit_tile_to_map(tile, row, column, map)
228 elif letter == 'T':
229 tile = town_map_dict['castle tower']
230 blit_tile_to_map(tile, row, column, map)
231 elif letter == 'W':
232 tile = town_map_dict['left vert castle wall']
233 blit_tile_to_map(tile, row, column, map)
234 elif letter == 'M':
235 tile = town_map_dict['main castle roof']
236 blit_tile_to_map(tile, row, column, map)
237 elif letter == 'L':
238 tile = town_map_dict['left castle roof piece']
239 blit_tile_to_map(tile, row, column, map)
240 elif letter == 'R':
241 tile = town_map_dict['right castle roof piece']
242 blit_tile_to_map(tile, row, column, map)
243 elif letter == '#':
244 tile = town_map_dict['tree']
245 blit_tile_to_map(tile, row, column, map)
246 elif letter == 'O':
247 tile = town_map_dict['castle door']
248 blit_tile_to_map(tile, row, column, map)
249 elif letter == 'Q':
250 tile = town_map_dict['castle window']
251 blit_tile_to_map(tile, row, column, map)
252
253 tile_map.close()
254
255 return map
256
257
258def scale_map(map):
259 """Double resolution of map to 32x32"""
260 map['surface'] = pg.transform.scale2x(map['surface'])
261 map['rect'] = map['surface'].get_rect()
262
263 return map
264
265
266def create_map_layer3(map, state):
267 """Layers for images that are already 32x32"""
268 tile_map = open(os.path.join('data', 'states', state, 'layer3.txt'), 'r')
269
270 for row, line in enumerate(tile_map):
271 for column, letter in enumerate(line):
272 if letter == 'W':
273 tile = town_map_dict['sword']
274 blit_tile_to_map(tile, row, column, map, 32)
275 elif letter == 'A':
276 tile = town_map_dict['shield']
277 blit_tile_to_map(tile, row, column, map, 32)
278 elif letter == 'P':
279 tile = town_map_dict['potion']
280 blit_tile_to_map(tile, row, column, map, 32)
281 elif letter == 'M':
282 tile = town_map_dict['gem']
283 blit_tile_to_map(tile, row, column, map, 32)
284 elif letter == 'I':
285 tile = town_map_dict['inn sign']
286 blit_tile_to_map(tile, row, column, map, 32)
287
288 tile_map.close()
289
290 return map
291
292
293
294def blit_tile_to_map(tile, row, column, map, side_length=16):
295 """Places tile to map"""
296 tile['rect'].x = column * side_length
297 tile['rect'].y = row * side_length
298
299 map['surface'].blit(tile['surface'], tile['rect'])
300
301
302def create_blockers(state):
303 """Creates invisible rect objects that will prevent the player from
304 walking into trees, buildings and other solid objects"""
305 tile_map = open(os.path.join('data', 'states', state, 'blockers.txt'), 'r')
306 blockers = []
307
308 for row, line in enumerate(tile_map):
309 for column, letter in enumerate(line):
310 if letter == 'B':
311 blockers.append(pg.Rect(column*32, row*32, 32, 32))
312
313 tile_map.close()
314
315 return blockers
316
317
318def create_level_surface(map):
319 """Creates the surface all images are blitted to"""
320 width = map['rect'].width
321 height = map['rect'].height
322
323 return pg.Surface((width, height)).convert()
324
325
326def create_viewport(map):
327 """Create the viewport to view the level through"""
328 return setup.SCREEN.get_rect(bottom=map['rect'].bottom)
329
330
331def set_sprite_positions(player, level_sprites, state):
332 """Set the start positions for all the sprites in the level"""
333 tile_map = open(os.path.join('data', 'states', state, 'sprite_start_pos.txt'), 'r')
334
335 for row, line in enumerate(tile_map):
336 for column, letter in enumerate(line):
337 if letter == 'P':
338 player.rect = pg.Rect(column*32, row*32, 32, 32)
339 elif letter == 'F':
340 fem_villager = person.FemaleVillager(column*32, row*32)
341 level_sprites.add(fem_villager)
342 elif letter == 'S':
343 soldier = person.Soldier(column*32, row*32)
344 level_sprites.add(soldier)
345 elif letter == 'A':
346 soldier = person.Soldier(column*32, row*32, 'right', 'resting')
347 level_sprites.add(soldier)
348 elif letter == 'B':
349 soldier = person.Soldier(column*32, row*32, 'left', 'resting')
350 level_sprites.add(soldier)
351
352 tile_map.close()
353
354
355def make_level_portals(state):
356 """Create portals to different levels on doors"""
357 tile_map = open(os.path.join('data', 'states', state, 'portals.txt'), 'r')
358 portal_group = pg.sprite.Group()
359
360 for row, line in enumerate(tile_map):
361 for column, letter in enumerate(line):
362 if letter == 'A':
363 portal_group.add(portal.Portal(column, row, c.CASTLE))
364
365 return portal_group
366
367
368
369get_image = tools.get_image
370town_map_dict = create_town_sprite_sheet_dict()
371