-
Notifications
You must be signed in to change notification settings - Fork 0
/
explosion.py
46 lines (37 loc) · 1.41 KB
/
explosion.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
from pygame.sprite import Sprite
class Explosion(Sprite):
"""Class to create an explosion"""
def __init__(self, ai_game, center, size):
super().__init__()
# Creating a list to store explosion images to create animation
self.images = []
for num in range(1, 6):
img = pygame.image.load(f"images/exp{num}.png")
if size == "very_small":
img = pygame.transform.scale(img, (60, 60))
if size == "small":
img = pygame.transform.scale(img, (80, 80))
if size == "big":
img = pygame.transform.scale(img, (120, 120))
self.images.append(img)
self.index = 0
self.image = self.images[self.index]
self.rect = self.image.get_rect()
# Assigning explosion position
self.rect.center = center
self.frame = 0
self.last_update = pygame.time.get_ticks()
self.frame_rate = 75
def update(self):
now = pygame.time.get_ticks()
if now - self.last_update > self.frame_rate:
self.last_update = now
self.frame += 1
if self.frame == len(self.images):
self.kill()
else:
center = self.rect.center
self.image = self.images[self.frame]
self.rect = self.image.get_rect()
self.rect.center = center