您好,欢迎访问三七文档
心血来潮想做个贪吃蛇,纯手码。基本功能有了,待优化的地方还很多,仅供参考主类package{importflash.display.DisplayObject;importflash.display.Shape;importflash.display.Sprite;importflash.events.Event;importflash.events.KeyboardEvent;importflash.events.MouseEvent;importflash.geom.Point;importflash.text.TextField;importflash.text.TextFormat;publicclassSnakeextendsSprite{privatevardir:int=3;//蛇头方向默认向下privatevarcontainer:Sprite;//容器privatevargameState:Boolean;//游戏状态true游戏中false结束privatevarstartBtn:TextField;privatevarscoreText:TextField;privatevarscore:int;//得分privatevar_stageWidth:int;privatevar_stageHeight:int;privatevarspace:int;//前进时间间隔privatevarodlx:int;privatevaroldy:int;privatevarbodyArr:Array;//存储蛇身privatevarsnekeHead:Shape;//蛇头privatevar_length:int;//蛇身数组长度publicfunctionSnake(){_stageWidth=this.stage.stageWidth;_stageHeight=this.stage.stageHeight;this.graphics.beginFill(0x000000,1);this.graphics.drawRect(0,0,_stageWidth,_stageHeight);this.graphics.endFill();varformat:TextFormat=newTextFormat();format.size=18;format.color=0xff0000;startBtn=newTextField();startBtn.x=(_stageWidth-startBtn.width)1;startBtn.y=(_stageHeight-startBtn.height)1;startBtn.width=100;startBtn.height=50;startBtn.defaultTextFormat=format;startBtn.text=开始游戏;startBtn.addEventListener(MouseEvent.CLICK,startGameHandler);addChild(startBtn);scoreText=newTextField();scoreText.x=100;scoreText.y=20;scoreText.width=100;scoreText.height=50;scoreText.border=true;scoreText.defaultTextFormat=format;scoreText.text=得分:;addChild(scoreText);}privatefunctionstartGameHandler(e:MouseEvent):void{gameState=true;this.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyhandler);this.stage.addEventListener(Event.ENTER_FRAME,keepMoviing);startBtn.visible=false;startBtn.text=;container=newSprite();addChild(container);drawsnake();addFood();}privatefunctionkeepMoviing(e:Event):void{if(!gameState){return;}//蛇前进space++;if(space%3!==0&&space!==0){return;}odlx=snekeHead.x;oldy=snekeHead.y;switch(dir){case0:snekeHead.x-=10;break;case1:snekeHead.x+=10;break;case2:snekeHead.y-=10break;case3:snekeHead.y+=10;break;default:break;}move();space=0;//检测是否出界if(snekeHead.x0||snekeHead.x_stageWidth||snekeHead.y0||snekeHead.y_stageHeight){resetHandler();return;}//碰撞检测varobj:DisplayObject;for(vari:int=0;icontainer.numChildren;i++){obj=container.getChildAt(i);if(snekeHead.hitTestObject(obj)){if(objasFood){container.removeChild(obj);//根据食物种类添加得分addScore();//生成食物addFood();addbody(_length+1);}elseif(objasBody){resetHandler();return;}}}}/***初始化蛇身**/privatefunctiondrawsnake():void{snekeHead=newShape();snekeHead.graphics.beginFill(0xff00ff,1);snekeHead.graphics.drawCircle(0,0,4);snekeHead.graphics.endFill();snekeHead.x=100;snekeHead.y=100;container.addChild(snekeHead);bodyArr=newArray();for(vari:int=0;i5;i++){addbody(i);}}/***移动*/privatefunctionmove():void{for(vari:int=_length-1;i-1;i--){if(i==0){(bodyArr[0]asBody).x=odlx;(bodyArr[0]asBody).y=oldy;continue;}(bodyArr[i]asBody).x=(bodyArr[i-1]asBody).x;(bodyArr[i]asBody).y=(bodyArr[i-1]asBody).y;}}/***更新得分*@paramvalue**/privatefunctionaddScore(value:int=10):void{score+=value;scoreText.text=得分:+score.toString();}/***添加食物*@parami**/privatefunctionaddFood():void{varfood:Food=newFood();food.x=_stageWidth-Math.random()*200;food.y=_stageHeight-Math.random()*300;container.addChild(food);}/***添加一节蛇身*@parami**/privatefunctionaddbody(i:int=0):void{varbody:Body=newBody();if(i5){//这里应该判断蛇最后一节body.x=(bodyArr[_length-1]asBody).x;body.y=(bodyArr[_length-1]asBody).y;}else{body.x=i*10+110;body.y=100;}container.addChild(body);bodyArr.push(body);_length=bodyArr.length;}/***检查改变的是哪个坐标*@paramva1*@paramva2*@return**/privatefunctioncheckpos(va1:Point,va2:Point):int{if(va1.x==va2.x){return0;}elseif(va1.y==va2.y){return1;}return-1;}/***方向控制*@parame**/privatefunctionkeyhandler(e:KeyboardEvent):void{if(e.keyCode==38){if(dir!==3){dir=2;}}if(e.keyCode==40){if(dir!==2){dir=3;}}if(e.keyCode==37){if(dir!==1){dir=0;}}if(e.keyCode==39){if(dir!==0){dir=1;}}}privatefunctionresetHandler():void{gameState=false;while(container.numChildren0){container.removeChildAt(0);}removeChild(container);this.stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyhandler);this.stage.removeEventListener(Event.ENTER_FRAME,keepMoviing);startBtn.text=游戏结束;startBtn.visible=true;score=0;scoreText.text=得分:;dir=3;}}}--------------------------------------------------------------------食物类package{importflash.display.Sprite;publicclassFoodextendsSprite{publicfunctionFood(){this.graphics.beginFill(0x87df09,1);this.graphics.drawRect(0,0,10,10);this.graphics.endFill();}}}--------------------------------------------------------------------蛇身类package{importflash.display.Sprite;publicclassBodyextendsSprite{publicfunctionBody(){this.graphics.beginFill(0x00ff00,1);this.graphics.drawCircle(0,0,3);this.graphics.endFill();}}}
本文标题:as3.0贪吃蛇
链接地址:https://www.777doc.com/doc-6210565 .html