8
8
WIDTH = 800
9
9
HEIGHT = 600
10
10
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 )}
12
13
DIAGONALS = True
13
14
GRID_SIZE = (60 , 40 )
14
15
15
- #Initialise grid
16
+ #Initialise Pygame and alogrithm
16
17
pygame .init ()
17
18
pygame .display .set_caption ("A* Path Finder by Pstefa" )
18
19
if FULL_SCREEN :
19
20
SCREEN = pygame .display .set_mode ((WIDTH , HEIGHT ), FULLSCREEN )
20
21
else :
21
22
SCREEN = pygame .display .set_mode ((WIDTH , HEIGHT ))
23
+ FONT = pygame .font .SysFont ("arial" , 12 )
22
24
RUNNING = True
23
25
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 ])
26
27
START = Cell .Cell (0 , 0 )
27
28
END = Cell .Cell (GRID_SIZE [0 ] - 1 , GRID_SIZE [1 ] - 1 )
28
29
WALLS = []
@@ -70,25 +71,30 @@ def reset():
70
71
OPEN = []
71
72
CLOSED = []
72
73
SOLUTION = []
73
- WALLS = []
74
74
START = Cell .Cell (0 , 0 )
75
75
END = Cell .Cell (GRID_SIZE [0 ] - 1 , GRID_SIZE [1 ] - 1 )
76
76
77
77
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
79
79
for event in pygame .event .get ():
80
80
if event .type == KEYUP :
81
- if len (SOLUTION ) != 0 :
82
- reset ()
83
81
if event .key == K_w :
84
82
w_is_down = False
85
83
elif event .key == K_r :
86
84
r_is_down = False
87
- elif event .key == K_s :
85
+ elif event .key == K_s and pygame . mouse . get_pos ()[ 1 ] < HEIGHT - 25 :
88
86
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 :
90
88
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 = []
91
95
elif event .type == KEYDOWN :
96
+ if len (SOLUTION ) != 0 :
97
+ reset ()
92
98
if event .key == K_ESCAPE :
93
99
RUNNING = False
94
100
if event .key == K_w :
@@ -102,37 +108,49 @@ def setup():
102
108
elif event .type == QUIT :
103
109
RUNNING = False
104
110
105
- if w_is_down :
111
+ if w_is_down and pygame . mouse . get_pos ()[ 1 ] < HEIGHT - 25 :
106
112
set_wall (pygame .mouse .get_pos ())
107
- if r_is_down :
113
+ if r_is_down and pygame . mouse . get_pos ()[ 1 ] < HEIGHT - 25 :
108
114
remove_wall (pygame .mouse .get_pos ())
109
115
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
+
110
127
def render_cells ():
111
128
SCREEN .fill ((255 , 255 , 255 ))
112
129
#Render cells
130
+ CELLx = CELL_SIZE [0 ] + 1
131
+ CELLy = CELL_SIZE [1 ] + 1
113
132
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 ))
115
134
pygame .draw .rect (SCREEN , colours ['black' ], cell )
116
135
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 ))
118
137
pygame .draw .rect (SCREEN , colours ['green' ], CELL )
119
138
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 ))
121
140
pygame .draw .rect (SCREEN , colours ['red' ], CELL )
122
141
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 ))
124
143
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 ))
127
146
pygame .draw .rect (SCREEN , colours ['blue' ], start )
128
147
pygame .draw .rect (SCREEN , colours ['red' ], end )
129
148
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 ]))
136
154
137
155
def return_f (node ):
138
156
return node .f
@@ -184,6 +202,11 @@ def execute():
184
202
for event in pygame .event .get ():
185
203
if event .type == QUIT :
186
204
RUNNING = False
205
+ if event .type == KEYDOWN :
206
+ if event .key == K_SPACE :
207
+ print ("Path finding canceled, resetting." )
208
+ EXECUTING = False
209
+ reset ()
187
210
if len (OPEN ) == 0 :
188
211
print ("No solution, Press any key to reset!" )
189
212
EXECUTING = False
@@ -217,4 +240,5 @@ def execute():
217
240
else :
218
241
execute ()
219
242
render_cells ()
220
- CLOCK .tick (144 )
243
+ render_text ()
244
+ pygame .display .flip ()
0 commit comments