-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.py
81 lines (61 loc) · 1.96 KB
/
UI.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
from RaceTrack import *
import AI
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
from pygame import *
import pygame
def main(size, scale):
# Initialize a new track
track = RaceTrack(size, scale)
# Begin the user input loop
running = True
while running:
# Check and process event queue
for event in pygame.event.get():
# On click add walls
if event.type == pygame.MOUSEBUTTONDOWN:
track.addWalls()
if event.type == pygame.KEYDOWN:
# On enter run the algorithm on the current track
if event.__dict__['unicode'] == '\r':
track.addPath(AI.AStar);
# On delete clear the board for a new run
if event.__dict__['unicode'] == '\b':
track.clearTrack();
# only do something if the event is of type QUIT
if event.type == pygame.QUIT:
# change the value to False, to exit the main loop
running = False
if __name__ == '__main__':
print(
"\n\n"
"Welcome to CAIR!\n"
"To interact with the UI upon parameter entry use the following commands:\n"
"Click and Hold: Draw walls that the car must navigate around\n"
"Enter: Run the pathfinding algorithm and trace the cars path\n"
"Delete: Remove the current path and draw a new one\n\n"
"You can choose to use custom size and scale parameters or press enter for the default.\n"
"I have found a size of 60 to be a sweet spot where you can have many obstacles and \n"
"sub minute runtimes on a slow laptop processor."
)
while True:
# Initialize the default
userInts = (60, 10)
# Ask for user input
userVariables = input(
"\nPress Enter for default, or input size and scale in form '60 10':"
).split(" ")
# Check for default and print if chosen
if userVariables == ['']:
print("60 10")
break
try:
userInts = list(map(lambda x : int(x), userVariables))
except:
print("All values must be castable to integers")
continue
if len(userInts) == 2:
break
elif len(userInts) != 2:
print("Invalid # of parameters")
main(*userInts)