/
flocking.py
74 lines (52 loc) · 2.26 KB
/
flocking.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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 18 09:28:39 2019
@author: install
"""
import pygame
import sys
import numpy as np
from boid import Boid
from boid import Pboid
pygame.init()
class Simulation:
def __init__(self):
self.display_width = 800
self.display_height = 600
self.display = pygame.display.set_mode((self.display_width, self.display_height))
pygame.display.set_caption("Simulation")
self.clock = pygame.time.Clock()
self.running = True
self.boids = [Boid(display_width=self.display_width, display_height=self.display_height) for x in range(100)]
self.pboids = [Pboid(display_width=self.display_width, display_height=self.display_height) for x in range(4)]
def run(self):
while self.running:
self.display.fill((0,0,0))
#draw boids:
for boid in self.boids:
if not boid.alive:
self.boids.remove(boid)
else:
try:
pygame.draw.circle(self.display, (255,255,255), (int(boid.position[0]), int(boid.position[1])), int(2))
boid.update()
boid.flock(self.boids, self.pboids)
except Exception as e:
pass
for pboid in self.pboids:
pygame.draw.circle(self.display, (255,0,127), (int(pboid.position[0]), int(pboid.position[1])), int(5))
pboid.update()
pboid.predate(self.boids,self.pboids)
#
#Make closing out possible
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
pygame.display.update()
self.clock.tick(15)
sys.stdout.write("\r fps: "+str(np.round(self.clock.get_fps()))+" Boids left: "+str(len(self.boids)))
#when self.running set to False.
pygame.quit()
if __name__ == "__main__":
simulation = Simulation()
simulation.run()