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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. from __future__ import annotations
  2. import sys
  3. from datetime import datetime
  4. from enum import Enum, auto
  5. from typing import Callable, TypedDict
  6. import cv2
  7. import pygame
  8. from cv2 import VideoCapture
  9. from pygame import Surface
  10. from pygame.font import Font
  11. from pygame.mixer import Sound
  12. from pygame.time import Clock
  13. pygame.init ()
  14. FPS = 30
  15. SYSTEM_FONT = pygame.font.SysFont ('notosanscjkjp', 24, bold = True)
  16. USER_FONT = pygame.font.SysFont ('notosanscjkjp', 32, italic = True)
  17. DEERJIKA_FONT = pygame.font.SysFont ('07nikumarufont', 50)
  18. def main (
  19. ) -> None:
  20. game = Game ()
  21. bg = Bg (game)
  22. deerjika = Deerjika (game, DeerjikaPattern.RELAXED,
  23. x = CWindow.WIDTH * 3 / 4, y = CWindow.HEIGHT - 120)
  24. CurrentTime (game, SYSTEM_FONT)
  25. Sound ('assets/bgm.mp3').play (loops = -1)
  26. while True:
  27. for event in pygame.event.get ():
  28. if event.type == pygame.QUIT:
  29. pygame.quit ()
  30. sys.exit ()
  31. game.redraw ()
  32. class DeerjikaPattern (Enum):
  33. NORMAL = auto ()
  34. RELAXED = auto ()
  35. SLEEPING = auto ()
  36. class Direction (Enum):
  37. LEFT = auto ()
  38. RIGHT = auto ()
  39. class Game:
  40. clock: Clock
  41. frame: int
  42. redrawers: list[Redrawer]
  43. screen: Surface
  44. def __init__ (
  45. self,
  46. ):
  47. self.screen = pygame.display.set_mode ((CWindow.WIDTH, CWindow.HEIGHT))
  48. self.clock = Clock ()
  49. self.frame = 0
  50. self.redrawers = []
  51. def redraw (
  52. self,
  53. ) -> None:
  54. for redrawer in sorted (self.redrawers, key = lambda x: x['layer']):
  55. if redrawer['obj'].enabled:
  56. redrawer['obj'].redraw ()
  57. pygame.display.update ()
  58. self.clock.tick (FPS)
  59. class GameObject:
  60. frame: int
  61. game: Game
  62. width: int
  63. height: int
  64. x: float
  65. y: float
  66. vx: float = 0
  67. vy: float = 0
  68. ax: float = 0
  69. ay: float = 0
  70. arg: float = 0
  71. enabled: bool = True
  72. def __init__ (
  73. self,
  74. game: Game,
  75. layer: int | None = None,
  76. enabled: bool = True,
  77. x: float = 0,
  78. y: float = 0,
  79. ):
  80. self.game = game
  81. self.enabled = enabled
  82. self.frame = 0
  83. if layer is None:
  84. if self.game.redrawers:
  85. layer = max (r['layer'] for r in self.game.redrawers) + 10
  86. else:
  87. layer = 0
  88. self.game.redrawers.append ({ 'layer': layer, 'obj': self })
  89. self.x = x
  90. self.y = y
  91. def redraw (
  92. self,
  93. ) -> None:
  94. self.x += self.vx
  95. self.y += self.vy
  96. self.vx += self.ax
  97. self.vy += self.ay
  98. self.frame += 1
  99. class Bg (GameObject):
  100. surface: Surface
  101. def __init__ (
  102. self,
  103. game: Game,
  104. ):
  105. super ().__init__ (game)
  106. self.surface = pygame.image.load ('assets/bg.jpg')
  107. self.surface = pygame.transform.scale (self.surface, (CWindow.WIDTH, CWindow.HEIGHT))
  108. def redraw (
  109. self,
  110. ) -> None:
  111. self.game.screen.blit (self.surface, (self.x, self.y))
  112. super ().redraw ()
  113. class Deerjika (GameObject):
  114. surfaces: list[Surface]
  115. size: int
  116. width: int
  117. height: int
  118. scale: float = .8
  119. def __init__ (
  120. self,
  121. game: Game,
  122. pattern: DeerjikaPattern = DeerjikaPattern.NORMAL,
  123. direction: Direction = Direction.LEFT,
  124. layer: int | None = None,
  125. x: float = 0,
  126. y: float = 0,
  127. ):
  128. super ().__init__ (game, layer, x = x, y = y)
  129. self.pattern = pattern
  130. self.direction = direction
  131. match pattern:
  132. case DeerjikaPattern.NORMAL:
  133. ...
  134. case DeerjikaPattern.RELAXED:
  135. match direction:
  136. case Direction.LEFT:
  137. self.width = 1280
  138. self.height = 720
  139. surface = pygame.image.load ('assets/deerjika_relax_left.png')
  140. self.surfaces = []
  141. for x in range (0, surface.get_width (), self.width):
  142. self.surfaces.append (
  143. surface.subsurface (x, 0, self.width, self.height))
  144. case Direction.RIGHT:
  145. ...
  146. self.size = len (self.surfaces)
  147. def redraw (
  148. self,
  149. ) -> None:
  150. surface = pygame.transform.scale (self.surfaces[self.frame % self.size],
  151. (self.width * self.scale, self.height * self.scale))
  152. self.game.screen.blit (surface, surface.get_rect (center = (self.x, self.y)))
  153. super ().redraw ()
  154. class CurrentTime (GameObject):
  155. font: Font
  156. def __init__ (
  157. self,
  158. game: Game,
  159. font: Font,
  160. ):
  161. super ().__init__ (game)
  162. self.font = font
  163. def redraw (
  164. self,
  165. ) -> None:
  166. for i in range (4):
  167. self.game.screen.blit (
  168. self.font.render (str (datetime.now ()), True, (0, 0, 0)),
  169. (i % 2, i // 2 * 2))
  170. class Balloon (GameObject):
  171. query: str
  172. answer: str
  173. class CWindow:
  174. WIDTH = 1024
  175. HEIGHT = 768
  176. class Redrawer (TypedDict):
  177. layer: int
  178. obj: GameObject
  179. def get_surfaces_from_video (
  180. video_path: str,
  181. ) -> list[Surface]:
  182. cap = VideoCapture (video_path)
  183. if not cap.isOpened ():
  184. return []
  185. fps = cap.get (cv2.CAP_PROP_FPS)
  186. surfaces: list[Surface] = []
  187. while cap.isOpened ():
  188. (ret, frame) = cap.read ()
  189. if not ret:
  190. break
  191. frame = cv2.cvtColor (frame, cv2.COLOR_BGR2RGB)
  192. frame_surface = pygame.surfarray.make_surface (frame)
  193. frame_surface = pygame.transform.rotate (frame_surface, -90)
  194. surfaces.append (pygame.transform.flip (frame_surface, True, False))
  195. cap.release ()
  196. return surfaces
  197. if __name__ == '__main__':
  198. main ()