刚开始的时候标题是用textField弄的,如果鼠标点到那里,焦点会指向那个,游戏中可能会控制不能,只要重新点一下即可
主要核心源码
CODE:
-
package
-
{
-
import adobe.utils.CustomActions;
-
import flash.display.Sprite;
-
import flash.display.Stage;
-
import flash.events.Event;
-
import flash.events.KeyboardEvent;
-
import flash.text.FontStyle;
-
import flash.text.TextField;
-
import flash.text.TextFormat;
-
import flash.utils.Timer;
-
import flash.events.TimerEvent;
-
import flash.display.DisplayObject;
-
-
-
/**
-
* ...
-
* @author
-
*/
-
public class Main extends Sprite {
-
public static var snakeType:String ;//运动方向
-
public var snakeLong:int ;//长度
-
public var snakeBody:Array ;//身体数组
-
public var outBodyArr:Array ;//还没吃到的身体
-
public var timer:Timer;//世界间隔
-
public var score:int ;//分数
-
-
public var scoreTxt:TextField = new TextField();
-
-
public static var p1:Head;
-
-
public function Main():void {
-
//监听添加舞台
-
if (stage) startgame();
-
else addEventListener(Event.ADDED_TO_STAGE, startgame);
-
-
-
}
-
-
//开始游戏
-
public function startgame(e:Event = null):void {
-
var menu:Menu = new Menu();
-
menu.x = 250; menu.y = 200;
-
stage.scaleMode = "showAll";
-
stage.addChild(menu);
-
stage.addEventListener(KeyboardEvent.KEY_UP,init);
-
}
-
-
//初始化
-
private function init(e:KeyboardEvent = null):void {
-
if (e.keyCode!=13) {
-
return;
-
}
-
-
stage.removeEventListener(KeyboardEvent.KEY_UP, init);
-
stage.removeChildAt(1);
-
removeEventListener(Event.ADDED_TO_STAGE, init);
-
-
//初始化数值
-
snakeBody = new Array();
-
outBodyArr = new Array();
-
snakeLong = 4;
-
score = 0;
-
snakeType = "right";
-
-
//创建头部
-
p1 = new Head;
-
p1.x = 100;
-
p1.y = 300;
-
snakeBody.push(p1);//把头部添加到身体数组
-
-
//继续添加身体
-
for (var i:int = 1; i <snakeLong ; i++) {
-
var b:Body = new Body();
-
b.x = p1.x - 20*i-1;
-
b.y = p1.y;
-
snakeBody.push(b);
-
stage.addChild(b);
-
}
-
stage.addChild(p1);//添加到舞台
-
-
//世界时间开始了
-
timer = new Timer(100);
-
timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
-
timer.start();
-
-
randMakeBody();//生成一个还没吃到的身体
-
-
//添加键盘监听
-
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeydownHandler);
-
-
//积分text
-
scoreTxt.text = "Score:" + score;
-
stage.addChild(scoreTxt);
-
}
-
-
//世界时间句柄
-
public function onTimerHandler(event:Event):void {
-
-
//超出屏幕就gameover
-
if (p1.x<0 || p1.x>780 || p1.y>580 || p1.y<0) {
-
gameOver();
-
}
-
-
-
/*
-
* ****游戏核心:***
-
* 遍历body
-
* 每次后面一个body的位置都reset成前一个body的位置
-
*/
-
for (var i:int =snakeBody.length-1; i>0 ; i--) {
-
snakeBody[i].x =snakeBody[i-1].x;
-
snakeBody[i].y = snakeBody[i - 1].y;
-
-
//头碰到身体的话就over
-
if (snakeBody[i+1]) {
-
if (p1.hitTestObject(snakeBody[i+1])) {
-
gameOver();
-
}
-
}
-
-
}
-
-
//根据snakeType移动snake
-
switch(snakeType) {
-
case "up":
-
p1.up();
-
break;
-
case "down":
-
p1.down();
-
break;
-
case "left":
-
p1.left();
-
break;
-
case "right":
-
p1.right();
-
break;
-
}
-
-
-
-
-
//吃外面的身体,遍历outBodyArr,然后碰撞检测,可能这方法不好,但是这小游戏可以吧?
-
for ( i = 0; i <outBodyArr.length;i++ ) {
-
if (p1.hitTestObject(outBodyArr[i])) {
-
stage.removeChild(outBodyArr[i]);
-
outBodyArr.splice(i, 1);
-
-
//吃完一个再生产一个
-
randMakeBody();
-
var b:Body = new Body();
-
b.x = -30; b.y = -30;//先隐藏到舞台外
-
stage.addChild(b);
-
snakeBody.push(b);
-
-
if (timer.delay-5>0) {
-
timer.delay -= 5;//加快速度
-
}
-
-
-
//积分
-
score++;
-
scoreTxt.text = "score:"+score;
-
}
-
}
-
-
-
-
-
}
-
-
-
//键盘响应,更改移动状态
-
public function onKeydownHandler(event:KeyboardEvent):void {
-
switch(event.keyCode) {
-
case 37:
-
if (snakeType != "right") {
-
snakeType = "left";
-
}
-
break;
-
case 39:
-
if (snakeType != "left") {
-
snakeType = "right";
-
}
-
break;
-
case 38:
-
if (snakeType != "down") {
-
snakeType = "up";
-
}
-
break;
-
case 40:
-
if (snakeType != "up") {
-
snakeType = "down";
-
}
-
break;
-
}
-
}
-
-
-
//在舞台随机生成可以吃的身体
-
public function randMakeBody():void {
-
var x:int = Math.random()*780 +1;
-
var y:int = Math.random()*580 +1;
-
var randBody:Body = new Body();
-
randBody.x = x;
-
randBody.y = y;
-
stage.addChild(randBody);
-
outBodyArr.push(randBody);
-
}
-
-
-
//gamemover
-
public function gameOver():void {
-
-
//清屏
-
while (stage.numChildren> 1) {
-
stage.removeChildAt(1);
-
-
}
-
-
//取消世界时间
-
timer.removeEventListener(TimerEvent.TIMER, onTimerHandler);
-
-
-
//显示"gameover"
-
var txt:TextField = new TextField();
-
var textformat:TextFormat = new TextFormat;
-
textformat.size =30;
-
txt.text = "Game Over! nSCORE:"+score+"nnPass Enter to restart";
-
txt.setTextFormat(textformat);
-
txt.width = 300; txt.height = 170;
-
txt.x = stage.stageWidth/2-txt.width/3 ; txt.y = stage.stageHeight/2-txt.height/2;
-
stage.addChild(txt);
-
-
//键盘监听,以致restart
-
stage.addEventListener(KeyboardEvent.KEY_UP,init);
-
}
-
-
}
-
-
}
不错不错,感觉LZ是搞游戏的……
评论 由 sjpsega — 2009年09月13号 @ 10:45 上午
And, yeah, screw trying to get justice through the courts. ,
评论 由 GanjaBoy89 — 2009年10月11号 @ 7:22 上午
有QQ么,留个联系方式交流一下
评论 由 perl — 2009年10月12号 @ 4:52 下午
qq:285837593
评论 由 admin — 2009年10月12号 @ 5:03 下午