Основная проблема заключается в том, что когда я атакую лицом вправо self.direction == 'right', анимация меняется нормально, что создает оптическую иллюзию того, что игрок вытягивает меч вправо, однако, когда игрок смотрит влево, а не меч "качается" влево, анимация расширяется вправо, толкая изображение вправо (создавая впечатление, что игрока отталкивают назад, а не атакуют влево).
Я пробовал, чтобы ИИ исправил это за меня, я потратил несколько дней, пытаясь понять это, но не смог. Это репозиторий, который поможет вам получить больше контекста:
https://github.com/gabriel-caesar/Pytho ... r_travails

Следуйте коду ниже:
Код: Выделить всё
# ./player.py
...
def animate(self):
# Handling flipping logic
if self.direction == 'right':
self.flip = False
elif self.direction == 'left':
self.flip = True
match self.animation_state:
case 'run':
self.player_frames = [ walk_1, walk_2, walk_3, walk_4 ]
self.animation_index += 0.1
if (self.animation_index > len(self.player_frames)):
self.animation_index = 0
self.image = pygame.transform.flip(self.player_frames[int(self.animation_index)], self.flip, False)
case 'attack':
self.player_frames = [ attack_2 ]
self.animation_index += 0.1
if (self.animation_index > len(self.player_frames)):
self.animation_index = 0
self.image = pygame.transform.flip(self.player_frames[int(self.animation_index)], self.flip, False)
case 'guard':
self.player_frames = [ guard_1 ]
self.image = pygame.transform.flip(self.player_frames[0], self.flip, False)
case 'jump':
self.player_frames = [ jump_1 ]
self.image = pygame.transform.flip(self.player_frames[0], self.flip, False)
case 'idle':
self.player_frames = [ idle_1 ]
self.image = pygame.transform.flip(self.player_frames[0], self.flip, False)
self.rect = self.image.get_rect()
...
Код: Выделить всё
# Initializing the player
player = Player(400, [800, 250])
playergroup = pygame.sprite.GroupSingle()
playergroup.add(player)
...
playergroup.draw(screen)
Код: Выделить всё
class Player(pygame.sprite.Sprite, Entity):
def __init__(self, hp, pos):
pygame.sprite.Sprite.__init__(self)
Entity.__init__(self, hp, pos)
# Animation flags
self.animation_state = 'idle'
self.animation_index = 0
self.player_frames = []
# Sprite rect and image
self.image = idle_1
self.rect = self.image.get_rect(midbottom = (pos[0], pos[1]))
...
Код: Выделить всё
class Entity():
def __init__(self, hp, pos):
self.hp = hp
self.pos = pos
self.vertical_velocity = 0
self.movement = [0, 0]
self.air_timer = 0
self.flip = False
self.direction = 'right'
self.guarding = False
self.attacking = False
self.attack_cooldown = 0
Подробнее здесь: https://stackoverflow.com/questions/798 ... -wrong-way
Мобильная версия