diff --git a/test.py b/test.py index 58cd2f6..4aaa0db 100644 --- a/test.py +++ b/test.py @@ -103,9 +103,9 @@ class Bg: self, game: Game, ): - self.base = BgBase (game) - self.jojoko = Jojoko (game) self.kita = KitaSun (game) + self.base = BgBase (game, self.kita.sun, layer = self.kita.layer - 5) + self.jojoko = Jojoko (game) self.grass = BgGrass (game) @@ -284,23 +284,60 @@ class BgBase (GameObject): bg_evening: Surface bg_grass: Surface bg_night: Surface - surface: Surface + sun: Sun def __init__ ( self, game: Game, + sun: Sun, + layer: float, ): - super ().__init__ (game) - self.bg = pygame.image.load ('assets/bg.jpg') - self.bg_evening = pygame.image.load ('assets/bg-evening.jpg') - self.bg_grass = pygame.image.load ('assets/bg-grass.png') - self.bg_night = pygame.image.load ('assets/bg-night.jpg') - self.surface = pygame.transform.scale (self.bg, (CWindow.WIDTH, CWindow.HEIGHT)) + super ().__init__ (game, layer = layer) + self.bg = self._load_image ('assets/bg.jpg') + self.bg_evening = self._load_image ('assets/bg-evening.jpg') + self.bg_grass = self._load_image ('assets/bg-grass.png') + self.bg_night = self._load_image ('assets/bg-night.jpg') + self.sun = sun + + @staticmethod + def _load_image ( + path: str, + ) -> Surface: + return pygame.transform.scale (pygame.image.load (path), + (CWindow.WIDTH, CWindow.HEIGHT)) def redraw ( self, ) -> None: - self.game.screen.blit (self.surface, (self.x, self.y)) + date_tmp = self.game.sky.observer.date + self.game.sky.observer.date = self.game.now.date () + sunrise_start: datetime = ( + (ephem.localtime (self.game.sky.observer.previous_rising (self.sun)) + - timedelta (minutes = 30))) + sunrise_end: datetime = sunrise_start + timedelta (hours = 1) + sunrise_centre: datetime = ( + sunrise_start + (sunrise_end - sunrise_start) / 2) + sunset_start: datetime = ( + (ephem.localtime (self.game.sky.observer.next_setting (self.sun)) + - timedelta (minutes = 30))) + sunset_end: datetime = sunset_start + timedelta (hours = 1) + sunset_centre: datetime = ( + sunset_start + (sunset_end - sunset_start) / 2) + self.game.sky.observer.date = date_tmp + surface: Surface = ((self.bg + if (sunrise_centre <= self.game.now < sunset_centre) + else self.bg_night) + .copy ()) + if sunrise_start <= self.game.now < sunrise_end: + self.bg_evening.set_alpha (255 - int ((abs (self.game.now - sunrise_centre) * 510) + / (sunrise_end - sunrise_centre))) + elif sunset_start <= self.game.now < sunset_end: + self.bg_evening.set_alpha (255 - int ((abs (self.game.now - sunset_centre) * 510) + / (sunset_end - sunset_centre))) + else: + self.bg_evening.set_alpha (0) + surface.blit (self.bg_evening, (0, 0)) + self.game.screen.blit (surface, (self.x, self.y)) super ().redraw ()