data/tilemap.py (view raw)
1__author__ = 'justinarmstrong'
2
3import os
4import pygame as pg
5import tools, setup
6from components import person
7
8
9def create_town_sprite_sheet_dict():
10 """Create a dictionary of sprite sheet tiles"""
11 tile_dict = {}
12 tileset1 = setup.GFX['tileset1']
13 tileset2 = setup.GFX['tileset2']
14 tileset3 = setup.GFX['tileset3']
15 sword = setup.GFX['sword']
16 shield = setup.GFX['shield']
17 potion = setup.GFX['potion']
18 gem = setup.GFX['gem']
19 castle_door = setup.GFX['castledoor']
20
21 tile_dict['pavement'] = get_tile(32, 48, tileset2)
22 tile_dict['house wall'] = get_tile(64, 48, tileset2)
23 tile_dict['house roof'] = get_tile(0, 144, tileset2)
24 tile_dict['house door'] = get_tile(48, 64, tileset2)
25 tile_dict['tree'] = get_tile(80, 48, tileset1, 16, 32)
26 tile_dict['well'] = get_tile(96, 50, tileset1, 16, 32)
27 tile_dict['moat'] = get_tile(16, 16, tileset2)
28 tile_dict['fence'] = get_tile(48, 64, tileset1)
29 tile_dict['grass'] = get_tile(0, 16, tileset1)
30 tile_dict['sword'] = get_tile(0, 0, sword, 32, 32)
31 tile_dict['shield'] = get_tile(0, 0, shield, 32, 32)
32 tile_dict['potion'] = get_tile(0, 0, potion, 32, 32)
33 tile_dict['gem'] = get_tile(0, 0, gem, 32, 32)
34 tile_dict['castle bridge'] = get_tile(48, 27, tileset1, 16, 32 )
35 tile_dict['flower1'] = get_tile(64, 64, tileset2)
36 tile_dict['flower2'] = get_tile(80, 64, tileset2)
37 tile_dict['horiz castle wall'] = get_tile(32, 0, tileset3, 48, 32)
38 tile_dict['vert castle wall'] = get_tile(0, 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 left'] = get_tile(112, 128, tileset2)
50 tile_dict['carpet right'] = get_tile(144, 128, tileset2)
51 tile_dict['castle window'] = get_tile(128, 59, tileset1)
52
53 return tile_dict
54
55
56def get_tile(x, y, tileset, width=16, height=16, scale=1):
57 """Gets the surface and rect for a tile"""
58 surface = get_image(x, y, width, height, tileset)
59 surface = pg.transform.scale(surface, (int(width*scale), int(height*scale)))
60 rect = surface.get_rect()
61
62 tile_dict = {'surface': surface,
63 'rect': rect}
64
65 return tile_dict
66
67
68def create_town_map():
69 """Blits the different layers of the map onto one surface"""
70 map = create_background()
71 map = create_map_layer1(map)
72 map = create_map_layer2(map)
73 map = scale_map(map)
74 map = create_map_layer3(map)
75
76 return map
77
78
79def create_background():
80 """Creates the background surface that the rest of
81 the town map will be blitted on"""
82 size = (25*16, 50*16)
83 surface = pg.Surface(size)
84 grass_tile = get_image(0, 0, 16, 16, setup.GFX['tileset2'])
85 grass_rect = grass_tile.get_rect()
86
87 for row in range(50):
88 for column in range(25):
89 grass_rect.y = row * 16
90 grass_rect.x = column * 16
91 surface.blit(grass_tile, grass_rect)
92
93 surface_rect = surface.get_rect()
94
95 background_dict = {'surface': surface,
96 'rect': surface_rect}
97
98 return background_dict
99
100
101def create_map_layer1(map):
102 """Creates the town from a tile map and creates a
103 surface on top of the background"""
104 tile_map = open(os.path.join('data', 'states', 'town_map.txt'), 'r')
105
106 for row, line in enumerate(tile_map):
107 for column, letter in enumerate(line):
108 if letter == '1':
109 tile = town_map_dict['pavement']
110 blit_tile_to_map(tile, row, column, map)
111
112 elif letter == '2':
113 tile = town_map_dict['house wall']
114 blit_tile_to_map(tile, row, column, map)
115
116 elif letter == '3':
117 tile = town_map_dict['house roof']
118 blit_tile_to_map(tile, row, column, map)
119
120 elif letter == 'T':
121 tile = town_map_dict['tree']
122 blit_tile_to_map(tile, row, column, map)
123
124 elif letter == 'W':
125 tile = town_map_dict['well']
126 blit_tile_to_map(tile, row, column, map)
127
128 elif letter == 'M':
129 tile = town_map_dict['moat']
130 blit_tile_to_map(tile, row, column, map)
131
132 elif letter == 'G':
133 tile = town_map_dict['grass']
134 blit_tile_to_map(tile, row, column, map)
135
136 elif letter == 'B':
137 tile = town_map_dict['castle bridge']
138 blit_tile_to_map(tile, row, column, map)
139
140 elif letter == 'C':
141 tile = town_map_dict['horiz castle wall']
142 blit_tile_to_map(tile, row, column, map)
143
144 elif letter == 'V':
145 tile = town_map_dict['vert castle wall']
146 blit_tile_to_map(tile, row, column, map)
147
148 elif letter == 'S':
149 tile = town_map_dict['castle side']
150 blit_tile_to_map(tile, row, column, map)
151
152 elif letter == 'Q':
153 tile = town_map_dict['carpet topleft']
154 blit_tile_to_map(tile, row, column, map)
155
156 elif letter == 'E':
157 tile = town_map_dict['carpet topright']
158 blit_tile_to_map(tile, row, column, map)
159
160 elif letter == 'R':
161 tile = town_map_dict['carpet bottomleft']
162 blit_tile_to_map(tile, row, column, map)
163
164 elif letter == 'U':
165 tile = town_map_dict['carpet bottomright']
166 blit_tile_to_map(tile, row, column, map)
167
168 elif letter == 'P':
169 tile = town_map_dict['carpet left']
170 blit_tile_to_map(tile, row, column, map)
171
172 elif letter == 'A':
173 tile = town_map_dict['carpet right']
174 blit_tile_to_map(tile, row, column, map)
175
176
177 tile_map.close()
178
179 return map
180
181
182def create_map_layer2(map):
183 """Creates doors and other items on top of the rest of the map"""
184 tile_map = open(os.path.join('data', 'states', 'town_layer2.txt'), 'r')
185
186 for row, line in enumerate(tile_map):
187 for column, letter in enumerate(line):
188 if letter == 'D':
189 tile = town_map_dict['house door']
190 blit_tile_to_map(tile, row, column, map)
191 elif letter == 'F':
192 tile = town_map_dict['fence']
193 blit_tile_to_map(tile, row, column, map)
194 elif letter == '$':
195 tile = town_map_dict['flower1']
196 blit_tile_to_map(tile, row, column, map)
197 elif letter == '*':
198 tile = town_map_dict['flower2']
199 blit_tile_to_map(tile, row, column, map)
200 elif letter == 'T':
201 tile = town_map_dict['castle tower']
202 blit_tile_to_map(tile, row, column, map)
203 elif letter == 'W':
204 tile = town_map_dict['vert castle wall']
205 blit_tile_to_map(tile, row, column, map)
206 elif letter == 'M':
207 tile = town_map_dict['main castle roof']
208 blit_tile_to_map(tile, row, column, map)
209 elif letter == 'L':
210 tile = town_map_dict['left castle roof piece']
211 blit_tile_to_map(tile, row, column, map)
212 elif letter == 'R':
213 tile = town_map_dict['right castle roof piece']
214 blit_tile_to_map(tile, row, column, map)
215 elif letter == '#':
216 tile = town_map_dict['tree']
217 blit_tile_to_map(tile, row, column, map)
218 elif letter == 'O':
219 tile = town_map_dict['castle door']
220 blit_tile_to_map(tile, row, column, map)
221 elif letter == 'Q':
222 tile = town_map_dict['castle window']
223 blit_tile_to_map(tile, row, column, map)
224
225 tile_map.close()
226
227 return map
228
229
230def scale_map(map):
231 """Double resolution of map to 32x32"""
232 map['surface'] = pg.transform.scale2x(map['surface'])
233 map['rect'] = map['surface'].get_rect()
234
235 return map
236
237
238def create_map_layer3(map):
239 """Layers for images that are already 32x32"""
240 tile_map = open(os.path.join('data', 'states', 'town_layer3.txt'), 'r')
241
242 for row, line in enumerate(tile_map):
243 for column, letter in enumerate(line):
244 if letter == 'W':
245 tile = town_map_dict['sword']
246 blit_tile_to_map(tile, row, column, map, 32)
247 elif letter == 'A':
248 tile = town_map_dict['shield']
249 blit_tile_to_map(tile, row, column, map, 32)
250 elif letter == 'P':
251 tile = town_map_dict['potion']
252 blit_tile_to_map(tile, row, column, map, 32)
253 elif letter == 'M':
254 tile = town_map_dict['gem']
255 blit_tile_to_map(tile, row, column, map, 32)
256
257 tile_map.close()
258
259 return map
260
261
262
263def blit_tile_to_map(tile, row, column, map, side_length=16):
264 """Places tile to map"""
265 tile['rect'].x = column * side_length
266 tile['rect'].y = row * side_length
267
268 map['surface'].blit(tile['surface'], tile['rect'])
269
270
271def create_blockers():
272 """Creates invisible rect objects that will prevent the player from
273 walking into trees, buildings and other solid objects"""
274 tile_map = open(os.path.join('data', 'states', 'town_blocker_layer.txt'), 'r')
275 blockers = []
276
277 for row, line in enumerate(tile_map):
278 for column, letter in enumerate(line):
279 if letter == 'B':
280 blockers.append(pg.Rect(column*32, row*32, 32, 32))
281
282 tile_map.close()
283
284 return blockers
285
286
287def create_level_surface(map):
288 """Creates the surface all images are blitted to"""
289 width = map['rect'].width
290 height = map['rect'].height
291
292 return pg.Surface((width, height)).convert()
293
294
295def create_viewport(map):
296 """Create the viewport to view the level through"""
297 return setup.SCREEN.get_rect(bottom=map['rect'].bottom)
298
299
300def set_sprite_positions(player, level_sprites):
301 """Set the start positions for all the sprites in the level"""
302 tile_map = open(os.path.join('data', 'states', 'sprite_start_pos.txt'), 'r')
303
304 for row, line in enumerate(tile_map):
305 for column, letter in enumerate(line):
306 if letter == 'P':
307 player.rect = pg.Rect(column*32, row*32, 32, 32)
308 elif letter == 'F':
309 fem_villager = person.FemaleVillager(column*32, row*32)
310 level_sprites.add(fem_villager)
311 elif letter == 'S':
312 soldier = person.Soldier(column*32, row*32)
313 level_sprites.add(soldier)
314
315 tile_map.close()
316
317
318get_image = tools.get_image
319town_map_dict = create_town_sprite_sheet_dict()
320
321
322