| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- import pygame
- import random
- from game.sound import sound_player
- class PygameButton:
- """
- 文字渲染按钮
- """
- 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):
- self.screen = screen
- self.text = text
- self.lines = text.split('\n')
- self.font = pygame.font.SysFont("Microsoft JhengHei", font_size, bold=is_bold)
- self.bg_color_normal = pygame.Color(bg_color)
- # # 计算 hover 颜色:加深 20%
- # h = self.bg_color_normal
- # 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)))
- self.bg_color_normal.a = bg_alpha
- h = self.bg_color_normal
- hover_rgb = (
- max(0, int(h.r * 0.8)),
- max(0, int(h.g * 0.8)),
- max(0, int(h.b * 0.8)),
- )
- self.bg_color_hover = pygame.Color(*hover_rgb)
- self.bg_color_hover.a = bg_alpha
- self.fg_color = fg_color
- self.action = action
- self.relx = relx
- self.rely = rely
- self.fix_shape = fix_shape
- self.anchor = anchor # 'center', 'topleft'
- self.hovered = False
- self._render()
- def _render(self):
- # 渲染多行文本
- self.text_surfs = [self.font.render(line, True, self.fg_color) for line in self.lines]
- widths = [surf.get_width() for surf in self.text_surfs]
- heights = [surf.get_height() for surf in self.text_surfs]
- if self.fix_shape is None:
- total_width = max(widths)
- total_height = sum(heights) + (len(heights)-1) * 5
- else:
- total_width = self.fix_shape[0]
- total_height = self.fix_shape[1]
- sw, sh = self.screen.get_size()
- x = int(sw * self.relx)
- y = int(sh * self.rely)
- # 根据锚点决定位置
- if self.anchor == 'center':
- topleft = (x - total_width//2 - 10, y - total_height//2 - 5)
- elif self.anchor == 'topleft':
- topleft = (x, y)
- else:
- topleft = (x - total_width//2 - 10, y - total_height//2 - 5)
- self.bg_rect = pygame.Rect(topleft[0], topleft[1], total_width + 20, total_height + 10)
- # 计算每行文本位置
- self.text_positions = []
- offset_y = self.bg_rect.y + 5
- for surf in self.text_surfs:
- line_x = self.bg_rect.x + (self.bg_rect.width - surf.get_width())//2 if self.anchor=='center' else self.bg_rect.x + 10
- self.text_positions.append((line_x, offset_y))
- offset_y += surf.get_height() + 5
- def draw(self):
- # 绘制背景
- # color = self.bg_color_hover if self.hovered else self.bg_color_normal
- # pygame.draw.rect(self.screen, color, self.bg_rect)
- color = self.bg_color_hover if self.hovered else self.bg_color_normal
- bg_surf = pygame.Surface(
- (self.bg_rect.width, self.bg_rect.height),
- pygame.SRCALPHA # 允许 per-pixel alpha
- )
- bg_surf.fill(color) # color 中已包含 a 通道
- self.screen.blit(bg_surf, self.bg_rect.topleft)
- # 绘制文本
- for surf, pos in zip(self.text_surfs, self.text_positions):
- self.screen.blit(surf, pos)
- def handle_event(self, event):
- if event.type == pygame.MOUSEMOTION:
- self.hovered = self.bg_rect.collidepoint(event.pos)
- elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
- if self.hovered:
- if self.action:
- self.action()
- sound_player.play("uiLeave")
- class PygameButtonBackIMG:
- """
- 一个仅使用图片作为按钮的类,不含文字渲染,图片本身包含文字。
- - default_img_path: 默认状态下的背景图路径
- - hover_img_path: 悬停状态下的背景图路径
- """
- def __init__(self, screen, relx, rely,
- default_img_path, hover_img_path,
- action=None, anchor='center'):
- 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.relx = relx
- self.rely = rely
- self.anchor = anchor # 'center' 或 'topleft'
- self.hovered = False
- # 按钮尺寸使用图片原始尺寸
- self.width, self.height = self.default_img.get_size()
- # 计算按钮位置
- sw, sh = self.screen.get_size()
- x = int(sw * self.relx)
- y = int(sh * self.rely)
- if self.anchor == 'center':
- topleft = (x - self.width//2, y - self.height//2)
- else:
- topleft = (x, y)
- 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)
- def handle_event(self, event):
- if event.type == pygame.MOUSEMOTION:
- self.hovered = self.bg_rect.collidepoint(event.pos)
- elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
- if self.hovered and self.action:
- sound_player.play("uiLeave")
- self.action()
- class Particle:
- 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.speed_y = random.uniform(0.3, 0.6)
- # 水平速度,模拟风吹
- self.speed_x = random.uniform(-0.3, 0.3)
- # 透明度和生命周期
- self.alpha = 191
- self.life = random.randint(100, 600) # 寿命更长
- 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 += random.uniform(0, 0.01)
- self.speed_x += random.uniform(-0.06, 0.06)
- # 减缓透明度下降速度
- self.life -= 1
- self.alpha = max(0, int(191 * min(1., self.life / 300)))
- # 重绘粒子
- 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
|