#animations import pygame, sys from pygame.locals import * class Player(pygame.sprite.Sprite): def __init__(self, pos_x, pos_y): super().__init__() self.sprites = [] self.sprites.append(pygame.image.load('attack_1.png')) self.sprites.append(pygame.image.load('attack_2.png')) self.sprites.append(pygame.image.load('attack_3.png')) self.sprites.append(pygame.image.load('attack_4.png')) self.sprites.append(pygame.image.load('attack_5.png')) self.sprites.append(pygame.image.load('attack_6.png')) self.sprites.append(pygame.image.load('attack_7.png')) self.sprites.append(pygame.image.load('attack_8.png')) self.sprites.append(pygame.image.load('attack_9.png')) self.sprites.append(pygame.image.load('attack_10.png')) # Scale the images to be 0.5 times bigger self.sprites = [pygame.transform.scale(sprite, (sprite.get_width() * 1.5, sprite.get_height() * 1.5)) for sprite in self.sprites] self.current_sprite = 0 self.image = self.sprites[self.current_sprite] self.rect = self.image.get_rect() self.rect.topleft = [pos_x, pos_y] self.is_animating = False def start_animation(self): self.is_animating = True def update(self): if self.is_animating: self.current_sprite += 0.2 # Speed of the animation if self.current_sprite >= len(self.sprites): self.current_sprite = 0 self.is_animating = False # Stop the animation once done self.image = self.sprites[int(self.current_sprite)] pygame.init() clock = pygame.time.Clock() screen_width = 400 screen_height = 400 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Sprite animation test") moving_sprites = pygame.sprite.Group() player = Player(100, 100) moving_sprites.add(player) while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_SPACE: player.start_animation() # Start the animation when space bar is pressed screen.fill((0, 0, 0)) # Clear the screen moving_sprites.update() moving_sprites.draw(screen) pygame.display.update() clock.tick(30) # Control the frame rate