/* 'game of life' todo: behavior near bitmap edge */ package { import flash.display.*; import flash.geom.*; import flash.filters.*; import flash.events.*; import flash.utils.Timer; [SWF(width="400", height="400", backgroundColor="0xffffff", frameRate="15")] /** * @author Matuesz Malczak (http://segfaultlabs.com) */ public class Main extends MovieClip { private var bd:BitmapData = new BitmapData( 200, 200, false ); private var bd2:BitmapData = new BitmapData( bd.width, bd.height, bd.transparent ); private var bd3:BitmapData = new BitmapData( bd.width, bd.height, bd.transparent ); private var wr:Rectangle = new Rectangle(0,0,bd.width,bd.height ); private var zp:Point = new Point(0,0); private var lock:Boolean = false; private var fil:ConvolutionFilter = new ConvolutionFilter(3,3,[ 1,1,1, 1,0.1,1, 1,1,1 ], 1, 0 ) public function Main() { var bgl:Bitmap = new Bitmap(bd3); bgl.x = bgl.y = 0 bgl.scaleX = 400 /bgl.width; bgl.scaleY = 400 /bgl.height; addChild(bgl); bd3.fillRect( new Rectangle(0,0,bd.width,bd.height), 0x100000 ); /* game of life oscillator called 'crocodile' :) */ bd3.setPixel( 13,11, 0x00ff00 ); bd3.setPixel( 14,11, 0x00ff00 ); bd3.setPixel( 15,11, 0x00ff00 ); bd3.setPixel( 13,12, 0x00ff00 ); bd3.setPixel( 15,12, 0x00ff00 ); bd3.setPixel( 13,13, 0x00ff00 ); bd3.setPixel( 14,13, 0x00ff00 ); bd3.setPixel( 15,13, 0x00ff00 ); bd3.setPixel( 13,14, 0x00ff00 ); bd3.setPixel( 14,14, 0x00ff00 ); bd3.setPixel( 15,14, 0x00ff00 ); bd3.setPixel( 13,15, 0x00ff00 ); bd3.setPixel( 14,15, 0x00ff00 ); bd3.setPixel( 15,15, 0x00ff00 ); bd3.setPixel( 13,16, 0x00ff00 ); bd3.setPixel( 14,16, 0x00ff00 ); bd3.setPixel( 15,16, 0x00ff00 ); bd3.setPixel( 13,17, 0x00ff00 ); bd3.setPixel( 15,17, 0x00ff00 ); bd3.setPixel( 13,18, 0x00ff00 ); bd3.setPixel( 14,18, 0x00ff00 ); bd3.setPixel( 15,18, 0x00ff00 ); /* random 40% */ var xx:int, yy:int, i:int; for ( i=0; i < 0.4 * bd.width * bd.height; i+=1 ) { xx = 20+Math.floor( (bd.width-20)*Math.random() ) yy = 20+Math.floor( (bd.height-20)*Math.random() ) bd3.setPixel(xx,yy,0x00ff00); }; addEventListener( Event.ENTER_FRAME, updateGLdata ); var t:Timer = new Timer(5000); t.addEventListener( TimerEvent.TIMER, addGlider ); t.start(); }; private function updateGLdata(evt:Event):void { if ( lock ) return; lock = true; bd.copyPixels( bd3, wr, zp ); bd2.applyFilter( bd, wr, zp, fil ); bd3.fillRect( wr, 0x100000 ); /* game of life */ bd3.threshold( bd2, wr, zp, "==", 0xff500000, 0xff00ff00, 0xffff0000, false ); bd3.threshold( bd2, wr, zp, "==", 0xff510000, 0xff00ff00, 0xffff0000, false ); bd3.threshold( bd2, wr, zp, "==", 0xff600000, 0xff00ff00, 0xffff0000, false ); lock = false; }; /* single glider fired from bottom left corner */ private function addGlider( evt:Event ):void { if ( lock ) return; lock = true; var i:int = bd3.height-4; bd3.setPixel(1,i, 0x00ff00); bd3.setPixel(2,i, 0x00ff00); bd3.setPixel(3,i, 0x00ff00); bd3.setPixel(3,i+1, 0x00ff00 ); bd3.setPixel(2,i+2, 0x00ff00); lock = false; }; } } game of life