标签:console vol 按钮 otto height listen onenter ice 贪吃蛇
/**
*
* @author
*
*/
class Game {
private _root: egret.DisplayObjectContainer;
public constructor(root: egret.DisplayObjectContainer) {
this._root = root;
this.init();
this.creatdiren();
}
public colorArr: Array<number> = [0xff0000,0x18ff00,0x009cff,0xe400ff,0xffe400,0xff8b8b];
//红色 绿色 蓝色 紫色e400ff 黄色ffe400 粉色 ff8b8b
//背景
private bg: egret.Sprite = null;
private snake: Snake;
//主角移动的速度
private vx: number = 0;
private vy: number = 0;
//舞台宽高
private stageW: number = 0;
private stageH: number = 0;
//按钮
private leftBtn: egret.TextField = null;
private rightBtn: egret.TextField = null;
private topBtn: egret.TextField = null;
private bottomBtn: egret.TextField = null;
private uiSprite: egret.Sprite = null;
private SnakeGrop: Array<Snake>;
private EnemyGrop: Array<EnemySnake> = [];
private touchType: string;
private touchTypeOld: string;
private init() {
this.stageH = egret.MainContext.instance.stage.stageHeight;
this.stageW = egret.MainContext.instance.stage.stageWidth;
//添加背景
this.bg = new egret.Sprite();
this.bg.graphics.beginFill(0x000000);
this.bg.graphics.drawRect(0,0,500,500);
this.bg.graphics.endFill();
this._root.addChild(this.bg);
this.SnakeGrop = [];
this.EnemyGrop = [];
//第一个蛇
this.snake = new Snake(0xffffff);
this.SnakeGrop.push(this.snake);
this._root.addChild(this.snake);
this.uiSprite = new egret.Sprite();
this._root.addChild(this.uiSprite);
this.leftBtn = new egret.TextField();
this.leftBtn.text = "左";
this.leftBtn.x = 0;
this.leftBtn.y = 50;
this.uiSprite.addChild(this.leftBtn);
this.rightBtn = new egret.TextField();
this.rightBtn.text = "右";
this.rightBtn.x = 100;
this.rightBtn.y = 50;
this.uiSprite.addChild(this.rightBtn);
this.topBtn = new egret.TextField();
this.topBtn.text = "上";
this.topBtn.x = 50;
this.topBtn.y = 0;
this.uiSprite.addChild(this.topBtn);
this.bottomBtn = new egret.TextField();
this.bottomBtn.text = "下";
this.bottomBtn.x = 50;
this.bottomBtn.y = 50;
this.uiSprite.addChild(this.bottomBtn);
this.uiSprite.x = (this.stageW - this.uiSprite.width) / 2;
this.uiSprite.y = this.stageH - this.uiSprite.height;
this.bottomBtn.touchEnabled = true;
this.topBtn.touchEnabled = true;
this.leftBtn.touchEnabled = true;
this.rightBtn.touchEnabled = true;
this.bottomBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchBegin,this);
this.topBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchBegin,this);
this.leftBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchBegin,this);
this.rightBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchBegin,this);
this._root.addEventListener(egret.Event.ENTER_FRAME,this.onEnterFrame,this);
}
private onTouchBegin(e: egret.TouchEvent) {
this.touchType = e.target.text;
if(this.touchTypeOld == null) {
this.touchTypeOld = this.touchType;
}
if(e.target.text == "左") {
if(this.touchTypeOld == "右") {
this.vx = 10;
this.vy = 0;
this.touchTypeOld = "右";
} else {
this.vx = -10;
this.vy = 0;
this.touchTypeOld = "左";
}
}
else if(e.target.text == "右") {
if(this.touchTypeOld == "左") {
this.vx = -10;
this.vy = 0;
this.touchTypeOld = "左";
} else {
this.vx = 10;
this.vy = 0;
this.touchTypeOld = "右";
}
}
else if(e.target.text == "上") {
if(this.touchTypeOld == "下") {
this.vy = 10;
this.vx = 0;
this.touchTypeOld = "下";
} else {
this.vy = -10;
this.vx = 0;
this.touchTypeOld = "上";
}
}
else if(e.target.text == "下") {
if(this.touchTypeOld == "上") {
this.vy = -10;
this.vx = 0;
this.touchTypeOld = "上";
} else {
this.vy = 10;
this.vx = 0;
this.touchTypeOld = "下";
}
}
}
private foodColor: number;
private onEnterFrame(e: egret.Event) {
if(this.cheackHit()) {
this.zhangda(true);
this.xiaoChu();
this.creatdiren();
} else {
this.zhangda(false);
}
if(this.SnakeGrop[0].x > 490) {
this.SnakeGrop[0].x = 0;
} else if(this.SnakeGrop[0].x < 0) {
this.SnakeGrop[0].x = 490;
} else if(this.SnakeGrop[0].y > 490) {
this.SnakeGrop[0].y = 0;
} else if(this.SnakeGrop[0].y < 0) {
this.SnakeGrop[0].y = 490;
}
}
private cheackHit(): boolean {
for(var i = 0;i < this.EnemyGrop.length;i++) {
if(this.snake.x == this.EnemyGrop[i].x && this.snake.y == this.EnemyGrop[i].y) {
this.foodColor = this.EnemyGrop[i].enemyColor;
this._root.removeChild(this.EnemyGrop[i]);
this.EnemyGrop.splice(i,1);
return true;
}
}
}
private xiaoChu() {
let type: number = 0;
if(this.SnakeGrop.length >= 4) {
for(var i = this.SnakeGrop.length - 1;i > 0;i--) {
if(this.SnakeGrop[i - 1].SnakeColor == this.SnakeGrop[i].SnakeColor) {
type++;
}
}
}
console.log(this.SnakeGrop);
if(type == 2) {
let snakenum = this.SnakeGrop.length - 1;
this._root.removeChild(this.SnakeGrop[snakenum]);
this._root.removeChild(this.SnakeGrop[snakenum - 1]);
this._root.removeChild(this.SnakeGrop[snakenum - 2]);
this.SnakeGrop.pop();
this.SnakeGrop.pop();
this.SnakeGrop.pop();
}
}
private zhangda(vol: boolean) {
if(vol) {
var _snake = new Snake(this.foodColor);
this.SnakeGrop.push(_snake);
this._root.addChild(_snake);
}
if(this.SnakeGrop.length > 1) {
for(var i = this.SnakeGrop.length - 1;i > 0;i--) {
this.SnakeGrop[i].x = this.SnakeGrop[i - 1].x;
this.SnakeGrop[i].y = this.SnakeGrop[i - 1].y;
}
}
this.SnakeGrop[0].x += this.vx;
this.SnakeGrop[0].y += this.vy;
}
private creatdiren() {
for(var i = this.EnemyGrop.length;i < 8;i++) {
var diren = new EnemySnake(this.colorArr[this.colorRandom()]);
// console.log(this.colorArr[this.colorRandom()]);
diren.x = this.zhengshuten();
diren.y = this.zhengshuten();
this._root.addChild(diren);
this.EnemyGrop.push(diren);
}
}
private zhengshuten(): number {
var num: number = 0;
num = Math.floor(Math.random() * 490);
if(num % 10 != 0) {
var mo = num % 10;
num = num - mo;
return num;
} else {
return num;
}
}
private colorRandom(): number {
let num: number = 0;
num = Math.floor(Math.random() * 6);
return num;
}
}
贪吃蛇-白鹭引擎
标签:console vol 按钮 otto height listen onenter ice 贪吃蛇
原文地址:http://www.cnblogs.com/falali009/p/6683293.html