1.贪吃蛇package{importflash.display.Sprite;importflash.display.StageScaleMode;importflash.events.Event;importflash.events.KeyboardEvent;importflash.events.TimerEvent;importflash.text.TextField;importflash.text.TextFormat;importflash.text.TextFormatAlign;importflash.utils.Timer;publicclassGameextendsSprite{publicvarframe:Sprite,randombrick:Sprite;publicvartxt_level:TextField=newTextField(),txt_score:TextField=newTextField(),txt_explain:TextField=newTextField(),txt_winlose:TextField=newTextField(),txt_author:TextField=newTextField();publicvartf1:TextFormat=newTextFormat(),tf2:TextFormat=newTextFormat();publicvarxx:int,yy:int;publicvarrank:uint=1;publicvartime:Timer;publicvarp_eat:Boolean=true,p_start:Boolean=true;publicvarbody:uint=5;publicvararr:Array=newArray();publicfunctionGame(){BASIC();stage.addEventListener(Event.ENTER_FRAME,TFEVENT);stage.addEventListener(KeyboardEvent.KEY_DOWN,START);stage.addEventListener(KeyboardEvent.KEY_DOWN,diction);functionSTART(e:KeyboardEvent):void{if(e.keyCode==13p_start==true){p_start=false;xx=-20;yy=0;body=5;initarr();txt_winlose.text="";time=newTimer(((rank<5)?(400-rank*50):(200-rank*10)));time.start();time.addEventListener(TimerEvent.TIMER,MOVE);stage.addEventListener(Event.ENTER_FRAME,RANDOMBRICK);stage.addEventListener(Event.ENTER_FRAME,WINLOSE);}}}privatefunctionWINLOSE(et:Event):void{if(arr.length==((rank<10)?(20):(15))){rank+=1;WINANDLOSE();txt_winlose.text="WIN";}if(arr.length!=0){if(arr[0].x<0||arr[0].x>390||arr[0].y<0||arr[0].y>390){WINANDLOSE();txt_winlose.text="LOSE";}if(arr.length!=0){for(vara:uint=2;a<arr.length;a++){if(arr[0].hitTestObject(arr[a])){WINANDLOSE();txt_winlose.text="LOSE";}}}}functionWINANDLOSE():void{p_start=true;for(varm:uint=0;m<arr.length;m++){removeChild(arr[m]);}arr.splice(0,arr.length-1);arr.shift();time.removeEventListener(TimerEvent.TIMER,MOVE);stage.removeEventListener(Event.ENTER_FRAME,RANDOMBRICK);stage.removeEventListener(Event.ENTER_FRAME,WINLOSE);}}privatefunctioninitarr():void{//初始化数组for(vari:uint=0;i<body;i++){varbrick:Sprite=BRICK();arr.push(brick);addChild(arr[i]);arr[i].x=i*20+200+3;arr[i].y=200+3;}}privatefunctionMOVE(evt:TimerEvent):void{vartemp:Array=arr.splice(arr.length-1,1);temp[0].x=arr[0].x+xx;temp[0].y=arr[0].y+yy;arr.unshift(temp[0]);}privatefunctionRANDOMBRICK(evt:Event):void{if(p_eat==true){p_eat=false;randombrick=BRICK();vardx:Number=Math.random()*400;vardy:Number=Math.random()*400;for(vare:uint=0;e<20;e++){if(dx>=20*edx<=20*(e+1)){randombrick.x=e*20+3;}if(dy>=20*edy<=20*(e+1)){randombrick.y=e*20+3;}}addChild(randombrick);randombrick.alpha=1;}if(arr[0].hitTestObject(randombrick)){p_eat=true;body+=1;arr.unshift(randombrick);randombrick.alpha=0.5;arr[0].x=arr[1].x+xx;arr[0].y=arr[1].y+yy;}}privatefunctionFRAME():Sprite{//绘制方格frame=newSprite();for(varm:uint=0;m<=20;m++){frame.graphics.moveTo(0,m*20);frame.graphics.lineStyle(1,0x0000ff);frame.graphics.lineTo(400,m*20);}for(varn:uint=0;n<=20;n++){frame.graphics.moveTo(n*20,0);frame.graphics.lineStyle(1,0x0000ff);frame.graphics.lineTo(n*20,400);}returnframe;}privatefunctionTF():void{tf1.size=14;tf1.color=0xffffff;tf1.bold=true;tf2.size=22;tf2.color=0xffff00;tf2.bold=true;tf2.align=TextFormatAlign.CENTER;}privatefunctionTFEVENT(event:Event):void{TF();txt_explain.setTextFormat(tf1,0,7);txt_explain.setTextFormat(tf1,12,17);tx...