-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
162 lines (131 loc) · 6.59 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
152
153
154
155
156
157
158
159
160
161
162
import gui_menu
import sys
from tile import *
from threading import Thread
from time import sleep
class Board:
size = width, height = 600, 600
black = (0, 0, 0)
grid = []
target_points = []
def __init__(self):
self.grid = self.generate_drawable_grid()
self.drawing_mode = True
self.generating_path = False
global screen, grid
pygame.init()
screen = pygame.display.set_mode((self.width, self.height))
screen.fill(self.black)
Thread(target=gui_menu.gui, args=(self, ), daemon=True).start() # get a thread to manage the Tkinter
def reset(self):
self.grid = self.generate_drawable_grid()
self.drawing_mode = True
self.generating_path = False
self.target_points = []
def generate_drawable_grid(self):
drawable_grid = []
for x in range(self.width // Tile.block_size): # go through each possible position
drawable_grid.append([])
for y in range(self.height // Tile.block_size):
drawable_grid[x].insert(y, DrawableTile(x * Tile.block_size, y * Tile.block_size, Tile.block_size,
Tile.block_size)) # Create a new tile
return drawable_grid
def find_path(self, show_visual):
self.convert_from_drawable_grid()
self.generating_path = True
current_tile = self.target_points[0] # set the current tile to the first tile
current_tile.f = 0
open_list = [current_tile]
closed_list = []
while self.generating_path:
smallest_f = float("inf")
current_tile = None
for tile in open_list:
if not isinstance(tile, TargetPosition):
if show_visual:
tile.change_colour(WalkableTile.open_list_colour)
if tile.f < smallest_f:
smallest_f = tile.f
current_tile = tile
if current_tile == self.target_points[1]: # If the path is at the end point
parent = current_tile.parent
while parent is not self.target_points[0]: # while the backtrack is not at the start
if not isinstance(parent, TargetPosition):
parent.change_colour(TraversableTile.path_colour)
parent = parent.parent
self.generating_path = False
break # end the search loop
if len(open_list) == 0:
gui_menu.pop_up_message("There is no possible path")
break
open_list.remove(current_tile)
closed_list.append(current_tile)
if not isinstance(current_tile, TargetPosition) and show_visual:
current_tile.change_colour(WalkableTile.closed_list_colour)
adjacent_tiles = current_tile.get_touching_walkways(self) # get the 8 surrounding tiles
for tile in adjacent_tiles:
if not isinstance(tile, TraversableTile) or tile in closed_list: # if the tile is not in the closed list and is not an obstacle
continue # if it is then skip the tile
current_path_g = current_tile.g + tile.distance_to(current_tile)
current_path_h = tile.distance_to(self.target_points[1])
current_path_f = current_path_g + current_path_h
if tile not in open_list: # if the tile is not in the open list
open_list.append(tile)
tile.parent = current_tile
tile.g = current_path_g
tile.h = current_path_h
tile.f = current_path_f
else:
if current_path_g < tile.g:
tile.parent = current_tile
tile.g = current_path_g
tile.h = current_path_h
tile.f = current_path_f
if show_visual:
sleep(0.01)
def convert_from_drawable_grid(self,):
self.drawing_mode = False
for x in range(len(self.grid)):
for y in range(len(self.grid[x])):
tile = self.grid[x][y]
if isinstance(tile, DrawableTile):
if tile.is_obstacle:
self.grid[x][y] = Obstacle(tile)
else:
self.grid[x][y] = WalkableTile(tile)
def draw_grid(board):
for x in board.grid:
for tile in x:
pygame.draw.rect(screen, Tile.border_colour, tile, 1) # Draw the outside of the rectangle
pygame.draw.rect(screen, tile.colour, tile.inner, 0) # Draw the inside of the rectangle
def main():
board = Board()
while True:
draw_grid(board)
for event in pygame.event.get():
if event.type == pygame.QUIT: # quit the game
pygame.quit()
sys.exit()
if board.drawing_mode:
if event.type == pygame.MOUSEBUTTONDOWN and ((event.button == 2) or (event.button == 1 and pygame.key.get_pressed()[pygame.K_LCTRL])):
x, y = Tile.get_mouse_grid_coord() # convert the mouse position to the grid x and y coords
cell = board.grid[x][y] # get the cell at the current point
if not isinstance(cell, TargetPosition):
if len(board.target_points) < 2: # ensure that there are no more than two points set
board.grid[x][y] = TargetPosition(cell.left, cell.top, cell.width, cell.height)
board.target_points.append(board.grid[x][y])
else:
board.target_points.remove(board.grid[x][y])
board.grid[x][y] = DrawableTile(cell.left, cell.top, cell.width, cell.height)
if board.drawing_mode:
if pygame.mouse.get_pressed()[0] == 1 and pygame.mouse.get_pressed()[2] == 0 and not pygame.key.get_pressed()[pygame.K_LCTRL]: # if lmb is clicked
x, y = Tile.get_mouse_grid_coord()
if not isinstance(board.grid[x][y], TargetPosition):
board.grid[x][y].set_obstacle(True) # set the tile as an obstacle
if pygame.mouse.get_pressed()[0] == 0 and pygame.mouse.get_pressed()[2] == 1: # if rmb is clicked
x, y = Tile.get_mouse_grid_coord()
if not isinstance(board.grid[x][y], TargetPosition):
board.grid[x][y].set_obstacle(False) # set the tile as an obstacle
pygame.display.update()
if __name__ == "__main__":
main()