-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (56 loc) · 1.9 KB
/
Copy pathmain.py
File metadata and controls
79 lines (56 loc) · 1.9 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
import random
import node
from utilities.globals import window, batch, circles, lines
from utilities.config import window_width, window_height, circle_radius, amount
from utilities.config import CONNECTION_DISTANCE, RANDOM_SPEEDS, SPEED_RANGE, FPS
from utilities.config import DIVISOR as K
from modes import CHEMISTRY, POLYGONS, connect_all # TODO: rework naming or import for connect_all
import pyglet
def create_circles(amount: int = 15):
for i in range(amount):
x, y = random.randint(0, window_width), random.randint(0, window_height)
circles.append(
node.Node(batch, x, y, r=circle_radius)
)
def set_speeds(x: int = None, y: int = None):
for node in circles:
node.set_speed(
x if x else random.randint(*SPEED_RANGE) / K,
y if y else random.randint(*SPEED_RANGE) / K
)
def check_neighbors():
l = len(circles)
for i in range(l):
this = circles[i]
for j in range(i + 1, l):
other = circles[j]
if this.distance_to(other) < CONNECTION_DISTANCE:
this.neighbors.add(other)
other.neighbors.add(this)
else:
try:
this.neighbors.remove(other)
other.neighbors.remove(this)
except KeyError:
pass
def clear_all():
window.clear()
def tick(dt=0.1):
if RANDOM_SPEEDS:
for node in circles:
node.change_speed_by(random.randint(*SPEED_RANGE) / K, random.randint(*SPEED_RANGE) / K)
for node in circles:
node.move(dt)
node.keep_in_bounds(window_width, window_height)
check_neighbors()
connect_all(POLYGONS)
pyglet.clock.schedule_interval(tick, 1.0 / FPS)
@window.event
def on_draw():
clear_all()
batch.draw()
tick()
if __name__ == '__main__':
create_circles(amount)
set_speeds()
pyglet.app.run()