Pygame - 制作雪碧"步行"在正确的方向

时间:2015-05-14 04:37:41

标签: python pygame sprite

我正在使用pygame继续我的工作。我目前正在攻击敌人的班级。基本上,它会从一侧走到另一侧。我可以让它在设定的边界之间行走,但我无法让图像显示敌人所面对的正确方向。如果有人能帮助我或给我一些建议,我真的很感激。谢谢。

我相信我的问题是update()函数。

这是我的敌人类

class Enemies(pygame.sprite.Sprite):
     enemy_moving_frames_r = []
     enemy_moving_frames_l = []

     change_x = 0
     change_y = 0

     boundary_top = 0
     boundary_bottom = 0
     boundary_left = 0
     boundary_right = 0

     level = None
     player = None
def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    sprite_sheet = SpriteSheet("stuff/skeleton_7.png")

    image = sprite_sheet.get_image(7, 7, 28, 64)
    self.enemy_moving_frames_r.append(image)
    image = sprite_sheet.get_image(37, 7, 33, 64)
    self.enemy_moving_frames_r.append(image)
    image = sprite_sheet.get_image(70, 7, 33, 64)
    self.enemy_moving_frames_r.append(image)
    image = sprite_sheet.get_image(107, 7, 33, 64)
    self.enemy_moving_frames_r.append(image)
    image = sprite_sheet.get_image(142, 7, 33, 64)
    self.enemy_moving_frames_r.append(image)


    image = sprite_sheet.get_image(7, 7, 28, 64)
    image = pygame.transform.flip(image, True, False)
    self.enemy_moving_frames_l.append(image)
    image = sprite_sheet.get_image(37, 7, 33, 64)
    image = pygame.transform.flip(image, True, False)
    self.enemy_moving_frames_l.append(image)
    image = sprite_sheet.get_image(70, 7, 33, 64)
    image = pygame.transform.flip(image, True, False)
    self.enemy_moving_frames_l.append(image)
    image = sprite_sheet.get_image(107, 7, 33, 64)
    image = pygame.transform.flip(image, True, False)
    self.enemy_moving_frames_l.append(image)
    image = sprite_sheet.get_image(142, 7, 33, 64)
    image = pygame.transform.flip(image, True, False)
    self.enemy_moving_frames_l.append(image)


    self.image = self.enemy_moving_frames_r[0]
    self.rect = self.image.get_rect()



def update(self):
    # Move left/right
    self.rect.x += self.change_x

    cur_pos = self.rect.x - self.level.world_shift

    print(cur_pos)

    if cur_pos < self.boundary_left:
        self.change_x *= -1
        frame = (self.rect.x // 30) % len(self.enemy_moving_frames_r)
        self.image = self.enemy_moving_frames_r[frame]
    elif cur_pos > self.boundary_right:
        self.change_x *= -1
        frame = (self.rect.x // 30) % len(self.enemy_moving_frames_l)
        self.image = self.enemy_moving_frames_l[frame]  

1 个答案:

答案 0 :(得分:2)

看起来你只是在敌人改变方向时(即当它击中其中一个边界时)更新帧。但是,即使精灵没有碰到边界,也应该根据当前位置和方向每帧更新帧:

dat %>% mutate_each_(list(~scale(.) %>% as.vector), vars=c("y","z"))