Projekt_Sztuczna_Inteligencja/main.py

172 lines
5.4 KiB
Python
Raw Normal View History

# libraries
import pygame
from pyglet.gl import * # for blocky textures
# other files of this project
from game import Game
2021-03-14 19:18:23 +01:00
import project_constants as const
2021-03-10 14:01:17 +01:00
2021-03-12 09:55:59 +01:00
def main():
2021-03-10 14:01:17 +01:00
pygame.init()
2021-03-14 19:18:23 +01:00
pygame.display.set_caption(const.V_NAME_OF_WINDOW)
2021-03-11 18:51:43 +01:00
# for blocky textures
glEnable(GL_TEXTURE_2D)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
# FPS clock
clock = pygame.time.Clock()
# creating some auxiliary functions
def is_quit_button_pressed(_events):
return any([_event.type == pygame.QUIT for _event in _events])
# setting flags for program
2021-03-10 14:01:17 +01:00
running = True
in_menu = True
2021-05-23 08:43:31 +02:00
auto = False
is_game_over = False
# create and initialize_gui_components game instance
game = Game()
game.initialize_gui_components()
while running:
# ============== #
# ==== MENU ==== #
# ============== #
2021-05-23 08:43:31 +02:00
while running and in_menu and not auto:
events = pygame.event.get()
# checking if game should stop running
running = not is_quit_button_pressed(events)
# drawing minefield and agent instances
game.draw_minefield()
# drawing gui overlay
game.run_in_game_menu_overlay(pygame.mouse.get_pos(), events)
# ticking to maintain good fps number
clock.tick(const.V_FPS)
# updating window graphics
pygame.display.flip()
# if ok button is clicked then leave menu section
in_menu = not game.button_ok.is_clicked(pygame.mouse.get_pos(), events)
2021-05-23 08:43:31 +02:00
# if auto button is clicked then leave menu and start auto-mode
auto = game.button_auto.is_clicked(pygame.mouse.get_pos(), events)
# ========================== #
# ==== BEFORE GAME LOOP ==== #
# ========================== #
2021-05-23 18:02:35 +02:00
# initializing action_sequence variable
action_sequence = None
# getting action sequence for agent
2021-05-23 18:02:35 +02:00
if auto and running:
2021-05-23 08:43:31 +02:00
in_menu = False
auto = game.set_random_mine_as_target()
2021-05-23 08:43:31 +02:00
action_sequence = game.get_action_sequence("mine")
2021-05-23 18:02:35 +02:00
elif running:
2021-05-23 08:43:31 +02:00
action_sequence = game.get_action_sequence("tile")
# initializing game attributes before the game loop
game.initialize_before_game_loop()
# =================== #
# ==== GAME LOOP ==== #
# =================== #
while running and not in_menu and not is_game_over:
events = pygame.event.get()
# checking if game should stop running
running = not is_quit_button_pressed(events)
# FPS control
clock.tick(const.V_FPS)
# getting time since last tick
time = clock.get_time()
# drawing minefield and agent instances
game.draw_minefield()
2021-05-23 08:43:31 +02:00
# handling auto button (clicking quits auto-mode)
if auto:
game.button_auto.set_flags(is_active=True)
auto = not game.button_auto.is_clicked(pygame.mouse.get_pos(), events)
# drawing inactive gui components so they don't "disappear"
game.run_in_game_menu_overlay(pygame.mouse.get_pos(), events)
game.set_is_active_flag_for_all_in_game_gui_components(False)
# updating graphics
pygame.display.flip()
# update turn
game.update_time(time)
game.update_turns()
# make the next move from sequence of actions
if game.agent_should_take_next_action(action_sequence):
# give agent next action
game.agent_take_next_action(action_sequence)
# reset values after the game loop
if game.agent_made_all_actions(action_sequence):
# clean up after game loop
game.agent_take_last_action()
game.cleanup_after_game_loop()
game.draw_minefield()
2021-05-26 13:47:02 +02:00
game.run_in_game_menu_overlay(pygame.mouse.get_pos(), events)
pygame.display.flip()
if auto:
if not game.agent.defuse_a_mine(game.get_mine(game.goal)):
print("BOOOOOOM\n\n")
is_game_over = True
else:
print("guess you will live a little longer...\n\n")
# reset in_menu flag
in_menu = True
# TODO temporarily disabling game over screen
# if you want to enable game over uncomment line below
# is_game_over = game.time_mine_exploded()
while running and is_game_over:
events = pygame.event.get()
clock.tick(const.V_FPS)
# runs game over screen
game.run_game_over_screen(pygame.mouse.get_pos(), events)
# checking if game should stop running
running = not is_quit_button_pressed(events) \
and not game.button_close.is_clicked(pygame.mouse.get_pos(), events)
# resetting is_game_over variable if player tries again
if game.button_try_again.is_clicked(pygame.mouse.get_pos(), events):
game.cleanup_after_game_loop()
is_game_over = False
in_menu = True
auto = False
# updating graphics
pygame.display.flip()
2021-03-12 09:55:59 +01:00
if __name__ == "__main__":
main()