Skip to content

Commit

Permalink
refactor: landscape with pg sprite
Browse files Browse the repository at this point in the history
  • Loading branch information
u8slvn committed Aug 14, 2024
1 parent 3c01d46 commit 3c56bf9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 38 deletions.
38 changes: 6 additions & 32 deletions src/doggo/landscape.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 6 additions & 4 deletions src/doggo/prepare.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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])
4 changes: 2 additions & 2 deletions src/doggo/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 3c56bf9

Please sign in to comment.