Projekt_Sztuczna_Inteligencja/assets/explosion.py

75 lines
1.8 KiB
Python
Raw Normal View History

import pygame
import os
from project_constants import V_TILE_SIZE, DIR_ASSETS, SCREEN
import assets.display_assets
class Explosion(pygame.sprite.Sprite):
def __init__(self, coords: (int, int)):
pygame.sprite.Sprite.__init__(self)
self.explosion_animation = [
pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "explosion_frames", "explosion-" + str(x) + ".png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
for x in range(11)
]
self.last_update = pygame.time.get_ticks()
self.frame_rate = 100
self.frame = 0
self.image = self.explosion_animation[0]
self.rect = pygame.Rect(
assets.display_assets.calculate_screen_position(coords),
(V_TILE_SIZE, V_TILE_SIZE)
)
def update(self, *args, **kwargs):
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.explosion_animation):
self.kill()
else:
self.image = self.explosion_animation[self.frame]
# only for explosion animation testing
def main():
pygame.init()
explosions = pygame.sprite.Group()
running = True
while running:
explosions.update()
SCREEN.fill((0, 0, 0))
explosions.draw(SCREEN)
pygame.display.flip()
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif keys[pygame.K_g]:
explosions.add(Explosion((2, 3)))
elif keys[pygame.K_h]:
explosions.add(Explosion((3, 6)))
if __name__ == '__main__':
main()