This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spriteanimation_example.py
65 lines (52 loc) · 1.92 KB
/
spriteanimation_example.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#https://stackoverflow.com/questions/14044147/animated-sprite-from-few-images#14044210
import pygame
import sys
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 128, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
loading_animation_count = 7
loading_animation_speed = 15
def load_image(name):
image = pygame.image.load(name)
return image
class TestSprite(pygame.sprite.Sprite):
def __init__(self):
super(TestSprite, self).__init__()
self.images = []
for i in xrange(loading_animation_count):
self.images.append(load_image('data/sprites/'+str(i)+'loading.png'))
# assuming both images are 64x64 pixels
self.index = 0
self.image = self.images[self.index]
self.rect = pygame.Rect(200, 200, 200, 200)
def update(self):
'''This method iterates through the elements inside self.images and
displays the next one each tick. For a slower animation, you may want to
consider using a timer of some sort so it updates slower.'''
self.index += 1
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
def main():
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
screen.fill(RED)
my_sprite = TestSprite()
my_group = pygame.sprite.Group(my_sprite)
while True:
event = pygame.event.poll()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
# Calling the 'my_group.update' function calls the 'update' function of all
# its member sprites. Calling the 'my_group.draw' function uses the 'image'
# and 'rect' attributes of its member sprites to draw the sprite.
my_group.update()
my_group.draw(screen)
pygame.display.flip()
clock.tick(loading_animation_speed)
if __name__ == '__main__':
main()