| @@ -1,6 +1,7 @@ | |||
| from __future__ import annotations | |||
| import sys | |||
| from datetime import datetime | |||
| from enum import Enum, auto | |||
| from typing import Callable, TypedDict | |||
| @@ -8,17 +9,26 @@ import cv2 | |||
| import pygame | |||
| from cv2 import VideoCapture | |||
| from pygame import Surface | |||
| from pygame.font import Font | |||
| from pygame.mixer import Sound | |||
| from pygame.time import Clock | |||
| pygame.init () | |||
| FPS = 24 | |||
| FPS = 30 | |||
| SYSTEM_FONT = pygame.font.SysFont ('notosanscjkjp', 24, bold = True) | |||
| USER_FONT = pygame.font.SysFont ('notosanscjkjp', 32, italic = True) | |||
| DEERJIKA_FONT = pygame.font.SysFont ('07nikumarufont', 50) | |||
| def main ( | |||
| ) -> None: | |||
| pygame.init () | |||
| game = Game () | |||
| bg = Bg (game) | |||
| deerjika = Deerjika (game, DeerjikaPattern.RELAXED) | |||
| deerjika = Deerjika (game, DeerjikaPattern.RELAXED, | |||
| x = CWindow.WIDTH * 3 / 4, y = CWindow.HEIGHT - 120) | |||
| CurrentTime (game, SYSTEM_FONT) | |||
| Sound ('assets/bgm.mp3').play (loops = -1) | |||
| while True: | |||
| for event in pygame.event.get (): | |||
| if event.type == pygame.QUIT: | |||
| @@ -56,7 +66,8 @@ class Game: | |||
| self, | |||
| ) -> None: | |||
| for redrawer in sorted (self.redrawers, key = lambda x: x['layer']): | |||
| redrawer['func'] () | |||
| if redrawer['obj'].enabled: | |||
| redrawer['obj'].redraw () | |||
| pygame.display.update () | |||
| self.clock.tick (FPS) | |||
| @@ -73,22 +84,25 @@ class GameObject: | |||
| ax: float = 0 | |||
| ay: float = 0 | |||
| arg: float = 0 | |||
| enabled: bool = True | |||
| def __init__ ( | |||
| self, | |||
| game: Game, | |||
| layer: int | None = None, | |||
| x: float = 0, | |||
| y: float = 0, | |||
| game: Game, | |||
| layer: int | 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 | |||
| else: | |||
| layer = 0 | |||
| self.game.redrawers.append ({ 'layer': layer, 'func': self.redraw }) | |||
| self.game.redrawers.append ({ 'layer': layer, 'obj': self }) | |||
| self.x = x | |||
| self.y = y | |||
| @@ -123,6 +137,9 @@ class Bg (GameObject): | |||
| class Deerjika (GameObject): | |||
| surfaces: list[Surface] | |||
| size: int | |||
| width: int | |||
| height: int | |||
| scale: float = .8 | |||
| def __init__ ( | |||
| self, | |||
| @@ -133,7 +150,7 @@ class Deerjika (GameObject): | |||
| x: float = 0, | |||
| y: float = 0, | |||
| ): | |||
| super ().__init__ (game, layer, x, y) | |||
| super ().__init__ (game, layer, x = x, y = y) | |||
| self.pattern = pattern | |||
| self.direction = direction | |||
| match pattern: | |||
| @@ -142,7 +159,13 @@ class Deerjika (GameObject): | |||
| case DeerjikaPattern.RELAXED: | |||
| match direction: | |||
| case Direction.LEFT: | |||
| self.surfaces = get_surfaces_from_video ('assets/deerjika_relax.mp4') | |||
| self.width = 1280 | |||
| self.height = 720 | |||
| surface = pygame.image.load ('assets/deerjika_relax_left.png') | |||
| self.surfaces = [] | |||
| for x in range (0, surface.get_width (), self.width): | |||
| self.surfaces.append ( | |||
| surface.subsurface (x, 0, self.width, self.height)) | |||
| case Direction.RIGHT: | |||
| ... | |||
| self.size = len (self.surfaces) | |||
| @@ -150,11 +173,32 @@ class Deerjika (GameObject): | |||
| def redraw ( | |||
| self, | |||
| ) -> None: | |||
| surface: Surface = self.surfaces[self.frame % self.size] | |||
| surface = pygame.transform.scale (self.surfaces[self.frame % self.size], | |||
| (self.width * self.scale, self.height * self.scale)) | |||
| self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y))) | |||
| super ().redraw () | |||
| class CurrentTime (GameObject): | |||
| font: Font | |||
| def __init__ ( | |||
| self, | |||
| game: Game, | |||
| font: Font, | |||
| ): | |||
| super ().__init__ (game) | |||
| self.font = font | |||
| def redraw ( | |||
| self, | |||
| ) -> None: | |||
| for i in range (4): | |||
| self.game.screen.blit ( | |||
| self.font.render (str (datetime.now ()), True, (0, 0, 0)), | |||
| (i % 2, i // 2 * 2)) | |||
| class CWindow: | |||
| WIDTH = 1024 | |||
| HEIGHT = 768 | |||
| @@ -162,7 +206,7 @@ class CWindow: | |||
| class Redrawer (TypedDict): | |||
| layer: int | |||
| func: Callable[[], None] | |||
| obj: GameObject | |||
| def get_surfaces_from_video ( | |||
| @@ -189,14 +233,5 @@ def get_surfaces_from_video ( | |||
| return surfaces | |||
| def set_chroma_key ( | |||
| surface: Surface, | |||
| color: tuple[int, int, int], | |||
| tolerance: int, | |||
| ) -> Surface: | |||
| # ChatGPT o kwakunin suru kutu | |||
| ... | |||
| if __name__ == '__main__': | |||
| main () | |||