added an explosion animation (type SPACE while in menu) that can be called by an event

This commit is contained in:
s452635 2021-06-18 03:49:25 +02:00
parent 26aba4eae9
commit ad665ca3e3
14 changed files with 95 additions and 4 deletions

74
assets/explosion.py Normal file
View File

@ -0,0 +1,74 @@
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()

23
main.py
View File

@ -1,12 +1,21 @@
# libraries
import pygame
from pyglet.gl import * # for blocky textures
# other files of this project
from game import Game
import project_constants as const
from assets.explosion import Explosion
def update_graphics():
const.EXPLOSIONS.update()
const.EXPLOSIONS.draw(const.SCREEN)
pygame.display.flip()
def main():
pygame.init()
pygame.display.set_caption(const.V_NAME_OF_WINDOW)
@ -43,6 +52,12 @@ def main():
# checking if game should stop running
running = not is_quit_button_pressed(events)
# TODO : added for testing, remove after moving the explosion line
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
# TODO : move this line to where explosion is called
const.EXPLOSIONS.add(Explosion((2, 3)))
# drawing minefield and agent instances
game.draw_minefield()
@ -53,7 +68,7 @@ def main():
clock.tick(const.V_FPS)
# updating window graphics
pygame.display.flip()
update_graphics()
# if ok button is clicked then leave menu section
in_menu = not game.button_ok.is_clicked(pygame.mouse.get_pos(), events)
@ -109,7 +124,7 @@ def main():
game.set_is_active_flag_for_all_in_game_gui_components(False)
# updating graphics
pygame.display.flip()
update_graphics()
# update turn
game.update_time(time)
@ -127,7 +142,7 @@ def main():
game.cleanup_after_game_loop()
game.draw_minefield()
game.run_in_game_menu_overlay(pygame.mouse.get_pos(), events)
pygame.display.flip()
update_graphics()
if auto:
if not game.agent.defuse_a_mine(game.get_mine(game.goal)):
@ -164,7 +179,7 @@ def main():
auto = False
# updating graphics
pygame.display.flip()
update_graphics()
if __name__ == "__main__":

View File

@ -41,7 +41,9 @@ V_BUTTON_HEIGHT = 40
SCREEN_WIDTH = V_TILE_AREA_WIDTH + 2 * V_SCREEN_PADDING + V_NUMBER_PADDING + V_SIDE_MENU_WIDTH + 10
SCREEN_HEIGHT = V_TILE_AREA_HEIGHT + 2 * V_SCREEN_PADDING + V_NUMBER_PADDING
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
EXPLOSIONS = pygame.sprite.Group()
# =============== #

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 258 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 330 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 398 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 629 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 718 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 882 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 813 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 611 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 467 B