forked from Carouan/Game_Sebilias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.py
46 lines (34 loc) · 1.22 KB
/
animation.py
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
import pygame
class AnimateSprite(pygame.sprite.Sprite):
def __init__(self, name):
super().__init__()
self.sprite_sheet = pygame.image.load(f"../sprites/{name}.png")
self.animation_index = 0
self.clock = 0
self.images = {
"down": self.get_images(0),
"left": self.get_images(32),
"right": self.get_images(64),
"up": self.get_images(96)
}
self.speed = 3
def change_animation(self, name):
self.image = self.images[name][self.animation_index]
self.image.set_colorkey((0, 0, 0))
self.clock += self.speed * 5
if self.clock >= 100:
self.animation_index += 1
if self.animation_index >= len(self.images[name]):
self.animation_index = 0
self.clock = 0
def get_images(self, y):
images = []
for i in range(0, 3):
x = i * 32
image = self.get_image(x, y)
images.append(image)
return images
def get_image(self, x, y):
image = pygame.Surface([32, 32])
image.blit(self.sprite_sheet, (0, 0), (x, y, 32, 32))
return image