python - Pygame - Making a Sprite "walk" in the right direction -
i continuing work using pygame. working on enemy class. basically, walk 1 side another. can walk between set boundaries, cannot image display proper direction enemy facing. if can please me out or give me advice, i'd appreciate it. thank you.
i believe issue update() function.
here enemy class
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]
it looks you're updating frames when enemy changes direction (i.e. when hits 1 of boundaries). frames should updated each frame based on current position , direction when sprite hasn't hit boundary:
def update(self): # move left/right self.rect.x += self.change_x cur_pos = self.rect.x - self.level.world_shift print(cur_pos) # test against boundaries: if cur_pos < self.boundary_left or cur_pos > self.boundary_right: self.change_x *= -1 # set animation frame: if self.change_x > 0: frame = (self.rect.x // 30) % len(self.enemy_moving_frames_r) self.image = self.enemy_moving_frames_r[frame] elif self.change_x < 0: frame = (self.rect.x // 30) % len(self.enemy_moving_frames_l) self.image = self.enemy_moving_frames_l[frame]
Comments
Post a Comment