import math import pygame.font from client_local.const import game as G from client_local.sprite.player import Player from client_local.ui.ui import UIInstance from client_local.frame import character class BloodBar(UIInstance): UI_IMG_GAME_ROOT = "res/img/ui/game/" def __init__(self, me: Player): super(BloodBar, self).__init__("game", "mask_player") self.rect.x = 0 self.rect.y = 120 self.me = me self.current_blood = 0 self.max_blood = 1 self.blood_font = pygame.font.Font("res/font/century.ttf", 11) self.blood_font.set_bold(True) self.blood_slice_img = pygame.image.load(BloodBar.UI_IMG_GAME_ROOT + '/' + "img_playerLife.png").convert_alpha() self.blood_slice_idx = 90 # 0~90 决定血条的长度 self.blood_slice_last_update_time = pygame.time.get_ticks() self.medal_img = pygame.image.load(BloodBar.UI_IMG_GAME_ROOT + '/' + "medal_30.png").convert_alpha() self.name = "" self.name_font = pygame.font.Font("res/font/simsun.ttc", 12) self.init() def init(self): self.set_max_blood(self.me.blood) def update(self): self.redraw_trigger = True current_time = pygame.time.get_ticks() if current_time - self.blood_slice_last_update_time > 100: # 每100ms刷新一次动画 self.blood_slice_last_update_time = current_time self.sheet = pygame.Surface(self.rect.size, pygame.SRCALPHA, 32) self.set_medal() self.set_name(self.name) self.current_blood = self.me.remain_blood self.set_blood(self.current_blood) # 动画改变血条长度 idx = math.ceil(self.current_blood * 90 / self.max_blood) if idx == self.blood_slice_idx: return if idx < self.blood_slice_idx: self.blood_slice_idx -= 1 elif idx > self.blood_slice_idx: self.blood_slice_idx += 1 def set_max_blood(self, max_blood): self.max_blood = min(max(0, max_blood), 999999) self.current_blood = self.max_blood self.set_blood(self.current_blood) def set_blood(self, blood: int): blood = min(max(0, blood), self.max_blood) text_img = self.blood_font.render(str(blood), False, (0, 255, 255)) self.sheet.blit(text_img, (2, 2)) for i in range(self.blood_slice_idx): self.sheet.blit(self.blood_slice_img, (i, 16)) def set_medal(self): self.sheet.blit(self.medal_img, (0, 22)) def set_name(self, name: str): self.name = name text_img = self.name_font.render(name, False, (255, 255, 255)) self.sheet.blit(text_img, (26, 24)) class ResourceObstacleBloodBar(UIInstance): UI_IMG_GAME_ROOT = "res/img/ui/game/" def __init__(self, me): super(ResourceObstacleBloodBar, self).__init__("game", "mask_player") # self.rect.x = 0 # self.rect.y = 120 self.blood_bar_len = 61 self.me = me self.screen = None self.current_blood = 0 self.max_blood = 1 self.blood_slice_img = pygame.image.load(f"{BloodBar.UI_IMG_GAME_ROOT}/img_playerLife.png").convert_alpha() self.blood_slice_idx = self.blood_bar_len # 0~100 决定血条的长度 # self.blood_slice_last_update_time = pygame.time.get_ticks() self.init() def init(self): self.set_max_blood(self.me.transport_amount) def update(self, screen=None): self.redraw_trigger = True if not self.screen: self.screen = screen self.current_blood = self.me.transport_remain self.set_blood(self.current_blood) # 动画改变血条长度 idx = math.ceil(self.current_blood * self.blood_bar_len / self.max_blood) if idx == self.blood_slice_idx: return if idx < self.blood_slice_idx: self.blood_slice_idx -= 1 elif idx > self.blood_slice_idx: self.blood_slice_idx += 1 def set_max_blood(self, max_blood): self.max_blood = min(max(0, max_blood), 999999) self.current_blood = self.max_blood self.set_blood(self.current_blood) def set_blood(self, blood: int): # blood = min(max(0, blood), self.max_blood) if self.screen: for i in range(self.blood_slice_idx): self.screen.blit(self.blood_slice_img, (self.me.x*40-12+i, self.me.y*40-32)) class ResourceBloodBar(UIInstance): UI_IMG_GAME_ROOT = "res/img/ui/game/" def __init__(self, me: Player): super(ResourceBloodBar, self).__init__("game", "mask_resource") self.rect.x = 0 self.rect.y = 205 self.me = me self.current_blood = 0 self.max_blood = 1 self.blood_font = pygame.font.Font("res/font/century.ttf", 11) self.blood_font.set_bold(True) self.blood_slice_img = pygame.image.load(f"{BloodBar.UI_IMG_GAME_ROOT}/img_playerLife.png").convert_alpha() self.blood_slice_idx = 90 # 0~90 决定血条的长度 self.blood_slice_last_update_time = pygame.time.get_ticks() self.init() def init(self): self.set_max_blood(G.TOTAL_TRANSPORT) def update_max_blood(self, max_blood): self.max_blood = min(max(1, max_blood), 999999) self.current_blood = self.max_blood self.set_blood(self.current_blood) def update(self): self.redraw_trigger = True current_time = pygame.time.get_ticks() if current_time - self.blood_slice_last_update_time > 100: # 每100ms刷新一次动画 self.blood_slice_last_update_time = current_time self.sheet = pygame.Surface(self.rect.size, pygame.SRCALPHA, 32) self.current_blood = max(max(G.TOTAL_TRANSPORT - G.CUMULATION_RESOURCE_LEAK, 0) * (1 - G.CUMULATION_INJURE / G.CUMULATION_HERO_BLOOD), 0) self.set_blood(self.current_blood) # 动画改变血条长度 idx = math.ceil(self.current_blood * 90 / self.max_blood) if idx == self.blood_slice_idx: return if idx < self.blood_slice_idx: self.blood_slice_idx -= 1 elif idx > self.blood_slice_idx: self.blood_slice_idx += 1 def set_max_blood(self, max_blood): self.max_blood = min(max(1, max_blood), 999999) self.current_blood = self.max_blood self.set_blood(self.current_blood) def set_blood(self, blood: int): # blood = min(max(0, blood), self.max_blood) # text_img = self.blood_font.render(str(blood), False, (0, 255, 255)) # self.sheet.blit(text_img, (2, 2)) for i in range(self.blood_slice_idx): self.sheet.blit(self.blood_slice_img, (i, 6)) def _render_boss_blood_sheet(bar): """缓存重叠切片;分别复制前缀和末端,保留半透明右边缘的原始像素。""" image = bar.blood_slice_img capacity = max(0, bar.rect.width - 2) length = max(0, min(bar.blood_slice_idx, capacity)) strip_key = (image, bar.rect.size) if getattr(bar, "_boss_strip_key", None) != strip_key: strip = pygame.Surface((capacity + image.get_width() - 1, image.get_height()), pygame.SRCALPHA, 32) for i in range(capacity): strip.blit(image, (i, 0)) bar._boss_strip = strip bar._boss_strip_key = strip_key bar._boss_sheet_key = None sheet_key = (bar.sheet, length) if getattr(bar, "_boss_sheet_key", None) == sheet_key: return if bar.sheet.get_size() != bar.rect.size: bar.sheet = pygame.Surface(bar.rect.size, pygame.SRCALPHA, 32) else: bar.sheet.fill((0, 0, 0, 0)) tail_width = image.get_width() - 1 if length < tail_width: # 很短的血条不能复用完整条的重叠尾部。 for i in range(length): bar.sheet.blit(image, (i + 2, 4)) elif length: bar.sheet.blit(bar._boss_strip, (2, 4), (0, 0, length, image.get_height())) if tail_width: bar.sheet.blit(bar._boss_strip, (length + 2, 4), (capacity, 0, tail_width, image.get_height())) bar._boss_sheet_key = (bar.sheet, length) class BossBloodBar(UIInstance): UI_IMG_GAME_ROOT = "res/img/ui/game/" def __init__(self, user, pos): super(BossBloodBar, self).__init__("game", "boss_blood_bar") self.rect.x = 96 + 200*((pos-1)%3) self.rect.y = 36 + 42*int((pos-1)/3) self.user = user self.current_blood = 0 self.max_blood = 1 # self.head_img = pygame.image.load(BloodBar.UI_IMG_GAME_ROOT + '/' + "icon_boss.png").convert_alpha() self.blood_slice_img = pygame.image.load(BloodBar.UI_IMG_GAME_ROOT + '/' + "img_bossLife2.png").convert_alpha() self.blood_slice_idx = 154 # 0~90 决定血条的长度 self.blood_slice_last_update_time = pygame.time.get_ticks() self.init() def init(self): self.set_max_blood(self.user.blood-self.user.fake_blood) def update(self): self.redraw_trigger = True current_time = pygame.time.get_ticks() if current_time - self.blood_slice_last_update_time > 100: # 每100ms刷新一次动画 # print('draw', self.user.chs_name) self.blood_slice_last_update_time = current_time self.current_blood = self.user.remain_blood - self.user.fake_blood self.set_blood(self.current_blood) # 动画改变血条长度 idx = math.ceil(self.current_blood * 154 / self.max_blood) if idx == self.blood_slice_idx: return if idx < self.blood_slice_idx: self.blood_slice_idx -= 1 elif idx > self.blood_slice_idx: self.blood_slice_idx += 1 def set_max_blood(self, max_blood): self.max_blood = max(0, max_blood) self.current_blood = self.max_blood self.set_blood(self.current_blood) def set_blood(self, blood: int): _render_boss_blood_sheet(self) class BossLongBloodBar(UIInstance): UI_IMG_GAME_ROOT = "res/img/ui/game/" def __init__(self, user, pos_y): super(BossLongBloodBar, self).__init__("game", "boss_long_blood_bar") self.rect.x = 96 self.rect.y = 36 + 42 * pos_y self.user = user self.current_blood = 0 self.max_blood = 1 self.blood_slice_img = pygame.image.load(BloodBar.UI_IMG_GAME_ROOT + '/' + "img_bossLife2.png").convert_alpha() self.blood_slice_idx = 554 # 0~90 决定血条的长度 self.blood_slice_last_update_time = pygame.time.get_ticks() self.init() def init(self): self.set_max_blood(self.user.blood-self.user.fake_blood) def update(self): self.redraw_trigger = True current_time = pygame.time.get_ticks() if current_time - self.blood_slice_last_update_time > 50: # 每100ms刷新一次动画 # print('draw', self.user.chs_name) self.blood_slice_last_update_time = current_time self.current_blood = self.user.remain_blood - self.user.fake_blood self.set_blood(self.current_blood) # 动画改变血条长度 idx = math.ceil(self.current_blood * 554 / self.max_blood) if idx == self.blood_slice_idx: return if idx < self.blood_slice_idx: # self.blood_slice_idx -= 1 self.blood_slice_idx -= max(1, int(abs(self.blood_slice_idx - idx) / 5)) elif idx > self.blood_slice_idx: # self.blood_slice_idx += 1 self.blood_slice_idx += max(1, int(abs(self.blood_slice_idx - idx) / 5)) def set_max_blood(self, max_blood): self.max_blood = max(0, max_blood) self.current_blood = self.max_blood self.set_blood(self.current_blood) def set_blood(self, blood: int): _render_boss_blood_sheet(self) class MapBloodBar(UIInstance): UI_IMG_GAME_ROOT = "res/img/ui/game/" def __init__(self, max_blood, remain_blood): super(MapBloodBar, self).__init__("game", "map_blood_bar") self.rect.x = 570 self.rect.y = 510 self.current_blood = 0 self.max_blood = 1 self.max_map_blood = max_blood self.remain_map_blood = remain_blood self.blood_slice_img = pygame.image.load(f"{BloodBar.UI_IMG_GAME_ROOT}/img_mapBlood.png").convert_alpha() self.bar_len = 180 self.blood_slice_idx = self.bar_len # 0~90 决定血条的长度 self.blood_slice_last_update_time = pygame.time.get_ticks() self.init() def init(self): self.set_max_blood(self.max_map_blood) def update(self): self.redraw_trigger = True current_time = pygame.time.get_ticks() if current_time - self.blood_slice_last_update_time > 50: # 每1000ms刷新一次动画 self.blood_slice_last_update_time = current_time self.sheet = pygame.Surface(self.rect.size, pygame.SRCALPHA, 32) self.current_blood = self.remain_map_blood self.set_blood(self.current_blood) # 动画改变血条长度 idx = math.ceil(self.current_blood * self.bar_len / self.max_blood) if idx == self.blood_slice_idx: return if idx < self.blood_slice_idx: self.blood_slice_idx -= 1 elif idx > self.blood_slice_idx: self.blood_slice_idx += 1 def set_max_blood(self, max_blood): self.max_blood = max(0, max_blood) self.current_blood = self.max_blood self.set_blood(self.current_blood) def set_blood(self, blood: int): blood = min(max(0, blood), self.max_blood) for i in range(self.blood_slice_idx): self.sheet.blit(self.blood_slice_img, (i+16, 4)) class MapResouceBar(UIInstance): UI_IMG_GAME_ROOT = "res/img/ui/game/" def __init__(self, max_blood, remain_blood): super(MapResouceBar, self).__init__("game", "map_resource_bar") self.rect.x = 570 self.rect.y = 510 self.current_blood = 0 self.max_blood = 1 self.max_map_blood = max_blood self.remain_map_blood = remain_blood self.blood_slice_img = pygame.image.load(f"{BloodBar.UI_IMG_GAME_ROOT}/img_mapResource.png").convert_alpha() self.bar_len = 180 self.blood_slice_idx = self.bar_len # 0~90 决定血条的长度 self.blood_slice_last_update_time = pygame.time.get_ticks() self.init() def init(self): self.set_max_blood(self.max_map_blood) def update(self): self.redraw_trigger = True current_time = pygame.time.get_ticks() if current_time - self.blood_slice_last_update_time > 50: # 每1000ms刷新一次动画 self.blood_slice_last_update_time = current_time self.sheet = pygame.Surface(self.rect.size, pygame.SRCALPHA, 32) self.current_blood = self.remain_map_blood self.set_blood(self.current_blood) # 动画改变血条长度 idx = math.ceil(self.current_blood * self.bar_len / self.max_blood) if idx == self.blood_slice_idx: return if idx < self.blood_slice_idx: self.blood_slice_idx -= 1 elif idx > self.blood_slice_idx: self.blood_slice_idx += 1 def set_max_blood(self, max_blood): self.max_blood = max(0, max_blood) self.current_blood = self.max_blood self.set_blood(self.current_blood) def set_blood(self, blood: int): blood = min(max(0, blood), self.max_blood) for i in range(self.blood_slice_idx): self.sheet.blit(self.blood_slice_img, (i+16, 4)) def update_remain_blood(self, remain_amount): self.remain_map_blood = remain_amount class BossHead(UIInstance): UI_IMG_GAME_ROOT = "res/img/ui/game/" def __init__(self, user, pos): super(BossHead, self).__init__("game", "icon_boss") self.rect.x = 57 + 200*((pos-1)%3) self.rect.y = 36 + 42*int((pos-1)/3) self.user = user self.surf = pygame.Surface((34, 33), pygame.SRCALPHA, 32) self.init() def init(self): self._render_head() def _render_head(self): self.sheet = pygame.Surface(self.rect.size, pygame.SRCALPHA, 32) self.surf = pygame.Surface((34, 33), pygame.SRCALPHA, 32) stand_frames = self.user.character["STAND_D"] face_frames = stand_frames.get("Face") face_anchor = face_frames[0] if face_frames else None for component in character.CHARACTER_COMPONENTS["D"]: if component not in stand_frames: continue frame = stand_frames[component][0] frame.image.set_alpha(255) if self.user.head_cx != -100000: if face_anchor is not None: # HEAD_POS 是头像中 Face 层的位置;其他层保留相对 Face 的原始偏移。 x = self.user.head_cx + frame.cx - face_anchor.cx y = self.user.head_cy + frame.cy - face_anchor.cy self.surf.blit(frame.image, (x, y)) else: # 单张整图的怪物没有 Face 层,保持原来的定位方式。 self.surf.blit(frame.image, (self.user.head_cx, self.user.head_cy)) else: self.surf.blit(frame.image, (-frame.cx-34, -frame.cy//2-12)) self.sheet.blit(self.surf, (2, 3)) def renew(self): self._render_head() def update(self): self.redraw_trigger = True