天天看点

贪吃蛇V1.0

贪吃蛇游戏开发详解

注意以下问题:

1.蛇移动算法。

蛇所有节点存放到数组。每次移动的时候。把节点的坐标设为其前面节点的坐标。最后在把头的坐标往前移动。坐标一直在往前移。

2.随机生成食物时,在蛇蛇身上时候,要重新生成(递归调用)。

3.把游戏区域分成若干个网格,蛇移动距离为网格单元大小,食物生成在网格中。

点击此试玩游戏(csdn服务器太垃圾。多刷新几次)

点击下载源代码

 代码如小:

一:游戏开始

贪吃蛇V1.0
package com.lux
贪吃蛇V1.0
贪吃蛇V1.0
... {
贪吃蛇V1.0
    import flash.display.*;
贪吃蛇V1.0
    import flash.events.Event;
贪吃蛇V1.0
    import flash.events.MouseEvent;
贪吃蛇V1.0
    import fl.controls.*;    
贪吃蛇V1.0
贪吃蛇V1.0
    public class GameStart extends MovieClip
贪吃蛇V1.0
贪吃蛇V1.0
    ...{ 
贪吃蛇V1.0
        public static var gamestart:MovieClip ;
贪吃蛇V1.0
        private var _score:Number=0; 
贪吃蛇V1.0
贪吃蛇V1.0
        //=====游戏所用时间
贪吃蛇V1.0
        private var time:Number ;
贪吃蛇V1.0
贪吃蛇V1.0
        public function GameStart()
贪吃蛇V1.0
贪吃蛇V1.0
        ...{     
贪吃蛇V1.0
            super();  
贪吃蛇V1.0
            gamestart = this ; 
贪吃蛇V1.0
            play_btn.addEventListener(MouseEvent.CLICK,playGame); 
贪吃蛇V1.0
        }  
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function playGame(e:MouseEvent):void...{ 
贪吃蛇V1.0
           this.gotoAndStop("two_tag");   
贪吃蛇V1.0
        } 
贪吃蛇V1.0
贪吃蛇V1.0
        public function replayGame(e:MouseEvent):void...{
贪吃蛇V1.0
             score_mc.score_txt.text = "0" ; 
贪吃蛇V1.0
             timer_mc.min_mc.text ="00" ;
贪吃蛇V1.0
             timer_mc.sec_mc.text ="00" ;
贪吃蛇V1.0
             this.gotoAndStop("two_tag");    
贪吃蛇V1.0
        }
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function addScore(_score:Number)...{
贪吃蛇V1.0
            this._score+=_score ;  
贪吃蛇V1.0
            score_mc.score_txt.text = this._score.toString();  
贪吃蛇V1.0
        }
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function getScore():Number...{
贪吃蛇V1.0
            return this._score ; 
贪吃蛇V1.0
        }
贪吃蛇V1.0
贪吃蛇V1.0
//        public function timerStop():void{
贪吃蛇V1.0
//            timer_mc.remove(); 
贪吃蛇V1.0
//        }
贪吃蛇V1.0
//        
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
    }
贪吃蛇V1.0

}

二:蛇移动的管理

贪吃蛇V1.0
package  com.lux
贪吃蛇V1.0
贪吃蛇V1.0
... {
贪吃蛇V1.0
    import de.polygonal.ds.NullIterator;
贪吃蛇V1.0
贪吃蛇V1.0
    import flash.display.MovieClip;
贪吃蛇V1.0
    import flash.events.Event;
贪吃蛇V1.0
    import flash.events.KeyboardEvent;
贪吃蛇V1.0
    import flash.events.TimerEvent;
贪吃蛇V1.0
    import flash.filters.BevelFilter;
贪吃蛇V1.0
    import flash.ui.Keyboard;
贪吃蛇V1.0
    import flash.utils.Timer;
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
    public class DaShe extends MovieClip ...{ 
贪吃蛇V1.0
贪吃蛇V1.0
       //蛇节点数组 
贪吃蛇V1.0
        private var a:Array ;
贪吃蛇V1.0
        //方向 
贪吃蛇V1.0
        private var up:uint =    1 ;
贪吃蛇V1.0
        private var down:uint =  2 ;
贪吃蛇V1.0
        private var left:uint =  3 ;
贪吃蛇V1.0
        private var right:uint = 4 ;
贪吃蛇V1.0
贪吃蛇V1.0
        //====保存随机生成的蛇  
贪吃蛇V1.0
        private var _newShe:XiaoShe ;  
贪吃蛇V1.0
贪吃蛇V1.0
        //====方向 
贪吃蛇V1.0
        private var _dir:uint ;  
贪吃蛇V1.0
        private var timer:Timer ;   
贪吃蛇V1.0
贪吃蛇V1.0
        private var _score:Number=0 ;
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function DaShe() ...{       
贪吃蛇V1.0
            super();    
贪吃蛇V1.0
            this._dir = this.right ; 
贪吃蛇V1.0
            a = new Array();
贪吃蛇V1.0
            //蛇初始化为两条小蛇的长度。               
贪吃蛇V1.0
            var head:XiaoShe = new XiaoShe(30,30);          
贪吃蛇V1.0
            a.push(head);       
贪吃蛇V1.0
            this.addChild(head);  
贪吃蛇V1.0
贪吃蛇V1.0
            var head1:XiaoShe = new XiaoShe(head.x-head.width,head.y);
贪吃蛇V1.0
            a.push(head1);      
贪吃蛇V1.0
            this.addChild(head1);       
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        }        
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function start():void...{ 
贪吃蛇V1.0
             timer = new Timer(100);
贪吃蛇V1.0
             timer.addEventListener(TimerEvent.TIMER,running);
贪吃蛇V1.0
             timer.start();  
贪吃蛇V1.0
贪吃蛇V1.0
            //===注册方向控制                  
贪吃蛇V1.0
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN,direction);
贪吃蛇V1.0
贪吃蛇V1.0
            //===随机生成新蛇 
贪吃蛇V1.0
            this.initFood();   
贪吃蛇V1.0
贪吃蛇V1.0
        }                     
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        private function running(e:Event):void...{                                          
贪吃蛇V1.0
贪吃蛇V1.0
            for(var i:uint=a.length-1;i>0;i--)...{ 
贪吃蛇V1.0
                a[i].x =a[i-1].x;    
贪吃蛇V1.0
                a[i].y =a[i-1].y;  
贪吃蛇V1.0
            }    
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
            switch(_dir)...{ 
贪吃蛇V1.0
                case this.up:
贪吃蛇V1.0
                     a[0].y-=this._SPEED;
贪吃蛇V1.0
                     break;
贪吃蛇V1.0
                case this.left:
贪吃蛇V1.0
                     a[0].x-=this._SPEED;
贪吃蛇V1.0
                     break;         
贪吃蛇V1.0
                case this.down:
贪吃蛇V1.0
                     a[0].y+=this._SPEED; 
贪吃蛇V1.0
                     break;       
贪吃蛇V1.0
                case this.right:   
贪吃蛇V1.0
                     a[0].x+=this._SPEED ;                   
贪吃蛇V1.0
            } 
贪吃蛇V1.0
            //==========碰到食物 
贪吃蛇V1.0
贪吃蛇V1.0
            if(isEat())...{ 
贪吃蛇V1.0
                eatFood();      
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
                //增加积分
贪吃蛇V1.0
               // this.addScore();  
贪吃蛇V1.0
               GameStart.gamestart.addScore(1);
贪吃蛇V1.0
            } 
贪吃蛇V1.0
贪吃蛇V1.0
            if(isDead())...{
贪吃蛇V1.0
                this.gameOver() ; 
贪吃蛇V1.0
            }           
贪吃蛇V1.0
贪吃蛇V1.0
        } 
贪吃蛇V1.0
//         
贪吃蛇V1.0
//        private function addScore():void{
贪吃蛇V1.0
//            this._score++;         
贪吃蛇V1.0
//            score_mc.score_txt.text = this._score.toString();
贪吃蛇V1.0
//        }
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        private function isDead():Boolean ...{   
贪吃蛇V1.0
贪吃蛇V1.0
            if(a[0].x<=0||a[0].x>=this._ACTIVEWIDTH||a[0].y<=0||a[0].y>=this._ACTIVEHEIGHT)...{
贪吃蛇V1.0
                return true ;
贪吃蛇V1.0
            }       
贪吃蛇V1.0
贪吃蛇V1.0
            for(var i:uint=1;i<a.length;i++)...{   
贪吃蛇V1.0
                if((a[0].x==a[i].x) &&(a[0].y==a[i].y))
贪吃蛇V1.0
                    return true ;
贪吃蛇V1.0
            } 
贪吃蛇V1.0
            return false ;  
贪吃蛇V1.0
        }
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function gameOver():void...{
贪吃蛇V1.0
            trace("游戏结束");            
贪吃蛇V1.0
            timer.stop();        
贪吃蛇V1.0
            timer.removeEventListener(TimerEvent.TIMER,running);
贪吃蛇V1.0
            this.stage.removeEventListener(KeyboardEvent.KEY_DOWN,direction);   
贪吃蛇V1.0
            this.parent.removeChild(this); 
贪吃蛇V1.0
            GameStart.gamestart.gotoAndStop("gameover_tag");  
贪吃蛇V1.0
            // GameStart.gamestart.timerStop();        
贪吃蛇V1.0
        } 
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function eatFood():void...{ 
贪吃蛇V1.0
            var _newshe:XiaoShe = new XiaoShe(a[a.length-1].x,a[a.length-1].y);
贪吃蛇V1.0
            a.push(_newshe);   
贪吃蛇V1.0
            this.randomLocation();
贪吃蛇V1.0
            this.addChild(_newshe);   
贪吃蛇V1.0
        } 
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function isEat():Boolean...{
贪吃蛇V1.0
贪吃蛇V1.0
            if((a[0].x==this._newShe.x)&&(a[0].y==this._newShe.y))...{ 
贪吃蛇V1.0
                return true ;
贪吃蛇V1.0
            } 
贪吃蛇V1.0
            return false ;
贪吃蛇V1.0
        }
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        //==============方向控制 
贪吃蛇V1.0
贪吃蛇V1.0
        private function direction(e:KeyboardEvent):void...{ 
贪吃蛇V1.0
            trace("有按键事件");
贪吃蛇V1.0
贪吃蛇V1.0
            switch(e.keyCode)...{
贪吃蛇V1.0
                case Keyboard.DOWN:
贪吃蛇V1.0
                     if(_dir!=this.up)
贪吃蛇V1.0
                        _dir = this.down ;    
贪吃蛇V1.0
                     break ;
贪吃蛇V1.0
                case Keyboard.RIGHT:  
贪吃蛇V1.0
                     if(_dir!=this.left)
贪吃蛇V1.0
                        _dir = this.right ;
贪吃蛇V1.0
                     break;
贪吃蛇V1.0
                case Keyboard.UP:
贪吃蛇V1.0
                     if(_dir!=this.down)
贪吃蛇V1.0
                        _dir =this.up ;
贪吃蛇V1.0
                     break;  
贪吃蛇V1.0
                case Keyboard.LEFT:
贪吃蛇V1.0
                     if(_dir!=this.right)
贪吃蛇V1.0
                        _dir =this.left ;                
贪吃蛇V1.0
            }         
贪吃蛇V1.0
贪吃蛇V1.0
        } 
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        private function initFood():void...{
贪吃蛇V1.0
            _newShe = new XiaoShe(0,0); 
贪吃蛇V1.0
贪吃蛇V1.0
            //===随机生成位置      
贪吃蛇V1.0
            this.randomLocation();          
贪吃蛇V1.0
            this.addChild(_newShe);    
贪吃蛇V1.0
        }         
贪吃蛇V1.0
贪吃蛇V1.0
        //==============重新随机移动蛇的位置。      
贪吃蛇V1.0
贪吃蛇V1.0
        private function randomLocation():void...{                          
贪吃蛇V1.0
            var _x:Number = Math.floor(Math.random()*30)*10 ;
贪吃蛇V1.0
            var _y:Number = Math.floor(Math.random()*40)*10 ;   
贪吃蛇V1.0
贪吃蛇V1.0
            trace("x:"+_x+","+"y:"+_y);
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
            if(check(_x,_y))...{ 
贪吃蛇V1.0
                trace("生成的小蛇和蛇身重合");
贪吃蛇V1.0
                this.randomLocation();
贪吃蛇V1.0
贪吃蛇V1.0
            }else...{
贪吃蛇V1.0
                this._newShe.x=_x ;
贪吃蛇V1.0
                this._newShe.y=_y ;
贪吃蛇V1.0
            }        
贪吃蛇V1.0
贪吃蛇V1.0
        }      
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        private function check(_x:Number,_y:Number):Boolean...{
贪吃蛇V1.0
贪吃蛇V1.0
            for(var i:uint;i<a.length;i++)...{     
贪吃蛇V1.0
贪吃蛇V1.0
                if((_x==a[i].x) && (_y==a[i].y)||(_x==0)||(_y==0)||(_x==300)||(_y==400))...{ 
贪吃蛇V1.0
                    trace("x==="+_x+","+"y=="+_y);
贪吃蛇V1.0
                    return true ;       
贪吃蛇V1.0
                } 
贪吃蛇V1.0
            } 
贪吃蛇V1.0
            return false ;    
贪吃蛇V1.0
        }
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        //====蛇移动速度            
贪吃蛇V1.0
        private var _SPEED:uint =10 ;      
贪吃蛇V1.0
贪吃蛇V1.0
        //====食物的大小  
贪吃蛇V1.0
        private var _FWIDTH = 10 ;   
贪吃蛇V1.0
贪吃蛇V1.0
        //====蛇活动窗口宽度
贪吃蛇V1.0
        private var _ACTIVEWIDTH = 300 ;
贪吃蛇V1.0
贪吃蛇V1.0
        //====蛇活动窗口高度
贪吃蛇V1.0
        private var _ACTIVEHEIGHT = 400;
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
    }
贪吃蛇V1.0

}

三:计时器

贪吃蛇V1.0
package com.lux
贪吃蛇V1.0
贪吃蛇V1.0
... {
贪吃蛇V1.0
    import flash.display.MovieClip;
贪吃蛇V1.0
    import flash.events.TimerEvent;
贪吃蛇V1.0
    import flash.text.TextField;
贪吃蛇V1.0
    import flash.utils.Timer;
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
    public class TimerUtil extends MovieClip...{
贪吃蛇V1.0
贪吃蛇V1.0
        private var _sec:Number=0 ;
贪吃蛇V1.0
        private var _min:Number=0
贪吃蛇V1.0
贪吃蛇V1.0
        var time:Timer ;    
贪吃蛇V1.0
贪吃蛇V1.0
        public function TimerUtil()...{ 
贪吃蛇V1.0
            super();  
贪吃蛇V1.0
        }  
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function start():void...{ 
贪吃蛇V1.0
            time = new Timer(1000) ;
贪吃蛇V1.0
            time.addEventListener(TimerEvent.TIMER,change);
贪吃蛇V1.0
            time.start();
贪吃蛇V1.0
        }    
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function remove():void...{
贪吃蛇V1.0
            time.stop();         
贪吃蛇V1.0
            this.removeEventListener(TimerEvent.TIMER,change); 
贪吃蛇V1.0
            this._sec =0;
贪吃蛇V1.0
            this._min =0;
贪吃蛇V1.0
        } 
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        private function change(e:TimerEvent):void...{
贪吃蛇V1.0
            //trace("计时器:"+_sec); 
贪吃蛇V1.0
            _sec++;            
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
            if(_sec>60)...{       
贪吃蛇V1.0
                _min++;     
贪吃蛇V1.0
贪吃蛇V1.0
                if(_min<10)...{
贪吃蛇V1.0
                    min_mc.text = "0"+_min.toString();
贪吃蛇V1.0
                } 
贪吃蛇V1.0
贪吃蛇V1.0
                else...{
贪吃蛇V1.0
                    min_mc.text=_min.toString() ;
贪吃蛇V1.0
                } 
贪吃蛇V1.0
                _sec=0 ;   
贪吃蛇V1.0
            }
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
            if(_sec<10)...{ 
贪吃蛇V1.0
                 sec_mc.text ="0"+_sec.toString() ;   
贪吃蛇V1.0
贪吃蛇V1.0
            }else...{ 
贪吃蛇V1.0
                 sec_mc.text=_sec.toString() ; 
贪吃蛇V1.0
            }
贪吃蛇V1.0
贪吃蛇V1.0
        }
贪吃蛇V1.0
贪吃蛇V1.0
    }
贪吃蛇V1.0

}

 四:节点,也叫食物

贪吃蛇V1.0
package com.lux
贪吃蛇V1.0
贪吃蛇V1.0
... {
贪吃蛇V1.0
    import flash.display.MovieClip;
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
    public class XiaoShe extends MovieClip...{
贪吃蛇V1.0
贪吃蛇V1.0
        //蛇的方向    
贪吃蛇V1.0
贪吃蛇V1.0
贪吃蛇V1.0
        public function XiaoShe(x:Number,y:Number)...{
贪吃蛇V1.0
            super();   
贪吃蛇V1.0
            this.x = x ;
贪吃蛇V1.0
            this.y = y ; 
贪吃蛇V1.0
        }                      
贪吃蛇V1.0
    }
贪吃蛇V1.0
}

继续阅读