资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
1. 贪吃蛇packageimport flash.display.Sprite;import flash.display.StageScaleMode;import flash.events.Event;import flash.events.KeyboardEvent;import flash.events.TimerEvent;import flash.text.TextField;import flash.text.TextFormat;import flash.text.TextFormatAlign;import flash.utils.Timer;public class Game extends Sprite public var frame:Sprite, randombrick:Sprite;public var txt_level:TextField = new TextField(), txt_score:TextField = new TextField(), txt_explain:TextField = new TextField(), txt_winlose:TextField = new TextField(), txt_author:TextField = new TextField();public var tf1:TextFormat = new TextFormat(), tf2:TextFormat = new TextFormat();public var xx:int, yy:int;public var rank:uint = 1;public var time:Timer;public var p_eat:Boolean = true, p_start:Boolean = true;public var body:uint = 5;public var arr:Array = new Array();public function Game()BASIC();stage.addEventListener(Event.ENTER_FRAME, TFEVENT);stage.addEventListener(KeyboardEvent.KEY_DOWN, START);stage.addEventListener(KeyboardEvent.KEY_DOWN, diction);function START(e:KeyboardEvent):void if (e.keyCode = 13 & p_start = true)p_start = false;xx = -20;yy = 0;body = 5;initarr();txt_winlose.text = ;time = new Timer(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);private function WINLOSE(et:Event):void if (arr.length = (rank 10) ? (20) : (15)rank += 1;WINANDLOSE();txt_winlose.text = WIN;if (arr.length != 0)if (arr0.x 390 | arr0.y 390)WINANDLOSE();txt_winlose.text = LOSE;if (arr.length != 0)for (var a:uint = 2; a arr.length; a+)if (arr0.hitTestObject(arra)WINANDLOSE();txt_winlose.text = LOSE;function WINANDLOSE():voidp_start = true;for (var m:uint = 0; m arr.length; m+)removeChild(arrm);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);private function initarr():void /初始化数组for (var i:uint = 0; i body; i+)var brick:Sprite = BRICK();arr.push(brick);addChild(arri);arri.x = i * 20 + 200 + 3;arri.y = 200 + 3;private function MOVE(evt:TimerEvent):void var temp:Array = arr.splice(arr.length - 1, 1);temp0.x = arr0.x + xx;temp0.y = arr0.y + yy;arr.unshift(temp0);private function RANDOMBRICK(evt:Event):void if (p_eat = true)p_eat = false;randombrick = BRICK();var dx:Number = Math.random() * 400;var dy:Number = Math.random() * 400;for (var e:uint = 0; e = 20 * e & dx = 20 * e & dy = 20 * (e + 1)randombrick.y = e * 20 + 3;addChild(randombrick);randombrick.alpha=1;if (arr0.hitTestObject(randombrick)p_eat = true;body += 1;arr.unshift(randombrick);randombrick.alpha = 0.5;arr0.x = arr1.x + xx;arr0.y = arr1.y + yy;private function FRAME():Sprite /绘制方格frame = new Sprite();for (var m:uint = 0; m = 20; m+)frame.graphics.moveTo(0, m * 20);frame.graphics.lineStyle(1, 0x0000ff);frame.graphics.lineTo(400, m * 20);for (var n:uint = 0; n = 20; n+)frame.graphics.moveTo(n * 20, 0);frame.graphics.lineStyle(1, 0x0000ff);frame.graphics.lineTo(n * 20, 400);return frame;private function TF():voidtf1.size = 14;tf1.color = 0xffffff;tf1.bold = true;tf2.size = 22;tf2.color = 0xffff00;tf2.bold = true;tf2.align = TextFormatAlign.CENTER;private function TFEVENT(event:Event):void TF();txt_explain.setTextFormat(tf1, 0, 7);txt_explain.setTextFormat(tf1, 12, 17);txt_explain.setTextFormat(tf2, 8, 10);txt_explain.setTextFormat(tf2, 17, 19);txt_level.text = LEVEL:n + rank;txt_level.setTextFormat(tf1, 0, 6);txt_level.setTextFormat(tf2, 7, txt_level.length);txt_score.text = SCORE:n + (body - 5);txt_score.setTextFormat(tf1, 0, 6);txt_score.setTextFormat(tf2, 7, txt_score.length);txt_winlose.setTextFormat(tf2);txt_author.setTextFormat(tf1);private function BRICK():Sprite /画蛇身体var panel:Sprite = new Sprite();panel.grap
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号