def gameEnv(): count = 0 targetBall = physics.Vector2D([WIN_WIDTH/2,WIN_HEIGHT/2]) hunter = myHunterParticle([100,100],mass=50,radius=10,color=red) hunter2 = myHunterParticle([100,100],mass=50,radius=5,color=tomato) # targetBall = myTarget([WIN_WIDTH/2,WIN_HEIGHT/2],20,100,black) thisswarm = createSwarm(50,targetBall,green,particle_size=5,particle_mass=50) # enemyswarm = createSwarm(10,thisswarm.collection[0],tomato,particle_size=5,particle_mass=50) # thisswarm.addHunters(enemyswarm.collection) thisswarm.addHunters([hunter,hunter2]) hunter.addTarget(thisswarm.collection[0]) hunter2.addTarget(thisswarm.collection[0]) particles = thisswarm.collection + [hunter,hunter2] gameDisplay = pygame.display.set_mode((WIN_WIDTH,WIN_HEIGHT)) myscene = list(combinations(particles,2)) hunter.enableBoundary(UniversalBoundary) hunter2.enableBoundary(UniversalBoundary) # targetBall.enableBoundary(UniversalBoundary) thisswarm.enableBoundary(UniversalBoundary) # enemyswarm.enableBoundary(UniversalBoundary) while True: clock.tick(FPS) # Give Particles Life hunter.addTarget(thisswarm.collection[0]) hunter2.addTarget(thisswarm.collection[1]) thisswarm.move() hunter.move() hunter2.move() # targetBall.move() # enemyswarm.move() # Draw Simulation gameDisplay.fill(white) # targetBall.draw(gameDisplay) thisswarm.drawSwarm(gameDisplay) hunter.draw(gameDisplay) hunter2.draw(gameDisplay) # enemyswarm.drawSwarm(gameDisplay) pygame.display.update() singleScene(myscene) if count>FPS: count=0 thisswarm.explore() count=count+1 # enemyswarm.target = thisswarm.collection[0] # Exit Condition for event in pygame.event.get(): if event.type == pygame.QUIT: # Close the game any way you want, or troll users who want to close your game. raise SystemExit if event.type == pygame.MOUSEBUTTONUP: pos = pygame.mouse.get_pos() print(pos) thisswarm.target = physics.Vector2D([pos[0],pos[1]])
def gameEnv(): count = 0 # targetBall = physics.Vector2D([100,100]) targetBall = TargetParticle([WIN_WIDTH / 2, WIN_HEIGHT / 2], 100, 20) thisswarm = createSwarm(10, targetBall, 5, particle_mass=50) thatswarm = createSwarm(10, targetBall, 5, particle_mass=50) thirdswarm = createSwarm(10, targetBall, 5, particle_mass=50) forthswarm = createSwarm(10, targetBall, 5, particle_mass=50) particles = thisswarm.collection + thatswarm.collection + thirdswarm.collection + forthswarm.collection + [ targetBall ] gameDisplay = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) myscene = list(combinations(particles, 2)) while True: clock.tick(FPS) thisswarm.move() thatswarm.move() thirdswarm.move() forthswarm.move() for obj1, obj2 in myscene: physics.perfectCollition(obj1, obj2, 0.5) targetBall.move() gameDisplay.fill(white) targetBall.draw(gameDisplay) thisswarm.drawSwarm(gameDisplay) thatswarm.drawSwarm(gameDisplay) thirdswarm.drawSwarm(gameDisplay) forthswarm.drawSwarm(gameDisplay) pygame.display.update() if count > FPS: count = 0 thisswarm.explore() thatswarm.explore() thirdswarm.explore() forthswarm.explore() count = count + 1 # Exit Condition for event in pygame.event.get(): if event.type == pygame.QUIT: # Close the game any way you want, or troll users who want to close your game. raise SystemExit if event.type == pygame.MOUSEBUTTONUP: pos = pygame.mouse.get_pos() print(pos) thisswarm.target = physics.Vector2D([pos[0], pos[1]]) thatswarm.target = physics.Vector2D([pos[0], pos[1]]) thirdswarm.target = physics.Vector2D([pos[0], pos[1]]) forthswarm.target = physics.Vector2D([pos[0], pos[1]])
def gameEnv(): newParticle = TargetParticle([WIN_WIDTH/2,WIN_HEIGHT/2],10,100) newSwarm = createSwarm(5,newParticle) particles = [newParticle] + newSwarm.collection gameDisplay = pygame.display.set_mode((WIN_WIDTH,WIN_HEIGHT)) Force = physics.Vector2D() myscene = list(combinations(particles,2)) while True: clock.tick(FPS) gameDisplay.fill(white) newParticle.applyForce(Force) for obj in particles: obj.move() for obj in particles: obj.draw(gameDisplay) for obj1,obj2 in myscene: physics.collition(obj1,obj2) # newParticle.draw(gameDisplay) # otherParticle.draw(gameDisplay) pygame.display.update() # Exit Condition for event in pygame.event.get(): if event.type == pygame.QUIT: # Close the game any way you want, or troll users who want to close your game. raise SystemExit # if event.type == pygame.KEYDOWN: keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: Force+=physics.Vector2D([-unit,0]) if keys[pygame.K_RIGHT]: Force+=physics.Vector2D([unit,0]) if keys[pygame.K_UP]: Force+=physics.Vector2D([0,-unit]) if keys[pygame.K_DOWN]: Force+=physics.Vector2D([0,unit]) if event.type == pygame.KEYUP: Force = physics.Vector2D()
def run_game(): clock = pygame.time.Clock() running = True paused = False show_forces = True tethered = True target_steps = 500 fps = 30 spf = int(target_steps / fps) t = 1.0 / target_steps while running: step = False for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_ESCAPE: running = False if event.key == K_RIGHT: atmosphere.wind_speed = \ atmosphere.wind_speed.add(physics.Vector2D(-.5, 0)) if event.key == K_LEFT: atmosphere.wind_speed = \ atmosphere.wind_speed.add(physics.Vector2D(.5, 0)) if event.key == K_SPACE: simulator.untether(kite) tethered = False if event.key == K_f: show_forces = not show_forces if event.key == K_s: step = True if event.key == K_p: paused = not paused elif event.type == QUIT: running = False if not running: break screen.fill(pygame.Color("skyblue")) pygame.draw.circle(screen, pygame.Color("red"), projector.project(anchor_position), 5, 3) if step or not paused: kite_sprite.update() screen.blit(kite_sprite.image, kite_sprite.rect) simulator.add_forces() if show_forces: debug_draw.draw_forces(kite) if tethered: pygame.draw.line(screen, pygame.Color("white"), projector.project(anchor_position), projector.project(kite.global_bridle()), 1) wind_speed_text = font.render( 'Windspeed: ' + str(atmosphere.wind_speed), False, (0, 0, 0)) lift = kite.total_lift().magnitude() drag = kite.total_drag().magnitude() l_d = 0 if drag > 0: l_d = lift / drag lift_drag_text = font.render('L/D: ' + str(round(l_d, 2)), False, (0, 0, 0)) kite_relative = kite.global_bridle().subtract(anchor_position) angle = kite_relative.angle() degrees = 180 - angle.degrees() if degrees < 0: degrees = 0 angle_text = font.render('Angle: ' + str(round(degrees, 2)), False, (0, 0, 0)) screen.blit(wind_speed_text, (10, 10)) screen.blit(lift_drag_text, (10, 50)) screen.blit(angle_text, (10, 90)) pygame.display.flip() simulator.apply_forces(t) for i in range(0, spf - 1): simulator.step(t) clock.tick(fps)
pygame.display.flip() simulator.apply_forces(t) for i in range(0, spf - 1): simulator.step(t) clock.tick(fps) pygame.init() SCREEN_WIDTH = 1000 SCREEN_HEIGHT = 600 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Wind Tunnel") anchor_position = physics.Vector2D(0, 3) projector = display.Projector(physics.Vector2D(SCREEN_WIDTH, SCREEN_HEIGHT)) debug_draw = display.DebugDraw(screen, projector) pygame.font.init() font = pygame.font.SysFont('Comic Sans MS', 30) atmosphere = flight.Atmosphere() atmosphere.wind_speed = physics.Vector2D(-1, 0) bridle = Bridle(4.1, 3.7, 4) bridle.initial_pos = anchor_position.subtract(physics.Vector2D(2, 0)) kite = flight.kites.BoxKite(4,
def __init__(self, n, target, collection=[]): self.n = n self.gbest = physics.Vector2D([0, 0]) self.collection = collection self.target = target
def explore(self): randForce = physics.Vector2D( [random.randint(0, 100), random.randint(0, 100)]) self.applyForce(randForce)
def get_points(self): points = [] points.append(physics.Vector2D(-180, 0)) # trailing edge is leading with a negative aoa points.append(physics.Vector2D(-170, -self._get_cl(1))) points.append(physics.Vector2D(-167, -self._get_cl(1.08))) points.append(physics.Vector2D(-164, -self._get_cl(.6))) points.append(physics.Vector2D(-160, -self._get_cl(.6))) points.append(physics.Vector2D(-135, -self._get_cl(1.05))) points.append(physics.Vector2D(-90, 0)) points.append(physics.Vector2D(-45, -self._get_cl(1.05))) points.append(physics.Vector2D(-20, -self._get_cl(.6))) points.append(physics.Vector2D(-16, -self._get_cl(.6))) points.append(physics.Vector2D(-13, -self._get_cl(1.08))) points.append(physics.Vector2D(-10, -self._get_cl(1))) points.append(physics.Vector2D(0, 0)) points.append(physics.Vector2D(10, self._get_cl(1))) points.append(physics.Vector2D(13, self._get_cl(1.08))) points.append(physics.Vector2D(16, self._get_cl(.6))) points.append(physics.Vector2D(20, self._get_cl(.6))) points.append(physics.Vector2D(45, self._get_cl(1.05))) points.append(physics.Vector2D(90, self._get_cl(0))) # trailing edge is leading with a positive aoa points.append(physics.Vector2D(135, self._get_cl(1.05))) points.append(physics.Vector2D(160, self._get_cl(.6))) points.append(physics.Vector2D(164, self._get_cl(.6))) points.append(physics.Vector2D(167, self._get_cl(1.08))) points.append(physics.Vector2D(170, self._get_cl(1))) points.append(physics.Vector2D(180, 0)) return points