import pygame, sys, os new_directory = "W:/11/hayden.derohan/Programming/Practice/PyGEMeye/VideoPygameTUTS/SPRITEANIMATIONS" os.chdir(new_directory) class Player(pygame.sprite.Sprite): def __init__(self, pos_x, pos_y): super().__init__() self.attack_animation = False 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')) self.current_sprite = 0 self.image = self.sprites[self.current_sprite] self.rect = self.image.get_rect() self.rect.topleft = [pos_x,pos_y] def attack(self): self.attack_animation = True def update(self,speed): if self.attack_animation == True: self.current_sprite += speed if int(self.current_sprite) >= len(self.sprites): self.current_sprite = 0 self.attack_animation = False self.image = self.sprites[int(self.current_sprite)] # General setup pygame.init() clock = pygame.time.Clock() # Game Screen screen_width = 400 screen_height = 400 screen = pygame.display.set_mode((screen_width,screen_height)) pygame.display.set_caption("Sprite Animation") # Creating the sprites and groups moving_sprites = pygame.sprite.Group() player = Player(100,100) moving_sprites.add(player) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: print("Mouse input_X -> Exit") pygame.quit() sys.exit() mouse_presses = pygame.mouse.get_pressed() if mouse_presses[0]: player.attack() print("Mouse input -> animation") if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: print("key_board input -> Exit") pygame.quit() sys.exit() player.attack() print("key_board input -> animation") # Drawing screen.fill((0,0,0)) moving_sprites.draw(screen) moving_sprites.update(0.25) pygame.display.flip() clock.tick(60)