Files changed (3)
+Holding the left mouse button will repel birds from wherever you clicked, the right one attracts them. Use the little red x to close the window, I've found that Python crashes when I just close the window.
+running = True #is the script running? gets set to False when the small x gets clicked, and the program then stops.
+loopscreen = False #Lets the screen loop, although radius calculations then don't expand to the opposite side of the screen
+numbirds=80 #number of birds. around 60-70 is good, but lower numbers allow the program to run much faster
+ for i in range(numbirds)] #the list of birds, in the form (name, position, velocity, last velocity, second last velocity)*number of birds
+viewangle=abs(cos(1.65806279)) #angle a bird can see, to each side of the current direction vector, around 95 is standard. the cos() is so that the isinview(x,y) function doesn't need to call acos() for every bird every frame
+lowerbound,upperbound=3,3 #this lets the user control how far the speed of the birds can deviate - speed is going to be random in the range of averagebirdspeed-lowerbound to averagebirdspeed+upperbound. 3 and 3 are standard.
+showaveragevector=False #shows the swarms average velocity vector (length is proportional to magnitude, but the factor is not necessarily 1)
+traceimage=False #if true, the white background doesn't get redrawn, and the bird trajectories will be traced out. also traces any lines that are being drawn.
+gravstrength = 0.0004 #1/2500 is good. This basically decides how far out of the screen area the birds will fly.
+def randomspeedvector(): #generates a random speed vector from the variables averagebirdspeed, upper and lower bound
+ return [random.randrange(1,averagebirdspeed*2+1)*plusminus(), random.randrange(1,averagebirdspeed*2+1)*plusminus()]
+def scalevector(vector, magnitude): #scale a vector to a certain magnitude without altering its direction
+ if ((( (b1b2v*birds[x] + b1b2v*birds[x])/(vmag(b1b2v)*vmag(birds[x]))))) <= viewangle:
+ return True #iff the angle between the birds velocity vector and the vector from bird 1 to bird 2 is within the birds viewing angle, bird 1 can see bird 2
+ for x in range(numbirds): #this block calculates the neighbour birds' average velocity and the neighbour birds' average position
+ mouse=pygame.mouse.get_pos() #this if-block checks whether the mouse is focused on the window and pressed, and then either attracts or repels birds from that point depending on which button is pressed
+ #Finally, we adjust the speed to a random speed in the range of averagebirdspeed-lowerbound and averagebirdspeed+upperbound. Not crucial, but makes the simulation look more natural (depending on how variables are set)
+ birds[b]=scalevector(birds[b], random.randrange(averagebirdspeed-lowerbound,averagebirdspeed+upperbound))
+ birds[b]=[(birds[b]+birds[b])%screensize, (birds[b]+birds[b])%screensize]
+def refreshscreen(): #draw the birds, also includes conditionals for the various visualization options
+ if highlightbird and count==0: #this bit can highlight bird 0 to demonstrate a specific bird's motion
+ pygame.draw.line(screen, red, averagebirdposition(), [averagebirdvelocity()+averagebirdposition(),averagebirdvelocity()+averagebirdposition()], 2)