def __init__( self, num_boids, ff, center=P3.P3(0, 0, 0), radius=20 ): #possibly also orientation, maybe something about obstacles? Flock.flock_count += 1 self.boids = [] self.distance_matrix = [[0 for _ in range(num_boids)] for _ in range(num_boids)] self.ff = ff # randomly distributes boids in circle on xy plane using center and radius for i in range(num_boids): r = radius * math.sqrt(random.random()) theta = 2 * math.pi * random.random() x = r * math.cos(theta) y = r * math.sin(theta) # eventually velocity is taken from config file v = P3.P3(1 * random.random() * random.randrange(-1, 2, 2) + 5, 1 * random.random() * random.randrange(-1, 2, 2) + 5, 0) self.boids.append( Boid.Boid( self, i, P3.P3(x, y, 0) + center + P3.P3(0, 0, 2 * random.randrange(-5, 6, 10)), v, Behavior.behavior)) self.update_dist_matrix()
def addBoid(self, mouseVec): maxSpeed = self.maxSpeed maxForce = self.maxForce color = (200, 90, random.randint(0, 255)) if len(self.boids) + 1 % 2 == 0: color = (random.randint(0, 255), 90, 200) else: color = (200, 90, random.randint(0, 255)) self.boids.append( Boid(mouseVec, maxSpeed, maxForce, self.scale, self.controller, self.width, self.height, color))
def __init__(self, num_boids, center, radius): #possibly also orientation, maybe something about obstacles? self.num_boids = num_boids self.center = center self.radius = radius self.boid_list = [] self.flockcount += 1 self.distance_matrix = [] for i in range(num_boids): boid_list.append(Boid(ident, position, vel, acc))
def CreateBoids(self, width, height, num, boidConfig): boids = [] for i in range(num): x = randint(boidConfig[6], boidConfig[7]) if randint(0, 1) == 1: x = -x y = randint(boidConfig[6], boidConfig[7]) if randint(0, 1) == 1: y = -y #boids.append(Boid([randint(0,width), randint(0,height)], [width,height], [x, y], [255, 255, 255], num, boidConfig)) boids.append( Boid([width / 2, height / 2], [width, height], [x, y], [255, 255, 255], num, boidConfig)) return boids
def __init__( self, num_boids, center, radius ): #possibly also orientation, maybe something about obstacles? self.num_boids = num_boids self.boids = [] Flock.flock_count += 1 self.distance_matrix = [[0 for _ in range(num_boids)] for _ in range(num_boids)] # defaults boid to grid row = -1 for i in range(num_boids): if i % 10 == 0: row += 1 self.boids.append( Boid(i, P3(200 + 10 * row, -20 + 4 * i, 50), P3(-15, 0, 0), P3(0, 0, 0), self))
def test(screen): test_mold_eaten = Food(21, 39, 800, 800) test_mold_not_eaten = Food(100, 39, 800, 800) test_herbivore_eaten = Boid(21, 39, 800, 800) test_herbivore_not_eaten = Boid(201, 39, 800, 800) test_carnivore = WildBoid(21, 39, 800, 800) test_mold_eaten.rect = test_mold_eaten.rect.move( int(test_mold_eaten.position.x), int(test_mold_eaten.position.y)) screen.blit( test_mold_eaten.image, (int(test_mold_eaten.position.x), int(test_mold_eaten.position.y))) test_mold_not_eaten.rect = test_mold_not_eaten.rect.move( int(test_mold_not_eaten.position.x), int(test_mold_not_eaten.position.y)) screen.blit(test_mold_not_eaten.image, (int( test_mold_not_eaten.position.x), int(test_mold_not_eaten.position.y))) test_herbivore_eaten.rect = test_herbivore_eaten.rect.move( int(test_herbivore_eaten.position.x), int(test_herbivore_eaten.position.y)) screen.blit(test_herbivore_eaten.image, (int(test_herbivore_eaten.position.x), int(test_herbivore_eaten.position.y))) test_herbivore_not_eaten.rect = test_herbivore_not_eaten.rect.move( int(test_herbivore_not_eaten.position.x), int(test_herbivore_not_eaten.position.y)) screen.blit(test_herbivore_not_eaten.image, (int(test_herbivore_not_eaten.position.x), int(test_herbivore_not_eaten.position.y))) test_carnivore.rect = test_carnivore.rect.move( int(test_carnivore.position.x), int(test_carnivore.position.y)) screen.blit( test_carnivore.image, (int(test_carnivore.position.x), int(test_carnivore.position.y))) food = [test_mold_eaten, test_mold_not_eaten] prey = [test_herbivore_eaten, test_herbivore_not_eaten] def test_eaten(): assert test_herbivore_eaten.eaten(food) ==\ (True, test_mold_eaten), "Should be (True, test_mold_eaten)" print("First test passed") def test_killed(): assert test_carnivore.killed(prey) ==\ (True, test_herbivore_eaten), "Should be (True, test_herbivore_eaten)" print("Second test passed") test_eaten() test_killed()
def makeBoid(self): maxSpeed = self.maxSpeed maxForce = self.maxForce num = self.num hue = (random.random() + self.goldren_ratio) % 1 for i in range(num): pos = random.random() * self.width, random.random() * self.height saturation = random.uniform(0.3, 0.7) value = random.uniform(0.3, 0.7) color = colorsys.hsv_to_rgb(hue, saturation, value) color = color[0] * 255, color[1] * 255, color[2] * 255 # color = (random.random()*127 + 127, random.random()*127 + 127, random.random()*127 + 127) # if i % 2 == 0: # color = (random.randint(0, 255), random.randint(150, 255) , 0) # else: # color = (random.randint(0, 255), 90, 200) newBoid = Boid(pos, maxSpeed, maxForce, self.scale, self.controller, self.width, self.height, color) self.boids.append(newBoid)
gl.glDrawArrays(gl.GL_POINTS, 0, self.count) def resizeGL(self, width, height): self.width, self.height = width, height gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() #glOrtho(left, rught, bottom, top, nearX, farZ) gl.glOrtho(-200, 200, -200, 200, -1, 1) if __name__ == '__main__': import sys import numpy import time myBoids = Boid.Boids() myBoids.setAreaWidth(200) class MainWindow(QtGui.QMainWindow): def __init__(self): super(MainWindow, self).__init__() coord = myBoids.getCoords2D() self.data = numpy.array(coord, dtype=numpy.float32) self.widget = GLPlotWidget() self.widget.set_data(self.data) # Put the window at (100, 100) self.setGeometry(100, 100, self.widget.width, self.widget.height) self.setCentralWidget(self.widget)
from Boid import* # test b = Boid(([1, 0, 4]),([5, 2, 9]),(4),([1, 0, 4])) b.printVelocity() b.printPosition() b.printDirection() b.printSpeed()
def addRndBoid(self): pos = self.getRndPos() boid = Boid(pos.x, pos.y, self) self.addBoid(boid)
def physicists(Randomise): if Randomise == False: p = (width / 2 - 720, height / 2 - 450) for i in range(0, 5): # Comp. Suite all_sprites.add( Boid((rnd.randint(1100, 1350), rnd.randint(30, 220)))) for i in range(0, 4): # Annex all_sprites.add(Boid((rnd.randint(800, 980), rnd.randint(30, 230)))) for i in range(0, 5): # Main Area all_sprites.add(Boid((rnd.randint(410, 590), rnd.randint(30, 300)))) for i in range(0, 2): # Corridor all_sprites.add( Boid((rnd.randint(680, 810), rnd.randint(280, 470)))) all_sprites.add(Boid((570, 540))) all_sprites.add(Boid((880, 420))) all_sprites.add(Boid((820, 530))) all_sprites.add(Boid((570, 630))) all_sprites.add(Boid((580, 710))) all_sprites.add(Boid((530, 750))) all_sprites.add(Boid((840, 690)))
for i in range(BOID_QUANTITY): # Init a random random velocity for each dimension: consider positive and negative velocities where # each component is always less than any of the other components... randVel = [(1 - 2 * random.random()) * (1 / len(DIMENSIONS)) * math.sqrt(MAX_VEL) for i in range(len(DIMENSIONS))] # Init a random random Acceleration for each dimension: consider positive and negative accelerations where # each component is always less than any of the other components... randAccel = [(1 - 2 * random.random()) * (1 / len(DIMENSIONS)) * math.sqrt(MAX_ACCEL) for i in range(len(DIMENSIONS))] # Init a random position randPos = [ random.random() * MAP_DIM_FUNC(i) for i in range(len(DIMENSIONS)) ] #*DIMENSIONS[i] # Create the boid # (IMPLICIT in construction) Use the default mass & none type b = Boid.Boid(randPos, randVel, randAccel) # Append the boid boids.append(b) # Color the swarm COLOR_SWARM_FUNCTION(boids) # compute the absolute values for the weights and distances absolute_ranges = BoidUtils.BoidUtils.absoluteValsFromRelative( REL_RANGES, MAX_RANGE) absolute_weights = BoidUtils.BoidUtils.absoluteValsFromRelative( REL_RULE_WEIGHTS, 1) # Create a variable to save the state of the simulation at each timestep: when we save a simulation # the boids list and the following states will be saved ... Relevant info concerning the settings # is also added ... BOIDFRAMES is a list where each entry in the list will be a copy of the pop # at each timestep...
background_image = pygame.image.load("sky.png").convert() while running: goal_on = False mouse_x = 0 mouse_y = 0 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: # add boid with left mouse click pos = pygame.mouse.get_pos() boid = Boid(pos[0],pos[1]) flock.append(boid) if event.type == pygame.MOUSEBUTTONDOWN and event.button == 2: # give boids goal with middle mouse click pos = pygame.mouse.get_pos() mouse_x = pos[0] mouse_y = pos[1] goal_on = True if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3: # add obstacles with right mouse click pos = pygame.mouse.get_pos() obstacle = Obstacle(pos[0],pos[1]) obstacles.append(obstacle) delta_time += clock.tick() / 1000.0 if (delta_time > (1 / max_fps)): delta_time = delta_time - (1 / max_fps)
clock = pygame.time.Clock() # Set clock button = Button() # Check for special scenarios if Divide == True: # Set up two-goal-scenario goal_left = Goal_left() goals.add(goal_left) goal_right = Goal_right() goals.add(goal_right) if Randomise == True: for i in range(N): # Create boids and add to sprite group boid = Boid((0, 0)) all_sprites.add(boid) for i in range(L): # Create leaders and add to sprite group leader = Leader() leaders.add(leader) for i in range(G): # Create goals and add to sprite group goal = Goal() goals.add(goal) for i in range(I): # Create goals and add to sprite group informer = Informed((0, 0)) all_sprites.add(informer) if Blackett == True:
import Boid a = Boid() a.dosomething()
import Boid r1 = [2,4] r2 = [5,3] r3 = [1,6] r4 = [3,3] b = Boid(r1,r2,r3,r4) print(b.getVelocity())
def setup(): size(640, 360) background(255) for i in range(30): boids.append(Boid(width, height))