// NotLife // David Bollinger, Apr 2006 // http://www.davebollinger.com // for Processing 0109 Beta /** Click and/or drag to spawn life
Press 'r' to fill randomly
Press 'z' to decrease spawn rate (die out faster overall)
Press 'x' to increase spawn rate (die out slower overall)
Press space to clear
*/ NotLifeCA ca; Palette pal; float spawnRate = 0.25; void setup() { size(360,360,P3D); ca = new NotLifeCA(); pal = new Palette("palette.jpg"); background(#ffffff); loadPixels(); } void draw() { if (mousePressed && mouseButton==LEFT) { ca.spawn(mouseX/2,mouseY/2); } ca.move(); ca.draw(pal); } void keyPressed() { if (key=='`') saveFrame("frame.tga"); if (key==' ') { ca.wipe(); background(#ffffff); } if (key=='r') { ca.create(); background(#ffffff); } if (key=='z') { if (spawnRate > 0.20) spawnRate -= 0.01; } if (key=='x') { if (spawnRate < 0.30) spawnRate += 0.01; } } class Palette { color [] colors; Palette(String filename) { colors = new color[256]; PImage img = loadImage(filename); for (int i=0; i<256; i++) colors[i] = img.pixels[i]; } color getColor() { return getColor((int)(random(256))); } color getColor(int i) { if (i<0) i=0; else if (i>255) i=255; return colors[i]; } } class NotLifeCA { static final int maxlife = 63; int cols, rows; int [][] cells; NotLifeCA() { cols = width/2; rows = height/2; cells = new int[rows][cols]; wipe(); } void wipe() { for (int r=0; r=0) && (c=0) && (r=rows) rrr-=rows; for (int cc=-1; cc<2; cc++) { int ccc = c + cc; if (ccc<0) ccc += cols; else if (ccc>=cols) ccc -= cols; if (random(1.0) < spawnRate) { if (cells[rrr][ccc] < 0) { cells[rrr][ccc] = maxlife; } } } } } } } } void draw(Palette pal) { int index = 0; for (int r=0; r= 0) { pixels[index] = pal.colors[cell<<2]; } index+=2; } index += width; } updatePixels(); } }