|
|
@@ -12,7 +12,9 @@ class PygameButton:
|
|
|
self.screen = screen
|
|
|
self.text = text
|
|
|
self.lines = text.split('\n')
|
|
|
- self.font = pygame.font.SysFont("Microsoft JhengHei", font_size, bold=is_bold)
|
|
|
+ # self.font = pygame.font.SysFont("Microsoft JhengHei", font_size, bold=is_bold)
|
|
|
+ # self.font = pygame.font.Font(r"res\font\SourceHanSerif-VF.ttc", font_size)
|
|
|
+ self.font = pygame.font.Font(r"res\font\SourceHanSerifCN-Bold.otf", font_size)
|
|
|
self.bg_color_normal = pygame.Color(bg_color)
|
|
|
# # 计算 hover 颜色:加深 20%
|
|
|
|
|
|
@@ -100,24 +102,30 @@ class PygameButtonBackIMG:
|
|
|
- default_img_path: 默认状态下的背景图路径
|
|
|
- hover_img_path: 悬停状态下的背景图路径
|
|
|
"""
|
|
|
- def __init__(self, screen, relx, rely,
|
|
|
+ def __init__(self, menu, screen, relx, rely,
|
|
|
default_img_path, hover_img_path,
|
|
|
- action=None, anchor='center'):
|
|
|
+ action=None, hover_in_action=None, hover_out_action=None,
|
|
|
+ anchor='center'):
|
|
|
+ self.menu = menu
|
|
|
self.screen = screen
|
|
|
# 加载按钮图片
|
|
|
self.default_img = pygame.image.load(default_img_path).convert_alpha()
|
|
|
self.hover_img = pygame.image.load(hover_img_path).convert_alpha()
|
|
|
self.action = action
|
|
|
+ self.hover_in_action = hover_in_action
|
|
|
+ self.hover_out_action = hover_out_action
|
|
|
self.relx = relx
|
|
|
self.rely = rely
|
|
|
self.anchor = anchor # 'center' 或 'topleft'
|
|
|
self.hovered = False
|
|
|
# 按钮尺寸使用图片原始尺寸
|
|
|
self.width, self.height = self.default_img.get_size()
|
|
|
+ self.spawn_timer = 0
|
|
|
# 计算按钮位置
|
|
|
sw, sh = self.screen.get_size()
|
|
|
x = int(sw * self.relx)
|
|
|
y = int(sh * self.rely)
|
|
|
+ self.x, self.y = x, y
|
|
|
if self.anchor == 'center':
|
|
|
topleft = (x - self.width//2, y - self.height//2)
|
|
|
else:
|
|
|
@@ -125,13 +133,29 @@ class PygameButtonBackIMG:
|
|
|
self.bg_rect = pygame.Rect(topleft[0], topleft[1], self.width, self.height)
|
|
|
|
|
|
def draw(self):
|
|
|
- # 根据悬停状态绘制对应图片
|
|
|
+ # (实时刷新) 根据悬停状态绘制对应图片
|
|
|
img = self.hover_img if self.hovered else self.default_img
|
|
|
self.screen.blit(img, self.bg_rect.topleft)
|
|
|
+ if self.hovered:
|
|
|
+ self.spawn_timer += 1
|
|
|
+ if self.spawn_timer >= 10:
|
|
|
+ self.spawn_timer = 0
|
|
|
+ for _ in range(10):
|
|
|
+ self.menu.particles.append(ButtonParticle(self.x, self.y))
|
|
|
|
|
|
def handle_event(self, event):
|
|
|
if event.type == pygame.MOUSEMOTION:
|
|
|
- self.hovered = self.bg_rect.collidepoint(event.pos)
|
|
|
+ # 先记录上一次的悬停状态
|
|
|
+ was_hovered = self.hovered
|
|
|
+ # 更新当前悬停状态
|
|
|
+ now_hovered = self.bg_rect.collidepoint(event.pos)
|
|
|
+ self.hovered = now_hovered
|
|
|
+ # 鼠标刚进入区域
|
|
|
+ if not was_hovered and now_hovered and self.hover_in_action:
|
|
|
+ self.hover_in_action()
|
|
|
+ # 鼠标刚离开区域
|
|
|
+ elif was_hovered and not now_hovered and self.hover_out_action:
|
|
|
+ self.hover_out_action()
|
|
|
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
|
if self.hovered and self.action:
|
|
|
sound_player.play("uiLeave")
|
|
|
@@ -177,3 +201,48 @@ class Particle:
|
|
|
|
|
|
def is_alive(self):
|
|
|
return self.life > 0 and self.alpha > 0
|
|
|
+
|
|
|
+
|
|
|
+class ButtonParticle:
|
|
|
+ def __init__(self, x, y):
|
|
|
+ self.x = x
|
|
|
+ self.y = y
|
|
|
+ self.radius = random.randint(1, 3)
|
|
|
+ # self.color = (200, 200, 200)
|
|
|
+ # self.color = (170, 188, 157)
|
|
|
+ self.color = (244, 213, 97)
|
|
|
+ # 垂直速度
|
|
|
+ self.speed_y = random.uniform(-5, 5)
|
|
|
+ # 水平速度,模拟风吹
|
|
|
+ self.speed_x = random.uniform(-10, 10)
|
|
|
+ # 透明度和生命周期
|
|
|
+ self.alpha = 255
|
|
|
+ self.fix_life = 30
|
|
|
+ self.life = random.randint(10, 20) # 寿命更长
|
|
|
+ self.surface = pygame.Surface((self.radius*2, self.radius*2), pygame.SRCALPHA)
|
|
|
+
|
|
|
+ def update(self):
|
|
|
+ # 更新位置
|
|
|
+ self.y -= self.speed_y
|
|
|
+ self.x += self.speed_x
|
|
|
+ self.speed_y *= 0.9
|
|
|
+ self.speed_x *= 0.9
|
|
|
+ # 减缓透明度下降速度
|
|
|
+ self.fix_life -= 1
|
|
|
+ if self.fix_life < 0:
|
|
|
+ self.life -= 1
|
|
|
+ self.alpha = max(0, int(191 * min(1., self.life / 30)))
|
|
|
+ # 重绘粒子
|
|
|
+ self.surface.fill((0,0,0,0))
|
|
|
+ pygame.draw.circle(
|
|
|
+ self.surface,
|
|
|
+ (self.color[0], self.color[1], self.color[2], self.alpha),
|
|
|
+ (self.radius, self.radius),
|
|
|
+ self.radius
|
|
|
+ )
|
|
|
+
|
|
|
+ def draw(self, screen):
|
|
|
+ screen.blit(self.surface, (self.x - self.radius, self.y - self.radius))
|
|
|
+
|
|
|
+ def is_alive(self):
|
|
|
+ return self.life > 0 and self.alpha > 0
|