all repos — Legends-RPG @ 042b912afd55ebeecab540dc4a44dd070f886b6a

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.NEAR_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            elif letter == 'N':
301                tile = tile_dict['black tile']
302                blit_tile_to_map(tile, row, column, map)
303
304    tile_map.close()
305
306    if file_name == 'layer2.txt':
307        map = pg.transform.scale2x(map)
308        map = create_map_layer3(map, state)
309
310    return map
311
312
313def scale_map(map):
314    """Double resolution of map to 32x32"""
315    map = pg.transform.scale2x(map)
316
317    return map
318
319
320def create_map_layer3(map, state):
321    """Layers for images that are already 32x32"""
322    tile_map = open(os.path.join('data', 'states', state, 'layer3.txt'), 'r')
323
324    for row, line in enumerate(tile_map):
325        for column, letter in enumerate(line):
326            if letter == 'W':
327                tile = tile_dict['sword']
328                blit_tile_to_map(tile, row, column, map, 32)
329            elif letter == 'A':
330                tile = tile_dict['shield']
331                blit_tile_to_map(tile, row, column, map, 32)
332            elif letter == 'P':
333                tile = tile_dict['potion']
334                blit_tile_to_map(tile, row, column, map, 32)
335            elif letter == 'M':
336                tile = tile_dict['gem']
337                blit_tile_to_map(tile, row, column, map, 32)
338            elif letter == 'I':
339                tile = tile_dict['inn sign']
340                blit_tile_to_map(tile, row, column, map, 32)
341            elif letter == 'B':
342                tile = tile_dict['chair']
343                blit_tile_to_map(tile, row, column, map, 32)
344
345
346    tile_map.close()
347
348    return map
349
350
351
352def blit_tile_to_map(tile, row, column, map, side_length=16):
353    """Places tile to map"""
354    tile['rect'].x = column * side_length
355    tile['rect'].y = row * side_length
356
357    map.blit(tile['surface'], tile['rect'])
358
359
360def create_blockers(state):
361    """Creates invisible rect objects that will prevent the player from
362    walking into trees, buildings and other solid objects"""
363    tile_map = open(os.path.join('data', 'states', state, 'blockers.txt'), 'r')
364    blockers = []
365
366    for row, line in enumerate(tile_map):
367        for column, letter in enumerate(line):
368            if letter == 'B':
369                blockers.append(pg.Rect(column*32, row*32, 32, 32))
370
371    tile_map.close()
372
373    return blockers
374
375
376def make_level_surface(map):
377    """Creates the surface all images are blitted to"""
378    map_rect = map.get_rect()
379    size = map_rect.size
380
381
382    return pg.Surface(size).convert()
383
384
385def create_viewport(map):
386    """Create the viewport to view the level through"""
387    map_rect = map.get_rect()
388    return setup.SCREEN.get_rect(bottom=map_rect.bottom)
389
390
391def set_sprite_positions(player, level_sprites, state, game_data):
392    """Set the start positions for all the sprites in the level"""
393    start_pos_key = state + ' start pos'
394    x =  game_data[start_pos_key][0]*32
395    y = game_data[start_pos_key][1]*32
396    player.rect = pg.Rect(x, y, 32, 32)
397
398    tile_map = open(os.path.join('data', 'states', state, 'sprite_start_pos.txt'), 'r')
399
400    for row, line in enumerate(tile_map):
401        for column, letter in enumerate(line):
402            if letter == 'F':
403                fem_villager = person.FemaleVillager(column*32, row*32)
404                level_sprites.add(fem_villager)
405            elif letter == 'S':
406                soldier = person.Soldier(column*32, row*32)
407                level_sprites.add(soldier)
408            elif letter == 'A':
409                soldier = person.Soldier(column*32, row*32, 'right', 'resting')
410                level_sprites.add(soldier)
411            elif letter == 'B':
412                soldier = person.Soldier(column*32, row*32, 'left', 'resting')
413                level_sprites.add(soldier)
414            elif letter == 'C':
415                king = person.King(column*32, row*32, 'down', 'resting')
416                level_sprites.add(king)
417            elif letter == 'D':
418                well = person.Well(column*32, row*32)
419                level_sprites.add(well)
420            elif letter == 'E':
421                fem_villager = person.FemaleVillager2(column*32, row*32)
422                level_sprites.add(fem_villager)
423            elif letter == 'G':
424                devil_villager = person.Devil(column*32, row*32)
425                level_sprites.add(devil_villager)
426            elif letter == 'H':
427                old_villager = person.OldMan(column*32, row*32)
428                level_sprites.add(old_villager)
429
430    tile_map.close()
431
432
433def make_level_portals(state):
434    """Create portals to different levels on doors"""
435    tile_map = open(os.path.join('data', 'states', state, 'portals.txt'), 'r')
436    portal_group = pg.sprite.Group()
437
438    for row, line in enumerate(tile_map):
439        for column, letter in enumerate(line):
440            if letter == 'A':
441                portal_group.add(portal.Portal(column, row, c.CASTLE))
442            elif letter == 'B':
443                portal_group.add(portal.Portal(column, row, c.TOWN))
444            elif letter == 'C':
445                portal_group.add(portal.Portal(column, row, c.INN))
446            elif letter == 'D':
447                portal_group.add(portal.Portal(column, row, c.MAGIC_SHOP))
448            elif letter == 'E':
449                portal_group.add(portal.Portal(column, row, c.POTION_SHOP))
450            elif letter == 'F':
451                portal_group.add(portal.Portal(column, row, c.WEAPON_SHOP))
452            elif letter == 'G':
453                portal_group.add(portal.Portal(column, row, c.ARMOR_SHOP))
454            elif letter == 'H':
455                portal_group.add(portal.Portal(column, row, c.HOUSE))
456
457    return portal_group
458
459
460
461get_image = tools.get_image
462tile_dict = create_town_sprite_sheet_dict()
463