Browse Source

#33 処理落ち対策

ai-migration
みてるぞ 3 weeks ago
parent
commit
a9ba0f697e
1 changed files with 34 additions and 3 deletions
  1. +34
    -3
      test.py

+ 34
- 3
test.py View File

@@ -153,6 +153,7 @@ class Game:


bgm: Sound bgm: Sound
clock: Clock clock: Clock
fps: float
frame: int frame: int
last_answered_at: datetime last_answered_at: datetime
now: datetime now: datetime
@@ -166,6 +167,7 @@ class Game:
self.now = datetime.now () self.now = datetime.now ()
self.screen = pygame.display.set_mode ((CWindow.WIDTH, CWindow.HEIGHT)) self.screen = pygame.display.set_mode ((CWindow.WIDTH, CWindow.HEIGHT))
self.clock = Clock () self.clock = Clock ()
self.fps = FPS
self.frame = 0 self.frame = 0
self.objects = [] self.objects = []
self.bgm = Sound ('assets/bgm.mp3') self.bgm = Sound ('assets/bgm.mp3')
@@ -182,7 +184,13 @@ class Game:
if obj.enabled: if obj.enabled:
obj.redraw () obj.redraw ()
pygame.display.update () 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 ( def _create_sky (
self, self,
@@ -249,6 +257,11 @@ class GameObject:


def redraw ( def redraw (
self, self,
) -> None:
self.update ()

def update (
self,
) -> None: ) -> None:
self.x += self.vx self.x += self.vx
self.y += self.vy self.y += self.vy
@@ -335,6 +348,8 @@ class Deerjika (Creature):
width (int): 幅 (px) width (int): 幅 (px)
""" """


FPS = 30

height: int height: int
scale: float = .8 scale: float = .8
surfaces: list[Surface] surfaces: list[Surface]
@@ -379,15 +394,21 @@ class Deerjika (Creature):
def redraw ( def redraw (
self, self,
) -> None: ) -> 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.width * self.scale, self.height * self.scale))
self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y))) self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y)))
super ().redraw () super ().redraw ()

def update (
self,
) -> None:
if (not self.balloon.enabled) and self.talking: if (not self.balloon.enabled) and self.talking:
self.talking = False self.talking = False
if (self.balloon.enabled and self.balloon.frame >= FPS * 1.5 if (self.balloon.enabled and self.balloon.frame >= FPS * 1.5
and not self.talking): and not self.talking):
self.read_out () self.read_out ()
super ().update ()


def talk ( def talk (
self, self,
@@ -445,7 +466,7 @@ class CurrentTime (GameObject):
) -> None: ) -> None:
for i in range (4): for i in range (4):
self.game.screen.blit ( 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)) (i % 2, i // 2 * 2))
super ().redraw () super ().redraw ()


@@ -557,6 +578,10 @@ class KitaSun (GameObject):
surface = pygame.transform.rotate (self.surface, -(90 + math.degrees (self.arg))) surface = pygame.transform.rotate (self.surface, -(90 + math.degrees (self.arg)))
self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y))) self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y)))
super ().redraw () super ().redraw ()

def update (
self,
) -> None:
self.sun.compute (self.game.sky.observer) self.sun.compute (self.game.sky.observer)
self.alt = self.sun.alt self.alt = self.sun.alt
self.az = self.sun.az self.az = self.sun.az
@@ -564,6 +589,7 @@ class KitaSun (GameObject):
self.arg = self.new_arg self.arg = self.new_arg
self.x = self.new_x self.x = self.new_x
self.y = self.new_y self.y = self.new_y
super ().update ()


@property @property
def new_x ( def new_x (
@@ -620,6 +646,10 @@ class Jojoko (GameObject):
surface.set_colorkey ((0, 255, 0)) surface.set_colorkey ((0, 255, 0))
self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y))) self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y)))
super ().redraw () super ().redraw ()

def update (
self,
) -> None:
self.moon.compute (self.game.sky.observer) self.moon.compute (self.game.sky.observer)
self.alt = self.moon.alt self.alt = self.moon.alt
self.az = self.moon.az self.az = self.moon.az
@@ -627,6 +657,7 @@ class Jojoko (GameObject):
self.arg = self.new_arg self.arg = self.new_arg
self.x = self.new_x self.x = self.new_x
self.y = self.new_y self.y = self.new_y
super ().update ()


@property @property
def phase ( def phase (


Loading…
Cancel
Save