diff --git a/assets/explosion.py b/assets/explosion.py new file mode 100644 index 0000000..ffa03d9 --- /dev/null +++ b/assets/explosion.py @@ -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() diff --git a/main.py b/main.py index 4b72059..f001cc2 100644 --- a/main.py +++ b/main.py @@ -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__": diff --git a/project_constants.py b/project_constants.py index 28e683e..1ea70e6 100644 --- a/project_constants.py +++ b/project_constants.py @@ -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() # =============== # diff --git a/resources/assets/explosion_frames/explosion-0.png b/resources/assets/explosion_frames/explosion-0.png new file mode 100644 index 0000000..70654e1 Binary files /dev/null and b/resources/assets/explosion_frames/explosion-0.png differ diff --git a/resources/assets/explosion_frames/explosion-1.png b/resources/assets/explosion_frames/explosion-1.png new file mode 100644 index 0000000..d861696 Binary files /dev/null and b/resources/assets/explosion_frames/explosion-1.png differ diff --git a/resources/assets/explosion_frames/explosion-10.png b/resources/assets/explosion_frames/explosion-10.png new file mode 100644 index 0000000..4426b89 Binary files /dev/null and b/resources/assets/explosion_frames/explosion-10.png differ diff --git a/resources/assets/explosion_frames/explosion-2.png b/resources/assets/explosion_frames/explosion-2.png new file mode 100644 index 0000000..783d3b8 Binary files /dev/null and b/resources/assets/explosion_frames/explosion-2.png differ diff --git a/resources/assets/explosion_frames/explosion-3.png b/resources/assets/explosion_frames/explosion-3.png new file mode 100644 index 0000000..37217fd Binary files /dev/null and b/resources/assets/explosion_frames/explosion-3.png differ diff --git a/resources/assets/explosion_frames/explosion-4.png b/resources/assets/explosion_frames/explosion-4.png new file mode 100644 index 0000000..cf49f9b Binary files /dev/null and b/resources/assets/explosion_frames/explosion-4.png differ diff --git a/resources/assets/explosion_frames/explosion-5.png b/resources/assets/explosion_frames/explosion-5.png new file mode 100644 index 0000000..122fe7e Binary files /dev/null and b/resources/assets/explosion_frames/explosion-5.png differ diff --git a/resources/assets/explosion_frames/explosion-6.png b/resources/assets/explosion_frames/explosion-6.png new file mode 100644 index 0000000..187dc95 Binary files /dev/null and b/resources/assets/explosion_frames/explosion-6.png differ diff --git a/resources/assets/explosion_frames/explosion-7.png b/resources/assets/explosion_frames/explosion-7.png new file mode 100644 index 0000000..7f666e3 Binary files /dev/null and b/resources/assets/explosion_frames/explosion-7.png differ diff --git a/resources/assets/explosion_frames/explosion-8.png b/resources/assets/explosion_frames/explosion-8.png new file mode 100644 index 0000000..e13974e Binary files /dev/null and b/resources/assets/explosion_frames/explosion-8.png differ diff --git a/resources/assets/explosion_frames/explosion-9.png b/resources/assets/explosion_frames/explosion-9.png new file mode 100644 index 0000000..cba3caa Binary files /dev/null and b/resources/assets/explosion_frames/explosion-9.png differ