伊地知ニジカ放送局だぬ゛ん゛. 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.6 KiB

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