Skip to content

Commit 8423728

Browse files
author
Paras Stefans
authored
Optimised and added controls
1 parent 986a367 commit 8423728

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

main.py

+49-25
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@
88
WIDTH = 800
99
HEIGHT = 600
1010
FULL_SCREEN = False
11-
colours = {'black': (0 ,0, 0), 'white': (255, 255, 255), 'red': (255, 0, 0), 'green': (0, 255, 0), 'blue': (0, 0, 255), 'orange': (255, 165, 0)}
11+
DRAW_GRID = False
12+
colours = {'black': (0 ,0, 0), 'white': (255, 255, 255), 'red': (255, 0, 0), 'green': (0, 255, 0), 'blue': (0, 0, 255), 'orange': (255, 165, 0), 'True': (0, 130, 0), 'False': (130, 0, 0)}
1213
DIAGONALS = True
1314
GRID_SIZE = (60, 40)
1415

15-
#Initialise grid
16+
#Initialise Pygame and alogrithm
1617
pygame.init()
1718
pygame.display.set_caption("A* Path Finder by Pstefa")
1819
if FULL_SCREEN:
1920
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT), FULLSCREEN)
2021
else:
2122
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
23+
FONT = pygame.font.SysFont("arial", 12)
2224
RUNNING = True
2325
EXECUTING = False
24-
CLOCK = pygame.time.Clock()
25-
CELL_SIZE = (WIDTH/GRID_SIZE[0], HEIGHT/GRID_SIZE[1])
26+
CELL_SIZE = (WIDTH/GRID_SIZE[0], (HEIGHT-25)/GRID_SIZE[1])
2627
START = Cell.Cell(0, 0)
2728
END = Cell.Cell(GRID_SIZE[0] - 1, GRID_SIZE[1] - 1)
2829
WALLS = []
@@ -70,25 +71,30 @@ def reset():
7071
OPEN = []
7172
CLOSED = []
7273
SOLUTION = []
73-
WALLS = []
7474
START = Cell.Cell(0, 0)
7575
END = Cell.Cell(GRID_SIZE[0] - 1, GRID_SIZE[1] - 1)
7676

7777
def setup():
78-
global w_is_down, r_is_down, RUNNING, EXECUTING, START, OPEN
78+
global w_is_down, r_is_down, RUNNING, EXECUTING, START, OPEN, DRAW_GRID, DIAGONALS, WALLS
7979
for event in pygame.event.get():
8080
if event.type == KEYUP:
81-
if len(SOLUTION) != 0:
82-
reset()
8381
if event.key == K_w:
8482
w_is_down = False
8583
elif event.key == K_r:
8684
r_is_down = False
87-
elif event.key == K_s:
85+
elif event.key == K_s and pygame.mouse.get_pos()[1] < HEIGHT-25:
8886
set_start(pygame.mouse.get_pos())
89-
elif event.key == K_e:
87+
elif event.key == K_e and pygame.mouse.get_pos()[1] < HEIGHT-25:
9088
set_end(pygame.mouse.get_pos())
89+
elif event.key == K_g:
90+
DRAW_GRID = not DRAW_GRID
91+
elif event.key == K_d:
92+
DIAGONALS = not DIAGONALS
93+
elif event.key == K_c:
94+
WALLS = []
9195
elif event.type == KEYDOWN:
96+
if len(SOLUTION) != 0:
97+
reset()
9298
if event.key == K_ESCAPE:
9399
RUNNING = False
94100
if event.key == K_w:
@@ -102,37 +108,49 @@ def setup():
102108
elif event.type == QUIT:
103109
RUNNING = False
104110

105-
if w_is_down:
111+
if w_is_down and pygame.mouse.get_pos()[1] < HEIGHT-25:
106112
set_wall(pygame.mouse.get_pos())
107-
if r_is_down:
113+
if r_is_down and pygame.mouse.get_pos()[1] < HEIGHT-25:
108114
remove_wall(pygame.mouse.get_pos())
109115

116+
def render_text():
117+
diagonals = FONT.render(f"Diagonals: {DIAGONALS}", True, colours[f'{DIAGONALS}'])
118+
draw_grid = FONT.render(f"Draw Grid: {DRAW_GRID}", True, colours[f'{DRAW_GRID}'])
119+
SCREEN.blit(diagonals, (0, HEIGHT - diagonals.get_height()))
120+
pygame.draw.line(SCREEN, colours['black'], (diagonals.get_width() + 3, HEIGHT-20), (diagonals.get_width()+3, HEIGHT))
121+
SCREEN.blit(draw_grid, (diagonals.get_width() + 6, HEIGHT - draw_grid.get_height()))
122+
controls = FONT.render("Toggle Diagonals: D, Toggle Grid: G, Remove Wall: R, Place Wall: W, Set Start: S, Set End: E, Clear: C, Start/Cancel Search: SPACE", True, colours['black'])
123+
lineX = diagonals.get_width() + draw_grid.get_width() + 10
124+
pygame.draw.line(SCREEN, colours["black"], (lineX, HEIGHT-20), (lineX, HEIGHT))
125+
SCREEN.blit(controls, (lineX + 5, HEIGHT - diagonals.get_height()))
126+
110127
def render_cells():
111128
SCREEN.fill((255, 255, 255))
112129
#Render cells
130+
CELLx = CELL_SIZE[0] + 1
131+
CELLy = CELL_SIZE[1] + 1
113132
for wall in WALLS:
114-
cell = pygame.Rect((wall.x*CELL_SIZE[0], wall.y*CELL_SIZE[1]), (CELL_SIZE[0], CELL_SIZE[1]))
133+
cell = pygame.Rect((wall.x*CELL_SIZE[0], wall.y*CELL_SIZE[1]), (CELLx, CELLy))
115134
pygame.draw.rect(SCREEN, colours['black'], cell)
116135
for cell in OPEN:
117-
CELL = pygame.Rect((cell.x*CELL_SIZE[0], cell.y*CELL_SIZE[1]), (CELL_SIZE[0], CELL_SIZE[1]))
136+
CELL = pygame.Rect((cell.x*CELL_SIZE[0], cell.y*CELL_SIZE[1]), (CELLx, CELLy))
118137
pygame.draw.rect(SCREEN, colours['green'], CELL)
119138
for cell in CLOSED:
120-
CELL = pygame.Rect((cell.x*CELL_SIZE[0], cell.y*CELL_SIZE[1]), (CELL_SIZE[0], CELL_SIZE[1]))
139+
CELL = pygame.Rect((cell.x*CELL_SIZE[0], cell.y*CELL_SIZE[1]), (CELLx, CELLy))
121140
pygame.draw.rect(SCREEN, colours['red'], CELL)
122141
for cell in SOLUTION:
123-
CELL = pygame.Rect((cell.x*CELL_SIZE[0], cell.y*CELL_SIZE[1]), (CELL_SIZE[0], CELL_SIZE[1]))
142+
CELL = pygame.Rect((cell.x*CELL_SIZE[0], cell.y*CELL_SIZE[1]), (CELLx, CELLy))
124143
pygame.draw.rect(SCREEN, colours['orange'], CELL)
125-
start = pygame.Rect((START.x*CELL_SIZE[0], START.y*CELL_SIZE[1]), (CELL_SIZE[0], CELL_SIZE[1]))
126-
end = pygame.Rect((END.x*CELL_SIZE[0], END.y*CELL_SIZE[1]), (CELL_SIZE[0], CELL_SIZE[1]))
144+
start = pygame.Rect((START.x*CELL_SIZE[0], START.y*CELL_SIZE[1]), (CELLx, CELLy))
145+
end = pygame.Rect((END.x*CELL_SIZE[0], END.y*CELL_SIZE[1]), (CELLx, CELLy))
127146
pygame.draw.rect(SCREEN, colours['blue'], start)
128147
pygame.draw.rect(SCREEN, colours['red'], end)
129148

130-
for i in range(GRID_SIZE[0]):
131-
pygame.draw.line(SCREEN, colours['black'], (i * CELL_SIZE[0], 0), (i * CELL_SIZE[0], HEIGHT))
132-
for j in range(GRID_SIZE[1]):
133-
pygame.draw.line(SCREEN, colours['black'], (0, j * CELL_SIZE[1]), (WIDTH, j * CELL_SIZE[1]))
134-
135-
pygame.display.flip()
149+
if DRAW_GRID:
150+
for i in range(GRID_SIZE[0]):
151+
pygame.draw.line(SCREEN, colours['black'], (i * CELL_SIZE[0], 0), (i * CELL_SIZE[0], HEIGHT-25))
152+
for j in range(GRID_SIZE[1] + 1):
153+
pygame.draw.line(SCREEN, colours['black'], (0, j * CELL_SIZE[1]), (WIDTH, j * CELL_SIZE[1]))
136154

137155
def return_f(node):
138156
return node.f
@@ -184,6 +202,11 @@ def execute():
184202
for event in pygame.event.get():
185203
if event.type == QUIT:
186204
RUNNING = False
205+
if event.type == KEYDOWN:
206+
if event.key == K_SPACE:
207+
print("Path finding canceled, resetting.")
208+
EXECUTING = False
209+
reset()
187210
if len(OPEN) == 0:
188211
print("No solution, Press any key to reset!")
189212
EXECUTING = False
@@ -217,4 +240,5 @@ def execute():
217240
else:
218241
execute()
219242
render_cells()
220-
CLOCK.tick(144)
243+
render_text()
244+
pygame.display.flip()

0 commit comments

Comments
 (0)