Mika Tan
Mika Tan

Reputation: 128

Trouble animating sprites (Pygame)

I am working on a school project with pygame and have unfortunately run into trouble with animating my sprites, or more specifically, changing the sprite from one to another, and I'm unsure on how to do it. Currently, I have a class of my spaceship sprite and background sprite:

game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, 'img')
space_background = pygame.image.load('background.png').convert()
starship_1 = pygame.image.load('starship2.png').convert()
starship_2 = pygame.image.load('starship3.png').convert()
starship_3 = pygame.image.load('starship4.png').convert()


class starship(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale(starship_1, (150,150))
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()
        self.rect.center = (400, 400)

all_sprites = pygame.sprite.Group()
BackGround = background()
StarShip = starship()
all_sprites.add(BackGround)
all_sprites.add(StarShip)

My while loop looks like this:

run = True

while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    
    if keys[pygame.K_LEFT] and StarShip.rect.x > 25:  
        StarShip.rect.x -= vel

    if keys[pygame.K_RIGHT] and StarShip.rect.x < 600:  
        StarShip.rect.x += vel

    if keys[pygame.K_UP] and StarShip.rect.y > 25: 
        StarShip.rect.y -= vel

    if keys[pygame.K_DOWN] and StarShip.rect.y < 600:
        StarShip.rect.y += vel

    
    win.fill((0,0,0))
    all_sprites.update()
    all_sprites.draw(win)
    pygame.display.update() 
    
pygame.quit()

This has basic movement of left/right/up/down. What I want to do is have my StarShip object constantly change between the variables starship_1, starship_2, starship_3 (which contain my 3 sprites of my starship), so it looks like the starship is moving.

My sprites look like this:

StarShip1

Starship2

Starship3

As you can see, it is the engine fire that is the difference between those sprites. How would I change between those 3 sprites every 1 second?

FYI: When the program is launched, this is what appears:Startup Picture

Thank you!

Upvotes: 0

Views: 91

Answers (1)

glory9211
glory9211

Reputation: 843

There are 2 parts to achieve this effect.

  1. Create a function to change image of the sprite. ( Easy Task)
  2. 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

Upvotes: 1

Related Questions