all repos — Legends-RPG @ 3303fa3f89ef24c3ff37369bf3a52f0c27363552

A fantasy mini-RPG built with Python and Pygame.

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