Advertisement
letsgetprocessing

Attract

Apr 30th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. from toxi.geom import Vec2D
  2.  
  3.  
  4. W = H = 500
  5. FPS = 20.0
  6. DURATION = 12
  7. N_FRAMES = DURATION * FPS
  8. N_SAMPLES = 1
  9. BG_COLOR = color(255)
  10. MAIN_COLOR = color(0)
  11. GAP = 6
  12. MAX_HELPLESSNESS = 8
  13. MAX_SPEED = 5
  14. UNIT_SIZE = 20
  15. RECORD = False
  16.  
  17.  
  18. DARK_RED = color(83, 12, 12)
  19. RED = color(143, 14, 14)
  20. ORANGE = color(250, 126, 10)
  21. WHITE = color(247, 246, 222)
  22.  
  23.  
  24. class Goal(object):
  25.     def __init__(self, x, y):
  26.         self.pos = Vec2D(x, y)
  27.         self.size = 30
  28.  
  29.     def display(self):
  30.         stroke(RED)
  31.         strokeWeight(3)
  32.         fill(DARK_RED)
  33.         ellipse(self.pos.x(), self.pos.y(), self.size - 6, self.size - 6)
  34.  
  35.  
  36. class Unit(object):
  37.     def __init__(self, x, y):
  38.         self.pos = Vec2D(x, y)
  39.         self.size = UNIT_SIZE
  40.         self.speed = 1
  41.         self.helplessness = 0
  42.  
  43.     def display(self):
  44.         stroke(RED)
  45.         strokeWeight(3)
  46.         fill(ORANGE)
  47.         ellipse(self.pos.x(), self.pos.y(), self.size, self.size)
  48.  
  49.     def update(self):
  50.         if self.helplessness < MAX_HELPLESSNESS and self.pos.distanceTo(goal.pos) > self.size / 2 + goal.size / 2 + GAP:
  51.             d = goal.pos.sub(self.pos).getNormalizedTo(self.speed)
  52.             score = -999999999
  53.             next_pos = None
  54.             min_dist = 0
  55.             for th in range(360):
  56.                 theta = radians(th)
  57.                 new_pos = self.pos.add(d.getRotated(theta))
  58.                 new_dist_to_goal = new_pos.distanceTo(goal.pos)
  59.                 min_dist_to_other = min(new_pos.distanceTo(unit.pos) for unit in units if not unit is self)
  60.                 new_score = min_dist_to_other - new_dist_to_goal
  61.                 if new_score > score and not min_dist_to_other < GAP + self.size:
  62.                     score = new_score
  63.                     next_pos = new_pos
  64.                     min_dist = min_dist_to_other
  65.             if next_pos:
  66.                 if self.pos.distanceTo(goal.pos) <= next_pos.distanceTo(goal.pos):
  67.                     next_pos = self.pos.add(next_pos.sub(self.pos).getNormalized())
  68.                     self.helplessness += 1
  69.                     self.speed = 1
  70.                 else:
  71.                     self.speed = min(self.speed + 1, MAX_SPEED)
  72.                 self.pos = next_pos
  73.         if N_FRAMES - frameCount < UNIT_SIZE / 2:
  74.             self.size = max(0, self.size - 2)
  75.  
  76.  
  77. def draw_(t):
  78.     background(WHITE)
  79.     noStroke()
  80.  
  81.     goal.display()
  82.     for unit in units:
  83.         unit.update()
  84.         unit.display()
  85.  
  86. def setup():
  87.     global units
  88.     global goal
  89.  
  90.     size(W, H)
  91.     frameRate(FPS)
  92.  
  93.     goal = Goal(width / 2, height / 2)
  94.     units = []
  95.     for i in range(300):
  96.         r = random(350, 800)
  97.         angle = random(TWO_PI)
  98.         x, y = r * cos(angle) + width / 2, r * sin(angle) + height / 2
  99.         for unit in units:
  100.             if Vec2D(x, y).distanceTo(unit.pos) < GAP + UNIT_SIZE:
  101.                 break
  102.         else:
  103.             units.append(Unit(x, y))
  104.  
  105. def draw():
  106.     if not RECORD:
  107.         t = (frameCount / float(N_FRAMES)) % 1.0
  108.         draw_(t)
  109.     elif N_SAMPLES <= 1:
  110.         t = (frameCount / float(N_FRAMES)) % 1.0
  111.         draw_(t)
  112.         if frameCount <= N_FRAMES:
  113.             saveFrame('gif/####.gif')
  114.         else:
  115.             exit()
  116.     else:
  117.         result = [[0, 0, 0] for i in range(W * H)]
  118.         for sample in range(N_SAMPLES):
  119.             t = (frameCount + sample / float(N_SAMPLES)) / N_FRAMES
  120.             draw_(t)
  121.             loadPixels()
  122.             for i, pix in enumerate(pixels):
  123.                 result[i][0] += red(pix)
  124.                 result[i][1] += green(pix)
  125.                 result[i][2] += blue(pix)
  126.         loadPixels()
  127.         for i, rgb in enumerate(result):
  128.             pixels[i] = color(rgb[0] / N_SAMPLES, rgb[1] / N_SAMPLES, rgb[2] / N_SAMPLES)
  129.         updatePixels()
  130.         if frameCount <= N_FRAMES:
  131.             saveFrame('gif/####.gif')
  132.         else:
  133.             exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement