Trouble animating sprites (Pygame)
Solution 1:
There are 2 parts to achieve this effect.
- Create a function to change image of the sprite. ( Easy Task)
- Call the above function periodically every x seconds. ( Intermediate Task)
Step 1. You can achieve this by just setting/loading the self.image variable with the next image.
Step 2.
clock = pygame.time.Clock()
time_counter = 0
images = ['starship_1', 'starship_2', 'starship_3']
current_img_index = 0
while run:
# Your Code
time_counter = clock.tick()
# 1000 is milliseconds == 1 second. Change this as desired
if time_counter > 1000:
# Incrementing index to next image but reseting back to zero when it hits 3
current_img_index = (current_img_index + 1) % 3
set_img_function(current_img_index) # Function you make in step 1
# Reset the timer
time_counter = 0
A good approach will be to complete step 1 and then bind it to a button. Test if it works and then move on to step 2.
Some good read about the functions used in this code to fully understand them is here