-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path56.bird_flocking_simulation_with_pygame.py
57 lines (44 loc) · 1.92 KB
/
56.bird_flocking_simulation_with_pygame.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
import pygame
import sys
import random
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
display_x = 1200
display_y = 700
screen = pygame.display.set_mode((display_x, display_y))
lightgreen = (144, 238, 144)
class FlyingBird():
def __init__(self):
self.circle_radius = 2
self.display_position_x = random.randint(self.circle_radius, display_x - self.circle_radius)
self.display_position_y = random.randint(self.circle_radius, display_y - self.circle_radius)
self.horizontal_speed = random.randint(-3, 3)
self.vertical_speed = random.randint(-3, 3)
def show(self):
pygame.draw.circle(screen, (0, 0, 0), (self.display_position_x, self.display_position_y), self.circle_radius)
def update(self, birds):
center_of_mass_x = sum(b.display_position_x for b in birds) / len(birds)
center_of_mass_y = sum(b.display_position_y for b in birds) / len(birds)
self.horizontal_speed += (center_of_mass_x - self.display_position_x) * 0.001
self.vertical_speed += (center_of_mass_y - self.display_position_y) * 0.001
self.display_position_x += self.horizontal_speed
self.display_position_y += self.vertical_speed
if self.display_position_x + self.circle_radius >= display_x or self.display_position_x - self.circle_radius <= 0:
self.horizontal_speed *= -1
if self.display_position_y + self.circle_radius >= display_y or self.display_position_y - self.circle_radius <= 0:
self.vertical_speed *= -1
total_bird_number = 100
list_of_birds = [FlyingBird() for _ in range(total_bird_number)]
game_running = True
while game_running:
clock.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.fill(lightgreen)
for bird in list_of_birds:
bird.update(list_of_birds)
bird.show()
pygame.display.update()