This repository was archived by the owner on May 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimations.py
More file actions
62 lines (44 loc) · 1.99 KB
/
Animations.py
File metadata and controls
62 lines (44 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
62
from Globals import *
fps = 20
def generate_animation_images((width, height), filename):
# images array will be filled with each frame of an animation
images = []
full_image = pygame.image.load(filename).convert_alpha()
full_width, full_height = full_image.get_size()
for i in xrange(int(full_width / width)):
images.append(full_image.subsurface((i*width, 0, width, height)))
return images
class Animations(pygame.sprite.Sprite):
def __init__(self, (width, height), filename, location):
pygame.sprite.Sprite.__init__(self)
self.all_images = generate_animation_images((width, height), filename)
# delay is time between animation frames
# last_update saves the time the animation was last updated
self.delay = 1000/fps
self.last_update = 0
# frame is the array location in images
self.frame = 0
self.location = location
# sets the animations current image
self.image = self.all_images[self.frame]
self.rect = self.image.get_rect()
# This method updates the animation image
def update_animation(self, total_time):
# checks if enough time has passed to change the image
if total_time - self.last_update > self.delay:
self.frame += 1
# checks if the new image is greater than the number of images
# starts image cycle over if true
if self.frame >= len(self.all_images):
self.frame = 0
# updates current animation image
self.image = self.all_images[self.frame]
# changes the last update time
self.last_update = total_time
# draws animation changes to the screen
screen.blit(self.image, self.location)
def update(self):
self.rect = self.image.get_rect()
self.rect.x = self.location[0]
self.rect.y = self.location[1]
screen.blit(self.image, self)