网站首页 mysql技术
跟我一起做游戏--偷吃蛇来了--第三期
发布时间:2017-02-25 06:58查看次数:3987
我们先分享下最后界面 有食物 有蛇 玩具通过点击按下鼠标 移动
弹起鼠标完成吃食物的动作;
同时移动时间 身体也同步运动...每个节点的位置就是他上一个节点的位置最终效果展示 贪吃蛇大作战
第一步 先初始化界面把蛇跟食物都渲染到界面上
private gameWidth: number = 630; //屏幕边缘
private gameHight: number = 500; //屏幕高度
private jifen:eui.Label; //积分数量
private paihangbang:eui.List;
private foodObj: Food; //食物对象
private snakeObj: snake; //蛇对象
//初始化界面
private initGame() {
this.snakeObj = new snake();
this.addChild(this.snakeObj);
this.randomFood();
this.initPaiHang();
}
private randomFood() {
//随机坐标
var tmpx = Math.random() * (this.gameHight);
var tmpy = Math.random() * (this.gameWidth);
//新建食物对象
this.foodObj = new Food(tmpx, tmpy);
//显示
this.addChild(this.foodObj);
}
//下边这些不重要!!!选择性学习
/**增加积分 */
private addJiFen(){
let tempJifen:number = parseInt(this.jifen.text);
tempJifen = tempJifen + 1;
this.jifen.text = tempJifen.toString();
}
private initPaiHang(){
this.paihangbang.dataProvider = new eui.ArrayCollection(['第一名:张三','第二名:李四','第三名:王五']);
// this.addChild(this.paihangbang);
}
第二步 初始化监听动作
public constructor() {
super();
this.skinName = "resource/BeiJing.exml";
this.initGame();
this.touchEnabled = true;
this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onMoveSatart, this);
this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onMove, this);
this.addEventListener(egret.TouchEvent.TOUCH_END, this.onMoveEnd, this);
}
第三步 动作执行
private timer: egret.Timer; //定义时钟对象
private during: number = 40; //位移
private moveEvent: egret.TouchEvent; //事件对象
private head: eui.Rect; //蛇头
/**点击按下 */
protected onMoveSatart(e: egret.TouchEvent) {
this.moveEvent = e;
this.snakeObj.move(e, this.during);
}
/**移动 */
//这里要理解是通过时钟事件来执行的
protected onMove(e: egret.TouchEvent) {
this.moveEvent = e;
if (this.timer == null) {
this.timer = new egret.Timer(this.during);
this.timer.addEventListener(egret.TimerEvent.TIMER, this.onTimer, this);
this.timer.start();
}
}
/**点击结束 */
protected onMoveEnd(e: egret.TouchEvent) {
if (this.timer != null) {
this.timer.stop();
this.timer = null;
}
}
protected onTimer(e: egret.TimerEvent) {
this.head = this.snakeObj.getHead(); //获取蛇头
if (this.hit(this.head, this.foodObj)) //检测蛇头跟食物是否碰撞
this.onEat(); //吃掉食物
this.snakeObj.move(this.moveEvent, this.during); //移动身体
}
private hit(a, b) {
return a.hitTestPoint(b.x,b.y);
}
跟我一起做游戏--偷吃蛇来了--第三期
就这样一个简单是贪吃蛇来了........当然这个是有BUG 比如蛇头的位置移动到边缘外
可拓展,排行榜~~~~
可拓展,后端基于PHP + memcached 的积分系统
可拓展 多人在线的渲染技术
未进步不止步~~~程序猿的路上越走越远了
德玛西亚 深圳 2017年2月25日14:48:10
class GameStart extends eui.Component {
private gameWidth: number = 630; //屏幕边缘
private gameHight: number = 500; //屏幕高度
private jifen:eui.Label; //积分数量
private paihangbang:eui.List;
private foodObj: Food; //食物对象
private snakeObj: snake; //蛇对象
public constructor() {
super();
this.skinName = "resource/BeiJing.exml";
this.initGame();
this.touchEnabled = true;
this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onMoveSatart, this);
this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onMove, this);
this.addEventListener(egret.TouchEvent.TOUCH_END, this.onMoveEnd, this);
}
//初始化界面
private initGame() {
this.snakeObj = new snake();
this.addChild(this.snakeObj);
this.randomFood();
this.initPaiHang();
}
private randomFood() {
//随机坐标
var tmpx = Math.random() * (this.gameHight);
var tmpy = Math.random() * (this.gameWidth);
//新建食物对象
this.foodObj = new Food(tmpx, tmpy);
//显示
this.addChild(this.foodObj);
}
private onEat() {
//在舞台上移除食物
this.removeChild(this.foodObj);
//增加积分
this.addJiFen();
//调用彩虹蛇吃食物的事件
this.snakeObj.afterEat(this.foodObj.fillColor);
//随机产生食物
this.randomFood();
}
/**增加积分 */
private addJiFen(){
let tempJifen:number = parseInt(this.jifen.text);
tempJifen = tempJifen + 1;
this.jifen.text = tempJifen.toString();
}
private initPaiHang(){
this.paihangbang.dataProvider = new eui.ArrayCollection(['第一名:张三','第二名:李四','第三名:王五']);
// this.addChild(this.paihangbang);
}
private timer: egret.Timer;
private during: number = 40;
private moveEvent: egret.TouchEvent;
private head: eui.Rect;
/**点击按下 */
protected onMoveSatart(e: egret.TouchEvent) {
this.moveEvent = e;
this.snakeObj.move(e, this.during);
}
/**移动 */
protected onMove(e: egret.TouchEvent) {
this.moveEvent = e;
if (this.timer == null) {
this.timer = new egret.Timer(this.during);
this.timer.addEventListener(egret.TimerEvent.TIMER, this.onTimer, this);
this.timer.start();
}
}
/**点击结束 */
protected onMoveEnd(e: egret.TouchEvent) {
if (this.timer != null) {
this.timer.stop();
this.timer = null;
}
}
protected onTimer(e: egret.TimerEvent) {
this.head = this.snakeObj.getHead();
if (this.hit(this.head, this.foodObj)) //检测蛇头跟食物是否碰撞
this.onEat(); //吃掉食物
this.snakeObj.move(this.moveEvent, this.during); //继续移动
}
private hit(a, b) {
return a.hitTestPoint(b.x,b.y);
}
}
关键字词:EGRET##