Merge branch 'master' into genetic_algorithm

This commit is contained in:
s452645 2021-06-20 20:43:26 +02:00
commit 2944099e93
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

@ -3,12 +3,21 @@ import threading
import pygame import pygame
from pyglet.gl import * # for blocky textures from pyglet.gl import * # for blocky textures
# other files of this project # other files of this project
from game import Game from game import Game
import project_constants as const 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(): def main():
pygame.init() pygame.init()
pygame.display.set_caption(const.V_NAME_OF_WINDOW) pygame.display.set_caption(const.V_NAME_OF_WINDOW)
@ -48,6 +57,12 @@ def main():
# checking if game should stop running # checking if game should stop running
running = not is_quit_button_pressed(events) 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 # drawing minefield and agent instances
game.draw_minefield() game.draw_minefield()
@ -58,7 +73,7 @@ def main():
clock.tick(const.V_FPS) clock.tick(const.V_FPS)
# updating window graphics # updating window graphics
pygame.display.flip() update_graphics()
# if ok button is clicked then leave menu section # if ok button is clicked then leave menu section
in_menu = not game.button_ok.is_clicked(pygame.mouse.get_pos(), events) in_menu = not game.button_ok.is_clicked(pygame.mouse.get_pos(), events)
@ -141,7 +156,7 @@ def main():
game.set_is_active_flag_for_all_in_game_gui_components(False) game.set_is_active_flag_for_all_in_game_gui_components(False)
# updating graphics # updating graphics
pygame.display.flip() update_graphics()
# update turn # update turn
game.update_time(time) game.update_time(time)
@ -166,7 +181,7 @@ def main():
game.cleanup_after_game_loop() game.cleanup_after_game_loop()
game.draw_minefield() game.draw_minefield()
game.run_in_game_menu_overlay(pygame.mouse.get_pos(), events) game.run_in_game_menu_overlay(pygame.mouse.get_pos(), events)
pygame.display.flip() update_graphics()
if auto: if auto:
if not game.agent.defuse_a_mine(game.get_mine(game.goal)): if not game.agent.defuse_a_mine(game.get_mine(game.goal)):
@ -204,7 +219,7 @@ def main():
auto = False auto = False
# updating graphics # updating graphics
pygame.display.flip() update_graphics()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -44,7 +44,9 @@ V_BUTTON_HEIGHT = 40
SCREEN_WIDTH = V_TILE_AREA_WIDTH + 2 * V_SCREEN_PADDING + V_NUMBER_PADDING + V_SIDE_MENU_WIDTH + 10 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_HEIGHT = V_TILE_AREA_HEIGHT + 2 * V_SCREEN_PADDING + V_NUMBER_PADDING
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 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