Actionscript:
-
stage.frameRate = 30;
-
scaleX = scaleY = 2.5;
-
var canvas:BitmapData = new BitmapData(200,200,false, 0x000000);
-
addChild(new Bitmap(canvas));
-
var w:int = canvas.width;
-
var size:int = canvas.width * canvas.height;
-
var read:Vector.<uint> = new Vector.<uint>(size);
-
var write:Vector.<uint> = new Vector.<uint>(size);
-
-
read = canvas.getVector(canvas.rect);
-
var loc:int = 199*w;
-
for(var i:int = 0; i <= 200; i++){
-
read[loc+i]=0xFFFFFFFF;
-
}
-
write = read.concat();
-
-
var shapes:Array = [];
-
shapes[0] = [[1,1],[1,1]];
-
shapes[1] = [[1,1,1,1]];
-
shapes[2] = [[1],[1],[1],[1]];
-
shapes[3] = [[1,1,1],[0,1,0]];
-
shapes[4] = [[1,0],[1,1],[1,1],[0,1]];
-
shapes[5] = [[1,0,0],[1,1,1]];
-
shapes[6] = [[1,1,0], [0,1,1]];
-
shapes[7] = [[1,1], [1,0],[1,0]];
-
function drawShape(xp:int, yp:int, pattern:Array, col:uint):void{
-
var i:int = 0;
-
var loc:int = xp + yp * w;
-
if (int(Math.random()*2) == 1){
-
pattern.reverse();
-
}
-
if (int(Math.random()*2)){
-
for (i= 0; i<pattern.length; i++){
-
pattern[i].reverse();
-
}
-
}
-
for (i = 0; i<pattern.length; i++){
-
for (var j:int = 0; j<pattern[i].length; j++){
-
if (pattern[i][j] == 1){
-
write[loc + i * w + j] = col;
-
}
-
}
-
}
-
}
-
-
var counter:int = 0;
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
var i:int;
-
canvas.lock();
-
if (counter % 20 == 1){
-
for ( i= 0; i<20; i++){
-
drawShape(5+ i * 10, 10, shapes[int(Math.random() * shapes.length)], Math.random()*0xFFFFFF);
-
}
-
}
-
counter++;
-
read = write.concat();
-
for (i = 0; i<size; i++){
-
var curr:uint = read[i];
-
if (curr != 0xFF000000 && curr != 0xFFFFFFFF){
-
var below:uint = read[i + w];
-
if (below == 0xFF000000 || below == curr){
-
var above:uint = read[i - w];
-
if (above == 0xFF000000){
-
write[i] = 0xFF000000;
-
}
-
write[i + w] = curr;
-
}else{
-
var index:uint;
-
if (int(Math.random()*2) == 1){
-
index = i + w - 1;
-
}else{
-
index = i + w + 1;
-
}
-
if (read[index] == 0xFF000000){
-
write[index] = curr;
-
}
-
}
-
}
-
}
-
canvas.setVector(canvas.rect, write);
-
canvas.unlock();
-
}
Quickly thrown together sand cellular automata - with some tetris shapes.