I created a class to handle maps in my RPG game, before it became a class this was a collection of functions I created, but I thought it could be used as a class. although this code is working but i am not sure what i made. changing the function set to OOP confuses me.
import os
import pygame
import helper_func as hf
class Map:
def __init__(self):
self.map = []
self.images = {}
self.tile_size = (50, 50)
def load_map(self, path):
''' load map from txt to 2d list '''
f = open(path, "r")
file = f.read()
f.close()
sep = file.split("\n")
for tile in sep:
numbers = tile.split(",")
self.map.append(numbers)
@staticmethod
def generate_key(image):
result = []
for char in image:
try:
if isinstance(int(char), int):
result.append(char)
except Exception:
pass
return "".join(result)
def map_images(self, path):
''' load tileset image to dictionary '''
images = os.listdir(path)
for image in images:
key = Map.generate_key(image)
self.images[key] = hf.load_image(f"{path}/{image}", self.tile_size[0], self.tile_size[1])
def render(self, screen):
y = 0
for row in self.map:
x = 0
for tile in row:
screen.blit(self.images[tile], (x, y))
x += self.tile_size[0]
y += self.tile_size[1]
Is there anything that needs to be fixed?