component.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import pygame
  2. import random
  3. from game.sound import sound_player
  4. class PygameButton:
  5. """
  6. 文字渲染按钮
  7. """
  8. def __init__(self, screen, text, relx, rely, font_size, bg_color, fg_color=(0,0,0), action=None, anchor='center', is_bold=True, fix_shape=None, bg_alpha=255):
  9. self.screen = screen
  10. self.text = text
  11. self.lines = text.split('\n')
  12. # self.font = pygame.font.SysFont("Microsoft JhengHei", font_size, bold=is_bold)
  13. # self.font = pygame.font.Font(r"res\font\SourceHanSerif-VF.ttc", font_size)
  14. self.font = pygame.font.Font(r"res\font\SourceHanSerifCN-Bold.otf", font_size)
  15. self.bg_color_normal = pygame.Color(bg_color)
  16. # # 计算 hover 颜色:加深 20%
  17. # h = self.bg_color_normal
  18. # self.bg_color_hover = pygame.Color(max(0, int(h.r*0.8)), max(0, int(h.g*0.8)), max(0, int(h.b*0.8)))
  19. self.bg_color_normal.a = bg_alpha
  20. h = self.bg_color_normal
  21. hover_rgb = (
  22. max(0, int(h.r * 0.8)),
  23. max(0, int(h.g * 0.8)),
  24. max(0, int(h.b * 0.8)),
  25. )
  26. self.bg_color_hover = pygame.Color(*hover_rgb)
  27. self.bg_color_hover.a = bg_alpha
  28. self.fg_color = fg_color
  29. self.action = action
  30. self.relx = relx
  31. self.rely = rely
  32. self.fix_shape = fix_shape
  33. self.anchor = anchor # 'center', 'topleft'
  34. self.hovered = False
  35. self._render()
  36. def _render(self):
  37. # 渲染多行文本
  38. self.text_surfs = [self.font.render(line, True, self.fg_color) for line in self.lines]
  39. widths = [surf.get_width() for surf in self.text_surfs]
  40. heights = [surf.get_height() for surf in self.text_surfs]
  41. if self.fix_shape is None:
  42. total_width = max(widths)
  43. total_height = sum(heights) + (len(heights)-1) * 5
  44. else:
  45. total_width = self.fix_shape[0]
  46. total_height = self.fix_shape[1]
  47. sw, sh = self.screen.get_size()
  48. x = int(sw * self.relx)
  49. y = int(sh * self.rely)
  50. # 根据锚点决定位置
  51. if self.anchor == 'center':
  52. topleft = (x - total_width//2 - 10, y - total_height//2 - 5)
  53. elif self.anchor == 'topleft':
  54. topleft = (x, y)
  55. else:
  56. topleft = (x - total_width//2 - 10, y - total_height//2 - 5)
  57. self.bg_rect = pygame.Rect(topleft[0], topleft[1], total_width + 20, total_height + 10)
  58. # 计算每行文本位置
  59. self.text_positions = []
  60. offset_y = self.bg_rect.y + 5
  61. for surf in self.text_surfs:
  62. line_x = self.bg_rect.x + (self.bg_rect.width - surf.get_width())//2 if self.anchor=='center' else self.bg_rect.x + 10
  63. self.text_positions.append((line_x, offset_y))
  64. offset_y += surf.get_height() + 5
  65. def draw(self):
  66. # 绘制背景
  67. # color = self.bg_color_hover if self.hovered else self.bg_color_normal
  68. # pygame.draw.rect(self.screen, color, self.bg_rect)
  69. color = self.bg_color_hover if self.hovered else self.bg_color_normal
  70. bg_surf = pygame.Surface(
  71. (self.bg_rect.width, self.bg_rect.height),
  72. pygame.SRCALPHA # 允许 per-pixel alpha
  73. )
  74. bg_surf.fill(color) # color 中已包含 a 通道
  75. self.screen.blit(bg_surf, self.bg_rect.topleft)
  76. # 绘制文本
  77. for surf, pos in zip(self.text_surfs, self.text_positions):
  78. self.screen.blit(surf, pos)
  79. def handle_event(self, event):
  80. if event.type == pygame.MOUSEMOTION:
  81. self.hovered = self.bg_rect.collidepoint(event.pos)
  82. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  83. if self.hovered:
  84. if self.action:
  85. self.action()
  86. sound_player.play("uiLeave")
  87. class PygameButtonBackIMG:
  88. """
  89. 一个仅使用图片作为按钮的类,不含文字渲染,图片本身包含文字。
  90. - default_img_path: 默认状态下的背景图路径
  91. - hover_img_path: 悬停状态下的背景图路径
  92. """
  93. def __init__(self, menu, screen, relx, rely,
  94. default_img_path, hover_img_path,
  95. action=None, hover_in_action=None, hover_out_action=None,
  96. anchor='center'):
  97. self.menu = menu
  98. self.screen = screen
  99. # 加载按钮图片
  100. self.default_img = pygame.image.load(default_img_path).convert_alpha()
  101. self.hover_img = pygame.image.load(hover_img_path).convert_alpha()
  102. self.action = action
  103. self.hover_in_action = hover_in_action
  104. self.hover_out_action = hover_out_action
  105. self.relx = relx
  106. self.rely = rely
  107. self.anchor = anchor # 'center' 或 'topleft'
  108. self.hovered = False
  109. # 按钮尺寸使用图片原始尺寸
  110. self.width, self.height = self.default_img.get_size()
  111. self.spawn_timer = 0
  112. # 计算按钮位置
  113. sw, sh = self.screen.get_size()
  114. x = int(sw * self.relx)
  115. y = int(sh * self.rely)
  116. self.x, self.y = x, y
  117. if self.anchor == 'center':
  118. topleft = (x - self.width//2, y - self.height//2)
  119. else:
  120. topleft = (x, y)
  121. self.bg_rect = pygame.Rect(topleft[0], topleft[1], self.width, self.height)
  122. def draw(self):
  123. # (实时刷新) 根据悬停状态绘制对应图片
  124. img = self.hover_img if self.hovered else self.default_img
  125. self.screen.blit(img, self.bg_rect.topleft)
  126. if self.hovered:
  127. self.spawn_timer += 1
  128. if self.spawn_timer >= 10:
  129. self.spawn_timer = 0
  130. for _ in range(10):
  131. self.menu.particles.append(ButtonParticle(self.x, self.y))
  132. def handle_event(self, event):
  133. if event.type == pygame.MOUSEMOTION:
  134. # 先记录上一次的悬停状态
  135. was_hovered = self.hovered
  136. # 更新当前悬停状态
  137. now_hovered = self.bg_rect.collidepoint(event.pos)
  138. self.hovered = now_hovered
  139. # 鼠标刚进入区域
  140. if not was_hovered and now_hovered and self.hover_in_action:
  141. self.hover_in_action()
  142. # 鼠标刚离开区域
  143. elif was_hovered and not now_hovered and self.hover_out_action:
  144. self.hover_out_action()
  145. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  146. if self.hovered and self.action:
  147. sound_player.play("uiLeave")
  148. self.action()
  149. class Particle:
  150. def __init__(self, x, y):
  151. self.x = x
  152. self.y = y
  153. self.radius = random.randint(1, 3)
  154. # self.color = (200, 200, 200)
  155. self.color = (170, 188, 157)
  156. # 垂直速度
  157. self.speed_y = random.uniform(0.3, 0.6)
  158. # 水平速度,模拟风吹
  159. self.speed_x = random.uniform(-0.3, 0.3)
  160. # 透明度和生命周期
  161. self.alpha = 191
  162. self.life = random.randint(100, 600) # 寿命更长
  163. self.surface = pygame.Surface((self.radius*2, self.radius*2), pygame.SRCALPHA)
  164. def update(self):
  165. # 更新位置
  166. self.y -= self.speed_y
  167. self.x += self.speed_x
  168. self.speed_y += random.uniform(0, 0.01)
  169. self.speed_x += random.uniform(-0.06, 0.06)
  170. # 减缓透明度下降速度
  171. self.life -= 1
  172. self.alpha = max(0, int(191 * min(1., self.life / 300)))
  173. # 重绘粒子
  174. self.surface.fill((0,0,0,0))
  175. pygame.draw.circle(
  176. self.surface,
  177. (self.color[0], self.color[1], self.color[2], self.alpha),
  178. (self.radius, self.radius),
  179. self.radius
  180. )
  181. def draw(self, screen):
  182. screen.blit(self.surface, (self.x - self.radius, self.y - self.radius))
  183. def is_alive(self):
  184. return self.life > 0 and self.alpha > 0
  185. class ButtonParticle:
  186. def __init__(self, x, y):
  187. self.x = x
  188. self.y = y
  189. self.radius = random.randint(1, 3)
  190. # self.color = (200, 200, 200)
  191. # self.color = (170, 188, 157)
  192. self.color = (244, 213, 97)
  193. # 垂直速度
  194. self.speed_y = random.uniform(-5, 5)
  195. # 水平速度,模拟风吹
  196. self.speed_x = random.uniform(-10, 10)
  197. # 透明度和生命周期
  198. self.alpha = 255
  199. self.fix_life = 30
  200. self.life = random.randint(10, 20) # 寿命更长
  201. self.surface = pygame.Surface((self.radius*2, self.radius*2), pygame.SRCALPHA)
  202. def update(self):
  203. # 更新位置
  204. self.y -= self.speed_y
  205. self.x += self.speed_x
  206. self.speed_y *= 0.9
  207. self.speed_x *= 0.9
  208. # 减缓透明度下降速度
  209. self.fix_life -= 1
  210. if self.fix_life < 0:
  211. self.life -= 1
  212. self.alpha = max(0, int(191 * min(1., self.life / 30)))
  213. # 重绘粒子
  214. self.surface.fill((0,0,0,0))
  215. pygame.draw.circle(
  216. self.surface,
  217. (self.color[0], self.color[1], self.color[2], self.alpha),
  218. (self.radius, self.radius),
  219. self.radius
  220. )
  221. def draw(self, screen):
  222. screen.blit(self.surface, (self.x - self.radius, self.y - self.radius))
  223. def is_alive(self):
  224. return self.life > 0 and self.alpha > 0