From 93fc438d8a3aa2b58ba2baa53dbb74c63b46efa4 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Wed, 25 Dec 2024 23:18:36 +0900 Subject: [PATCH] =?UTF-8?q?#31=20layer=20=E3=82=92=20GameObject=20?= =?UTF-8?q?=E3=81=AE=20=E5=B1=9E=E6=80=A7=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test.py | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/test.py b/test.py index 7c46d5e..023ba19 100644 --- a/test.py +++ b/test.py @@ -146,7 +146,7 @@ class Game: frame (int): フレーム・カウンタ last_answered_at (datetime): 最後に回答した時刻 now (datetime): 基準日時 - redrawers (list[Redrawer]): 再描画するクラスのリスト + objects (list[GameObject]): 再描画するクラスのリスト screen (Surface): 基底スクリーン sky (Sky): 天体情報 """ @@ -156,7 +156,7 @@ class Game: frame: int last_answered_at: datetime now: datetime - redrawers: list[Redrawer] + objects: list[GameObject] screen: Surface sky: Sky @@ -167,7 +167,7 @@ class Game: self.screen = pygame.display.set_mode ((CWindow.WIDTH, CWindow.HEIGHT)) self.clock = Clock () self.frame = 0 - self.redrawers = [] + self.objects = [] self.bgm = Sound ('assets/bgm.mp3') self.bgm.set_volume (.15) self.bgm.play (loops = -1) @@ -178,9 +178,9 @@ class Game: ) -> None: self.now = datetime.now () self.sky.observer.date = self.now - timedelta (hours = 9) - for redrawer in sorted (self.redrawers, key = lambda x: x['layer']): - if redrawer['obj'].enabled: - redrawer['obj'].redraw () + for obj in sorted (self.objects, key = lambda obj: obj.layer): + if obj.enabled: + obj.redraw () pygame.display.update () self.clock.tick (FPS) @@ -219,6 +219,7 @@ class GameObject: frame: int game: Game height: int + layer: float vx: float = 0 vy: float = 0 width: int @@ -228,22 +229,23 @@ class GameObject: def __init__ ( self, game: Game, - layer: int | None = None, - enabled: bool = True, - x: float = 0, - y: float = 0, + layer: float | None = None, + enabled: bool = True, + x: float = 0, + y: float = 0, ): self.game = game self.enabled = enabled self.frame = 0 if layer is None: - if self.game.redrawers: - layer = max (r['layer'] for r in self.game.redrawers) + 10 + if self.game.objects: + layer = max (obj.layer for obj in self.game.objects) + 10 else: layer = 0 - self.game.redrawers.append ({ 'layer': layer, 'obj': self }) + self.layer = layer self.x = x self.y = y + self.game.objects.append (self) def redraw ( self, @@ -346,7 +348,7 @@ class Deerjika (Creature): game: Game, pattern: DeerjikaPattern = DeerjikaPattern.NORMAL, direction: Direction = Direction.LEFT, - layer: int | None = None, + layer: float | None = None, x: float = 0, y: float = 0, balloon: Balloon | None = None, @@ -699,19 +701,6 @@ class CWindow: HEIGHT = 768 -class Redrawer (TypedDict): - """ - 再描画処理を行ふゲーム・オブゼクトとその優先順位のペア - - Attributes: - layer (int): レイア - obj (GameObject): ゲーム・オブゼクト - """ - - layer: int - obj: GameObject - - def get_surfaces_from_video ( video_path: str, ) -> list[Surface]: