-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (72 loc) · 2.91 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# this script contains the main loop
# imports
from classdef import Grid, Choice
from dijkstra import dijkstra
from astar import aStar
from bestfirst import bestFirst
from showPath import showpath
from gui import gui, notFound
import pygame
import logging
# constants
# screen resolution
WIDTH = 700
HEIGHT = 700
# colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BG_COL = (0, 64, 128)
# functions
def redraw():
win.fill(BLACK) # fills the window after each frame
pygame.draw.rect(win, BG_COL, (0, 0, round(WIDTH * 0.25), HEIGHT)) # draws frame for holding the options
pygame.draw.rect(win, WHITE, mainGrid.getPos()) # draws frame for the node grid
mainGrid.draw(win, buttonFont)
pygame.display.update()
# user input
choice = Choice()
gui(choice)
delay = choice.speed
algorithms = [dijkstra, aStar, bestFirst]
pathfinder = algorithms[choice.algo]
# creating the grid
mainGrid = Grid((WIDTH // 4), 0, WIDTH - (WIDTH // 4), HEIGHT, choice.nodes)
# setting up a logger
logging.basicConfig(filename='history.log', format='%(asctime)s %(message)s', level=logging.DEBUG)
logger = logging.getLogger() # getting an object called logger
# pygaame window
pygame.init() # pygame window init
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Pathfinder Visualization')
clk = pygame.time.Clock()
FPS = 60 # frames per second
buttonFont = pygame.font.SysFont('Microsoft YaHei Light', 25) # button font
run = True # for controlling the main loop
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# anyting between the lines are in the main loop
while run:
clk.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False # stop the game if user tries to quit
if pygame.mouse.get_pressed()[0]: # only detects left click
mainGrid.clickOnGrid(pygame.mouse.get_pos()) # checks if the click happened on grid
if mainGrid.vizStarted:
if mainGrid.start not in mainGrid.open and mainGrid.start not in mainGrid.closed:
mainGrid.open.append(mainGrid.start) # append the start node to the open list
mainGrid.start.hcost = mainGrid.start.calcHcost(mainGrid.end)
mainGrid.start.fcost = mainGrid.start.gcost + mainGrid.start.hcost
if pathfinder(mainGrid, delay):
if len(mainGrid.open) < 1:
run = False # path not found
else:
showpath(mainGrid.end) # draws the path it has found
redraw()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if len(mainGrid.open) < 1: # no valid path found
notFound()
logger.info(f'Algorithm: {choice.algo} Speed: {choice.speed} Nodes in a row: {choice.nodes}. Path not found')
else:
logger.info(f'Algorithm: {choice.algo} Speed: {choice.speed} Nodes in a row: {choice.nodes}. Path found')
pygame.quit()