Do not step on white pieces
Written using the game (egret engine) Egret
Game Address
Ready to work
- Learn egret engine and install the writing tool
- Install the game engine
- Installation Egret Wing3
- Create a project
After you create a project you can choose different versions of the engine, create success you can also view the API, set for release.
Directory structure is as follows
The main code is stored in the file src (egret engine support code to the typescript )
Coding
Let me talk about the whole idea: that the whole game broken down a bit, a small grid as a module, and each module as a big, big game overall as a module, a module as a timer, to start the game and the game ends respectively, as a module. Figure:
- Without further ado open line and line and open
- egret provide server services
egret startserver -a
-a representation of the file monitor automatically updated
BoxGraphics
// 负责初始化小格子
private init() {
this.touchEnabled = true;
this.width = GameData.getBoxWidth();
this.height = GameData.getBoxHeight();
this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.click, this);
}
/**
* drawBox
* 绘制内容
*/
public drawBox(canTouch:boolean=false) {
this._canTouch = canTouch;
this.graphics.clear();
if(canTouch) {
this.graphics.beginFill(0);
} else {
this.graphics.beginFill(0xffffff);
}
this.graphics.lineStyle(1, 0);
this.graphics.drawRect(0,0,GameData.getBoxWidth(),GameData.getBoxHeight());
this.graphics.endFill();
}
/**
* click
* 当前方块被点击后的响应事件
*/
private click(evt:egret.TouchEvent):void {
this.graphics.clear();
if(this._canTouch) {
this.graphics.beginFill(0xcccccc);
} else {
this.graphics.beginFill(0xff0000);
}
this.graphics.lineStyle(1, 0);
this.graphics.drawRect(0,0,GameData.getBoxWidth(),GameData.getBoxHeight());
this.graphics.endFill();
var event:GameEvent;
//不能点击,抛出错误事件
if(!this._canTouch) {
event = new GameEvent(GameEvent.GAME_OVER);
} else {
event = new GameEvent(GameEvent.GAME_HIT);
}
this.dispatchEvent(event);
}
GroupRect
- Line grid
private init():void {
this._boxs = [];
// 生成一行中的每一个格子 并给每个格子添加对应事件
for(var i:number=0;i<GameData.column;i++) {
var box:BoxGraphics = new BoxGraphics();
this._boxs.push(box);
box.addEventListener(GameEvent.GAME_HIT, this.clickRight, this);
box.addEventListener(GameEvent.GAME_OVER, this.boxGameOver, this);
this.addChild(box);
box.x = GameData.getBoxWidth()*i;
}
}
Gameview
- Main game interface
private init():void {
this._boxGroups = [];
var len:number = GameData.row+1;
// 循环生成每一列格子
for(var i:number=0;i<len;i++) {
var boxg:GroupRect = new GroupRect();
this._boxGroups.push(boxg);
this.addChild(boxg);
boxg.addEventListener(GameEvent.GAME_OVER, this.gameOver, this);
boxg.addEventListener(GameEvent.GAME_HIT, this.clickRight, this);
}
/*
this.scoreText = new egret.TextField();
this.scoreText.textColor = 0xff0000;
this.scoreText.bold = true;
this.scoreText.size = 100;
*/
// 设置 分数
this.scoreText = new egret.BitmapText();
this.scoreText.x = 180;
this.scoreText.y = 50;
this.scoreText.text = String(0);
this.addChild(this.scoreText);
}
- Click on the game moving function
public move() {
var len:number = GameData.row+1;
for(var i:number=0;i<len;i++) {
// 游戏加速
this._boxGroups[i].y += GameData.speed;
//移动到舞台外侧了
if(this._boxGroups[i].y>=GameData.getStageHeight()){
// 如果格子没有被点击 游戏结束
if(!this._boxGroups[i].isHit) {
this.gameOver();
return;
}
// 设置对应格子的位置
if(i==0) {
this._boxGroups[i].y = this._boxGroups[4].y - GameData.getBoxHeight();
} else {
this._boxGroups[i].y = this._boxGroups[i-1].y - GameData.getBoxHeight();
}
this._boxGroups[i].create();
}
}
}
Main
- Entry file
/**
* 初始化游戏函数
* 初始化gameview
* 初始化定时器
* 初始化开始|结束 画布
* 添加事件监听
*/
private init():void {
this.gameview = new GameView();
this.addChild(this.gameview);
this.gameview.addEventListener(GameEvent.GAME_OVER, this.gameover,this);
this.timer = new egret.Timer(20,0);
this.timer.addEventListener(egret.TimerEvent.TIMER, this.timers, this);
this.gameoverPanel = new GameOverPanel();
this.gameoverPanel.addEventListener(GameEvent.GAME_START,this.startgame,this);
this.startgamePanel = new StartGamePanel();
this.startgamePanel.addEventListener(GameEvent.GAME_START, this.startgame, this);
this.addChild(this.startgamePanel);
}
//定义事件
/**
* 游戏结束
*/
private gameover(evt:GameEvent):void {
this.timer.stop();
this.gameoverPanel.update();
this.addChild(this.gameoverPanel);
}
/**
* 开始游戏
* 重新设置游戏速度 分数
* 去除游戏开始|结束画布
*/
private startgame(evt:GameEvent):void {
GameData.speed = 10;
GameData.setScore(0);
this.gameview.startgame();
if(this.startgamePanel.parent) {
this.removeChild(this.startgamePanel);
}
if(this.gameoverPanel.parent) {
this.removeChild(this.gameoverPanel);
}
this.timer.start();
}
release
egret publish
over
Here, related presentations about the game is basically over, if there is an error or not rigorous place, be sure to give correct me, very grateful. If you like or inspire, welcomed the star, the author is also a kind of encouragement.