-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
181 lines (149 loc) · 5.58 KB
/
render.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import pygame
# ---------------- display settings --------------
GAME_NAME = "SNAKE (10x10)"
N_ROWS = 10
N_COLS = 10
NET_W, NET_H = 450, 900
GAME_W, GAME_H = 450, 450
INTERVAL = 1000
WINDOW_BUFFER = 25
SCREEN_WIDTH = 950
SCREEN_HEIGHT = 500
BLOCK_W, BLOCK_H = GAME_W / N_COLS, GAME_H / N_ROWS
GAME_X = 450
GAME_Y = 0
GAME_TOP_LEFT = (WINDOW_BUFFER + GAME_X, GAME_Y + WINDOW_BUFFER)
NODE_SIZE = 10
BUFFER = 8
TITLE_X = 0
TITLE_Y = 0
NETWORK_X = 0
NETWORK_Y = 75
# ---------------- color settings --------------
RED = (255, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLACK = (0, 0, 0)
BLUE = (70, 130, 180)
ORANGE = (255, 165, 13)
def spawn_window():
# Initialize screen
global screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
STEP = pygame.USEREVENT + 1
pygame.time.set_timer(STEP, INTERVAL)
# Render without network
def render_basic(snake, apple):
global screen
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
screen.fill(BLACK)
draw_square()
draw_snake(snake)
draw_apple(apple)
pygame.display.flip()
def render(snake, apple, net, genome, node_centers, hidden_nodes):
global screen
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
screen.fill(BLACK)
draw_square()
draw_title()
draw_snake(snake)
draw_apple(apple)
draw_network(
net,
genome,
node_centers,
hidden_nodes
)
pygame.display.flip()
def draw_square():
global screen
draw = GAME_TOP_LEFT[0] - BUFFER, GAME_TOP_LEFT[1] - BUFFER
rect = pygame.Rect(draw, (GAME_W + 2 * BUFFER, GAME_H + 2 * BUFFER))
pygame.draw.rect(screen, WHITE, rect, width=BUFFER // 2)
def draw_apple(apple_coordinate):
global screen
x, y = apple_coordinate
rect = pygame.Rect(getLeftTop(x, y), (BLOCK_W - BUFFER * 2, BLOCK_H - BUFFER * 2))
pygame.draw.rect(screen, RED, rect)
def getLeftTop(x, y):
return (x / N_ROWS) * GAME_W + BUFFER + GAME_TOP_LEFT[0], (y / N_ROWS) * GAME_H + BUFFER + GAME_TOP_LEFT[1]
def draw_snake(snake):
global screen
for i, (x, y) in enumerate(snake):
rect = pygame.Rect(getLeftTop(x, y), (BLOCK_W - BUFFER * 2, BLOCK_H - BUFFER * 2))
pygame.draw.rect(screen, YELLOW if i == len(snake) - 1 else WHITE, rect)
def draw_connections(first_set, second_set, net, genome, node_centers):
global screen
for first in first_set:
for second in second_set:
if (first, second) in genome.connections:
start = node_centers[first]
stop = node_centers[second]
weight = genome.connections[(first, second)].weight
color = BLUE if weight >= 0 else ORANGE
surf = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.SRCALPHA)
alpha = 255 * (0.3 + net.values[first] * 0.7)
pygame.draw.line(surf, color + (alpha,), start, stop, width=5)
screen.blit(surf, (0, 0))
def draw_title():
global screen
font = pygame.font.Font("cmunbtl.otf", 24)
title = font.render(f'Neat Algorithm: {GAME_NAME}', True, WHITE)
sub_title = font.render(f'Network activation: ', True, WHITE)
screen.blit(title, (TITLE_X + WINDOW_BUFFER, TITLE_Y + WINDOW_BUFFER))
screen.blit(sub_title, (TITLE_X + WINDOW_BUFFER, TITLE_Y + WINDOW_BUFFER * 2.3))
def draw_network(net, genome, node_centers, hidden_nodes):
global screen
font = pygame.font.Font("cmunbtl.otf", 24)
node_names = {
-1 : "d_N_wall",
-2 : "d_S_wall",
-3 : "d_E_wall",
-4 : "d_W_wall",
-5 : "tail_N",
-6 : "tail_S",
-7 : "tail_E",
-8 : "tail_W",
-9 : "apple_N",
-10 : "apple_S",
-11 : "apple_E",
-12 : "apple_W",
0: 'up', 1 : "left", 2 : "down", 3 : "right"
}
# draw connections between input and output nodes
draw_connections(net.input_nodes, net.output_nodes, net, genome, node_centers)
draw_connections(net.input_nodes, hidden_nodes, net, genome, node_centers)
draw_connections(hidden_nodes, hidden_nodes, net, genome, node_centers)
draw_connections(hidden_nodes, net.output_nodes, net, genome, node_centers)
# draw input nodes
for i, input_node in enumerate(net.input_nodes):
center = node_centers[input_node]
center2 = center[0] - 5.5 * NODE_SIZE - 75, center[1] - 13
img = font.render(node_names[input_node], True, WHITE)
screen.blit(img, center2)
color = (net.values[input_node] * 255, 0, 0)
pygame.draw.circle(screen, color, center, NODE_SIZE)
pygame.draw.circle(screen, WHITE, center, NODE_SIZE, width=5)
# draw output nodes
for i, output_node in enumerate(net.output_nodes):
center = node_centers[output_node]
color = (net.values[output_node] * 255, 0, 0)
pygame.draw.circle(screen, color, center, NODE_SIZE)
pygame.draw.circle(screen, WHITE, center, NODE_SIZE, width=5)
center2 = center[0] + 1.5 * NODE_SIZE, center[1] - 13
img = font.render(node_names[output_node], True, WHITE)
screen.blit(img, center2)
# draw hidden nodes
for hidden in hidden_nodes:
center = node_centers[hidden]
color = (net.values[hidden] * 255, 0, 0)
# center2 = center[0] - 5.5 * NODE_SIZE, center[1] - 10
# img = font.render(str(hidden), True, WHITE)
# screen.blit(img, center2)
pygame.draw.circle(screen, color, center, NODE_SIZE)
pygame.draw.circle(screen, WHITE, center, NODE_SIZE, width=5)