├── attack_1.png ├── attack_10.png ├── attack_2.png ├── attack_3.png ├── attack_4.png ├── attack_5.png ├── attack_6.png ├── attack_7.png ├── attack_8.png ├── attack_9.png └── sprite_animation_final.py /attack_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/animation/224886bf56e919fedd50b2c7557e10a2829bc609/attack_1.png -------------------------------------------------------------------------------- /attack_10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/animation/224886bf56e919fedd50b2c7557e10a2829bc609/attack_10.png -------------------------------------------------------------------------------- /attack_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/animation/224886bf56e919fedd50b2c7557e10a2829bc609/attack_2.png -------------------------------------------------------------------------------- /attack_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/animation/224886bf56e919fedd50b2c7557e10a2829bc609/attack_3.png -------------------------------------------------------------------------------- /attack_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/animation/224886bf56e919fedd50b2c7557e10a2829bc609/attack_4.png -------------------------------------------------------------------------------- /attack_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/animation/224886bf56e919fedd50b2c7557e10a2829bc609/attack_5.png -------------------------------------------------------------------------------- /attack_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/animation/224886bf56e919fedd50b2c7557e10a2829bc609/attack_6.png -------------------------------------------------------------------------------- /attack_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/animation/224886bf56e919fedd50b2c7557e10a2829bc609/attack_7.png -------------------------------------------------------------------------------- /attack_8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/animation/224886bf56e919fedd50b2c7557e10a2829bc609/attack_8.png -------------------------------------------------------------------------------- /attack_9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/animation/224886bf56e919fedd50b2c7557e10a2829bc609/attack_9.png -------------------------------------------------------------------------------- /sprite_animation_final.py: -------------------------------------------------------------------------------- 1 | import pygame, sys 2 | 3 | class Player(pygame.sprite.Sprite): 4 | def __init__(self, pos_x, pos_y): 5 | super().__init__() 6 | self.attack_animation = False 7 | self.sprites = [] 8 | self.sprites.append(pygame.image.load('attack_1.png')) 9 | self.sprites.append(pygame.image.load('attack_2.png')) 10 | self.sprites.append(pygame.image.load('attack_3.png')) 11 | self.sprites.append(pygame.image.load('attack_4.png')) 12 | self.sprites.append(pygame.image.load('attack_5.png')) 13 | self.sprites.append(pygame.image.load('attack_6.png')) 14 | self.sprites.append(pygame.image.load('attack_7.png')) 15 | self.sprites.append(pygame.image.load('attack_8.png')) 16 | self.sprites.append(pygame.image.load('attack_9.png')) 17 | self.sprites.append(pygame.image.load('attack_10.png')) 18 | self.current_sprite = 0 19 | self.image = self.sprites[self.current_sprite] 20 | 21 | self.rect = self.image.get_rect() 22 | self.rect.topleft = [pos_x,pos_y] 23 | 24 | def attack(self): 25 | self.attack_animation = True 26 | 27 | def update(self,speed): 28 | if self.attack_animation == True: 29 | self.current_sprite += speed 30 | if int(self.current_sprite) >= len(self.sprites): 31 | self.current_sprite = 0 32 | self.attack_animation = False 33 | 34 | self.image = self.sprites[int(self.current_sprite)] 35 | 36 | # General setup 37 | pygame.init() 38 | clock = pygame.time.Clock() 39 | 40 | # Game Screen 41 | screen_width = 400 42 | screen_height = 400 43 | screen = pygame.display.set_mode((screen_width,screen_height)) 44 | pygame.display.set_caption("Sprite Animation") 45 | 46 | # Creating the sprites and groups 47 | moving_sprites = pygame.sprite.Group() 48 | player = Player(100,100) 49 | moving_sprites.add(player) 50 | 51 | while True: 52 | for event in pygame.event.get(): 53 | if event.type == pygame.QUIT: 54 | pygame.quit() 55 | sys.exit() 56 | if event.type == pygame.KEYDOWN: 57 | player.attack() 58 | 59 | # Drawing 60 | screen.fill((0,0,0)) 61 | moving_sprites.draw(screen) 62 | moving_sprites.update(0.25) 63 | pygame.display.flip() 64 | clock.tick(60) --------------------------------------------------------------------------------