-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (88 loc) · 3.27 KB
/
main.py
File metadata and controls
110 lines (88 loc) · 3.27 KB
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
import sys
import pygame
from display import Display
from pathfinding import Pathfinder
# Static color values
white = (255, 255, 255)
grey = (128, 128, 128)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
orange = (255, 128, 0)
yellow = (255, 255, 0)
green = (0, 255, 0)
# Creates the display and runs the main loop
def main(rows, columns, cut_corners):
display = Display(rows, columns, 20)
pathfinder = Pathfinder(rows, columns, cut_corners)
running = True
while running:
run_mouse(display, pathfinder)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
run_key_input(display, event, pathfinder)
draw_nodes(display, pathfinder)
display.draw_grid()
pygame.display.update()
# Mouse movement and interaction ran every tick
def run_mouse(display, pathfinder):
mouse_pos = pygame.mouse.get_pos()
grid_pos = display.get_grid_pos(mouse_pos)
# Do not allow any mouse configuration when a path is already found
if not pathfinder.found_path:
if pygame.mouse.get_pressed()[0]:
if pathfinder.start is None:
pathfinder.start = grid_pos
elif grid_pos != pathfinder.start and pathfinder.target is None:
pathfinder.target = grid_pos
elif pygame.mouse.get_pressed()[2]:
node = pathfinder.world.get_or_create(grid_pos)
node.passable = False
# Key input on key event
def run_key_input(display, event, pathfinder):
if event.key == pygame.K_SPACE:
pathfinder.find_path()
elif event.key == pygame.K_ESCAPE:
pathfinder.reset()
elif event.key == pygame.K_DELETE:
mouse_pos = pygame.mouse.get_pos()
grid_pos = display.get_grid_pos(mouse_pos)
pathfinder.world.remove_node(grid_pos)
# Also allow clearing start and target using delete
if pathfinder.start == grid_pos:
pathfinder.start = None
if pathfinder.target == grid_pos:
pathfinder.target = None
# Draws all nodes from a pathfinder on the display
def draw_nodes(display, pathfinder):
for x in range(display.rows):
for y in range(display.columns):
color = white
coord = (x, y)
node = pathfinder.world.get_node(coord)
if coord == pathfinder.start:
color = red
elif coord == pathfinder.target:
color = blue
elif node is not None:
color = yellow
if node.path_part:
color = green
elif not node.passable:
color = black
elif node.visited:
color = orange
display.draw_rectangle(coord, color)
# Only invoke script when running from the main python file
if __name__ == "__main__":
rows = 40
columns = 40
cut_corners = False
# Either provide no args or all args
if len(sys.argv) > 1:
rows = int(sys.argv[1]) # Amount of rows to use
columns = int(sys.argv[2]) # Amount of columns to use
cut_corners = sys.argv[3] == 'True' # If the pathfinder should cut corners and make diagonal paths
main(rows, columns, cut_corners)