diff --git a/test.py b/test.py index 023ba19..2157e25 100644 --- a/test.py +++ b/test.py @@ -153,6 +153,7 @@ class Game: bgm: Sound clock: Clock + fps: float frame: int last_answered_at: datetime now: datetime @@ -166,6 +167,7 @@ class Game: self.now = datetime.now () self.screen = pygame.display.set_mode ((CWindow.WIDTH, CWindow.HEIGHT)) self.clock = Clock () + self.fps = FPS self.frame = 0 self.objects = [] self.bgm = Sound ('assets/bgm.mp3') @@ -182,7 +184,13 @@ class Game: if obj.enabled: obj.redraw () pygame.display.update () - self.clock.tick (FPS) + delta_time = self.clock.tick (FPS) / 1000 + self.fps = 1 / delta_time + if delta_time > 1 / FPS: + for _ in range (int (FPS * delta_time) - 1): + for obj in self.objects: + if obj.enabled: + obj.update () def _create_sky ( self, @@ -249,6 +257,11 @@ class GameObject: def redraw ( self, + ) -> None: + self.update () + + def update ( + self, ) -> None: self.x += self.vx self.y += self.vy @@ -335,6 +348,8 @@ class Deerjika (Creature): width (int): 幅 (px) """ + FPS = 30 + height: int scale: float = .8 surfaces: list[Surface] @@ -379,15 +394,21 @@ class Deerjika (Creature): def redraw ( self, ) -> None: - surface = pygame.transform.scale (self.surfaces[self.frame % len (self.surfaces)], + surface = pygame.transform.scale (self.surfaces[self.frame * self.FPS // FPS + % len (self.surfaces)], (self.width * self.scale, self.height * self.scale)) self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y))) super ().redraw () + + def update ( + self, + ) -> None: if (not self.balloon.enabled) and self.talking: self.talking = False if (self.balloon.enabled and self.balloon.frame >= FPS * 1.5 and not self.talking): self.read_out () + super ().update () def talk ( self, @@ -445,7 +466,7 @@ class CurrentTime (GameObject): ) -> None: for i in range (4): self.game.screen.blit ( - self.font.render (str (self.game.now), True, (0, 0, 0)), + self.font.render (f"{ self.game.now } { self.game.fps } fps", True, (0, 0, 0)), (i % 2, i // 2 * 2)) super ().redraw () @@ -557,6 +578,10 @@ class KitaSun (GameObject): surface = pygame.transform.rotate (self.surface, -(90 + math.degrees (self.arg))) self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y))) super ().redraw () + + def update ( + self, + ) -> None: self.sun.compute (self.game.sky.observer) self.alt = self.sun.alt self.az = self.sun.az @@ -564,6 +589,7 @@ class KitaSun (GameObject): self.arg = self.new_arg self.x = self.new_x self.y = self.new_y + super ().update () @property def new_x ( @@ -620,6 +646,10 @@ class Jojoko (GameObject): surface.set_colorkey ((0, 255, 0)) self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y))) super ().redraw () + + def update ( + self, + ) -> None: self.moon.compute (self.game.sky.observer) self.alt = self.moon.alt self.az = self.moon.az @@ -627,6 +657,7 @@ class Jojoko (GameObject): self.arg = self.new_arg self.x = self.new_x self.y = self.new_y + super ().update () @property def phase (