here2share

# pygame_swarming.py

Apr 22nd, 2021
639
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # pygame_swarming.py
  2. # Andrew Davison 2013
  3.  
  4. import pygame, os, math, time, random
  5. from pygame.locals import *
  6. pygame.init()
  7.  
  8. # set the width and height of the screen
  9. WIDTH = 1000
  10. HEIGHT = 1000
  11.  
  12. CENTREX = WIDTH / 2
  13. CENTREY = HEIGHT / 2
  14.  
  15. NUMBEROFBIRDS = 200
  16.  
  17.  
  18. POSITIONSPREAD = 10000
  19. SPEEDSPREAD = 5
  20. MAXSPEED = 10
  21.  
  22. BORDER = 100
  23. LEADERBORDER = 200
  24. BORDERSPEEDCHANGE = 0.2
  25.  
  26. MINDIST = 10.0
  27. MATCHSPEEDWINDOW = 40.0
  28.  
  29. LEADERBIRDRANDOMSPEEDCHANGE = 0.2
  30. LEADERMAXSPEED = 5.0
  31.  
  32. barriers = [[450,500],[475,500],[500,500],[525,500],[625,500],[650,500],[675,500],[700,500],[400,500],[425,500],[400,525],[400,550],[400,650],[400,675],[400,700],[375,700],[350,700],[325,700],[300,700],[275,700],[250,700],[800,200],[250,700],[100,100]]
  33. BARRIERRADIUS = 30
  34.  
  35.  
  36. size = [WIDTH, HEIGHT]
  37. screen = pygame.display.set_mode(size)
  38.  
  39. # This makes the normal mouse pointer invisible in graphics window
  40. pygame.mouse.set_visible(0)
  41.  
  42.  
  43.  
  44. birdlist = []
  45.  
  46.  
  47. # Generate leader bird
  48. leaderbirdx = 300.0
  49. leaderbirdy = 300.0
  50. leaderbirdvx = 5.0
  51. leaderbirdvy = 0.0
  52.  
  53.  
  54. # Generate birds
  55. i = 0
  56. while (i < NUMBEROFBIRDS):
  57.     x = random.uniform(CENTREX - POSITIONSPREAD, CENTREX + POSITIONSPREAD)
  58.     y = random.uniform(CENTREY - POSITIONSPREAD, CENTREY + POSITIONSPREAD)
  59.     vx = random.uniform(-SPEEDSPREAD, SPEEDSPREAD)
  60.     vy = random.uniform(-SPEEDSPREAD, SPEEDSPREAD)
  61.    
  62.     newbird = [x, y, vx, vy]
  63.  
  64.     birdlist.append(newbird)
  65.     i += 1
  66.  
  67.  
  68.  
  69.  
  70. while(1):
  71.  
  72.     screen.fill((0,0,0))
  73.  
  74.  
  75.     # Update leader bird position and speed
  76.     if (leaderbirdx < LEADERBORDER):
  77.         leaderbirdvx += BORDERSPEEDCHANGE
  78.     if (leaderbirdy < LEADERBORDER):
  79.         leaderbirdvy += BORDERSPEEDCHANGE
  80.     if (leaderbirdx > WIDTH - LEADERBORDER):
  81.         leaderbirdvx -= BORDERSPEEDCHANGE
  82.     if (leaderbirdy > HEIGHT - LEADERBORDER):
  83.         leaderbirdvy -= BORDERSPEEDCHANGE
  84.  
  85.  
  86.     # Draw leaderbird and update
  87.     #pygame.draw.circle(screen, (255,0,255), (int(leaderbirdx), int(leaderbirdy)), 5, 0)
  88.     leaderbirdvx += random.uniform(-LEADERBIRDRANDOMSPEEDCHANGE, LEADERBIRDRANDOMSPEEDCHANGE)
  89.     leaderbirdvy += random.uniform(-LEADERBIRDRANDOMSPEEDCHANGE, LEADERBIRDRANDOMSPEEDCHANGE)
  90.  
  91.  
  92.     # Cap maximum speed
  93.     speed = math.sqrt(leaderbirdvx*leaderbirdvx + leaderbirdvy*leaderbirdvy)
  94.     if (speed > LEADERMAXSPEED):
  95.         leaderbirdvx = leaderbirdvx * LEADERMAXSPEED/speed
  96.         leaderbirdvy = leaderbirdvy * LEADERMAXSPEED/speed
  97.  
  98.  
  99.     leaderbirdx += leaderbirdvx
  100.     leaderbirdy += leaderbirdvy
  101.  
  102.  
  103.    
  104.  
  105.     # Draw birds, positions and speeds
  106.     i = 0
  107.     while (i < NUMBEROFBIRDS):
  108.  
  109.         # Make copies for clarity
  110.         x = birdlist[i][0]
  111.         y = birdlist[i][1]
  112.         vx = birdlist[i][2]
  113.         vy = birdlist[i][3]
  114.  
  115.         colr = int(float(i) * 255.0/NUMBEROFBIRDS)
  116.         colg = int((NUMBEROFBIRDS-float(i)) * 255.0/NUMBEROFBIRDS)
  117.         colb = 255
  118.        
  119.         pygame.draw.circle(screen, (colr,colg,colb), (int(x), int(y)), 2, 0)
  120.  
  121.         # Birds move away from border
  122.         if (x < BORDER):
  123.             vx += BORDERSPEEDCHANGE
  124.         if (y < BORDER):
  125.             vy += BORDERSPEEDCHANGE
  126.         if (x > WIDTH - BORDER):
  127.             vx -= BORDERSPEEDCHANGE
  128.         if (y > HEIGHT - BORDER):
  129.             vy -= BORDERSPEEDCHANGE
  130.  
  131.         # Birds move towards leader bird
  132.         leaderdiffx = leaderbirdx - x
  133.         leaderdiffy = leaderbirdy - y
  134.         vx += 0.007 * leaderdiffx
  135.         vy += 0.007 * leaderdiffy
  136.  
  137.         # Move away from other nearby birds
  138.         # Also calculate average velocity of birds in larger window
  139.         j = 0
  140.         # For calculating average velocity of other birds
  141.         avxtotal = 0
  142.         avytotal = 0
  143.         avcount = 0
  144.         while (j < NUMBEROFBIRDS):
  145.             if (j != i):
  146.                 dx = birdlist[j][0] - x
  147.                 dy = birdlist[j][1] - y
  148.                 dist = math.sqrt(dx*dx + dy*dy)
  149.                 if (dist < MINDIST):
  150.                     vx -= dx * 0.2
  151.                     vy -= dy * 0.2
  152.                 if (dist < MATCHSPEEDWINDOW):
  153.                     avxtotal += birdlist[j][2]
  154.                     avytotal += birdlist[j][3]
  155.                     avcount += 1
  156.             j += 1
  157.         # Match to average velocity of nearby birds
  158.         if (avcount != 0):
  159.             avx = avxtotal / avcount
  160.             avy = avytotal / avcount
  161.             vx = 0.9 * vx + 0.1 * avx
  162.             vy = 0.9 * vy + 0.1 * avy
  163.  
  164.         # Bounce off obstacles and slow down
  165.         for barrier in barriers:
  166.             dx = barrier[0] - x
  167.             dy = barrier[1] - y
  168.             dist = math.sqrt(dx*dx + dy*dy)
  169.             if (dist < BARRIERRADIUS + 15):
  170.                 vx -= dx * 0.1
  171.                 vx *= 0.6
  172.                 vy -= dy * 0.1
  173.                 vy *= 0.6
  174.                
  175.         # Cap maximum speed
  176.         speed = math.sqrt(vx*vx + vy*vy)
  177.         if (speed > MAXSPEED):
  178.             vx = vx * MAXSPEED/speed
  179.             vy = vy * MAXSPEED/speed
  180.  
  181.         # Update positions according to speeds
  182.         birdlist[i][0] += vx
  183.         birdlist[i][1] += vy
  184.         birdlist[i][2] = vx
  185.         birdlist[i][3] = vy
  186.         i += 1
  187.  
  188.     for barrier in barriers:
  189.         pygame.draw.circle(screen, (0,100,255), (int(barrier[0]), int(barrier[1])), BARRIERRADIUS, 0)
  190.        
  191.  
  192.     #time.sleep(0.1)
  193.     pygame.display.flip()
  194.     i += 1
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×