貘良了 发表于 2009-12-4 14:44:24

俄罗斯方块(AS3)

新建一个Flash,文档类绑定为Tetris.代码:

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 =;
      private const PAT:Array =[[],
                                  [, ],
                                  [, ],
                                  [, ],
                                  [, , ],
                                  [, , ],
                                  [, ]];
      private const SPEED:Array = ;

      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 = keytable = function():void {px -= space(px-1, py, piece)};
            keytable = keytable = function():void {rotate(true)};
            keytable = function():void {rotate(false)};
            keytable = keytable = function():void {px += space(px+1, py, piece)};
            keytable = keytable = function():void {drop(); pick();};
            stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
                if (keytable) {
                  keytable();
                  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;
                  if (space(px, py+1, piece)) {
                        ++py;
                        repaint();
                  } else {
                        drop();
                        pick();
                  }
                }
            });
      }

      private function rotate(clock:Boolean):void {
            var r:Array = new Array(piece.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 = piece;
                  else
                        r = piece;
            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.length))
                        g = piece;
                  if (g == 0)
                        g = field;
                  if(g == 0) continue;
                  var body:Body = new Body(COLOR);
                  addChild(body);
                  body.x = (i+.5)*UNIT;
                  body.y = (j-.5)*UNIT;
                }
            for (j = 0; j < next.length; ++j)
                for (i = 0; i < next.length; ++i) {
                  if(next == 0) continue;
                  var body:Body = new Body(COLOR]);
                  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.length; ++i) {
                  if (0 > (x+i) || (x+i) >= W)
                        return 0;
                  if (p && field)
                        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.length; ++i)
                  if (piece)
                        field = piece;
            for (j=0; j<H; ++j)
                if (field.indexOf(0) < 0) {
                  field.splice(j, 1);
                  field.unshift([]);
                  for (i=0; i<W; ++i)
                        field = 0;
                }

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

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

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();   
    }   
}

franniss 发表于 2009-12-4 19:31:24

吖 还是日语注释

貘良了 发表于 2009-12-4 20:44:11

我顺便解释下日语注释的内容,
// アンテナを描く
// 内側四角を描く   
// 左目を描く
// 右目を描く
// 口を描く   

没有任何图片素材,就靠这几个东西在画俄罗斯的方块。
简而言之,class Body 是一个画精灵的类。
页: [1]
查看完整版本: 俄罗斯方块(AS3)