Simple plateformer game playing

This commit is contained in:
2026-10-10 13:38:11 +02:00
commit 5cc2f19887
17 changed files with 531 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
"""
Reads map files and returns a Map object.
It provides a method to read a map file by it's name and return the corresponding Map object.
It provides a method to get all of the map names in the maps directory.
"""
import os
from game.map import Map
import tomllib
class MapReader:
DIRECTORY = "maps/"
def __init__(self):
pass
def get_map_names(self):
return [f[:-5] for f in os.listdir(self.DIRECTORY) if f.endswith('.toml')]
def read_map(self, map_name):
import configparser
map_path = os.path.join(self.DIRECTORY, map_name + ".toml")
if not os.path.exists(map_path):
raise FileNotFoundError(f"Map file {map_name} not found in {self.DIRECTORY}")
with open(map_path, 'rb') as f:
config = tomllib.load(f)
map_data = {
'name': map_name,
'width': config.get('width', 0),
'height': config.get('height', 0),
'difficulty': config.get('difficulty', 0.0),
'layout': self._parse_layout(config.get('layout', ''))
}
return Map(map_data)
def _get_block_characters_mapping(self):
# Returns a dictionary mapping block characters to their corresponding block classes.
from game.blocks import BLOCKS
return {block().character: block for block in BLOCKS}
def _parse_layout(self, mapString):
# Takes the blocks and translates their characters into the corresponding block objects.
block_characters_mapping = self._get_block_characters_mapping()
layout = []
for line in mapString.strip().splitlines():
row = []
for char in line:
block_class = block_characters_mapping.get(char)
if block_class:
row.append(block_class())
else:
row.append(None)
layout.append(row)
return layout