-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (115 loc) · 4.83 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import pygame
from src.resources import Board, Astar
from src.resources.Point import Square
from src.resources.Astar import A_star
from src.util import board_utils
from enum import Enum
import time
class Colors(Enum):
barrier = (0, 0, 0)
visited = (0, 255, 0)
empty = (255, 255, 255)
path = (255, 69, 0)
# pygamesetup
pygame.init()
# defining the width and height
size = (int(1920), int(1080))
# defining margin and block size
MARGIN = 1
BLOCK_SIZE = 50
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
clock = pygame.time.Clock()
running = True
font = pygame.font.Font(pygame.font.get_default_font(), int(BLOCK_SIZE / 5))
maze = Board.Board(5, 5, size, screen, MARGIN, BLOCK_SIZE, font, offset=50)
a_star = Astar.A_star(maze)
# font
while running:
# poll for events
# pygame.QUIT event means the user clicked X to close your window
# NOTE: in the grid, elements should be accesed as [row][column] and NOT [column][row].
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# left clicks make current cell barrier
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
try:
column, row = board_utils.get_square(pos, MARGIN, BLOCK_SIZE)
maze.make_barrier(row, column)
except:
print("out of bounds")
print(pos)
# rigth clicks delete current cell and make it empty
if pygame.mouse.get_pressed()[2]:
try:
pos = pygame.mouse.get_pos()
column, row = board_utils.get_square(pos, MARGIN, BLOCK_SIZE)
maze.make_empty(row, column)
except:
print("out of bounds")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_g:
# making square goal
# TODO: ensure that just one goal exist per maze
pos = pygame.mouse.get_pos()
column, row = board_utils.get_square(pos, MARGIN, BLOCK_SIZE)
maze.make_goal(row, column, a_star)
if event.key == pygame.K_d:
# deleting current square and leaving it white
pos = pygame.mouse.get_pos()
column, row = board_utils.get_square(pos, MARGIN, BLOCK_SIZE)
maze.make_empty(row, column)
if event.key == pygame.K_s:
pos = pygame.mouse.get_pos()
column, row = board_utils.get_square(pos, MARGIN, BLOCK_SIZE)
maze.make_start(row, column, a_star)
if event.key == pygame.K_i:
pos = pygame.mouse.get_pos()
column, row = board_utils.get_square(pos, MARGIN, BLOCK_SIZE)
# g and h functions
# getting current square
currentSquare = maze.grid[row][column]
print(f"g: {currentSquare.euclidean_distance(maze.start)}")
print(f"h: {currentSquare.euclidean_distance(maze.goal)}")
print(
f"f: {currentSquare.euclidean_distance(maze.start)+currentSquare.euclidean_distance(maze.goal)}"
)
# drawing data
g = maze.font.render(
f"g: {currentSquare.euclidean_distance(maze.start)}", True, "black"
)
h = maze.font.render(
f"h: {currentSquare.euclidean_distance(maze.goal)}", True, "black"
)
f = maze.font.render(
f"f: {currentSquare.euclidean_distance(maze.goal) + currentSquare.euclidean_distance(maze.start)}",
True,
"black",
)
screen.blit(g, (0, 0))
currentSquare.make_path()
print(maze.grid[row][column].compute_neighbors(maze))
if event.key == pygame.K_r:
if not maze.has_goal() and not maze.has_start():
maze.add_message("Maze could not be solved")
maze.add_message("Goal and start point")
maze.add_message("Not setted properly")
else:
maze.add_message("solving...")
solver = A_star(maze)
solver.solve()
# before solving...
if event.key == pygame.K_SPACE:
print("pressing space")
pos = pygame.mouse.get_pos()
# getting square
column, row = board_utils.get_square(pos, MARGIN, BLOCK_SIZE)
screen.fill("cadetblue4")
maze.draw()
# fill the screen with a color to wipe away anything from last frame
# RENDER YOUR GAME HERE
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60) # limits FPS to 60
pygame.quit()