From 77f2d1e2022c5792efe9badbc384a6bf4f5b4d87 Mon Sep 17 00:00:00 2001 From: phe65kjig <2567779553@qq.com> Date: Sat, 25 May 2024 18:10:42 +0800 Subject: [PATCH] ADD file via upload --- ui.py | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 ui.py diff --git a/ui.py b/ui.py new file mode 100644 index 0000000..fb8d462 --- /dev/null +++ b/ui.py @@ -0,0 +1,111 @@ +import pygame +from settings import * + +class UI: + def __init__(self): + + # general + # 获取游戏窗口表面 + self.display_surface = pygame.display.get_surface() + # 设置 UI 使用的字体和字号 + self.font = pygame.font.Font(UI_FONT,UI_FONT_SIZE) + + # bar setup + # 设置生命值条的矩形位置和大小 + self.health_bar_rect = pygame.Rect(10,10,HEALTH_BAR_WIDTH,BAR_HEIGHT) + # 设置能量条的矩形位置和大小 + self.energy_bar_rect = pygame.Rect(10,34,ENERGY_BAR_WIDTH,BAR_HEIGHT) + + # convert weapon dictionary + # 转换武器字典为图像列表 + self.weapon_graphics = [] + for weapon in weapon_data.values(): + path = weapon['graphic'] + weapon = pygame.image.load(path).convert_alpha() + self.weapon_graphics.append(weapon) + + # convert magic dictionary + # 转换魔法字典为图像列表 + self.magic_graphics = [] + for magic in magic_data.values(): + magic = pygame.image.load(magic['graphic']).convert_alpha() + self.magic_graphics.append(magic) + + + def show_bar(self,current,max_amount,bg_rect,color): + # 绘制背景 + pygame.draw.rect(self.display_surface,UI_BG_COLOR,bg_rect) + + # 计算当前值在最大值中的比例,并转换为像素宽度 + ratio = current / max_amount + current_width = bg_rect.width * ratio + current_rect = bg_rect.copy() + current_rect.width = current_width + + # 绘制当前值对应的条形 + pygame.draw.rect(self.display_surface,color,current_rect) + # 绘制边框 + pygame.draw.rect(self.display_surface,UI_BORDER_COLOR,bg_rect,3) + + def show_exp(self,exp): + # 渲染经验值文本 + text_surf = self.font.render(str(int(exp)),False,TEXT_COLOR) + # 获取屏幕尺寸,设置文本位置在屏幕右下角 + x = self.display_surface.get_size()[0] - 20 + y = self.display_surface.get_size()[1] - 20 + text_rect = text_surf.get_rect(bottomright = (x,y)) + # 绘制背景矩形以便显示文本 + pygame.draw.rect(self.display_surface,UI_BG_COLOR,text_rect.inflate(20,20)) + # 将文本渲染到屏幕上 + self.display_surface.blit(text_surf,text_rect) + # 绘制文本背景的边框 + pygame.draw.rect(self.display_surface,UI_BORDER_COLOR,text_rect.inflate(20,20),3) + + def selection_box(self,left,top, has_switched): + # 创建项目框背景矩形 + bg_rect = pygame.Rect(left,top,ITEM_BOX_SIZE,ITEM_BOX_SIZE) + # 绘制项目框的背景 + pygame.draw.rect(self.display_surface,UI_BG_COLOR,bg_rect) + # 如果已切换,则绘制活跃状态的边框 + if has_switched: + pygame.draw.rect(self.display_surface,UI_BORDER_COLOR_ACTIVE,bg_rect,3) + # 否则绘制常规状态的边框 + else: + pygame.draw.rect(self.display_surface,UI_BORDER_COLOR,bg_rect,3) + return bg_rect + + def weapon_overlay(self,weapon_index,has_switched): + # 在屏幕上绘制武器图像 + # 获取武器框的背景矩形位置 + bg_rect = self.selection_box(10,630,has_switched) + # 获取当前武器的图像 + weapon_surf = self.weapon_graphics[weapon_index] + # 设置武器图像的位置为框的中心 + weapon_rect = weapon_surf.get_rect(center = bg_rect.center) + # 绘制武器图像到屏幕上 + self.display_surface.blit(weapon_surf,weapon_rect) + + def magic_overlay(self,magic_index,has_switched): + # 在屏幕上绘制魔法图像 + magic_surf = self.magic_graphics[magic_index] + # 获取魔法框的背景矩形位置 + bg_rect = self.selection_box(80,635,has_switched) + # 获取当前魔法的图像 + magic_surf = self.magic_graphics[magic_index] + # 设置魔法图像的位置为框的中心 + magic_rect = magic_surf.get_rect(center = bg_rect.center) + # 绘制魔法图像到屏幕上 + self.display_surface.blit(magic_surf,magic_rect) + + def display(self,player): + # 显示玩家的生命值条、能量条、经验值以及当前选择的武器和魔法 + # 显示生命值条 + self.show_bar(player.health,player.stats['health'],self.health_bar_rect,HEALTH_COLOR) + # 显示能量条 + self.show_bar(player.energy,player.stats['energy'],self.energy_bar_rect,ENERGY_COLOR) + # 显示经验值 + self.show_exp(player.exp) + # 显示当前武器图标 + self.weapon_overlay(player.weapon_index,not player.can_switch_weapon) + # 显示当前魔法图标 + self.magic_overlay(player.magic_index,not player.can_switch_magic) \ No newline at end of file