-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
59 lines (49 loc) · 1.34 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
import pygame
from random import randint
from boid import Boid
from pygame.locals import (
K_ESCAPE,
K_SPACE,
KEYDOWN,
MOUSEBUTTONUP,
QUIT,
)
# Initialize pygame modules
pygame.init()
# Create the screen object
pygame.display.set_caption("Flocking Simulation")
screen = pygame.display.set_mode((1000, 1000))
background = pygame.Surface(screen.get_size())
background.fill((0, 0, 0))
# Make groups of sprites
boids = pygame.sprite.Group()
# Populate with boids
numBoids = 30
for i in range(numBoids):
position = (randint(0, 1000), randint(0, 1000))
boids.add(Boid(position))
# Setup the clock to limit fps
clock = pygame.time.Clock()
# Running control var
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
boids.add(Boid(pos))
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
if event.key == K_SPACE:
print("Space")
# Pause the sim
# Paint the background
screen.blit(background, (0, 0))
# Update then paint the boids
boids.update(boids)
for boid in boids:
screen.blit(boid.surf, boid.rect)
pygame.display.flip()
clock.tick(60)