//brightness eaters birthing tartanaters eater [] e; torch [] t; int w,h,count; BImage swap; int noses; void setup(){ count = 0; noses = 0; swap = loadImage("d.gif"); w = swap.width; h = swap.height; size(701,458);//reminder for non applet //size(w,h);//reminder for applet println("Canvas size: width:"+w+" height:"+h); e = new eater[300]; t = new torch[2]; for (int i = 0; i < e.length/2; i++){ e[i] = new eater(int(random(w)),int(random(h)),int(random(10,240)),true); } for (int i = e.length/2; i < e.length; i++){ e[i] = new eater(int(random(w)),int(random(h)),int(random(10,240)),false); } } void loop(){ background(swap); for (int i = 0; i < e.length; i++){ if(e[i].hp > 0){ e[i].move(); e[i].eat(); e[i].draw();//reminder to not draw eater sprites collide(i); } } for (int i = 0; i < t.length-2; i++){ t[i].trace(); t[i].draw();//reminder to not draw tartan sprites } } void mousePressed(){ swap.save("breeders.tif"); println("save count "+count++); } void keyPressed(){ noses++; noses %= 2; } class eater{ int x,y,hp,d,maxHp,bright,step; boolean dark; eater(int x, int y, int bright, boolean dark){ this.x = x; this.y = y; this.bright = bright; this.dark = dark; maxHp = 25; hp = 20; d = 0; step = 25; if (dark){ step = -25; } } void draw(){ color c = swap.pixels[x + y * w]; color cb = swap.pixels[x + y * w]; int r = c>>16&0xff; int g = c>>8&0xff; int b = c&0xff; int a = hp*10; int xb = x; int yb = y; stroke(r+step,g+step,b+step,a+30); fill(r,g,b,a); ellipse(x-5,y-5,10,10); if (noses == 1){ switch(d){ case 0: yb -= 2; cb = swap.pixels[((x+1)%w) + y * w]; break; case 1: xb -= 2; cb = swap.pixels[x + ((y+1)%h) * w]; break; case 2: xb -= 4; cb = swap.pixels[wr(x-1,w-1) + y * w]; yb -= 2; break; case 3: xb -= 2; cb = swap.pixels[x + wr(y-1,h-1) * w]; yb -= 4; break; } noStroke(); fill(tone(cb,+step<<1)); //fill(cb); ellipse(xb,yb,4,4); } } void eat(){ color c = swap.pixels[x + y * w]; int b = grey(c); if (b > bright && dark){ hp += (maxHp - hp)<<1; swap.pixels[x + y * w] = tone (c,step); }else if (b < bright && !dark){ hp += (maxHp - hp)<<1; swap.pixels[x + y * w] = tone (c,step); } else if (hp > 0){ hp--; } } void move(){ //locate nearest bright pixel and move to when starved int [] opt = new int[4]; opt[0] = grey(swap.pixels[((x+1)%w) + y * w]); opt[1] = grey(swap.pixels[x + ((y+1)%h) * w]); opt[2] = grey(swap.pixels[wr(x-1,w-1) + y * w]); opt[3] = grey(swap.pixels[x + wr(y-1,h-1) * w]); int [] cmp = new int[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; d = 0; } if (cmp[cmp.length-1] == opt[1]){ ch = 1; d = 1; } if (cmp[cmp.length-1] == opt[2]){ ch = 2; d = 2; } if (cmp[cmp.length-1] == opt[3]){ ch = 3; d = 3; } }else{ if (cmp[0] == opt[0]){ ch = 0; d = 0; } if (cmp[0] == opt[1]){ ch = 1; d = 1; } if (cmp[0] == opt[2]){ ch = 2; d = 2; } if (cmp[0] == opt[3]){ ch = 3; d = 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; } } } class torch{ int x,y,d,step,bright; boolean dark; torch(int x, int y, int d, int bright, boolean dark){ this.x = x; this.y = y; this.d = d; this.bright = bright; this.dark = dark; if(dark){ step = -10; } else { step = 10; } } void draw(){ color c = swap.pixels[x + y * w]; int r=c>>16&0xff; int g=c>>8&0xff; int b=c&0xff; noStroke(); if (dark){ fill(r-30,g-30,b-30,130); } else { fill(r+30,g+30,b+30,130); } rect(x-3,y-3,6,6); } void trace(){ if (dark){ switch(d){ case 0: if(grey(swap.pixels[(x+1)%w + y * w]) > bright){ x = (x+1)%w; }else if(grey(swap.pixels[(x+1)%w + y * w]) <= bright){ d = (d+1)%4; } break; case 1: if (grey(swap.pixels[x + (y+1)%h * w]) > bright){ y = (y+1)%h; }else if(d == 1 && grey(swap.pixels[x + (y+1)%h * w]) <= bright){ d = (d+1)%4; } break; case 2: if (d == 2 && grey(swap.pixels[wr(x-1,w-1) + y * w]) > bright){ x = wr(x-1,w-1); }else if(grey(swap.pixels[wr(x-1,w-1) + y * w]) <= bright){ d = (d+1)%4; } break; case 3: if(grey(swap.pixels[x + wr(y-1,h-1) * w]) > bright){ y = wr(y-1,h-1); }else if(grey(swap.pixels[x + wr(y-1,h-1) * w]) <= bright){ d = (d+1)%4; } break; } if (grey(swap.pixels[x + y * w]) - bright > bright){ swap.pixels[x + y * w] = tone(swap.pixels[x + y * w],step); } }else{ switch(d){ case 0: if(grey(swap.pixels[(x+1)%w + y * w]) <= bright){ x = (x+1)%w; }else if(grey(swap.pixels[(x+1)%w + y * w]) > bright){ d = (d+1)%4; } break; case 1: if (grey(swap.pixels[x + (y+1)%h * w]) <= bright){ y = (y+1)%h; }else if(d == 1 && grey(swap.pixels[x + (y+1)%h * w]) > bright){ d = (d+1)%4; } break; case 2: if (d == 2 && grey(swap.pixels[wr(x-1,w-1) + y * w]) <= bright){ x = wr(x-1,w-1); }else if(grey(swap.pixels[wr(x-1,w-1) + y * w]) > bright){ d = (d+1)%4; } break; case 3: if(grey(swap.pixels[x + wr(y-1,h-1) * w]) <= bright){ y = wr(y-1,h-1); }else if(grey(swap.pixels[x + wr(y-1,h-1) * w]) > bright){ d = (d+1)%4; } break; } if (grey(swap.pixels[x + y * w]) + step < bright){ swap.pixels[x + y * w] = tone(swap.pixels[x + y * w],step); } } } } void collide(int num){ int range = 4; for (int i = 0; i >16&0xff; int g=c>>8&0xff; int b=c&0xff; r += val; g += val; b += val; return color(r,g,b); } int grey(color p){ int r=p>>16&0xff; int g=p>>8&0xff; int b=p&0xff; return (r+g+b)/3; } void newLength(torch newt) { torch [] tempt = new torch[t.length + 2]; System.arraycopy(t, 0, tempt, 0, t.length); tempt[t.length] = newt; t = tempt; }