/** Drag the left mouse button to add life, other mouse buttons to remove it. The R key to reinitialise. The colours indicate how long there has been life in a cell: 1. blue, 2. cyan, 3. green, 4. yellow, 5+ red. On death, the colours simply fade. */ void dummy() { } private static int[] cells; private static int cells_h; private static int cells_w; private static final int pcshift=2; public final void setup() { size(256, 416, P2D); frameRate(12); cells_w = width>>pcshift; cells_h = height>>pcshift; cells=new int[cells_w*cells_h]; reinit(); } private static void reinit() { Random r=new Random(); int add=r.nextInt(32)+32; int x=r.nextInt(cells_w),y=r.nextInt(cells_h); for (int i=0;i>3)&0x1f1f1f; while (add-->0) { cells[y*cells_w+x]=0x80; x=(x+r.nextInt(5)-2+cells_w)%cells_w; y=(y+r.nextInt(5)-2+cells_h)%cells_h; } } private final void update() { for (int i=0;i>1)&0x7f7f7f; for (int y=0,i=0;y>>22)|((n&0x4000)>>>14)|((n&0x40)>>>6); } public final void mouseDragged() { int cx=mouseX>>pcshift; if (cx<0) cx=0; else if (cx>=cells_w) cx=cells_w-1; int cy=mouseY>>pcshift; if (cy<0) cy=0; else if (cy>=cells_h) cy=cells_h-1; final int idx = cy*cells_w+cx; cells[idx]=mouseButton==LEFT ? cells[idx]|0x80 : cells[idx]&0x7f7f7f; } public void keyPressed() { if (key=='r') reinit(); } public final void draw() { update(); loadPixels(); for (int y=0;y>pcshift)*cells_w; for (int x=0,i=y*width+x; x>pcshift)]; } } updatePixels(); }