Tetris Sand CA

Actionscript:
  1. stage.frameRate = 30;
  2. scaleX = scaleY = 2.5;
  3. var canvas:BitmapData = new BitmapData(200,200,false, 0x000000);
  4. addChild(new Bitmap(canvas));
  5. var w:int = canvas.width;
  6. var size:int = canvas.width * canvas.height;
  7. var read:Vector.<uint> = new Vector.<uint>(size);
  8. var write:Vector.<uint> = new Vector.<uint>(size);
  9.  
  10. read = canvas.getVector(canvas.rect);
  11. var loc:int = 199*w;
  12. for(var i:int = 0; i <= 200; i++){
  13.        read[loc+i]=0xFFFFFFFF;
  14. }
  15. write = read.concat();
  16.  
  17. var shapes:Array = [];
  18. shapes[0] = [[1,1],[1,1]];
  19. shapes[1] = [[1,1,1,1]];
  20. shapes[2] = [[1],[1],[1],[1]];
  21. shapes[3] = [[1,1,1],[0,1,0]];
  22. shapes[4] = [[1,0],[1,1],[1,1],[0,1]];
  23. shapes[5] = [[1,0,0],[1,1,1]];
  24. shapes[6] = [[1,1,0], [0,1,1]];
  25. shapes[7] = [[1,1], [1,0],[1,0]];
  26. function drawShape(xp:int, yp:int, pattern:Array, col:uint):void{
  27.     var i:int = 0;
  28.     var loc:int = xp + yp * w;
  29.     if (int(Math.random()*2) == 1){
  30.         pattern.reverse();
  31.     }
  32.     if (int(Math.random()*2)){
  33.         for (i= 0; i<pattern.length; i++){
  34.             pattern[i].reverse();
  35.         }
  36.     }
  37.     for (i = 0; i<pattern.length; i++){
  38.          for (var j:int = 0; j<pattern[i].length; j++){
  39.              if (pattern[i][j] == 1){
  40.                 write[loc + i * w + j] = col;
  41.              }
  42.         }
  43.     }
  44. }
  45.  
  46. var counter:int = 0;
  47. addEventListener(Event.ENTER_FRAME, onLoop);
  48. function onLoop(evt:Event):void {
  49.      var i:int;
  50.      canvas.lock();
  51.      if (counter % 20 == 1){
  52.         for ( i= 0; i<20; i++){
  53.              drawShape(5+  i * 10, 10, shapes[int(Math.random() * shapes.length)], Math.random()*0xFFFFFF);
  54.         }
  55.      }
  56.      counter++;
  57.      read = write.concat();
  58.      for (i = 0; i<size; i++){
  59.          var curr:uint = read[i];
  60.          if (curr != 0xFF000000 && curr != 0xFFFFFFFF){
  61.             var below:uint = read[i + w];
  62.             if (below == 0xFF000000 || below == curr){
  63.               var above:uint = read[i - w];
  64.               if (above == 0xFF000000){
  65.                  write[i] = 0xFF000000;
  66.               }
  67.               write[i + w] = curr;
  68.             }else{
  69.                 var index:uint;
  70.                 if (int(Math.random()*2) == 1){
  71.                     index = i + w - 1;
  72.                 }else{
  73.                     index = i + w + 1;
  74.                 }
  75.                 if (read[index] == 0xFF000000){
  76.                     write[index] = curr;
  77.                 }
  78.             }
  79.          }
  80.      }
  81.      canvas.setVector(canvas.rect, write);
  82.      canvas.unlock();
  83. }

Quickly thrown together sand cellular automata - with some tetris shapes.

This entry was posted in BitmapData, Vector, misc and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*