From 33db8ccae0e6d69ac0dd36500b849a55b160e584 Mon Sep 17 00:00:00 2001 From: phe65kjig <2567779553@qq.com> Date: Sat, 25 May 2024 18:11:01 +0800 Subject: [PATCH] ADD file via upload --- weapon.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 weapon.py 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