pythoncsvpygametiled

Overlapping Tile Layers from CSV files in Pygame


I am trying to create a display surface in pygame that has mutliple tile layers involved in the making of it. I have a background, accessories, brown_dirt, yellow_dirt, and water csv files that I am reading from. I am then matching each to its respective tile from a tile set that I parsed into 32x32 tiles.

What I need help with is how to layer the tile layers based on a specific order. The last link on the Youtube video from minutes 50-1:10 he walks through adding a y index to allow for this to work, but I don't believe that would work for my problem because I have a surface for each given tile I want to put.

Below is all of the relevant code for this issue, but the most important sections can be found within the YSortCameraGroup. Here is all the relevant code:

def import_cut_graphics(image_path):
    surface = pygame.image.load(image_path).convert_alpha()
    tile_num_x = int(surface.get_size()[0] / 32)
    tile_num_y = int(surface.get_size()[1] / 32)

    cut_tiles = []

    for row in range(tile_num_y):
        for col in range(tile_num_x):
            x = col * 32
            y = row * 32
            new_surface = pygame.Surface((32, 32))
            new_surface.blit(surface, (0, 0), pygame.Rect(x, y, 32, 32))

            cut_tiles.append(new_surface)
            
    return cut_tiles

tile_list = import_cut_graphics('./images/tiles/tiles-original.png')

class Tile(pygame.sprite.Sprite):
    """class to read and create individual tiles and place in display"""
    def __init__(self, x, y, image_number, main_group, sub_group=""):
        super().__init__()

        if image_number == -1:
            pass
        else:
            self.image = tile_list[image_number]

            main_group.add(self)

            # rect and positioning
            self.rect = self.image.get_rect()
            self.rect.topleft = (x, y)   

class YSortCameraGroup(pygame.sprite.Group):
    def __init__(self):
        super().__init__()

        
    def custom_draw(self):
        for sprite in self.sprites():
            display_surface.blit(sprite.image, sprite.rect)
            


# create sprite groups
visible_sprites = YSortCameraGroup()

main_tile_group = pygame.sprite.Group()


background_tiles = []
brown_dirt_tiles = []
accessories_tiles = []
water_tiles = []
yellow_dirt_tiles = []


with open("./csv/Level One/Background.csv") as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        background_tiles.append(row)
with open("./csv/Level One/Accessories.csv") as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        accessories_tiles.append(row)
with open("./csv/Level One/Brown Dirt.csv") as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        brown_dirt_tiles.append(row)
with open("./csv/Level One/Water.csv") as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        water_tiles.append(row)
with open("./csv/Level One/Yellow Dirt.csv") as file:
    csvreader = csv.reader(file)
    for row in csvreader:
        yellow_dirt_tiles.append(row)

# i have all the csv files in arrays and the tile_list, now I need to match them 

for i in range(len(background_tiles)):
    for j in range(len(background_tiles[i])): 
        # background tiles is a sequence of ID's
        Tile(j * 32, i * 32, int(background_tiles[i][j]), main_tile_group)

for i in range(len(brown_dirt_tiles)):
    for j in range(len(brown_dirt_tiles[i])): 
        # background tiles is a sequence of ID's
        Tile(j * 32, i * 32, int(brown_dirt_tiles[i][j]), visible_sprites)

for i in range(len(accessories_tiles)):
    for j in range(len(accessories_tiles[i])): 
        # background tiles is a sequence of ID's
        Tile(j * 32, i * 32, int(accessories_tiles[i][j]), visible_sprites)

for i in range(len(water_tiles)):
    for j in range(len(water_tiles[i])): 
        # background tiles is a sequence of ID's
        Tile(j * 32, i * 32, int(water_tiles[i][j]), visible_sprites)

for i in range(len(yellow_dirt_tiles)):
    for j in range(len(yellow_dirt_tiles[i])): 
        # background tiles is a sequence of ID's
        Tile(j * 32, i * 32, int(yellow_dirt_tiles[i][j]), visible_sprites)

Related questions and links I have tried: Overlay tiles onto sprite in pygame , How do I read a .csv file for a tile map in pygame? , and https://www.youtube.com/watch?v=QU1pPzEGrqw&ab_channel=ClearCode .

Each of these either addresses a different problem or touches too shallow on the subject. I am fairly certain the changes need to be made inside the custom_draw method I have written (from the Youtube video, but am unsure what to add).

The code produces this display: Picture of display with grass and mushrooms

Does anyone know any resources I could go to or approaches to this process that I might have missed?


Solution

  • The background of your tile images is not transparent. Create tile surfaces in RGBA format with the pygame.SRCALPHA flag:

    new_surface = pygame.Surface((32, 32))

    new_surface = pygame.Surface((32, 32), pygame.SRCALPHA)