Home > Net >  How do I read a .csv file for a tile map in pygame?
How do I read a .csv file for a tile map in pygame?

Time:08-14

I'm making a sequel to an entry I made in the GMTK Game Jam. It's a platformer where the player has to get through randomly selected maps of various game modes, like a platforming race, a boss battle, and what I can only describe as dodgeball but with a giant killer pac man. For this, I want to make many levels, and slowly editing lists in a python script isn't gonna cut it for this. So I want to learn how to read .csv files and use them to create a level using many tiles. But how would I go about this?

So far I have a main script, a script for sprites, and a levels script. The last one has two test level maps inside of it, and I want to replace it with a folder of various .csv files. In my main script, I have a game class. In the __init__ method, I set a list of levels pulling from the level script, randomly choose one to load, and loop through it to create a sprite from my sprites script when the index is a certain value.

#Set tile map
        self.level = [levels.plat_lvl_1, levels.plat_lvl_2]

        self.tile_map = random.choice(self.level)

        for i in range(len(self.tile_map)):
            for j in range(len(self.tile_map[i])):
                if self.tile_map[i][j] == 1:
                    sprites.Tile(j * 32, i * 32, 1, self.tile_group)

                elif self.tile_map[i][j] == 2:
                    sprites.Tile(j * 32, i * 32, 2, self.tile_group, self.platform_group)

                elif self.tile_map[i][j] == 3:
                    sprites.Tile(j * 32, i * 32, 3, self.tile_group, self.platform_group)

                elif self.tile_map[i][j] == 4:
                    sprites.Tile(j * 32, i * 32, 4, self.tile_group, self.platform_group)

                elif self.tile_map[i][j] == 5:
                    self.player = sprites.Player(j * 32, i * 32, self.player_group, self.platform_group)

                elif self.tile_map[i][j] == 6:
                    sprites.Tile(j * 32, i * 32 - 32, 6, self.tile_group)

How would I replace a list with a .csv file, and how would I need to loop through it or strip it or whatever in order to properly create my levels? Here is the link to the entire project as it is currently if you need it.

Thank you very much!

-Dominic.

CodePudding user response:

I have this function to convert csv maps to a List[List[int]] format.

def convert_csv_to_2d_list(csv_file: str):
    tile_map = []
    with open(csv_file, "r") as f:
        for map_row in csv.reader(f):
            tile_map.append(list(map(int, map_row)))
    return tile_map

Its usage will then be:

self.tile_map = convert_csv_to_2d_list(csv_file="csv_file_location.csv")
for row in range(len(self.tile_map)):
    for col in range(len(self.tile_map[row])):
        # specific codes follows here

CodePudding user response:

You can do something like this:

import csv

with open('level_file.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
   
    for row in csv_reader:
        for coloumn in range(len(row)):
            if row[coloumn] == 1:
                #do sprite stuff
                sprites.Tile(coloumn * 32, row * 32, 1, self.tile_group)

            elif row[coloumn] == 2:
                #etc
   
  • Related