Reputation: 143
I am continuing my work using pygame. I am currently working on the enemy class. Basically, it will walk from one side to another. I can get it to walk between the set boundaries, but I cannot get the image to display the proper direction the enemy is facing. If anyone can please help me out or give me some advice, I'd really appreciate it. Thank you.
I believe my issue is the update() function.
Here is my 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]
Upvotes: 0
Views: 248
Reputation: 24417
It looks like you're only updating the frames when the enemy changes direction (i.e. when it hits one of the boundaries). However the frames should be updated each frame based on the current position and direction even when the sprite hasn't hit a 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]
Upvotes: 2