Projekt_Sztuczna_Inteligencja/main.py
2021-04-17 21:33:50 +02:00

88 lines
2.4 KiB
Python

# libraries
import ctypes
import pygame
import time
from pyglet.gl import * # for blocky textures
# other files of this project
import project_constants as const
import minefield as mf
import searching_algorithms.bfs as bfs
from display_assets import display_sapper
def main():
pygame.init()
pygame.display.set_caption(const.V_NAME_OF_WINDOW)
# FPS clock
clock = pygame.time.Clock()
# for blocky textures
glEnable(GL_TEXTURE_2D)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
# create an instance of Minefield, pass necessary data
minefield = mf.Minefield(const.MAP_RANDOM_10x10)
# get sequence of actions found by BFS algorythm
action_sequence = bfs.graphsearch(initial_state=bfs.State(row=minefield.agent.position[0],
column=minefield.agent.position[1],
direction=const.Direction.UP),
minefield=minefield)
running = True
while running:
# FPS control
clock.tick(const.V_FPS)
# ================ #
# === GRAPHICS === #
# ================ #
# background grid (fills frame with white, blits grid)
const.SCREEN.fill((255, 255, 255))
const.SCREEN.blit(
const.ASSET_BACKGROUND,
(
const.V_SCREEN_PADDING,
const.V_SCREEN_PADDING
)
)
# draw tiles and mines
minefield.draw(const.SCREEN)
# sapper
display_sapper(
minefield.agent.position[0],
minefield.agent.position[1],
minefield.agent.direction
)
# update graphics after blitting
pygame.display.update()
# make the next move from sequence of actions
if any(action_sequence):
action = action_sequence.pop(0)
if action == const.Action.ROTATE_LEFT:
minefield.agent.rotate_left()
elif action == const.Action.ROTATE_RIGHT:
minefield.agent.rotate_right()
elif action == const.Action.GO:
minefield.agent.go()
time.sleep(const.ACTION_INTERVAL)
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if __name__ == "__main__":
main()