forked from LogicJake/A-star-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (54 loc) · 2.11 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
# -*- coding: utf-8 -*-
# @Author: LogicJake
# @Date: 2019-01-06 16:52:35
# @Last Modified time: 2019-01-07 13:10:25
import pygame
import sys
from pygame import locals
from screen import Screen
from search import A_star
def main():
pygame.init()
screen = Screen()
step = 1
exit = False
pygame.display.set_caption('click block to set starting block')
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and step == 1:
click_x, click_y = pygame.mouse.get_pos()
screen.set_start(click_x, click_y)
step = 2
pygame.display.set_caption(
'click block to set obstacle blocks, press space to next step')
elif event.type == pygame.MOUSEBUTTONDOWN and step == 2:
click_x, click_y = pygame.mouse.get_pos()
screen.set_obstacle(click_x, click_y)
elif event.type == pygame.MOUSEBUTTONDOWN and step == 3:
click_x, click_y = pygame.mouse.get_pos()
exit = screen.set_end(click_x, click_y)
# exit = True
elif event.type == locals.KEYUP and event.key == locals.K_SPACE and step == 2:
step = 3
pygame.display.set_caption(
'click block to set finishing block')
elif event.type == locals.QUIT or (event.type == locals.KEYUP and event.key == locals.K_ESCAPE):
pygame.quit()
sys.exit()
screen.draw()
pygame.display.flip()
if exit:
break
pygame.display.set_caption('press space to step the search')
search = A_star(screen)
while True:
for event in pygame.event.get():
if event.type == locals.QUIT or (event.type == locals.KEYUP and event.key == locals.K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == locals.KEYUP and event.key == locals.K_SPACE and not search.over:
search.step()
screen.draw()
pygame.display.flip()
if __name__ == '__main__':
main()