伊地知ニジカ放送局だぬ゛ん゛. https://www.youtube.com/@deerjika
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.py 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. from __future__ import annotations
  2. import sys
  3. from enum import Enum, auto
  4. from typing import Callable, TypedDict
  5. import cv2
  6. import pygame
  7. from cv2 import VideoCapture
  8. from pygame import Surface
  9. from pygame.time import Clock
  10. FPS = 24
  11. def main (
  12. ) -> None:
  13. pygame.init ()
  14. game = Game ()
  15. bg = Bg (game)
  16. deerjika = Deerjika (game, DeerjikaPattern.RELAXED)
  17. while True:
  18. for event in pygame.event.get ():
  19. if event.type == pygame.QUIT:
  20. pygame.quit ()
  21. sys.exit ()
  22. game.redraw ()
  23. class DeerjikaPattern (Enum):
  24. NORMAL = auto ()
  25. RELAXED = auto ()
  26. SLEEPING = auto ()
  27. class Direction (Enum):
  28. LEFT = auto ()
  29. RIGHT = auto ()
  30. class Game:
  31. clock: Clock
  32. frame: int
  33. redrawers: list[Redrawer]
  34. screen: Surface
  35. def __init__ (
  36. self,
  37. ):
  38. self.screen = pygame.display.set_mode ((CWindow.WIDTH, CWindow.HEIGHT))
  39. self.clock = Clock ()
  40. self.frame = 0
  41. self.redrawers = []
  42. def redraw (
  43. self,
  44. ) -> None:
  45. for redrawer in sorted (self.redrawers, key = lambda x: x['layer']):
  46. redrawer['func'] ()
  47. pygame.display.update ()
  48. self.clock.tick (FPS)
  49. class GameObject:
  50. frame: int
  51. game: Game
  52. width: int
  53. height: int
  54. x: float
  55. y: float
  56. vx: float = 0
  57. vy: float = 0
  58. ax: float = 0
  59. ay: float = 0
  60. arg: float = 0
  61. def __init__ (
  62. self,
  63. game: Game,
  64. layer: int | None = None,
  65. x: float = 0,
  66. y: float = 0,
  67. ):
  68. self.game = game
  69. self.frame = 0
  70. if layer is None:
  71. if self.game.redrawers:
  72. layer = max (r['layer'] for r in self.game.redrawers) + 10
  73. else:
  74. layer = 0
  75. self.game.redrawers.append ({ 'layer': layer, 'func': self.redraw })
  76. self.x = x
  77. self.y = y
  78. def redraw (
  79. self,
  80. ) -> None:
  81. self.x += self.vx
  82. self.y += self.vy
  83. self.vx += self.ax
  84. self.vy += self.ay
  85. self.frame += 1
  86. class Bg (GameObject):
  87. surface: Surface
  88. def __init__ (
  89. self,
  90. game: Game,
  91. ):
  92. super ().__init__ (game)
  93. self.surface = pygame.image.load ('assets/bg.jpg')
  94. self.surface = pygame.transform.scale (self.surface, (CWindow.WIDTH, CWindow.HEIGHT))
  95. def redraw (
  96. self,
  97. ) -> None:
  98. self.game.screen.blit (self.surface, (self.x, self.y))
  99. super ().redraw ()
  100. class Deerjika (GameObject):
  101. surfaces: list[Surface]
  102. size: int
  103. def __init__ (
  104. self,
  105. game: Game,
  106. pattern: DeerjikaPattern = DeerjikaPattern.NORMAL,
  107. direction: Direction = Direction.LEFT,
  108. layer: int | None = None,
  109. x: float = 0,
  110. y: float = 0,
  111. ):
  112. super ().__init__ (game, layer, x, y)
  113. self.pattern = pattern
  114. self.direction = direction
  115. match pattern:
  116. case DeerjikaPattern.NORMAL:
  117. ...
  118. case DeerjikaPattern.RELAXED:
  119. match direction:
  120. case Direction.LEFT:
  121. self.surfaces = get_surfaces_from_video ('assets/deerjika_relax.mp4')
  122. case Direction.RIGHT:
  123. ...
  124. self.size = len (self.surfaces)
  125. def redraw (
  126. self,
  127. ) -> None:
  128. surface: Surface = self.surfaces[self.frame % self.size]
  129. self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y)))
  130. super ().redraw ()
  131. class CWindow:
  132. WIDTH = 1024
  133. HEIGHT = 768
  134. class Redrawer (TypedDict):
  135. layer: int
  136. func: Callable[[], None]
  137. def get_surfaces_from_video (
  138. video_path: str,
  139. ) -> list[Surface]:
  140. cap = VideoCapture (video_path)
  141. if not cap.isOpened ():
  142. return []
  143. fps = cap.get (cv2.CAP_PROP_FPS)
  144. surfaces: list[Surface] = []
  145. while cap.isOpened ():
  146. (ret, frame) = cap.read ()
  147. if not ret:
  148. break
  149. frame = cv2.cvtColor (frame, cv2.COLOR_BGR2RGB)
  150. frame_surface = pygame.surfarray.make_surface (frame)
  151. frame_surface = pygame.transform.rotate (frame_surface, -90)
  152. surfaces.append (pygame.transform.flip (frame_surface, True, False))
  153. cap.release ()
  154. return surfaces
  155. def set_chroma_key (
  156. surface: Surface,
  157. color: tuple[int, int, int],
  158. tolerance: int,
  159. ) -> Surface:
  160. # ChatGPT o kwakunin suru kutu
  161. ...
  162. if __name__ == '__main__':
  163. main ()