# Map generator for zelda clone

I'm starting on a zelda clone in pygame for coding practice, and was thinking of how to program the maps. The idea, similar to the early Legend of Zelda games, is that a square, tile-based map will be generated with the character in it. If the character moves to a 'transition' area on the map (the map edge, a set of stairs, a hole, etc) the map will transition as needed.

In this code so far, I have one test map in place. My plan is to instead have the entire map library of the game stored in a text file as binary, which is accessed and read whenever the map needs to transition.

Each map is stored as a series of nested lists that are referenced using the Cartesian Coordinate system (I think this is also called a 2-dimensional array). Each tile is stored as a number, which is referenced in the code to translate it to a tile, which is then drawn onto the map based on its position in the game_map list.

import pygame
pygame.init()

terrain_list = [ 'adventure_pygame/clear.bmp', 'adventure_pygame/trees.bmp']

game_map = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], # x = 1
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 2
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 3
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 4
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 5
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 6
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 7
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 8
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 9
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 10
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 11
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 12
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 13
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 14
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # x = 15
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]  # x = 16
]
hitboxes = True

win = pygame.display.set_mode((512, 512))

pygame.display.set_caption("My Game")

clock = pygame.time.Clock()

def redraw_game_window():   # Redraw the screen; should include draw() methods for the background and each object on the screen

for map_x in range(16):
for map_y in range(16):
terrain = game_map[map_x][map_y]
terrain_tile = terrain_list[terrain]
win.blit(pygame.image.load(terrain_tile), (map_x * 32, map_y * 32))

pygame.display.update()

#mainloop
run = True
while run:
clock.tick(60) # sets the FPS

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
print('space key down')
if keys[pygame.K_LEFT]:
print('left key down')
if keys[pygame.K_RIGHT]:
print('right key down')

redraw_game_window()

pygame.quit

• Well, what would you like a code review on. Representing a square, tile-based map as a list of lists? Use of pygame? I don't see transition. – greybeard May 23 at 16:53