-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite_animation.py
84 lines (60 loc) · 1.86 KB
/
sprite_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pygame
from pygame.locals import *
import os
# taken from https://www.youtube.com/watch?v=d06aVDzOfV8
# github: https://github.com/russs123/Explosion
pygame.init()
clock = pygame.time.Clock()
fps = 60
screen_width = 600
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Explosion Demo')
#define colours
bg = (50, 50, 50)
def draw_bg():
screen.fill(bg)
explosion_image_list = []
for num in range(1, 6):
img = pygame.image.load(os.path.join('Assets', f'exp{num}.png'))
img = pygame.transform.scale(img, (100, 100))
explosion_image_list.append(img)
#create Explosion class
class Explosion(pygame.sprite.Sprite):
def __init__(self, x, y):
global explosion_image_list
pygame.sprite.Sprite.__init__(self)
self.index = 0
self.image = explosion_image_list[self.index]
self.rect = self.image.get_rect()
self.rect.center = [x, y]
self.counter = 0
def update(self):
explosion_speed = 4
#update explosion animation
self.counter += 1
if self.counter >= explosion_speed and self.index < len(explosion_image_list ) - 1:
self.counter = 0
self.index += 1
self.image = explosion_image_list [self.index]
#if the animation is complete, reset animation index
if self.index >= len(explosion_image_list) - 1 and self.counter >= explosion_speed:
self.kill()
explosion_group = pygame.sprite.Group()
run = True
while run:
clock.tick(fps)
#draw background
draw_bg()
explosion_group.draw(screen)
explosion_group.update()
#event handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
explosion = Explosion(pos[0], pos[1])
explosion_group.add(explosion)
pygame.display.update()
pygame.quit()