import pygame from support import import_folder from random import choice class AnimationPlayer: def __init__(self): self.frames = { # 魔法效果 # 导入火焰动画帧 'flame': import_folder('../graphics/particles/flame/frames'), # 导入光环动画 'aura': import_folder('../graphics/particles/aura'), # 导入治疗动画帧 'heal': import_folder('../graphics/particles/heal/frames'), # 攻击效果 # 导入爪击动画 'claw': import_folder('../graphics/particles/claw'), # 导入斩击动画 'slash': import_folder('../graphics/particles/slash'), # 导入闪光动画 'sparkle': import_folder('../graphics/particles/sparkle'), # 导入叶子攻击动画 'leaf_attack': import_folder('../graphics/particles/leaf_attack'), # 导入雷电动画 'thunder': import_folder('../graphics/particles/thunder'), # 怪物死亡效果 # 导入乌贼死亡效果动画 'squid': import_folder('../graphics/particles/smoke_orange'), # 导入浣熊死亡效果动画 'raccoon': import_folder('../graphics/particles/raccoon'), # 导入幽灵死亡效果动画 'spirit': import_folder('../graphics/particles/nova'), # 导入竹子死亡效果动画 'bamboo': import_folder('../graphics/particles/bamboo'), # 叶子效果 'leaf': ( # 导入叶子动画帧 import_folder('../graphics/particles/leaf1'), import_folder('../graphics/particles/leaf2'), import_folder('../graphics/particles/leaf3'), import_folder('../graphics/particles/leaf4'), import_folder('../graphics/particles/leaf5'), import_folder('../graphics/particles/leaf6'), # 反射叶子动画帧 self.reflect_images(import_folder('../graphics/particles/leaf1')), self.reflect_images(import_folder('../graphics/particles/leaf2')), self.reflect_images(import_folder('../graphics/particles/leaf3')), self.reflect_images(import_folder('../graphics/particles/leaf4')), self.reflect_images(import_folder('../graphics/particles/leaf5')), self.reflect_images(import_folder('../graphics/particles/leaf6')) ) } def reflect_images(self,frames): # 创建一个新的空列表来存储翻转后的图像帧 new_frames = [] # 对输入的每个图像帧进行处理 for frame in frames: # 使用pygame.transform.flip函数进行水平翻转 flipped_frame = pygame.transform.flip(frame,True,False) # 将翻转后的帧添加到新的列表中 new_frames.append(flipped_frame) # 返回翻转后的图像帧列表 return new_frames def create_grass_particles(self,pos,groups): # 从'leaf'类型的动画帧中随机选择一个动画帧序列 animation_frames = choice(self.frames['leaf']) # 创建一个ParticleEffect对象,使用选择的动画帧序列和指定位置 ParticleEffect(pos,animation_frames,groups) def create_particles(self,animation_type,pos,groups): # 根据指定的动画类型从self.frames中获取相应的动画帧序列 animation_frames = self.frames[animation_type] # 创建一个ParticleEffect对象,使用指定的动画帧序列和位置,并将其添加到指定的精灵组中 ParticleEffect(pos,animation_frames,groups) class ParticleEffect(pygame.sprite.Sprite): def __init__(self,pos,animation_frames,groups): super().__init__(groups) # 粒子类型为'magic' self.sprite_type = 'magic' # 当前帧的索引 self.frame_index = 0 # 动画播放速度 self.animation_speed = 0.15 # 粒子效果的动画帧序列 self.frames = animation_frames # 当前显示的图像帧 self.image = self.frames[self.frame_index] # 图像帧的矩形边界 self.rect = self.image.get_rect(center = pos) def animate(self): # 更新帧索引以播放动画 self.frame_index += self.animation_speed # 如果帧索引超过了动画帧序列的长度,则销毁粒子效果 if self.frame_index >= len(self.frames): self.kill() # 否则,更新当前显示的图像帧 else: self.image = self.frames[int(self.frame_index)] def update(self): # 调用animate方法更新粒子效果的动画 self.animate()