-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddNode.py
75 lines (60 loc) · 2.12 KB
/
AddNode.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
import pygame, sys, random
import pygame.locals as pl
import pygame.event as pe
import AddEdge
from DFS import *
from Node import *
from Graph import *
import Handle_Button
# collide fund for checking if two nodes collide
def collide(pos, node):
if node.isOver(pos):
return True
return False
# create_node func for creatind a new node
def create_node(node, pos, i):
node.pos = pos
node.text = "%d" % (i)
node.data = i
node.display = True
# add_node func for drawing the nodes on the window
def add_node(window, buttons, colors, graph, nodes, edges, i):
node = None
while True:
window.fill(colors['paper'])
pygame.draw.line(window, colors['emerald'], (20, 70), (780, 70))
Handle_Button.draw_btns(buttons, colors['emerald'])
node = Node(window, colors['green'])
pos = pygame.mouse.get_pos()
# event handling
for event in pe.get():
if event.type == pl.QUIT:
pygame.quit()
sys.exit()
if event.type == pl.KEYDOWN:
if event.key == pl.K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == pl.MOUSEBUTTONDOWN:
if pos[1] > 90:
if len(nodes) != 0:
if not any([collide(pos, x) for x in nodes]):
create_node(node, pos, i)
nodes.append(node)
i += 1
else:
create_node(node, pos, i)
nodes.append(node)
i += 1
Handle_Button.button_handler(window, buttons, colors, graph, nodes, edges, i, pos)
if event.type == pl.MOUSEMOTION:
for button in buttons:
if button.isOver(pos):
button.color = colors['silver']
else:
button.color = colors['blacksteel']
graph.nodes = nodes
graph.edges = edges
# showing the graph
graph.graph_show()
pygame.display.update()