flash on 2012-3-16

by MMMMMonchi
♥0 | Line 148 | Modified 2012-03-16 12:08:06 | MIT License
play

ActionScript3 source code

/**
 * Copyright MMMMMonchi ( http://wonderfl.net/user/MMMMMonchi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/n4ck
 */

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    public class Main extends Sprite {
        
        private var keys:Array=[];
        private var status:Status = new Status();
        private var map/*Array*/:Array;    // テトリス盤面
        private const WIDTH:int = 12;    // 横幅
        private const HEIGHT:int = 21;    // 縦幅
        private const FALL_SPEED:Number = 0.05;    // 落ちるスピード
        
        
        public function Main() {
            map = [];
            for (var y:int = 0; y < HEIGHT; y++)
            {
                map[y] = new Array();
                for (var x:int = 0; x < WIDTH; x++)
                {
                    // 左端/右端/下端は壁なので1を入れ、それ以外のところは0を入れる
                    if (x == 0 || x == WIDTH - 1 || y == HEIGHT - 1) map[y][x] = 1;
                    else map[y][x] = 0;
                }
            }
            
            
            initalize();
            addEventListener(Event.ENTER_FRAME,onEnterFrame);
            stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
            
            
            
                        
        }
        private function initalize():void
        {
            status.x = 4;
            status.y = 0;
            status.block = Block.SHAPE[0];
            }
        private function onEnterFrame(e:Event):void
        {
            status.y += FALL_SPEED;
            draw();
            }
        private function draw():void
        {
            graphics.clear(); // 初期化
            graphics.beginFill(0x0);
            graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            graphics.endFill();
 
    for (var y:int = 0; y < HEIGHT; y++)
    {
        for (var x:int = 0; x < WIDTH; x++)
        {
            // 注目しているマスに0が入っていない、つまり壁かブロックが入っている場合
            if (map[y][x])
            {    
                // 壁だったら
                if (x == 0 || x == WIDTH - 1 || y == HEIGHT - 1)
                {
                    graphics.lineStyle(1.0);
                    graphics.beginFill(0x555555);
                }
                // ブロックだったら
                else
                {
                    graphics.lineStyle(2.0);
                    graphics.beginFill(Block.COLOR[map[y][x]]);
                }
                graphics.drawRect(x * Block.WIDTH, y * Block.HEIGHT, Block.WIDTH, Block.HEIGHT);
                graphics.endFill();
            }
        }
        var block:Array = status.block;
    for (y = 0; y < Block.SIZE; y++)
    {
        for (x = 0; x < Block.SIZE; x++)
        {
            // 4*4配列の中でブロックの描画があるところだけを表示する
            if (block[y][x])
            {
                graphics.lineStyle(1.5);
                graphics.beginFill(Block.COLOR[block[y][x]]);
                graphics.drawRect((status.x + x) * Block.WIDTH, (status.y + y) * Block.HEIGHT, Block.WIDTH, Block.HEIGHT);
                graphics.endFill();
            }
        }
    }
    }
        }
        private function endProcess():void
        {
            }
        private function onKeyDown(e:KeyboardEvent):void
        {
            keys[e.keyCode]=true;
            }
        private function onKeyUp(e:KeyboardEvent):void
        {
            keys[e.keyCode]=false;
            }          
    }
}

class Status
{
    public var x:Number;                // このx, yの位置からブロックを表示する
    public var y:Number;            
    public var block:Array;    // 今持っているブロック
}

class Block{
      public static const SIZE:int=4;
      public static const WIDTH:int=20;
      public static const HEIGHT:int=20;
      public static const COLOR:Array=[0x0, 0xFEFBB3, 0x69DEEE, 0x3DC642, 0xF33433, 0x797CFB, 0xF88C2D, 0xFFAAFF];
      public static const SHAPE:Array=[[
            [0, 0, 0, 0],
            [0, 1, 1, 0],
            [0, 1, 1, 0],
            [0, 0, 0, 0]
        ],
        [
            [0, 2, 0, 0],
            [0, 2, 0, 0],
            [0, 2, 0, 0],
            [0, 2, 0, 0]
        ],
        [
            [0, 0, 0, 0],
            [0, 3, 3, 0],
            [3, 3, 0, 0],
            [0, 0, 0, 0]
        ],
        [
            [0, 0, 0, 0],
            [4, 4, 0, 0],
            [0, 4, 4, 0],
            [0, 0, 0, 0]
        ],
        [
            [0, 0, 0, 0],
            [5, 0, 0, 0],
            [5, 5, 5, 0],
            [0, 0, 0, 0]
        ],
        [
            [0, 0, 0, 0],
            [0, 0, 6, 0],
            [6, 6, 6, 0],
            [0, 0, 0, 0]
        ],
        [
            [0, 0, 0, 0],
            [0, 7, 0, 0],
            [7, 7, 7, 0],
            [0, 0, 0, 0]
        ]];
      
      
    }