component.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.bg_color_normal = pygame.Color(bg_color)
  14. # # 计算 hover 颜色:加深 20%
  15. # h = self.bg_color_normal
  16. # 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)))
  17. self.bg_color_normal.a = bg_alpha
  18. h = self.bg_color_normal
  19. hover_rgb = (
  20. max(0, int(h.r * 0.8)),
  21. max(0, int(h.g * 0.8)),
  22. max(0, int(h.b * 0.8)),
  23. )
  24. self.bg_color_hover = pygame.Color(*hover_rgb)
  25. self.bg_color_hover.a = bg_alpha
  26. self.fg_color = fg_color
  27. self.action = action
  28. self.relx = relx
  29. self.rely = rely
  30. self.fix_shape = fix_shape
  31. self.anchor = anchor # 'center', 'topleft'
  32. self.hovered = False
  33. self._render()
  34. def _render(self):
  35. # 渲染多行文本
  36. self.text_surfs = [self.font.render(line, True, self.fg_color) for line in self.lines]
  37. widths = [surf.get_width() for surf in self.text_surfs]
  38. heights = [surf.get_height() for surf in self.text_surfs]
  39. if self.fix_shape is None:
  40. total_width = max(widths)
  41. total_height = sum(heights) + (len(heights)-1) * 5
  42. else:
  43. total_width = self.fix_shape[0]
  44. total_height = self.fix_shape[1]
  45. sw, sh = self.screen.get_size()
  46. x = int(sw * self.relx)
  47. y = int(sh * self.rely)
  48. # 根据锚点决定位置
  49. if self.anchor == 'center':
  50. topleft = (x - total_width//2 - 10, y - total_height//2 - 5)
  51. elif self.anchor == 'topleft':
  52. topleft = (x, y)
  53. else:
  54. topleft = (x - total_width//2 - 10, y - total_height//2 - 5)
  55. self.bg_rect = pygame.Rect(topleft[0], topleft[1], total_width + 20, total_height + 10)
  56. # 计算每行文本位置
  57. self.text_positions = []
  58. offset_y = self.bg_rect.y + 5
  59. for surf in self.text_surfs:
  60. line_x = self.bg_rect.x + (self.bg_rect.width - surf.get_width())//2 if self.anchor=='center' else self.bg_rect.x + 10
  61. self.text_positions.append((line_x, offset_y))
  62. offset_y += surf.get_height() + 5
  63. def draw(self):
  64. # 绘制背景
  65. # color = self.bg_color_hover if self.hovered else self.bg_color_normal
  66. # pygame.draw.rect(self.screen, color, self.bg_rect)
  67. color = self.bg_color_hover if self.hovered else self.bg_color_normal
  68. bg_surf = pygame.Surface(
  69. (self.bg_rect.width, self.bg_rect.height),
  70. pygame.SRCALPHA # 允许 per-pixel alpha
  71. )
  72. bg_surf.fill(color) # color 中已包含 a 通道
  73. self.screen.blit(bg_surf, self.bg_rect.topleft)
  74. # 绘制文本
  75. for surf, pos in zip(self.text_surfs, self.text_positions):
  76. self.screen.blit(surf, pos)
  77. def handle_event(self, event):
  78. if event.type == pygame.MOUSEMOTION:
  79. self.hovered = self.bg_rect.collidepoint(event.pos)
  80. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  81. if self.hovered:
  82. if self.action:
  83. self.action()
  84. sound_player.play("uiLeave")
  85. class PygameButtonBackIMG:
  86. """
  87. 一个仅使用图片作为按钮的类,不含文字渲染,图片本身包含文字。
  88. - default_img_path: 默认状态下的背景图路径
  89. - hover_img_path: 悬停状态下的背景图路径
  90. """
  91. def __init__(self, screen, relx, rely,
  92. default_img_path, hover_img_path,
  93. action=None, anchor='center'):
  94. self.screen = screen
  95. # 加载按钮图片
  96. self.default_img = pygame.image.load(default_img_path).convert_alpha()
  97. self.hover_img = pygame.image.load(hover_img_path).convert_alpha()
  98. self.action = action
  99. self.relx = relx
  100. self.rely = rely
  101. self.anchor = anchor # 'center' 或 'topleft'
  102. self.hovered = False
  103. # 按钮尺寸使用图片原始尺寸
  104. self.width, self.height = self.default_img.get_size()
  105. # 计算按钮位置
  106. sw, sh = self.screen.get_size()
  107. x = int(sw * self.relx)
  108. y = int(sh * self.rely)
  109. if self.anchor == 'center':
  110. topleft = (x - self.width//2, y - self.height//2)
  111. else:
  112. topleft = (x, y)
  113. self.bg_rect = pygame.Rect(topleft[0], topleft[1], self.width, self.height)
  114. def draw(self):
  115. # 根据悬停状态绘制对应图片
  116. img = self.hover_img if self.hovered else self.default_img
  117. self.screen.blit(img, self.bg_rect.topleft)
  118. def handle_event(self, event):
  119. if event.type == pygame.MOUSEMOTION:
  120. self.hovered = self.bg_rect.collidepoint(event.pos)
  121. elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  122. if self.hovered and self.action:
  123. sound_player.play("uiLeave")
  124. self.action()
  125. class Particle:
  126. def __init__(self, x, y):
  127. self.x = x
  128. self.y = y
  129. self.radius = random.randint(1, 3)
  130. # self.color = (200, 200, 200)
  131. self.color = (170, 188, 157)
  132. # 垂直速度
  133. self.speed_y = random.uniform(0.3, 0.6)
  134. # 水平速度,模拟风吹
  135. self.speed_x = random.uniform(-0.3, 0.3)
  136. # 透明度和生命周期
  137. self.alpha = 191
  138. self.life = random.randint(100, 600) # 寿命更长
  139. self.surface = pygame.Surface((self.radius*2, self.radius*2), pygame.SRCALPHA)
  140. def update(self):
  141. # 更新位置
  142. self.y -= self.speed_y
  143. self.x += self.speed_x
  144. self.speed_y += random.uniform(0, 0.01)
  145. self.speed_x += random.uniform(-0.06, 0.06)
  146. # 减缓透明度下降速度
  147. self.life -= 1
  148. self.alpha = max(0, int(191 * min(1., self.life / 300)))
  149. # 重绘粒子
  150. self.surface.fill((0,0,0,0))
  151. pygame.draw.circle(
  152. self.surface,
  153. (self.color[0], self.color[1], self.color[2], self.alpha),
  154. (self.radius, self.radius),
  155. self.radius
  156. )
  157. def draw(self, screen):
  158. screen.blit(self.surface, (self.x - self.radius, self.y - self.radius))
  159. def is_alive(self):
  160. return self.life > 0 and self.alpha > 0