社区应用 最新帖子 精华区 社区服务 会员列表 统计排行 道具中心
主题 : 俄罗斯方块(AS3)
貘良了 离线
级别: 分版主
显示用户信息 
0  发表于: 2009-12-04   

俄罗斯方块(AS3)

新建一个Flash,文档类绑定为Tetris.代码:
复制代码
  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.text.TextField;
  4.     import flash.events.Event;
  5.     import flash.events.KeyboardEvent;
  6.     [SWF(width="550",height="500",backgroundColor="0xFFFFFF",frameRate="30")]
  7.     public class Tetris extends Sprite {
  8.         private const W:int = 10;
  9.         private const H:int = 20;
  10.         private const UNIT:int = 22;
  11.         private const COLOR:Array =[0x000000, 0x00FFFF, 0xFFFF00, 0x22FF22, 0xFF2222, 0x4444FF, 0xFF8844, 0xFF22FF];
  12.         private const PAT:Array =[[[1, 1, 1, 1]],
  13.                                   [[0, 2, 0], [2, 2, 2]],
  14.                                   [[3, 3, 0], [0, 3, 3]],
  15.                                   [[0, 4, 4], [4, 4, 0]],
  16.                                   [[5, 5], [5, 0], [5, 0]],
  17.                                   [[6, 6], [0, 6], [0, 6]],
  18.                                   [[7, 7], [7, 7]]];
  19.         private const SPEED:Array = [30, 20, 10, 5];
  20.         private const VK_H:int = 72; // h
  21.         private const VK_J:int = 74; // j
  22.         private const VK_K:int = 75; // k
  23.         private const VK_L:int = 76; // l
  24.         private const VK_LEFT:int = 37; // left
  25.         private const VK_RIGHT:int = 39; // right
  26.         private const VK_UP:int = 38; // up
  27.         private const VK_DOWN:int = 40; // down
  28.         private const VK_SPC:int = 32; // space
  29.         private var field:Array = [];
  30.         private var piece:Array;
  31.         private var next:Array;
  32.         private var text:TextField = new TextField();
  33.         private var keytable:Array = [];
  34.         private var count:int = 0;
  35.         private var step:int = 0;
  36.         private var px:int;
  37.         private var py:int;
  38.         public static var param:int = 0;
  39.         public function Tetris() {
  40.             text.x = W*UNIT;
  41.             text.text="Next:";
  42.             addChild(text);
  43.             var t:TextField = new TextField();
  44.             t.x = W * UNIT;
  45.             t.y = 8 * UNIT;
  46.             t.text = "usage:\n   h(or left),j(or up),k,\n   l(or right),SPACE(or down)";
  47.             t.autoSize = "left";
  48.             addChild(t);
  49.             field = new Array(H).map(function():*{
  50.                 return new Array(W).map(function():*{
  51.                     return 0;
  52.                 })
  53.             });
  54.             keytable[VK_H] = keytable[VK_LEFT] = function():void {px -= space(px-1, py, piece)};
  55.             keytable[VK_J] = keytable[VK_UP] = function():void {rotate(true)};
  56.             keytable[VK_K] = function():void {rotate(false)};
  57.             keytable[VK_L] = keytable[VK_RIGHT] = function():void {px += space(px+1, py, piece)};
  58.             keytable[VK_SPC] = keytable[VK_DOWN] = function():void {drop(); pick();};
  59.             stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
  60.                 if (keytable[e.keyCode]) {
  61.                     keytable[e.keyCode]();
  62.                     repaint();
  63.                 }
  64.             });
  65.             pick();
  66.             pick();
  67.             addEventListener(Event.ENTER_FRAME, function(e:Event):void {
  68.                 param = (param + 9) % 360;
  69.                 for (var j:int = 0; j < numChildren; j++) {
  70.                     if(getChildAt(j) is Body) Body(getChildAt(j)).update();
  71.                 }
  72.                 if (--step < 0) {
  73.                     step=SPEED[int(count/10)];
  74.                     if (space(px, py+1, piece)) {
  75.                         ++py;
  76.                         repaint();
  77.                     } else {
  78.                         drop();
  79.                         pick();
  80.                     }
  81.                 }
  82.             });
  83.         }
  84.         private function rotate(clock:Boolean):void {
  85.             var r:Array = new Array(piece[0].length).map(function():*{return [];});
  86.             for (var j:int = 0; j<piece.length; ++j)
  87.                 for (var i:int = 0; i < r.length; ++i)
  88.                     if (clock)
  89.                         r[i][piece.length-1-j] = piece[j][i];
  90.                     else
  91.                         r[r.length-1-i][j] = piece[j][i];
  92.             if (space(px, py, r))
  93.                 piece = r;
  94.         }
  95.         private function repaint():void {
  96.             for (var j:int = numChildren - 1; j >= 0; j--) {
  97.                 if (getChildAt(j) is Body) removeChildAt(j);
  98.             }
  99.             graphics.clear();
  100.             graphics.lineStyle(0);
  101.             graphics.drawRect(0, 0, W*UNIT, H*UNIT);
  102.             graphics.endFill();
  103.             for (j = 0; j < H; ++j)
  104.                 for (var i:int = 0; i < W; ++i) {
  105.                     var g:int = 0;
  106.                     if (py <= j && j < (py+piece.length) && px <= i && i < (px+piece[0].length))
  107.                         g = piece[j-py][i-px];
  108.                     if (g == 0)
  109.                         g = field[j][i];
  110.                     if(g == 0) continue;
  111.                     var body:Body = new Body(COLOR[g]);
  112.                     addChild(body);
  113.                     body.x = (i+.5)*UNIT;
  114.                     body.y = (j-.5)*UNIT;
  115.                 }
  116.             for (j = 0; j < next.length; ++j)
  117.                 for (i = 0; i < next[j].length; ++i) {
  118.                     if(next[j][i] == 0) continue;
  119.                     var body:Body = new Body(COLOR[next[j][i]]);
  120.                     addChild(body);
  121.                     body.x = (i+W+1)*UNIT;
  122.                     body.y = (j+2)*UNIT;
  123.                 }
  124.         }
  125.         private function space(x:int, y:int, p:Array):int {
  126.             for (var j:int = 0; j < p.length; ++j) {
  127.                 if (0 > (y+j) || (y+j) >= H)
  128.                     return 0;
  129.                 for (var i:int = 0; i < p[j].length; ++i) {
  130.                     if (0 > (x+i) || (x+i) >= W)
  131.                         return 0;
  132.                     if (p[j][i] && field[y+j][x+i])
  133.                         return 0;
  134.                 }
  135.             }
  136.             return 1;
  137.         }
  138.         private function drop():void {
  139.             for (; space(px, py+1, piece); py++)
  140.                 ;
  141.             for (var j:int = 0; j < piece.length; ++j)
  142.                 for (var i:int = 0; i < piece[j].length; ++i)
  143.                     if (piece[j][i])
  144.                         field[py+j][px+i] = piece[j][i];
  145.             for (j=0; j<H; ++j)
  146.                 if (field[j].indexOf(0) < 0) {
  147.                     field.splice(j, 1);
  148.                     field.unshift([]);
  149.                     for (i=0; i<W; ++i)
  150.                         field[0][i] = 0;
  151.                 }
  152.             count++;
  153.             if (count/10 >= SPEED.length)
  154.                 count = 0;
  155.         }
  156.         private function pick():void {
  157.             piece = next;
  158.             if (piece != null) {
  159.                 px = (W-piece[0].length)/2;
  160.                 py = 0;
  161.                 if (!space(px, py, piece))
  162.                     text.text="GAME OVER";
  163.             }
  164.             next = PAT[int(Math.random()*PAT.length)];
  165.         }
  166.     }
  167. }
  168. import flash.display.Sprite;  
  169. import flash.filters.GlowFilter;  
  170.   
  171. class Body extends Sprite{  
  172.     private var eye1:Sprite;
  173.     private var eye2:Sprite;
  174.     private var antena:Sprite;
  175.     public function Body(color:uint){    
  176.         scaleX = scaleY = .19;
  177.         var l1:Leg = new Leg(color);  
  178.         l1.x = -30; l1.y = 150  
  179.         addChild(l1);  
  180.   
  181.         var l2:Leg = new Leg(color);  
  182.         l2.x = 30; l2.y = 150  
  183.         addChild(l2);  
  184.   
  185.         // アンテナを描く  
  186.         antena = new Sprite();  
  187.         antena.graphics.lineStyle(4, 0x000000);  
  188.         antena.graphics.moveTo(-30, 40);  
  189.         antena.graphics.lineTo(  0, 60);  
  190.         antena.graphics.lineTo( 30, 40);  
  191.         addChild(antena);  
  192.     
  193.         // 外側四角を描く  
  194.         var s2:Sprite = new Sprite();  
  195.         s2.graphics.lineStyle(6, 0x000000);  
  196.         s2.graphics.beginFill(color);  
  197.         s2.graphics.drawRect(-60, 0, 120, 90);  
  198.         s2.graphics.endFill();  
  199.         s2.y = 60;  
  200.         addChild(s2);  
  201.     
  202.         // 内側四角を描く  
  203.         var s3:Sprite = new Sprite();  
  204.         s3.graphics.lineStyle(4, 0x000000);  
  205.         s3.graphics.beginFill(0xffffff, 0.8);  
  206.         s3.graphics.drawRect(-50, 0, 100, 70);  
  207.         s3.graphics.endFill();  
  208.         s3.y = 70;  
  209.         addChild(s3);  
  210.     
  211.         // 左目を描く  
  212.         eye1 = new Sprite();  
  213.         eye1.graphics.beginFill(0x000000);  
  214.         eye1.graphics.drawCircle(0, 0, 5);  
  215.         eye1.x = -30;  
  216.         eye1.y = 100;  
  217.         addChild(eye1);  
  218.     
  219.         // 右目を描く  
  220.         eye2 = new Sprite();  
  221.         eye2.graphics.beginFill(0x000000);  
  222.         eye2.graphics.drawCircle(0, 0, 5);  
  223.         eye2.x = 30;  
  224.         eye2.y = 90;  
  225.         addChild(eye2);  
  226.     
  227.         // 口を描く  
  228.         var s8:Sprite = new Sprite();  
  229.         s8.graphics.lineStyle(1, 0x000000);  
  230.         s8.graphics.beginFill(0x000000);  
  231.         s8.graphics.moveTo(  0, 120);  
  232.         s8.graphics.lineTo(-10, 130);  
  233.         s8.graphics.lineTo( 15, 130);  
  234.         s8.graphics.endFill();  
  235.         addChild(s8);
  236.         update();
  237.     }
  238.     public function update():void{
  239.         var s:Number = Math.cos(Tetris.param / 180 * Math.PI);
  240.         eye1.y = 95 + 5 * s;
  241.         eye2.y = 95 - 5 * s;
  242.         antena.scaleX = s;
  243.     }
  244. }  
  245.   
  246. class Leg extends Sprite{  
  247.     public function Leg(color){  
  248.         graphics.lineStyle(4, 0x000000);  
  249.         graphics.beginFill(color);  
  250.         graphics.moveTo( 20, -12);  
  251.         graphics.lineTo(  0,  12);  
  252.         graphics.lineTo(-20, -12);  
  253.         graphics.endFill();  
  254.     }  
  255. }  
评价一下你浏览此帖子的感受

精彩

感动

搞笑

开心

愤怒

无聊

灌水
franniss 离线
级别: 分版主
显示用户信息 
1  发表于: 2009-12-04   
吖 还是日语注释
貘良了 离线
级别: 分版主
显示用户信息 
2  发表于: 2009-12-04   
我顺便解释下日语注释的内容,
// アンテナを描く  
// 内側四角を描く  
// 左目を描く
// 右目を描く  
// 口を描く  

没有任何图片素材,就靠这几个东西在画俄罗斯的方块。
简而言之,class Body 是一个画精灵的类。
描述
快速回复

认证码:

验证问题:
12-5=?,答案:7 正确答案:7
按"Ctrl+Enter"直接提交