追风想追风的追风

2009年08月14号

flash as3 游戏 贪食蛇 源码 教程 actionscript3

类归于: Flash as3游戏 — 标签: — admin @ 11:09 上午

刚开始的时候标题是用textField弄的,如果鼠标点到那里,焦点会指向那个,游戏中可能会控制不能,只要重新点一下即可

主要核心源码

CODE:
  1. package
  2. {
  3.     import adobe.utils.CustomActions;
  4.     import flash.display.Sprite;
  5.     import flash.display.Stage;
  6.     import flash.events.Event;
  7.     import flash.events.KeyboardEvent;
  8.     import flash.text.FontStyle;
  9.     import flash.text.TextField;
  10.     import flash.text.TextFormat;
  11.     import flash.utils.Timer;
  12.     import flash.events.TimerEvent;
  13.     import flash.display.DisplayObject;
  14.    
  15.    
  16.     /**
  17.      * ...
  18.      * @author
  19.      */
  20.     public class Main extends Sprite {
  21.         public static var snakeType:String ;//运动方向
  22.         public var snakeLong:int ;//长度
  23.         public var snakeBody:Array ;//身体数组
  24.         public var outBodyArr:Array ;//还没吃到的身体
  25.         public var timer:Timer;//世界间隔
  26.         public var score:int ;//分数
  27.        
  28.         public var scoreTxt:TextField = new TextField();
  29.  
  30.         public static var p1:Head;
  31.        
  32.         public function Main():void {
  33.             //监听添加舞台
  34.             if (stage) startgame();
  35.             else addEventListener(Event.ADDED_TO_STAGE, startgame);
  36.            
  37.            
  38.         }
  39.        
  40.         //开始游戏
  41.         public function startgame(e:Event = null):void {
  42.             var menu:Menu = new Menu();
  43.             menu.x = 250; menu.y = 200;
  44.             stage.scaleMode = "showAll";
  45.             stage.addChild(menu);
  46.             stage.addEventListener(KeyboardEvent.KEY_UP,init);
  47.         }
  48.        
  49.         //初始化
  50.         private function init(e:KeyboardEvent = null):void {
  51.             if (e.keyCode!=13) {
  52.                 return;
  53.             }
  54.            
  55.             stage.removeEventListener(KeyboardEvent.KEY_UP, init);
  56.             stage.removeChildAt(1);
  57.             removeEventListener(Event.ADDED_TO_STAGE, init);
  58.            
  59.             //初始化数值
  60.             snakeBody = new Array();
  61.             outBodyArr = new Array();
  62.             snakeLong = 4;
  63.             score = 0;
  64.             snakeType = "right";
  65.            
  66.             //创建头部
  67.             p1 = new Head;
  68.             p1.x = 100;
  69.             p1.y = 300;
  70.             snakeBody.push(p1);//把头部添加到身体数组
  71.            
  72.             //继续添加身体
  73.             for (var i:int = 1; i <snakeLong ; i++) {
  74.                 var b:Body = new Body();
  75.                 b.x = p1.x - 20*i-1;
  76.                 b.y = p1.y;
  77.                 snakeBody.push(b);
  78.                 stage.addChild(b);
  79.             }
  80.             stage.addChild(p1);//添加到舞台
  81.  
  82.             //世界时间开始了
  83.             timer = new Timer(100);
  84.             timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
  85.             timer.start();
  86.            
  87.             randMakeBody();//生成一个还没吃到的身体
  88.            
  89.             //添加键盘监听
  90.             stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeydownHandler);
  91.            
  92.               //积分text
  93.               scoreTxt.text = "Score:" + score;
  94.               stage.addChild(scoreTxt);
  95.         }
  96.        
  97.         //世界时间句柄
  98.         public function onTimerHandler(event:Event):void {
  99.            
  100.             //超出屏幕就gameover
  101.             if (p1.x<0 || p1.x>780 || p1.y>580 || p1.y<0) {
  102.                   gameOver();
  103.             }
  104.            
  105.            
  106.             /*
  107.              * ****游戏核心:***
  108.              * 遍历body
  109.              * 每次后面一个body的位置都reset成前一个body的位置
  110.              */
  111.             for (var i:int =snakeBody.length-1; i>0  ; i--) {
  112.                      snakeBody[i].x =snakeBody[i-1].x;
  113.                      snakeBody[i].y = snakeBody[i - 1].y;
  114.                     
  115.                      //头碰到身体的话就over
  116.                      if (snakeBody[i+1]) {
  117.                          if (p1.hitTestObject(snakeBody[i+1])) {
  118.                               gameOver();
  119.                          }
  120.                      }
  121.  
  122.             }   
  123.            
  124.             //根据snakeType移动snake
  125.             switch(snakeType) {
  126.                 case "up":
  127.                   p1.up();
  128.                 break;
  129.                 case "down":
  130.                   p1.down();
  131.                 break;
  132.                 case "left":
  133.                   p1.left();
  134.                 break;
  135.                 case "right":
  136.                   p1.right();
  137.                 break;
  138.             }
  139.            
  140.            
  141.    
  142.  
  143.             //吃外面的身体,遍历outBodyArr,然后碰撞检测,可能这方法不好,但是这小游戏可以吧?
  144.             for ( i = 0; i <outBodyArr.length;i++ ) {
  145.                 if (p1.hitTestObject(outBodyArr[i])) {
  146.                     stage.removeChild(outBodyArr[i]);
  147.                     outBodyArr.splice(i, 1);
  148.                    
  149.                     //吃完一个再生产一个
  150.                     randMakeBody();
  151.                     var b:Body = new Body();
  152.                     b.x = -30; b.y = -30;//先隐藏到舞台外
  153.                     stage.addChild(b);
  154.                     snakeBody.push(b);
  155.                    
  156.                     if (timer.delay-5>0) {
  157.                         timer.delay -= 5;//加快速度
  158.                     }
  159.                    
  160.                    
  161.                     //积分
  162.                     score++;
  163.                     scoreTxt.text = "score:"+score;
  164.                 }
  165.             }
  166.            
  167.            
  168.  
  169.            
  170.         }
  171.        
  172.        
  173.         //键盘响应,更改移动状态
  174.         public function onKeydownHandler(event:KeyboardEvent):void {
  175.             switch(event.keyCode) {
  176.                 case 37:
  177.                  if (snakeType != "right") {
  178.                    snakeType = "left";
  179.                  }
  180.                 break;
  181.                 case 39:
  182.                  if (snakeType != "left") {         
  183.                    snakeType = "right";
  184.                  }
  185.                 break;
  186.                 case 38:
  187.                  if (snakeType != "down") {
  188.                    snakeType = "up";
  189.                  }
  190.                 break;
  191.                 case 40:
  192.                  if (snakeType != "up") {
  193.                     snakeType = "down";
  194.                  }
  195.                 break;
  196.             }
  197.         }
  198.        
  199.        
  200.         //在舞台随机生成可以吃的身体
  201.         public function randMakeBody():void {
  202.             var x:int = Math.random()*780 +1;
  203.             var y:int = Math.random()*580 +1;
  204.             var randBody:Body = new Body();
  205.             randBody.x = x;
  206.             randBody.y = y;
  207.             stage.addChild(randBody);
  208.             outBodyArr.push(randBody);
  209.         }
  210.        
  211.        
  212.         //gamemover
  213.         public function gameOver():void {
  214.               
  215.             //清屏
  216.               while (stage.numChildren> 1) {
  217.                   stage.removeChildAt(1);
  218.       
  219.               }
  220.               
  221.               //取消世界时间
  222.               timer.removeEventListener(TimerEvent.TIMER, onTimerHandler);
  223.               
  224.  
  225.               //显示"gameover"
  226.               var txt:TextField = new TextField();
  227.               var textformat:TextFormat = new TextFormat;
  228.               textformat.size =30;
  229.               txt.text = "Game Over! nSCORE:"+score+"nnPass Enter to restart";
  230.               txt.setTextFormat(textformat);
  231.               txt.width = 300; txt.height = 170;
  232.               txt.x = stage.stageWidth/2-txt.width/3 ; txt.y = stage.stageHeight/2-txt.height/2;
  233.               stage.addChild(txt);
  234.              
  235.              //键盘监听,以致restart
  236.              stage.addEventListener(KeyboardEvent.KEY_UP,init);
  237.         }
  238.        
  239.     }
  240.    
  241. }

4 条评论 »

  1. 不错不错,感觉LZ是搞游戏的……

    评论 由 sjpsega — 2009年09月13号 @ 10:45 上午

  2. And, yeah, screw trying to get justice through the courts. ,

    评论 由 GanjaBoy89 — 2009年10月11号 @ 7:22 上午

  3. 有QQ么,留个联系方式交流一下

    评论 由 perl — 2009年10月12号 @ 4:52 下午

  4. qq:285837593

    评论 由 admin — 2009年10月12号 @ 5:03 下午

这篇文章上的评论 RSS feed TrackBack URL

留下评论

Copyright @ 2007-2008 Programmed By Cjv Skin by cjv 闽ICP备06021916号 WordPress 所驱动