//brightness eaters BImage swap; eater [] e; int w,h,count; void setup(){ count = 0; swap = loadImage("b.gif"); w = swap.width; h = swap.height; size(553,745); //size(w,h); e = new eater[1000]; for (int i = 0; i < e.length/2; i++){ e[i] = new eater(int(random(w)),int(random(h)),random(50,200),true); } for (int i = e.length/2; i < e.length; i++){ e[i] = new eater(int(random(w)),int(random(h)),random(50,200),false); } colorMode(HSB); noStroke(); } void loop(){ background(swap); for (int i = 0; i < e.length; i++){ if(e[i].hp > 0){ if (e[i].hp < 15 && e[i].hp > 0){ e[i].move(); } e[i].eat(); e[i].draw();//reminder to not draw eater sprites } } } void mousePressed(){ swap.save("drain.tif"); println("save count "+count++); } class eater{ int x,y,hp; float bright; boolean dark; eater(int x, int y, float bright, boolean dark){ this.x = x; this.y = y; this.bright = bright; this.dark = dark; hp = 20; } void draw(){ float s = hp; if (s > 255){ s = 255; } colorMode(RGB); if(dark){ fill(0,0,0,hp*10); }else{ fill(255,255,255,hp*10); } ellipse(x-3,y-3,6,6); colorMode(HSB); } void eat(){ color c = swap.pixels[x + y * w]; float u = hue(c); float s = saturation(c); float b = brightness(c); if (b > bright && dark){ hp++; swap.pixels[x + y * w] = drain (c,10,true); //path halo swap.pixels[(x+1)%w + y * w] = drain (swap.pixels[(x+1)%w + y * w],2,true); swap.pixels[x + (y+1)%h * w] = drain (swap.pixels[x + (y+1)%h * w],2,true); swap.pixels[wr(x-1,w-1) + y * w] = drain (swap.pixels[wr(x-1,w-1) + y * w],2,true); swap.pixels[x + wr(y-1,h-1) * w] = drain (swap.pixels[x + wr(y-1,h-1) * w],2,true); }else if (b < bright && !dark){ hp++; swap.pixels[x + y * w] = drain (c,10,false); //path halo swap.pixels[(x+1)%w + y * w] = drain (swap.pixels[(x+1)%w + y * w],2,false); swap.pixels[x + (y+1)%h * w] = drain (swap.pixels[x + (y+1)%h * w],2,false); swap.pixels[wr(x-1,w-1) + y * w] = drain (swap.pixels[wr(x-1,w-1) + y * w],2,false); swap.pixels[x + wr(y-1,h-1) * w] = drain (swap.pixels[x + wr(y-1,h-1) * w],2,false); } else if (hp > 0){ hp--; } } void move(){ //locate nearest bright pixel and move to when starved float [] opt = new float[4]; opt[0] = brightness(swap.pixels[((x+1)%w) + y * w]); opt[1] = brightness(swap.pixels[x + ((y+1)%h) * w]); opt[2] = brightness(swap.pixels[wr(x-1,w-1) + y * w]); opt[3] = brightness(swap.pixels[x + wr(y-1,h-1) * w]); float [] cmp = new float[4]; System.arraycopy(opt, 0, cmp, 0, opt.length); sort(cmp); int ch = 4; if(dark){ if (cmp[cmp.length-1] == opt[0]){ ch = 0; } if (cmp[cmp.length-1] == opt[1]){ ch = 1; } if (cmp[cmp.length-1] == opt[2]){ ch = 2; } if (cmp[cmp.length-1] == opt[3]){ ch = 3; } }else{ if (cmp[0] == opt[0]){ ch = 0; } if (cmp[0] == opt[1]){ ch = 1; } if (cmp[0] == opt[2]){ ch = 2; } if (cmp[0] == opt[3]){ ch = 3; } } switch(ch){ case 0: x = (x+1)%w; break; case 1: y = (y+1)%h; break; case 2: x = wr(x-1,w-1); break; case 3: y = wr(y-1,h-1); break; } } } //screen wrap around function int wr(int v, int lim){ if (v < 0){ v = lim; } return v; } color drain (color c, float val, boolean t){ float u = hue(c); float s = saturation(c); float b = brightness(c); if(t){ b -= val; }else{ b += val; } return color(u,s,b); }