Browse Source

#31 layer を GameObject の 属性化

ai-migration
みてるぞ 3 weeks ago
parent
commit
93fc438d8a
1 changed files with 16 additions and 27 deletions
  1. +16
    -27
      test.py

+ 16
- 27
test.py View File

@@ -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]:


Loading…
Cancel
Save