Kaydet (Commit) 7ad67d8e authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

Config system for map

üst 4eb5bbd9
......@@ -3,23 +3,35 @@ import random
import time
from collections import UserList
from contextlib import suppress
from configparser import ConfigParser
from typing import Tuple
import pygame
from pygame.locals import QUIT
from lordlarkamarasi.textures import AVAILABLE_BLOCKS, TILE_SIZE, Blocks
from lordlarkamarasi.textures import AVAILABLE_BLOCKS, BASE_PATH, TILE_SIZE, Blocks
class Map(UserList):
def __init__(self, width, height):
def __init__(self, width, height, surface):
self.width = width
self.height = height
self.data = list(
list(itertools.repeat(Blocks.GRASS, width)) for _ in range(height)
)
self._shake_map()
self.data = surface
@classmethod
def from_file(cls, file):
cfg = ConfigParser()
cfg.read(BASE_PATH / file)
meta = cfg['meta']
width, height = meta.getint('width'), meta.getint('height')
ground = cfg['ground']
default = Blocks.get_by_id(ground.getint('default'))
surface = [list(itertools.repeat(default, width)) for _ in range(height)]
return cls(width, height, surface)
def _shake_map(self):
for _ in range((self.width * self.height) // 32):
print(random.choice(AVAILABLE_BLOCKS))
......@@ -44,7 +56,7 @@ class Game:
pygame.display.set_caption("Lordlar Kamarası")
self.surface = pygame.display.set_mode(window)
self.map = Map(*(meter // 25 for meter in window))
self.map = Map.from_file('maps/base_map.ini')
def process_event(self, event: pygame.event.EventType):
self.draw_map()
......
[meta]
width = 32
height = 32
[ground]
default = 0
......@@ -10,8 +10,7 @@ BASE_PATH = Path(__file__).parent
def relative_load(path: str) -> pygame.Surface:
return pygame.image.load(os.fspath(BASE_PATH / path))
return pygame.image.load(os.fspath(BASE_PATH / path))
@dataclass
class Block:
......@@ -21,16 +20,17 @@ class Block:
class Blocks(Enum):
STONE = Block(0, relative_load("textures/land/stone.png"))
GRASS = Block(1, relative_load("textures/land/grass.png"))
GRASS = Block(0, relative_load("textures/land/grass.png"))
STONE = Block(1, relative_load("textures/land/stone.png"))
WATER = Block(2, relative_load("textures/land/water.png"))
SAND = Block(3, relative_load("textures/land/sand.png"))
AVAILABLE_BLOCKS = list(
filter(lambda block: not block.value.must_connect, Blocks.__members__.values())
)
MUST_CONNECT_BLOCKS = list(
filter(lambda block: block.value.must_connect, Blocks.__members__.values())
) # use set substractions
@classmethod
def get_by_id(cls, id):
block, = filter(lambda member: member.value.id == id, cls.__members__.values())
return cls(block)
ALL_BLOCKS = set(Blocks.__members__.values())
AVAILABLE_BLOCKS = set(filter(lambda block: not block.value.must_connect, ALL_BLOCKS))
MUST_CONNECT_BLOCKS = ALL_BLOCKS - AVAILABLE_BLOCKS
TILE_SIZE = 25
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment