-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
172 lines (140 loc) · 5.95 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
163
164
165
166
167
168
169
170
171
172
import arcade
import pymunk
import os
from user_modules import Spot
from user_modules import UserMap
from user_modules import Car
from user_modules import Path
from user_modules import Obstacle
from user_modules import TextButton
from user_modules import SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_TITLE, DIV, DT, G_W, G_H
class AICar(arcade.Window):
def __init__(self, width, height, title):
super().__init__(width, height, title)
# Related to Map
self.PATH = None
self.MAP = None
self.CAR = None
self.obstacle_button = None
self.reset_button = None
self.debug_button = None
self.obstacles = None
self.text_state = "Click and Drag your mouse over the grid to place obstacles"
self.extra_text = " "
self.status = None
arcade.set_background_color(arcade.color.WHITE)
# Related to Physics
self.space = None
def setup(self):
# Setup Space
self.space = pymunk.Space()
self.space.gravity = (0.0, 0.0)
self.status = {'start_selected': False, 'goal_selected': False, 'found_path': False, 'draw_obstacles': True, 'debug': False}
self.extra_text = " "
# Setting up Map
self.MAP = UserMap(DIV)
for i in range(DIV):
spots = []
for j in range(DIV):
spots.append(Spot(i, j))
self.MAP.add_spots(spots)
self.MAP.calculate_inital_grid()
# Setting up GUI
self.obstacle_button = TextButton(110, SCREEN_HEIGHT - 20, 200, 30, "Finish Drawing")
self.reset_button = TextButton(360, SCREEN_HEIGHT - 20, 80, 30, "Reset")
self.debug_button = TextButton(450, SCREEN_HEIGHT - 20, 80, 30, "Debug")
print("Setup Finished")
# Setting up Space
self.CAR = Car()
self.space.add(self.CAR.body, self.CAR.shape)
# Setting up Path
self.PATH = Path()
# Setting Obstacles
self.obstacles_shape_list = arcade.ShapeElementList()
def next_state(self):
self.status['goal_selected'] = False
self.status['found_path'] = False
self.MAP.next_state()
self.CAR.next_state()
self.PATH = Path()
def on_draw(self):
arcade.start_render()
if not self.status['found_path']:
self.MAP.draw_grid()
self.MAP.draw_pathfinder()
else:
self.PATH.draw_path(self.status['debug'])
self.obstacles_shape_list.draw()
self.CAR.draw_car(self.status['debug'])
self.obstacle_button.draw()
self.reset_button.draw()
self.debug_button.draw()
arcade.draw_text(self.text_state, 5, SCREEN_HEIGHT - 50, arcade.color.BLACK, 14, anchor_x="left", anchor_y="top")
arcade.draw_text(self.extra_text, 5, SCREEN_HEIGHT - 75, arcade.color.BLACK, 14, anchor_x="left", anchor_y="top")
def on_update(self, delta_time):
if not self.status['found_path']:
result = self.MAP.update_map(self.status['goal_selected'])
if result is not None:
self.status['found_path'] = result['status']
if self.status['found_path']:
self.PATH.add_spots(result['path'])
obstacles = self.MAP.get_obstacles()
for o in obstacles:
obs = Obstacle(o)
self.space.add(obs.return_shape())
self.obstacles_shape_list.append(obs.get_obstacle())
points, boundaries = self.PATH.get_points_and_boundaries()
self.CAR.set_path(points, boundaries)
else:
self.extra_text = "No Path Found! Select New Path"
self.status['goal_selected'] = False
self.MAP.reset_goal()
else:
moving = self.CAR.follow_path()
self.CAR.update()
if not moving:
self.next_state()
self.space.step(DT)
def on_mouse_drag(self, x, y, dx, dy, button, modifiers):
row = x // G_W
column = y // G_H
if self.status['draw_obstacles']:
self.MAP.set_wall(row, column)
def on_mouse_release(self, x, y, button, modifiers):
if self.obstacle_button.clicked(x, y):
self.status['draw_obstacles'] = not self.status['draw_obstacles']
if not self.status['draw_obstacles']:
self.text_state = "Select two end points, first for the start then for the end."
self.obstacle_button.change_text("Draw Obstacles")
else:
self.obstacle_button.change_text("Finish Drawing")
self.text_state = "Click and Drag your mouse over the grid to place obstacles"
elif self.reset_button.clicked(x, y):
self.setup()
elif self.debug_button.clicked(x, y):
self.status['debug'] = not self.status['debug']
else:
if not self.status['draw_obstacles']:
row = x // G_W
column = y // G_H
if not self.status['start_selected']:
start = self.MAP.set_start(row, column)
if start is not None:
self.status['start_selected'] = True
self.CAR.set_start(start)
else:
self.extra_text = "You cannot start on a wall!"
else:
if not self.status['goal_selected']:
goal = self.MAP.set_goal(row, column)
if goal is not None:
self.status['goal_selected'] = True
self.CAR.set_goal(goal)
else:
self.extra_text = "Goal cannot be set over an obstacle!"
def main():
window = AICar(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
window.setup()
arcade.run()
if __name__ == "__main__":
main()