-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
102 lines (76 loc) · 2.2 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
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
import math
import random
import pygame
from boid import Boid
width = 1280
height = 1280
boids = []
boid_count = 100
min_vector = -2
max_vector = 2
local_area = 75
closest = 4
res = 5
def draw(x, y):
pygame.draw.polygon(screen, "azure4", [(x, y + res * 2), (x + res, y), (x - res, y)])
def steer(b):
p_sum = pygame.Vector2()
v_sum = pygame.Vector2()
count = 0
for boid in boids:
if math.sqrt((boid.p.x - b.p.x) ** 2 + (boid.p.y - b.p.y) ** 2) <= local_area:
if boid != b:
count += 1
p_sum += boid.p
v_sum += boid.v
if count == 0:
return
# Separation
sep_v = (p_sum - b.p) / count
# Cohesion
# coh_v = p_sum / count
# Alignment
alig_v = (v_sum - b.v) / count
total_v = sep_v + alig_v
if total_v.x > max_vector:
total_v.x = max_vector
elif total_v.x < min_vector:
total_v.x = min_vector
if total_v.y > max_vector:
total_v.y = max_vector
if total_v.y < min_vector:
total_v.y = min_vector
b.v = total_v
return
if __name__ == '__main__':
# pygame setup
pygame.init()
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
running = True
# Set the window title
pygame.display.set_caption("Flocking Simulation")
# Generate boids
for i in range(boid_count):
p = pygame.Vector2()
p.x = random.randint(0, width)
p.y = random.randint(0, height)
v = pygame.Vector2()
v.x = random.randint(min_vector, max_vector)
v.y = random.randint(min_vector, max_vector)
boids.append(Boid(p, v))
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill("cadetblue2")
# Render boid
for boid in boids:
steer(boid)
draw(boid.p.x % width, boid.p.y % height)
boid.p.x = boid.p.x + boid.v.x + random.randint(-1, 1)
boid.p.y = boid.p.y + boid.v.y + random.randint(-1, 1)
# flip() the display to put your work on screen
pygame.display.flip()
clock.tick(60)
pygame.quit()