diff --git a/magic.py b/magic.py new file mode 100644 index 0000000..72f8cd3 --- /dev/null +++ b/magic.py @@ -0,0 +1,63 @@ +import pygame +from settings import * +from random import randint + +class MagicPlayer: + def __init__(self,animation_player): + # 初始化 MagicPlayer 类,传入 AnimationPlayer 实例作为参数 + self.animation_player = animation_player + # 定义魔法音效字典 + self.sounds = { + # 创建 'heal' 音效对象 + 'heal': pygame.mixer.Sound('../audio/heal.wav'), + # 创建 'flame' 音效对象 + 'flame':pygame.mixer.Sound('../audio/Fire.wav') + } + + def heal(self,player,strength,cost,groups): + # 检查玩家的能量是否足够支付治疗的法力消耗 + if player.energy >= cost: + # 播放治疗音效 + self.sounds['heal'].play() + # 增加玩家的健康值,扣除相应的法力消耗 + player.health += strength + player.energy -= cost + # 如果玩家的健康值超过了最大健康值,则将健康值设为最大健康值 + if player.health >= player.stats['health']: + player.health = player.stats['health'] + # 创建治疗效果的粒子效果 + self.animation_player.create_particles('aura',player.rect.center,groups) + self.animation_player.create_particles('heal',player.rect.center,groups) + + def flame(self,player,cost,groups): + # 检查玩家的能量是否足够支付火焰释放的法力消耗 + if player.energy >= cost: + # 扣除玩家的法力消耗 + player.energy -= cost + # 播放火焰释放音效 + self.sounds['flame'].play() + # 根据玩家的状态确定火焰释放的方向 + if player.status.split('_')[0] == 'right': direction = pygame.math.Vector2(1,0) + elif player.status.split('_')[0] == 'left': direction = pygame.math.Vector2(-1,0) + elif player.status.split('_')[0] == 'up': direction = pygame.math.Vector2(0,-1) + else: direction = pygame.math.Vector2(0,1) + # 根据方向生成火焰粒子效果 + # 创建 1 到 5 个火焰粒子 + for i in range(1,6): + # 如果是水平方向 + if direction.x: + # 计算火焰粒子的位置偏移 + offset_x = (direction.x * i) * TILESIZE + # 计算火焰粒子的具体位置,考虑随机偏移 + x = player.rect.centerx + offset_x + randint(-TILESIZE // 3, TILESIZE // 3) + y = player.rect.centery + randint(-TILESIZE // 3, TILESIZE // 3) + self.animation_player.create_particles('flame',(x,y),groups) + # 如果是垂直方向 + else: + # 计算火焰粒子的位置偏移 + offset_y = (direction.y * i) * TILESIZE + # 计算火焰粒子的具体位置,考虑随机偏移 + x = player.rect.centerx + randint(-TILESIZE // 3, TILESIZE // 3) + y = player.rect.centery + offset_y + randint(-TILESIZE // 3, TILESIZE // 3) + # 创建火焰粒子效果,并添加到指定的精灵组中 + self.animation_player.create_particles('flame',(x,y),groups) \ No newline at end of file