diff --git a/weapon.py b/weapon.py new file mode 100644 index 0000000..a098c5d --- /dev/null +++ b/weapon.py @@ -0,0 +1,29 @@ +import pygame + +class Weapon(pygame.sprite.Sprite): + def __init__(self,player,groups): + super().__init__(groups) + # 精灵类型为武器 + self.sprite_type = 'weapon' + # 根据玩家的状态获取方向('up', 'down', 'left', 'right') + direction = player.status.split('_')[0] + + # 图像加载 + # 根据方向和武器类型获取完整路径 + full_path = f'../graphics/weapons/{player.weapon}/{direction}.png' + # 加载武器图像并转换为透明度有alpha通道的格式 + self.image = pygame.image.load(full_path).convert_alpha() + + # 放置位置 + # 如果方向向右,则将武器放置在玩家矩形的右侧中间,稍微偏移一些 + if direction == 'right': + self.rect = self.image.get_rect(midleft = player.rect.midright + pygame.math.Vector2(0,16)) + # 如果方向向左,则将武器放置在玩家矩形的左侧中间,稍微偏移一些 + elif direction == 'left': + self.rect = self.image.get_rect(midright = player.rect.midleft + pygame.math.Vector2(0,16)) + # 如果方向向下,则将武器放置在玩家矩形的底部中间,稍微偏移一些 + elif direction == 'down': + self.rect = self.image.get_rect(midtop = player.rect.midbottom + pygame.math.Vector2(-10,0)) + # 默认情况下(向上),将武器放置在玩家矩形的顶部中间,稍微偏移一些 + else: + self.rect = self.image.get_rect(midbottom = player.rect.midtop + pygame.math.Vector2(-10,0)) \ No newline at end of file