貪吃蛇遊戲開發詳解
注意以下問題:
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(); // } private function isDead():Boolean ...{ if(a[0].x<=0||a[0].x>=this._ACTIVEWIDTH||a[0].y<=0||a[0].y>=this._ACTIVEHEIGHT)...{ return true ; } for(var i:uint=1;i<a.length;i++)...{ if((a[0].x==a[i].x) &&(a[0].y==a[i].y)) return true ; } return false ; } public function gameOver():void...{ trace("遊戲結束"); timer.stop(); timer.removeEventListener(TimerEvent.TIMER,running); this.stage.removeEventListener(KeyboardEvent.KEY_DOWN,direction); this.parent.removeChild(this); GameStart.gamestart.gotoAndStop("gameover_tag"); // GameStart.gamestart.timerStop(); } public function eatFood():void...{ var _newshe:XiaoShe = new XiaoShe(a[a.length-1].x,a[a.length-1].y); a.push(_newshe); this.randomLocation(); this.addChild(_newshe); } public function isEat():Boolean...{ if((a[0].x==this._newShe.x)&&(a[0].y==this._newShe.y))...{ return true ; } return false ; } //==============方向控制 private function direction(e:KeyboardEvent):void...{ trace("有按鍵事件"); switch(e.keyCode)...{ case Keyboard.DOWN: if(_dir!=this.up) _dir = this.down ; break ; case Keyboard.RIGHT: if(_dir!=this.left) _dir = this.right ; break; case Keyboard.UP: if(_dir!=this.down) _dir =this.up ; break; case Keyboard.LEFT: if(_dir!=this.right) _dir =this.left ; } } private function initFood():void...{ _newShe = new XiaoShe(0,0); //===随機生成位置 this.randomLocation(); this.addChild(_newShe); } //==============重新随機移動蛇的位置。 private function randomLocation():void...{ var _x:Number = Math.floor(Math.random()*30)*10 ; var _y:Number = Math.floor(Math.random()*40)*10 ; trace("x:"+_x+","+"y:"+_y); if(check(_x,_y))...{ trace("生成的小蛇和蛇身重合"); this.randomLocation(); }else...{ this._newShe.x=_x ; this._newShe.y=_y ; } } private function check(_x:Number,_y:Number):Boolean...{ for(var i:uint;i<a.length;i++)...{ if((_x==a[i].x) && (_y==a[i].y)||(_x==0)||(_y==0)||(_x==300)||(_y==400))...{ trace("x==="+_x+","+"y=="+_y); return true ; } } return false ; } //====蛇移動速度 private var _SPEED:uint =10 ; //====食物的大小 private var _FWIDTH = 10 ; //====蛇活動視窗寬度 private var _ACTIVEWIDTH = 300 ; //====蛇活動視窗高度 private var _ACTIVEHEIGHT = 400; }}
三:計時器
package com.lux ... { import flash.display.MovieClip; import flash.events.TimerEvent; import flash.text.TextField; import flash.utils.Timer; public class TimerUtil extends MovieClip...{ private var _sec:Number=0 ; private var _min:Number=0 var time:Timer ; public function TimerUtil()...{ super(); } public function start():void...{ time = new Timer(1000) ; time.addEventListener(TimerEvent.TIMER,change); time.start(); } public function remove():void...{ time.stop(); this.removeEventListener(TimerEvent.TIMER,change); this._sec =0; this._min =0; } private function change(e:TimerEvent):void...{ //trace("計時器:"+_sec); _sec++; if(_sec>60)...{ _min++; if(_min<10)...{ min_mc.text = "0"+_min.toString(); } else...{ min_mc.text=_min.toString() ; } _sec=0 ; } if(_sec<10)...{ sec_mc.text ="0"+_sec.toString() ; }else...{ sec_mc.text=_sec.toString() ; } } }}
四:節點,也叫食物
package com.lux ... { import flash.display.MovieClip; public class XiaoShe extends MovieClip...{ //蛇的方向 public function XiaoShe(x:Number,y:Number)...{ super(); this.x = x ; this.y = y ; } } }