-
Notifications
You must be signed in to change notification settings - Fork 0
/
not_a_swarm_yet.py
99 lines (81 loc) · 3.02 KB
/
not_a_swarm_yet.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
###
# not_a_swarm_yet.py
#
# author: Kristina Striegnitz
#
# version: 2/3/2010
#
# Starting point for implementing flocking behavior. This program just
# creates two creatures which move around the screen and bounce off
# the edges.
###
import random, math
from boids_display import run_display
# These are global constants. That is global variables, with a value
# that never changes while the program runs.
WIDTH = 800
HEIGHT = 600
def run_swarm():
# Create two creatures (boids). Place them at a random position on
# the screen, give them a random velocity, and a random color.
b1_x_pos = random.randint(0,WIDTH)
b1_y_pos = random.randint(0,HEIGHT)
b1_x_vel = random.randint(-400,400)
b1_y_vel = random.randint(-400,400)
boid1 = [b1_x_pos, b1_y_pos, b1_x_vel, b1_y_vel, random_color()]
b2_x_pos = random.randint(0,WIDTH)
b2_y_pos = random.randint(0,HEIGHT)
b2_x_vel = random.randint(0,400)
b2_y_vel = random.randint(0,400)
boid2 = [b2_x_pos, b2_y_pos, b2_x_vel, b2_y_vel, random_color()]
boids = [boid1, boid2]
# Call the function run_display which we imported from the
# file/module boids_display.py. run_display takes a list of boids,
# the name of a function which describes how each boids position
# and speed should be updated as time passes, and the width and
# the height of the display window.
run_display(boids, update_boids, WIDTH, HEIGHT)
# This function takes two parameters: a list of boids and a time in
# miliseconds. The time parameter indicates how much time has passed
# since the last time update was called. The function specifies how
# each boid from the list of boids given as a parameter gets updated,
# that is how each boid's position and velocity changes given the time
# that has passed since the last update.
# The function update_boids gets called every few milliseconds by
# run_display.
def update_boids(boids, time):
for index in range(len(boids)):
b = boids[index]
# Limit velocity to 500 pixels per second horizontally and vertically
b[2] = speed_limit(b[2], 500)
b[3] = speed_limit(b[3], 500)
# Update the boid's position based on its velocity and the
# time that has passed since the last update.
b[0] += float(b[2])/1000 * time
b[1] += float(b[3])/1000 * time
# Make the boid bounce off the walls.
if b[0] < 0:
b[0] = 0
b[2] = -b[2]
elif b[0] > WIDTH:
b[0] = WIDTH
b[2] = -b[2]
if b[1] < 0:
b[1] = 0
b[3] = -b[3]
elif b[1] > HEIGHT:
b[1] = HEIGHT
b[3] = -b[3]
# This function picks a random color from a given list of colors.
def random_color():
colors = [(255,140,0), (139,0,0), (139,0,139), (0,100,0), (255,69,0)]
return random.choice(colors)
def speed_limit(vel, limit):
if vel > limit:
return limit
elif vel < -limit:
return -limit
else:
return vel
### START EVERYTHING ###
run_swarm()