Projekt_Sztuczna_Inteligencja/main.py

145 lines
4.3 KiB
Python

# libraries
import pygame
from pyglet.gl import * # for blocky textures
# other files of this project
from game import Game
import project_constants as const
def main():
pygame.init()
pygame.display.set_caption(const.V_NAME_OF_WINDOW)
# 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
running = True
in_menu = True
is_game_over = False
# create and initialize_gui_components game instance
game = Game()
game.initialize_gui_components()
while running:
# ============== #
# ==== MENU ==== #
# ============== #
while running and in_menu:
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)
# ========================== #
# ==== BEFORE GAME LOOP ==== #
# ========================== #
# getting action sequence for agent
action_sequence = game.get_action_sequence()
# 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()
# 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()
# reset in_menu flag
in_menu = True
is_game_over = game.time_mine_exploded()
# TODO temporarily disabling game over screen
# if you want to enable game over comment line below
is_game_over = False
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
# updating graphics
pygame.display.flip()
if __name__ == "__main__":
main()