From 3c56bf9a44911db5a24a34572a2e4aff6d3d660d Mon Sep 17 00:00:00 2001 From: u8slvn Date: Wed, 14 Aug 2024 14:50:42 +0200 Subject: [PATCH] refactor: landscape with pg sprite --- src/doggo/landscape.py | 38 ++++++-------------------------------- src/doggo/prepare.py | 10 ++++++---- src/doggo/world.py | 4 ++-- 3 files changed, 14 insertions(+), 38 deletions(-) diff --git a/src/doggo/landscape.py b/src/doggo/landscape.py index 2db51ba..ba5b0c5 100644 --- a/src/doggo/landscape.py +++ b/src/doggo/landscape.py @@ -1,44 +1,18 @@ from __future__ import annotations -from abc import ABC -from abc import abstractmethod - import pygame as pg from doggo import ASSETS_PATH -class LandscapeBase(ABC): - """Abstract class for static landscape objects.""" - - @abstractmethod - def render(self, surface: pg.Surface) -> None: - raise NotImplementedError - - -class Landscape(LandscapeBase): - """The landscape of the world.""" +class Landscape(pg.sprite.Sprite): + """Abstract class for landscape.""" - def __init__(self, items: list[LandscapeBase] | None = None) -> None: - self._items = items or [] - def add(self, item: LandscapeBase) -> None: - """Add a static item to the landscape.""" - self._items.append(item) - - def render(self, surface: pg.Surface) -> None: - """Render the landscape.""" - for item in self._items: - item.render(surface) - - -class StaticLandscape(LandscapeBase): +class StaticLandscape(Landscape): """Abstract class for static landscape.""" def __init__(self, topleft: tuple[int, int], path: str) -> None: - self.topleft = topleft - self._surface = pg.image.load(ASSETS_PATH.joinpath(path)).convert_alpha() - - def render(self, surface: pg.Surface) -> None: - """Render the ground.""" - surface.blit(self._surface, self.topleft) + super().__init__() + self.image = pg.image.load(ASSETS_PATH.joinpath(path)).convert_alpha() + self.rect = self.image.get_rect(topleft=topleft) diff --git a/src/doggo/prepare.py b/src/doggo/prepare.py index 885d37a..ee3de27 100644 --- a/src/doggo/prepare.py +++ b/src/doggo/prepare.py @@ -1,6 +1,8 @@ # Prepare module is responsible for building the World dependencies. from __future__ import annotations +import pygame as pg + from doggo import config from doggo.dog.body import Body from doggo.dog.body import Fur @@ -28,17 +30,17 @@ def build_dog() -> Dog: ) -def build_bg_landscape() -> Landscape: +def build_bg_landscape() -> pg.sprite.Group[Landscape]: """Build the background landscape.""" mountain = StaticLandscape(topleft=(0, 0), path="landscape/mountain.png") - return Landscape(items=[mountain]) + return pg.sprite.Group([mountain]) -def build_fg_landscape() -> Landscape: +def build_fg_landscape() -> pg.sprite.Group[Landscape]: """Build the foreground landscape.""" ground = StaticLandscape( topleft=(0, config.WORLD_GROUND), path="landscape/ground.png" ) - return Landscape(items=[ground]) + return pg.sprite.Group([ground]) diff --git a/src/doggo/world.py b/src/doggo/world.py index 80f6553..e0f7fe4 100644 --- a/src/doggo/world.py +++ b/src/doggo/world.py @@ -61,9 +61,9 @@ def update(self) -> None: def render(self) -> None: """Render the world.""" self.screen.fill((135, 206, 235)) - self.bg_landscape.render(surface=self.screen) + self.bg_landscape.draw(surface=self.screen) self.dog.render(surface=self.screen) - self.fg_landscape.render(surface=self.screen) + self.fg_landscape.draw(surface=self.screen) self.window.flip() def start(self) -> None: