ArrayList snakes = new ArrayList(); void setup(){ size(500,500); // frameRate(4); Snake s,first,last; first = new Snake(); snakes.add(first); last = first; for(int i = 0; i < 50; i++){ s = new Snake(); snakes.add(s); s.target = last; last = s; } first.target = last; noStroke(); rectMode(CENTER); } void draw(){ background(50); for(Snake s : snakes){ s.move(); s.draw(); } } float MV = .2; int SEGCOUNT = 20; class Snake{ Snake target; float r = random(128,255); float g = random(128,255); float b = random(128,255); ArrayList spots = new ArrayList(); float xs, ys; Spot head; Spot tail; Snake(){ float x = random(500); float y = random(500); Spot s = null; head = new Spot(x,y); spots.add(head); for(int i = 0; i < SEGCOUNT-1; i++){ s = new Spot(x+i,y+i); spots.add(s); } tail = s; } void move(){ head.x += xs; head.y += ys; for(int i = spots.size()-1; i >= 1; i--){ Spot behind = spots.get(i); Spot front = spots.get(i-1); behind.x = front.x; behind.y = front.y ; } float targetX = mouseX; float targetY = mouseY; if(! mousePressed){ targetX = target.tail.x; targetY = target.tail.y; } if(head.x < targetX) xs += MV; if(head.x > targetX) xs -= MV; if(head.y < targetY) ys += MV; if(head.y > targetY) ys -= MV; if(head.x < 0) {head.x = 0; xs = abs(xs) * .5; } if(head.x > 500) {head.x = 500; xs = -abs(xs) * .5; } if(head.y < 0) {head.y = 0; ys = abs(ys) * .5; } if(head.y > 500) {head.y = 500; ys = -abs(ys) * .5; } } void draw(){ for(int i = 1; i < spots.size() - 1; i++){ float cmul = map(i,0,SEGCOUNT,1,0); fill(r*cmul,g*cmul,b*cmul); Spot front = spots.get(i); Spot behind = spots.get(i+1); float a = atan2(front.y - behind.y, front.x - behind.x); pushMatrix(); translate(front.x,front.y); rotate(a); float sz = map(i,0,SEGCOUNT, 80,20); rect(0,0,sz,sz); popMatrix(); } } } class Spot{ float x,y; Spot(float a, float b){ x = a; y = b; } }