-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
55 lines (45 loc) · 1.63 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
# !/bin/python3
# main.py
"""Integrating the quadtree algorithm with GUI."""
import pygame
import quadtree
# Initializing pygame
pygame.init()
# width and height of the main window screen
width, height = 1200, 600
# Creating a surface
surface = pygame.display.set_mode((width, height))
# Title of the window:
pygame.display.set_caption('Quadtree-Python')
# Creating a quadtree
root = quadtree.Rectangle(0, 0, width, height)
qt = quadtree.Quadtree(root, 4)
points = [] # list to store the point clicks
color = (13, 17, 23) # main window color
color_rect = (201, 209, 217) # color for rectangles
color_point = (54, 213, 83) # color for points
surface.fill(color) # filling the main window with color
# Window loop
running = True
while running:
# Drawing the root rectangle
pygame.draw.rect(surface, color_rect, pygame.Rect(0, 0, width, height), 1)
ev_list = pygame.event.get() # list of events
for event in ev_list:
# Check if the quit event has happened and exit the loop
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos() # Get coordinates of mouse click
if pos not in points:
points.append(pos)
pygame.draw.circle(surface, color_point, pos, 2)
qt.insert(quadtree.Point(*pos))
qt.printsub()
# Drawing the rectangle tree
for r in quadtree.RECTANGLES:
pygame.draw.rect(surface, color_rect, pygame.Rect(*eval(str(r))), 1)
# Updating the window
pygame.display.flip()
# Exiting PyGame
pygame.quit()