-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
117 lines (88 loc) · 3.41 KB
/
Copy pathmain.py
File metadata and controls
117 lines (88 loc) · 3.41 KB
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
import pygame
from Drawer import Drawer
from Grid import Grid
from RectangleController import RectangleController
pygame.init()
# Keyboard control guide:
# O = New obstacle, A = New rectangle group, R = Restart, T = Set target, G = Start animation
def get_grid_pos(mouse_x, mouse_y):
return mouse_x // (WIDTH // width_rectangle_amount), mouse_y // (HEIGHT // height_rectangle_amount)
# return True if obstacle is created
def add_obstacle(grid, mouse_x, mouse_y):
pos = get_grid_pos(mouse_x, mouse_y)
grid.add_obstacle(pos)
# returns true if rectangle group is created
def add_rectangle_group(rectangle_controller, group_id, mouse_x, mouse_y):
pos = get_grid_pos(mouse_x, mouse_y)
return rectangle_controller.add_rectangle_group(group_id, pos, grid.target)
def create_target(pos):
grid_pos = get_grid_pos(pos[0], pos[1])
grid.add_target(grid_pos)
def create_random_obstacles(percentage):
grid.random_obstacles(percentage)
def animate():
rectangle_controller.grow_groups()
def restart():
global user_input_allowed
global next_rectangle_group_id
global created_first_rectangle
grid.restart()
rectangle_controller.restart()
created_first_rectangle = False
next_rectangle_group_id = 0
user_input_allowed = True
next_rectangle_group_id = 0
# Setup the screen
WIDTH = 600 # optional value
HEIGHT = 300 # optional value
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shortest path finder")
# Setup sizes
width_rectangle_amount = 60 # optional value, WIDTH divisor preferred
height_rectangle_amount = 30 # optional value, HEIGHT divisor preferred
rectangle_width = WIDTH // width_rectangle_amount
rectangle_height = HEIGHT // height_rectangle_amount
vertical_line_width = rectangle_width // 10
horizontal_line_width = rectangle_height // 10
# Instantiate classes
grid = Grid(width_rectangle_amount, height_rectangle_amount)
rectangle_controller = RectangleController(grid)
grid.rectangle_controller = rectangle_controller
drawer = Drawer(WIDTH, HEIGHT, screen, grid, rectangle_controller, rectangle_width, rectangle_height,
vertical_line_width, horizontal_line_width)
# Other variables
created_first_rectangle = False
next_rectangle_group_id = 0
# Program loop
running = True
user_input_allowed = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pressed = pygame.key.get_pressed()
mouse_x, mouse_y = pygame.mouse.get_pos()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_g and next_rectangle_group_id > 0:
user_input_allowed = False
if event.key == pygame.K_a and user_input_allowed:
if add_rectangle_group(rectangle_controller, next_rectangle_group_id, mouse_x, mouse_y):
next_rectangle_group_id += 1
if pressed[pygame.K_r]:
restart()
if user_input_allowed:
if pressed[pygame.K_o]:
add_obstacle(grid, mouse_x, mouse_y)
if pressed[pygame.K_t]:
create_target((mouse_x, mouse_y))
if pressed[pygame.K_z]:
create_random_obstacles(25) # obstacle percentage in grid
drawer.draw()
pygame.display.update()
if user_input_allowed:
clock.tick(120)
else:
animate()
clock.tick(30)
pygame.quit()