Generating Tile Map

Our game is an RPG game and it will be a tile-based game. In this tutorial, we are going to create a map. There are some editors for creating tilemap and using them on the game. However, I'm not thinking to use it. I mentioned the first tutorial for this game, I said, "it will be a survival game like Minecraft, Terraria, etc.". When we created a new game on these games, they generated a map randomly. In that case, we are going to generate our map randomly. It's just the beginning, of course, we should optimize this map with a lot of things.

Well, we need tiles for creating a map. I'm going to create a dictionary that will contain tiles. I'm not going to draw the texture of these tiles. These tiles will be represented by filled color surfaces. Let's create a new file called tilemap.py and add it tiles:
# dimension of each tiles
TILE_SIZE = 32

# texture of colors
YELLOW  = (255, 255, 0)
RED     = (255, 0, 0)
BLUE    = (0 , 0, 255)
GREEN   = (0, 255, 0)
BROWN   = (160, 82, 45)
We will use these colors to creating texture. I'm going to type a function called create_texture:
def create_texture(color):
    image = pygame.Surface((TILE_SIZE,TILE_SIZE))
    image.fill(color)
    return image
Let's create our textures in the dictionary:
# 0x0 -> grass
# 0xb -> dirt
textures = {
    0x0 : create_texture(GREEN),
    0xb : create_texture(BROWN)
}
I would like to visualize how we generate map randomly:
generating tile map
According to the above image, We have two functions that are generate_map() function and draw_map() function. generate_map() function returns a list which contains random tile from tiles each cell. The data, we got from generate_map() function, are passed to draw_map() function and this function use this map data for drawing to the screen. If you examine codes of functions, you realize we used nested loops for generating data and drawing map. Let's code it:
# generate with tiles randomly
def generate_map(width, height, tilesize = TILE_SIZE):
    map_data = []
    for i in range(height // tilesize):
        map_data.append([])
        for j in range(width // tilesize):
            rand_index = random.randint(0,1)
            # convert to hex from string value
            tile = int(hex(tiles[rand_index]), 16)
            map_data[i].append(tile)
    return map_data


def draw_map(screen, map_data):
    MAP_HEIGHT = len(map_data) 
    MAP_WIDTH = len(map_data[0])
    for row in range(MAP_HEIGHT):
        for col in range(MAP_WIDTH):
            screen.blit(textures[map_data[row][col]],
                        (col*TILE_SIZE, row*TILE_SIZE))        
I am going to use these functions in main.py:
...

map_data = generate_map(640, 480)

def main():
    running = True
    # the game loop
    while running:
        ...
        
        # draw
        screen.fill((255,255,255))
        draw_map(screen, map_data)
        sprites_group.draw(screen)
        ...
So our tile map looks like it:
Generating Tile Map
We got it, however, it's not exactly what we want. Because everything is just a mess and we just use two tiles, think about what happen if we try to add another tiles to the map. The random concept is cause of this. We have to look another alternatives. 

You can reach full of source code from this link


No comments:

Post a Comment