all repos — Legends-RPG @ 20eb9ef9856bba8c568c465383ea6ca2fa495a77

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