-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
60 lines (52 loc) · 2.17 KB
/
game.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
import pygame
from pygame.locals import *
from queue import PriorityQueue
import sys
import helper.config as config
from objs.button import Button
from helper.utils import drawAGrid, click
from algorithm.a_star_search import a_star_search
def game():
global set_started, set_ended
pygame.init()
config.screen = pygame.display.set_mode((config.WINDOW_WIDTH, config.WINDOW_WIDTH))
#clock = pygame.time.Clock()
config.screen.fill(config.BACKGROUND_COLOR)
pygame.display.set_caption("Route Finder: Visualization")
pygame.display.flip()
config.game_grid = drawAGrid();
playing = True
config.start_key, config.end = None, None
set_started, set_ended = False, False
while playing:
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
key = pygame.key.name(event.key)
print(key)
pos = pygame.mouse.get_pos()
clicked_node = click(pos)
if key == 'r':
clicked_node.draw_and_act(clicked_node.reset, config.screen)
elif not set_started and key == 's':
config.start_key = clicked_node
clicked_node.draw_and_act(clicked_node.start, config.screen)
set_started = True
elif clicked_node != config.start_key and not set_ended and key == 'e':
config.end = clicked_node
clicked_node.draw_and_act(clicked_node.end, config.screen)
set_ended = True
elif key == 'b':
clicked_node.draw_and_act(clicked_node.obstacle, config.screen)
elif key == 'a':
[y.draw_and_act(y.reset, config.screen) for x in config.game_grid for y in x]
set_started, set_ended = False, False
if set_started and set_ended and key == 'p':
a_star_search()
if event.type == pygame.QUIT:
playing = False
pygame.display.update()
pygame.quit()
sys.exit()
if __name__ == "__game__":
game()