对上述代码进行注解
时间: 2023-07-09 21:25:19 浏览: 42
好的,以下是代码的注释:
```python
class Item:
def __init__(self, l, t, w, h, index, font): # 初始化方法,接收位置、大小、索引和字体参数
self.rect = pygame.Rect(l, t, w, h) # 创建一个矩形对象,表示选项卡的位置和大小
self.index = index # 选项卡的索引,用于标识选项卡
self.font = font # 字体对象,用于在界面上显示文本
def display_names(self, surface, name, cost, selected): # 显示名称和价格的方法
color = TEXT_COLOR_SELECTED if selected else TEXT_COLOR # 设置文本颜色
title_surf = self.font.render(name, False, color) # 创建名称文本的Surface对象
title_rect = title_surf.get_rect(midtop=self.rect.midtop + pygame.math.Vector2(0, 20)) # 设置名称文本的位置
cost_surf = self.font.render(f'{int(cost)}', False, color) # 创建价格文本的Surface对象
cost_rect = cost_surf.get_rect(midbottom=self.rect.midbottom - pygame.math.Vector2(0, 20)) # 设置价格文本的位置
surface.blit(title_surf, title_rect) # 在界面上绘制名称文本
surface.blit(cost_surf, cost_rect) # 在界面上绘制价格文本
def display_bar(self, surface, value, max_value, selected): # 显示进度条的方法
# 绘制进度条的设置
top = self.rect.midtop + pygame.math.Vector2(0, 60) # 进度条的顶部
bottom = self.rect.midbottom - pygame.math.Vector2(0, 60) # 进度条的底部
color = BAR_COLOR_SELECTED if selected else BAR_COLOR # 进度条的颜色
# 进度条的设置
full_height = bottom[1] - top[1] # 进度条的总高度
relative_number = (value / max_value) * full_height # 进度条的相对高度
value_rect = pygame.Rect(top[0] - 15, bottom[1] - relative_number, 30, 10) # 进度条的位置和大小
# 绘制进度条
pygame.draw.line(surface, color, top, bottom, 5) # 绘制进度条的底部线条
pygame.draw.rect(surface, color, value_rect) # 绘制进度条的实际值
def trigger(self, player): # 触发升级的方法
upgrade_attribute = list(player.stats.keys())[self.index] # 获取升级的属性
if player.exp >= player.upgrade_cost[upgrade_attribute] and player.stats[upgrade_attribute] < player.max_stats[upgrade_attribute]: # 如果满足升级条件
player.exp -= player.upgrade_cost[upgrade_attribute] # 扣除升级费用
player.stats[upgrade_attribute] *= 1.2 # 提升属性值
player.upgrade_cost[upgrade_attribute] *= 1.4 # 增加升级费用
if player.stats[upgrade_attribute] > player.max_stats[upgrade_attribute]: # 如果属性值超出最大值
player.stats[upgrade_attribute] = player.max_stats[upgrade_attribute] # 将属性值设为最大值
def display(self, surface, selection_num, name, value, max_value, cost): # 在界面上显示所有元素的方法
# 绘制选项卡的背景和边框
if self.index == selection_num:
pygame.draw.rect(surface, UPGRADE_BG_COLOR_SELECTED, self.rect)
pygame.draw.rect(surface, UI_BORDER_COLOR, self.rect, 4)
else:
pygame.draw.rect(surface, UI_BG_COLOR, self.rect)
pygame.draw.rect(surface, UI_BORDER_COLOR, self.rect, 4)
self.display_names(surface, name, cost, self.index == selection_num) # 绘制名称和价格文本
self.display_bar(surface, value, max_value, self.index == selection_num) # 绘制进度条