FLEX ActionScript 游戏开发教程之[贪吃蛇]Step By Step



 我也是刚刚接触到Flex的游戏开发, 继j2me的手机游戏开发后,再现学现卖,发一个Flash版的贪吃蛇游戏开发的Step by Step教程,希望对新手有些帮助. :).

 

Step 1.


下载J2SE和Flex 3.1 SDK(http://download.macromedia.com/pub/flex/sdk/flex_sdk_3.zip,免费的,哈哈),安装好Java,解压 Flex SDK到路径G:/javaspace/flex_sdk_3


Step 2.


到我的电脑->属性->高级->环境变量,设置PATH路径加上: C:/j2sdk1.4.2_08/bin;G:/javaspace/flex_sdk_3/bin


Step 3.


创建G:/javaspace/workspace/SnakeGameFlash和G:/javaspace/workspace/SnakeGameFlash/asx两个目录.


Step 4.


创建G:/javaspace/workspace/SnakeGameFlash/SnakeGame.mxml文件,内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application  name="SnakeGame"
  3.  xmlns:mx="http://www.adobe.com/2006/mxml" 
  4.  xmlns:custom="asx.*"
  5.  width="600" height="400"
  6.  frameRate="8" 
  7.  creationComplete="creationCompleteHandler(event);"
  8.   
  9. >
  10.   <mx:Script>
  11.         <![CDATA[
  12.             import flash.events.MouseEvent;
  13.             import mx.events.FlexEvent;
  14.             import asx.SnakeMc;
  15.    
  16.             private function creationCompleteHandler(event:FlexEvent):void
  17.             {
  18.         var mc:SnakeMc  = new SnakeMc();
  19.         swfldr.addChild(mc);       
  20.             }
  21.     
  22.         ]]>
  23.     </mx:Script>
  24.   <mx:SWFLoader id="swfldr" width="512" height="280"/>
  25. </mx:Application>

 

Step 5.


创建G:/javaspace/workspace/SnakeGameFlash/asx/SnakeBlock.as文件,内容如下:

  1. package asx
  2. {
  3.  import flash.display.Sprite;
  4.  public class SnakeBlock extends Sprite
  5.  {
  6.   public var r:uint=2;
  7.   public var xx:uint=0;
  8.   public var yy:uint=0;
  9.   public function SnakeBlock()
  10.   {
  11.    super();
  12.   }
  13.   
  14.  }
  15. }

Step 6.
创建G:/javaspace/workspace/SnakeGameFlash/asx/SnakeMc.as文件,内容如下:

 

  1. package asx
  2. {
  3.  import flash.display.MovieClip;
  4.  import flash.events.Event;
  5.  import flash.events.KeyboardEvent;
  6.  import flash.text.TextField;
  7.  import flash.ui.Keyboard;
  8.  public class SnakeMc extends MovieClip
  9.  {
  10.    
  11.   public const blocksize:uint = 8;
  12.   public const blWidth:uint   = 64
  13.   public const blHeight:uint  = 32;
  14.   public const numFood:uint = 20;
  15.   public var t:TextField = null;
  16.   public var t2:TextField = null;
  17.   public var ss:SnakeBlock    = null;
  18.   public var sa:Array     = new Array();
  19.   public var kbq:Array    = new Array();
  20.   public var rr:Array    = new Array();
  21.   public var food:Array  = new Array();
  22.   public var scores:uint  = 0;
  23.   public var bonus:uint   = 0;
  24.   public var tickcount:Number =0;
  25.   
  26.   
  27.   public function CreateSprite(w:uint=0):SnakeBlock
  28.   {
  29.    var s:SnakeBlock = new SnakeBlock();
  30.    this.addChild(s);
  31.    with(s)
  32.    {
  33.     if (w==0// blue snake color the other times 
  34.      graphics.beginFill(0x555588);
  35.          else
  36.      graphics.beginFill(255<<16); // red food color first time 
  37.                    
  38.     graphics.lineTo((blocksize-1),0 ); 
  39.     graphics.lineTo((blocksize-1), (blocksize-1)); 
  40.     graphics.lineTo(0, (blocksize-1)); 
  41.     graphics.endFill(); // draw a square 
  42.    }
  43.    return s;
  44.   }
  45.   
  46.   public function AppendSprite(w:uint=0):SnakeBlock
  47.   {
  48.    var s:SnakeBlock = CreateSprite(w);
  49.    sa.push(s);
  50.    if(sa.length>1)
  51.    {
  52.     s.x = sa[sa.length-2].x;
  53.     s.y = sa[sa.length-2].y;
  54.     s.xx = sa[sa.length-2].xx;
  55.     s.yy = sa[sa.length-2].yy;
  56.     s.r = (sa[sa.length-2].r+2)%4;
  57.     UpdateSnakePos(s);
  58.     s.r = sa[sa.length-2].r;
  59.    }
  60.    else
  61.    {
  62.     s.xx = s.x/blocksize;
  63.     s.yy = s.y/blocksize
  64.    }
  65.    return s;
  66.   }
  67.   
  68.   public function UpdateSnakePos(s:SnakeBlock):void
  69.   {
  70.    with(s)
  71.    {
  72.     switch(r)
  73.           {
  74.           case 0:
  75.            x -=blocksize;
  76.            xx--;
  77.            break;
  78.           case 1:
  79.            y -=blocksize;
  80.            yy--;
  81.            break;
  82.           case 2:
  83.            x +=blocksize;
  84.            xx++;
  85.            break;
  86.           case 3:
  87.            y +=blocksize;
  88.            yy++;
  89.            break;
  90.           }
  91.          
  92.    }
  93.   }
  94.   
  95.   public function upateSnakesPos():void
  96.   {
  97.    var i:int =0;
  98.    for(i=0;i<sa.length;i++)
  99.    {
  100.     var s:SnakeBlock =sa[i];
  101.     for(var j:int=0;j<rr.length;j++)
  102.     {
  103.      if(i==(rr[j]>>16))
  104.      {
  105.       s.r = (rr[j]0xffff)%4;
  106.      }
  107.     
  108.     }
  109.     UpdateSnakePos(s);
  110.    }
  111.    
  112.    var tt:Array = new Array();
  113.    for(i=0;i<rr.length;i++)
  114.    {
  115.     var tmp:uint = (rr[i]>>16);
  116.     if(tmp <sa.length+10)
  117.     {
  118.      rr[i] =((tmp+1)<<16)|(rr[i]0xffff);
  119.      tt.push(rr[i]);
  120.     }
  121.    }
  122.    rr = null;
  123.    rr = tt;
  124.   }
  125.   
  126.   
  127.   
  128.   public function onkeydown(evt:KeyboardEvent):void 
  129.   {
  130.    
  131.    if(evt.keyCode==Keyboard.SPACE)
  132.    {
  133.     if(ss==null)
  134.     {
  135.      cleanup();
  136.      ss=AppendSprite();
  137.      for(var i:uint;i<0;i++)
  138.      {
  139.       AppendSprite();
  140.      }
  141.      genFood();   
  142.     }
  143.     
  144.    }
  145.    else if(ss!=null)
  146.    {
  147.     
  148.     switch(evt.keyCode)
  149.     {
  150.      case Keyboard.LEFT:
  151.      case 65:
  152.       kbq.unshift(0);
  153.       break;
  154.      case Keyboard.UP:
  155.      case 87:
  156.       kbq.unshift(1);
  157.       break;
  158.      case Keyboard.RIGHT:
  159.      case 68:
  160.       kbq.unshift(2);
  161.       break;
  162.       
  163.      case Keyboard.DOWN:
  164.      case 83:
  165.       kbq.unshift(3);
  166.       break;
  167.      default:
  168.       break;  
  169.       
  170.     };
  171.     
  172.    }
  173.    
  174.   }
  175.   
  176.   public function IntersectSnake(xx:int, yy:int):Boolean
  177.   {
  178.    var i:uint=0;
  179.    for(i=0;i<sa.length;i++)
  180.    {
  181.     if(xx==sa[i].xx && yy==sa[i].yy)
  182.     {
  183.      return true;
  184.     }
  185.    }
  186.    return false;
  187.   }
  188.   
  189.   public function IsGameOver():Boolean
  190.   {
  191.   
  192.    
  193.    if(ss.xx<0 || ss.xx >blWidth-1 || ss.yy<0 || ss.yy >blHeight-1)
  194.    {
  195.     return true;
  196.    }
  197.    var i:uint =0;
  198.    for(i=1;i<sa.length;i++)
  199.    {
  200.     if(ss.xx==sa[i].xx && ss.yy==sa[i].yy)
  201.     {
  202.      return true;
  203.     }
  204.    }
  205.   
  206.    
  207.    return false;
  208.   }
  209.   
  210.   public function getFood(xx:int,yy:int):SnakeBlock
  211.   {
  212.    for(var i:int=0;i<food.length;i++)
  213.    {
  214.     if(food[i].xx ==xx && food[i].yy ==yy)
  215.     {
  216.      return food[i]; 
  217.     }
  218.    }
  219.    return null;
  220.   }
  221.   
  222.   public function genFood():void
  223.   {
  224.    if(food.length!=0)
  225.      return;
  226.    for(;food.length <numFood;)
  227.    {
  228.     
  229.     var xx:int= Math.random()*(blWidth-1);
  230.     var yy:int= Math.random()*(blHeight-1);
  231.     
  232.     if(getFood(xx,yy) ==null &&!IntersectSnake(xx,yy)  )
  233.     {
  234.      var s:SnakeBlock =  CreateSprite(1);
  235.      s.x = xx*blocksize;
  236.      s.y = yy*blocksize;
  237.      s.xx = xx;
  238.      s.yy = yy;
  239.      food.push(s);
  240.     }  
  241.    }
  242.    
  243.   }
  244.   
  245.   
  246.   public function eatFood():void
  247.   {
  248.    if(ss!=null)
  249.    {
  250.    
  251.     for(var i:int=0;i<food.length;i++)
  252.     {
  253.      if(food[i].xx ==ss.xx && food[i].yy ==ss.yy)
  254.      {
  255.       AppendSprite();
  256.       if(sa.length>2)
  257.       {
  258.        bonus +=4*(sa.length-2);
  259.        if(tickcount>0)
  260.        {
  261.         bonus +=sa.length/tickcount;
  262.        }
  263.       }
  264.       
  265.       scores += 10+bonus;
  266.       t.text = "Score: "+scores;
  267.       
  268.       this.removeChild(food[i]);
  269.       food.splice(i,1);
  270.       return;
  271.      }
  272.     }
  273.     
  274.    }
  275.   }
  276.   
  277.   public function cleanup():void
  278.   {
  279.    while(sa.length>0)
  280.    {
  281.     this.removeChild(sa.pop()); 
  282.    }
  283.   
  284.    while(food.length>0)
  285.    {
  286.     this.removeChild(food.pop());
  287.    }
  288.    
  289.    rr.length=0;
  290.    kbq.length=0;
  291.       
  292.    ss   = null;
  293.    scores   = 0;
  294.    bonus    = 0;
  295.    tickcount  = 0;
  296.    
  297.    
  298.   }
  299.   
  300.   public function GameFrame():void
  301.   {
  302.    if(ss!=null)
  303.     {
  304.      tickcount++;
  305.      if(kbq.length>0)
  306.        {
  307.         var c:uint=0;
  308.            c = kbq.pop(); // pick the next turn in the queue (may be undefined if queue is empty)
  309.            // and check that it is not undefined and not a 180 degree turn
  310.            // (annoying to be able to turn into the snake with one key press)
  311.            //
  312.            if (c%2!=ss.r%2
  313.            { 
  314.           rr.push(c); // change current direction to the new value
  315.            }
  316.        } 
  317.        
  318.        upateSnakesPos();
  319.        if(IsGameOver())
  320.        {
  321.         t.text = "Game Over/t-/tPress SPACE";
  322.       ss = null;
  323.       t2.text = food.length.toString();
  324.         return;
  325.        }
  326.        eatFood();
  327.        genFood();
  328.        t2.text = food.length.toString();
  329.          
  330.     } 
  331.   }
  332.   
  333.   public function onEnterFrame(evt:Event):void
  334.   {
  335.     GameFrame();
  336.   }
  337.   
  338.   public function onAddtoStage(evt:Event):void
  339.   {
  340.    this.stage.addEventListener(KeyboardEvent.KEY_DOWN,onkeydown);
  341.    this.addEventListener(Event.ENTER_FRAME,onEnterFrame); 
  342.   }
  343.   
  344.   public function SnakeMc()
  345.   {
  346.    
  347.    addEventListener(Event.ADDED_TO_STAGE ,onAddtoStage);
  348.    super();
  349.    
  350.      t    = new TextField();
  351.    t.x      = 1;
  352.    t.y   = 255;
  353.    t.width  = 511;
  354.    t.height = 16;
  355.    t.text = "Snake Game/t-/tPress SPACE";
  356.    addChild(t);
  357.    
  358.    t2   = new TextField();
  359.    t2    = new TextField();
  360.    t2.x      = 1;
  361.    t2.y   = 271;
  362.    t2.width  = 511;
  363.    t2.height = 16;
  364.    t2.text = food.length.toString();
  365.    addChild(t2);
  366.    
  367.    this.graphics.beginFill(0xeeeeee); 
  368.    this.graphics.lineStyle(1); 
  369.    this.graphics.lineTo(5110); 
  370.    this.graphics.lineTo(511256); 
  371.    this.graphics.lineTo(0256); 
  372.   }
  373.   
  374.   
  375.  }
  376. }

 

Step 7.


创建G:/javaspace/workspace/SnakeGameFlash/build.bat文件(用把*.as和SnakeGame.mxml编译和生成Flash),内容如下:

  1. mxmlc --strict=true --file-specs SnakeGame.mxml
  2. pause

 

Step 8.


运行G:/javaspace/workspace/SnakeGameFlash/build.bat,生成SnakeGame.swf

 

Step 9

 

运行测试SnakeGame.swf.

教程结束

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值