In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

niconico Tetris

Get Adobe Flash player
by nitoyon 21 Dec 2008
// forked from flashrod's Tetris
// forked from nitoyon's ニコニコテレビちゃんを動かしてみた
package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.Event;
    import flash.events.KeyboardEvent;

    public class Tetris extends Sprite {
        private const W:int = 10;
        private const H:int = 20;
        private const UNIT:int = 22;
        private const COLOR:Array =[0x000000, 0x00FFFF, 0xFFFF00, 0x22FF22, 0xFF2222, 0x4444FF, 0xFF8844, 0xFF22FF];
        private const PAT:Array =[[[1, 1, 1, 1]],
                                  [[0, 2, 0], [2, 2, 2]],
                                  [[3, 3, 0], [0, 3, 3]],
                                  [[0, 4, 4], [4, 4, 0]],
                                  [[5, 5], [5, 0], [5, 0]],
                                  [[6, 6], [0, 6], [0, 6]],
                                  [[7, 7], [7, 7]]];
        private const SPEED:Array = [30, 20, 10, 5];

        private const VK_H:int = 72; // h
        private const VK_J:int = 74; // j
        private const VK_K:int = 75; // k
        private const VK_L:int = 76; // l
        private const VK_LEFT:int = 37; // left
        private const VK_RIGHT:int = 39; // right
        private const VK_UP:int = 38; // up
        private const VK_DOWN:int = 40; // down
        private const VK_SPC:int = 32; // space

        private var field:Array = [];
        private var piece:Array;
        private var next:Array;
        private var text:TextField = new TextField();
        private var keytable:Array = [];
        private var count:int = 0;
        private var step:int = 0;
        private var px:int;
        private var py:int;
        public static var param:int = 0;

        public function Tetris() {
            text.x = W*UNIT;
            text.text="Next:";
            addChild(text);
            var t:TextField = new TextField();
            t.x = W * UNIT;
            t.y = 8 * UNIT;
            t.text = "usage:\n   h(or left),j(or up),k,\n   l(or right),SPACE(or down)";
            t.autoSize = "left";
            addChild(t);
            field = new Array(H).map(function():*{
                return new Array(W).map(function():*{
                    return 0;
                })
            });
            keytable[VK_H] = keytable[VK_LEFT] = function():void {px -= space(px-1, py, piece)};
            keytable[VK_J] = keytable[VK_UP] = function():void {rotate(true)};
            keytable[VK_K] = function():void {rotate(false)};
            keytable[VK_L] = keytable[VK_RIGHT] = function():void {px += space(px+1, py, piece)};
            keytable[VK_SPC] = keytable[VK_DOWN] = function():void {drop(); pick();};
            stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
                if (keytable[e.keyCode]) {
                    keytable[e.keyCode]();
                    repaint();
                }
            });
            pick();
            pick();
            addEventListener(Event.ENTER_FRAME, function(e:Event):void {
                param = (param + 9) % 360;
                for (var j:int = 0; j < numChildren; j++) {
                    if(getChildAt(j) is Body) Body(getChildAt(j)).update();
                }

                if (--step < 0) {
                    step=SPEED[int(count/10)];
                    if (space(px, py+1, piece)) {
                        ++py;
                        repaint();
                    } else {
                        drop();
                        pick();
                    }
                }
            });
        }

        private function rotate(clock:Boolean):void {
            var r:Array = new Array(piece[0].length).map(function():*{return [];});
            for (var j:int = 0; j<piece.length; ++j)
                for (var i:int = 0; i < r.length; ++i)
                    if (clock)
                        r[i][piece.length-1-j] = piece[j][i];
                    else
                        r[r.length-1-i][j] = piece[j][i];
            if (space(px, py, r))
                piece = r;
        }

        private function repaint():void {
            for (var j:int = numChildren - 1; j >= 0; j--) {
                if (getChildAt(j) is Body) removeChildAt(j);
            }
            graphics.clear();
            graphics.lineStyle(0);
            graphics.drawRect(0, 0, W*UNIT, H*UNIT);
            graphics.endFill();
            for (j = 0; j < H; ++j)
                for (var i:int = 0; i < W; ++i) {
                    var g:int = 0;
                    if (py <= j && j < (py+piece.length) && px <= i && i < (px+piece[0].length))
                        g = piece[j-py][i-px];
                    if (g == 0)
                        g = field[j][i];
                    if(g == 0) continue;
                    var body:Body = new Body(COLOR[g]);
                    addChild(body);
                    body.x = (i+.5)*UNIT;
                    body.y = (j-.5)*UNIT;
                }
            for (j = 0; j < next.length; ++j)
                for (i = 0; i < next[j].length; ++i) {
                    if(next[j][i] == 0) continue;
                    var body:Body = new Body(COLOR[next[j][i]]);
                    addChild(body);
                    body.x = (i+W+1)*UNIT;
                    body.y = (j+2)*UNIT;
                }
        }

        private function space(x:int, y:int, p:Array):int {
            for (var j:int = 0; j < p.length; ++j) {
                if (0 > (y+j) || (y+j) >= H)
                    return 0;
                for (var i:int = 0; i < p[j].length; ++i) {
                    if (0 > (x+i) || (x+i) >= W)
                        return 0;
                    if (p[j][i] && field[y+j][x+i])
                        return 0;
                }
            }
            return 1;
        }

        private function drop():void {
            for (; space(px, py+1, piece); py++)
                ;
            for (var j:int = 0; j < piece.length; ++j)
                for (var i:int = 0; i < piece[j].length; ++i)
                    if (piece[j][i])
                        field[py+j][px+i] = piece[j][i];
            for (j=0; j<H; ++j)
                if (field[j].indexOf(0) < 0) {
                    field.splice(j, 1);
                    field.unshift([]);
                    for (i=0; i<W; ++i)
                        field[0][i] = 0;
                }

            count++;
            if (count/10 >= SPEED.length)
                count = 0;
        }

        private function pick():void {
            piece = next;
            if (piece != null) {
                px = (W-piece[0].length)/2;
                py = 0;
                if (!space(px, py, piece))
                    text.text="GAME OVER";
            }
            next = PAT[int(Math.random()*PAT.length)];
        }
    }
}

import flash.display.Sprite;   
import flash.filters.GlowFilter;   
  
class Body extends Sprite{   
    private var eye1:Sprite;
    private var eye2:Sprite;
    private var antena:Sprite;
    public function Body(color:uint){     
        scaleX = scaleY = .19;
        var l1:Leg = new Leg(color);   
        l1.x = -30; l1.y = 150   
        addChild(l1);   
  
        var l2:Leg = new Leg(color);   
        l2.x = 30; l2.y = 150   
        addChild(l2);   
  
        // アンテナを描く   
        antena = new Sprite();   
        antena.graphics.lineStyle(4, 0x000000);   
        antena.graphics.moveTo(-30, 40);   
        antena.graphics.lineTo(  0, 60);   
        antena.graphics.lineTo( 30, 40);   
        addChild(antena);   
     
        // 外側四角を描く   
        var s2:Sprite = new Sprite();   
        s2.graphics.lineStyle(6, 0x000000);   
        s2.graphics.beginFill(color);   
        s2.graphics.drawRect(-60, 0, 120, 90);   
        s2.graphics.endFill();   
        s2.y = 60;   
        addChild(s2);   
     
        // 内側四角を描く   
        var s3:Sprite = new Sprite();   
        s3.graphics.lineStyle(4, 0x000000);   
        s3.graphics.beginFill(0xffffff, 0.8);   
        s3.graphics.drawRect(-50, 0, 100, 70);   
        s3.graphics.endFill();   
        s3.y = 70;   
        addChild(s3);   
     
        // 左目を描く   
        eye1 = new Sprite();   
        eye1.graphics.beginFill(0x000000);   
        eye1.graphics.drawCircle(0, 0, 5);   
        eye1.x = -30;   
        eye1.y = 100;   
        addChild(eye1);   
     
        // 右目を描く   
        eye2 = new Sprite();   
        eye2.graphics.beginFill(0x000000);   
        eye2.graphics.drawCircle(0, 0, 5);   
        eye2.x = 30;   
        eye2.y = 90;   
        addChild(eye2);   
     
        // 口を描く   
        var s8:Sprite = new Sprite();   
        s8.graphics.lineStyle(1, 0x000000);   
        s8.graphics.beginFill(0x000000);   
        s8.graphics.moveTo(  0, 120);   
        s8.graphics.lineTo(-10, 130);   
        s8.graphics.lineTo( 15, 130);   
        s8.graphics.endFill();   
        addChild(s8); 

        update();
    }

    public function update():void{
        var s:Number = Math.cos(Tetris.param / 180 * Math.PI);
        eye1.y = 95 + 5 * s;
        eye2.y = 95 - 5 * s;
        antena.scaleX = s; 
    }
}   
  
class Leg extends Sprite{   
    public function Leg(color){   
        graphics.lineStyle(4, 0x000000);   
        graphics.beginFill(color);   
        graphics.moveTo( 20, -12);   
        graphics.lineTo(  0,  12);   
        graphics.lineTo(-20, -12);   
        graphics.endFill();   
    }   
}