0

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Dominic B.
  • 77
  • 7

3 Answers3

1

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
Jobo Fernandez
  • 905
  • 4
  • 13
  • I tried using your function, but apparently the line for appending a value to the tile map has an error, and I don't think I can fix it. Here is the exact error: ValueError: invalid literal for int() with base 10: '' – Dominic B. Sep 05 '22 at 01:37
  • You are most likely using strings inside your csv file. This is a quick fix inside the ```convert_csv_to_2d_list``` function: ```tile_map.append(map_row)``` instead of ```tile_map.append(list(map(int, map_row)))``` – Jobo Fernandez Sep 05 '22 at 01:45
  • Thanks! One last thing though, for some reason my game class doesn't recognize the player variable when I set it. I use 'if self.tile_map[row][col] == 5: self.player = sprites.Player(row * 32, col * 32, self.player_group, self.platform_group)' but it returns an error saying "AttributeError: Game object has no attribute Player". Do you know why this results when I attempt to use this method? – Dominic B. Sep 14 '22 at 02:05
  • You may have to post that as another question and show more details. It's a bit difficult to imagine your game architecture based only from few info you shared. – Jobo Fernandez Sep 14 '22 at 02:37
1

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 column in range(len(row)):
            if row[column] == 1:
                #do sprite stuff
                sprites.Tile(column * 32, row * 32, 1, self.tile_group)

            elif row[column] == 2:
                #etc
   
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
duck
  • 58
  • 6
  • When I use this code, for some reason, the player doesn't get set as a variable in the game. Like, I set a self.player variable as a instance of a player sprite, but the game class doesn't recognize the player as an actual part of itself, and won't run when I use this. Do I have to return it from this or something? – Dominic B. Aug 25 '22 at 01:58
0

import csv

    #LOADING A MAP DATA
    with open("YOUR CSV FILE") as f:
        data = csv.reader(f, delimiter=',')
        y = 0
        for row in data:
            x = 0
            for column in range(len(row)):
                x += 1

                #IF THE TILE IS IN THE ROW THEN WE RENDERING IT
                if row[column] == "-1":
                    #Render your tile here
      
                    display.blit(image, (x * your tile width, y * your tile height , tile width size, tile height size))

            y += 1
R4GE J4X
  • 41
  • 2