You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.3 KiB

import pygame
from settings import *
class Upgrade:
def __init__(self,player):
# 获取显示表面
self.display_surface = pygame.display.get_surface()
# 绑定玩家实例
self.player = player
# 属性数量
self.attribute_nr = len(player.stats)
# 属性名称列表
self.attribute_names = list(player.stats.keys())
# 属性最大值列表
self.max_values = list(player.max_stats.values())
# 设置字体
self.font = pygame.font.Font(UI_FONT, UI_FONT_SIZE)
# 创建升级选项
# 设置界面高度
self.height = self.display_surface.get_size()[1] * 0.8
# 设置界面宽度
self.width = self.display_surface.get_size()[0] // 6
# 创建升级选项
self.create_items()
# 选择系统
# 当前选择的属性索引
self.selection_index = 0
# 选择操作时间戳
self.selection_time = None
# 是否可以移动选择
self.can_move = True
def input(self):
# 获取按键状态
keys = pygame.key.get_pressed()
# 处理按键输入
if self.can_move:
# 右键选择下一个属性
if keys[pygame.K_RIGHT] and self.selection_index < self.attribute_nr - 1:
self.selection_index += 1
self.can_move = False
self.selection_time = pygame.time.get_ticks()
# 左键选择上一个属性
elif keys[pygame.K_LEFT] and self.selection_index >= 1:
self.selection_index -= 1
self.can_move = False
self.selection_time = pygame.time.get_ticks()
# 空格键触发当前选择的升级项
if keys[pygame.K_SPACE]:
self.can_move = False
self.selection_time = pygame.time.get_ticks()
self.item_list[self.selection_index].trigger(self.player)
def selection_cooldown(self):
# 处理选择操作的冷却
if not self.can_move:
# 获取当前时间
current_time = pygame.time.get_ticks()
# 检查是否达到冷却时间
if current_time - self.selection_time >= 300:
# 可以继续移动选择
self.can_move = True
def create_items(self):
# 初始化升级项列表
self.item_list = []
# 计算升级项的水平位置
for item, index in enumerate(range(self.attribute_nr)):
# horizontal position
full_width = self.display_surface.get_size()[0]
increment = full_width // self.attribute_nr
left = (item * increment) + (increment - self.width) // 2
# 计算升级项的垂直位置
top = self.display_surface.get_size()[1] * 0.1
# 创建升级项对象并添加到列表中
item = Item(left,top,self.width,self.height,index,self.font)
self.item_list.append(item)
def display(self):
# 处理输入
self.input()
# 处理选择操作的冷却
self.selection_cooldown()
for index, item in enumerate(self.item_list):
# 获取属性信息
name = self.attribute_names[index]
value = self.player.get_value_by_index(index)
max_value = self.max_values[index]
cost = self.player.get_cost_by_index(index)
# 在界面上显示升级项
item.display(self.display_surface,self.selection_index,name,value,max_value,cost)
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)
# 设置标题文本的位置
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)
# 设置成本文本的位置
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]
# 属性增加20%
player.stats[upgrade_attribute] *= 1.2
# 升级成本增加40%
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)