-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation.py
More file actions
61 lines (47 loc) · 1.99 KB
/
Copy pathanimation.py
File metadata and controls
61 lines (47 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import pygame
class AnimateSprite(pygame.sprite.Sprite):
def __init__(self, sprite_name, size=(200, 200)):
super().__init__()
self.size = size
self.image = pygame.image.load(f"PygameAssets-main/{sprite_name}.png")
self.image = pygame.transform.scale(self.image, size)
self.current_image = 0
self.images = animations.get(sprite_name)
self.animation = False
#definir une mehode pour demarrer l'animation
def start_animation(self):
self.animation = True
# animer le sprite
def animate(self, loop=False):
#verifier si l'animation est active
if self.animation:
# passer a l'image suivant
self.current_image += 1
# verifier si on a atteint la fin de l'animation
if self.current_image >= len(self.images):
# remettre l'animation au départ
self.current_image = 0
#veirifier si l'animation n'est pas en mode boucle
if loop is False:
#desactiver l'animation
self.animation = False
# modifier l'image precedente par la suivante
self.image = self.images[self.current_image]
self.image = pygame.transform.scale(self.image, self.size)
# definir une fonction pour charger les images d'un sprite
def load_animation_image(sprite_name):
# charger les 24 images de ce sprite das le dossier correspondant
images = []
# recuperer le chemin du dossier pour ce sprite
path = f"PygameAssets-main/{sprite_name}/{sprite_name}"
# boucler sur chaque image dans ce dossier
for num in range(1, 24):
image_path = path + str(num) + '.png'
images.append(pygame.image.load(image_path))
return images
# definir un dict qui va definir les images chargé de chaques sprite
animations = {
'mummy': load_animation_image('mummy'),
'player': load_animation_image('player'),
'alien': load_animation_image('alien')
}