-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode.py
68 lines (58 loc) · 2 KB
/
node.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
import pygame as py
from car import decodeCommand
from config_variables import *
class Node:
def __init__(self, id, x, y, type, color, label="", index=0):
self.id = id
self.x = x
self.y = y
self.type = type
self.color = color
self.label = label
self.index = index
def draw_node(self, world):
colorScheme = self.getNodeColors(world)
py.draw.circle(world.win, colorScheme[0], (self.x, self.y), NODE_RADIUS)
py.draw.circle(world.win, colorScheme[1], (self.x, self.y), NODE_RADIUS - 2)
if self.type != MIDDLE:
text = NODE_FONT.render(self.label, 1, BLACK)
world.win.blit(
text,
(
self.x
+ (self.type - 1)
* ((text.get_width() if not self.type else 0) + NODE_RADIUS + 5),
self.y - text.get_height() / 2,
),
)
def getNodeColors(self, world):
if self.type == INPUT:
ratio = world.bestInputs[self.index]
elif self.type == OUTPUT:
ratio = 1 if decodeCommand(world.bestCommands, self.index) else 0
else:
ratio = 0
col = [[0, 0, 0], [0, 0, 0]]
for i in range(3):
col[0][i] = int(
ratio * (self.color[1][i] - self.color[3][i]) + self.color[3][i]
)
col[1][i] = int(
ratio * (self.color[0][i] - self.color[2][i]) + self.color[2][i]
)
return col
class Connection:
def __init__(self, input, output, wt):
self.input = input
self.output = output
self.wt = wt
def drawConnection(self, world):
color = GREEN if self.wt >= 0 else RED
width = int(abs(self.wt * CONNECTION_WIDTH))
py.draw.line(
world.win,
color,
(self.input.x + NODE_RADIUS, self.input.y),
(self.output.x - NODE_RADIUS, self.output.y),
width,
)