贪吃蛇V1.0

 


 

贪吃蛇游戏开发详解

注意以下问题:

1.蛇移动算法。

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

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

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

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

点击下载源代码

 代码如小:

一:游戏开始

 

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

        
/**//**   
        * 每吃到食物,增加游戏积分   
        
*/
 
        
public function addScore(_score:Number)...{
            
this._score+=_score ;  
            score_mc.score_txt.text 
= this._score.toString();  
        }

              
        
public function getScore():Number...{
            
return this._score ; 
        }

         
//        public function timerStop():void{
//            timer_mc.remove(); 
//        }
//        
  
        

    }

}

 

二:蛇移动的管理

package  com.lux
... {
    
import de.polygonal.ds.NullIterator;
    
    
import flash.display.MovieClip;
    
import flash.events.Event;
    
import flash.events.KeyboardEvent;
    
import flash.events.TimerEvent;
    
import flash.filters.BevelFilter;
    
import flash.ui.Keyboard;
    
import flash.utils.Timer;

    
public class DaShe extends MovieClip ...
        
       
//蛇节点数组 
        private var a:Array ;
        
//方向 
        private var up:uint =    1 ;
        
private var down:uint =  2 ;
        
private var left:uint =  3 ;
        
private var right:uint = 4 ;
               
        
//====保存随机生成的蛇  
        private var _newShe:XiaoShe ;  
               
        
//====方向 
        private var _dir:uint ;  
        
private var timer:Timer ;   
        
        
private var _score:Number=0 ;
    
        
public function DaShe() ...{       
            
super();    
            
this._dir = this.right ; 
            a 
= new Array();
            
//蛇初始化为两条小蛇的长度。               
            var head:XiaoShe = new XiaoShe(30,30);          
            a.push(head);       
            
this.addChild(head);  
              
            var head1:XiaoShe 
= new XiaoShe(head.x-head.width,head.y);
            a.push(head1);      
            
this.addChild(head1);       
            
                 
        }
        
         
        
public function start():void...
             timer 
= new Timer(100);
             timer.addEventListener(TimerEvent.TIMER,running);
             timer.start();  
                
            
//===注册方向控制                  
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN,direction);
             
            
//===随机生成新蛇 
            this.initFood();   
                 
        }
                     
              
        
private function running(e:Event):void...{                                          
            
for(var i:uint=a.length-1;i>0;i--)...
                a[i].x 
=a[i-1].x;    
                a[i].y 
=a[i-1].y;  
            }
    
 
            
switch(_dir)...
                
case this.up:
                     a[
0].y-=this._SPEED;
                     
break;
                
case this.left:
                     a[
0].x-=this._SPEED;
                     
break;         
                
case this.down:
                     a[
0].y+=this._SPEED; 
                     
break;       
                
case this.right:   
                     a[
0].x+=this._SPEED ;                   
            }
 
            
//==========碰到食物 
            if(isEat())...
                eatFood();      
                 
                
                
//增加积分
               
// this.addScore();  
               GameStart.gamestart.addScore(1);
            }
 
            
if(isDead())...{
                
this.gameOver() ; 
            }
           
                  
        }
 
//         
//        private function addScore():void{
//            this._score++;         
//            score_mc.score_txt.text = this._score.toString();
//        }
        
        
/** *//**
        * 蛇头是否碰到墙壁 。 
        * 碰上:ture 否则:false 
        
*/
 
        
private function isDead():Boolean 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值