春节快到了,还没下雪,不如写个游戏来滑雪吧

985 阅读2分钟

PK创意闹新春,我正在参加「春节创意投稿大赛」,详情请看:春节创意投稿大赛

开始界面

准备工作
  1. 找一个漂亮的背景

bg.jpg

  1. 设计一个不那么漂亮的开始按钮

btn_start.png

代码布局
  1. 让背景图的宽高等于画布的宽高
this.sky = utils.createBitmapByName("bg");
this.addChild(this.sky);
let stageW: number = this.stage.stageWidth;
let stageH: number = this.stage.stageHeight;
this.sky.width = stageW;
this.sky.height = stageH;
  1. 定位按钮并添加开始游戏事件
this.btn_start = utils.createBitmapByName("startBtn");//开始按钮
this.btn_start.width = stageW - 40;
this.btn_start.x = (stageW - this.btn_start.width) / 2; //居中布局
this.btn_start.y = stageH - 125;
this.btn_start.touchEnabled = true;//开启触碰
this.btn_start.addEventListener(egret.TouchEvent.TOUCH_TAP, this.gameStart, this);//点击按钮开始游戏
this.addChild(this.btn_start);
  1. 运行效果如下

首页.png

游戏界面

准备工作
  1. 找一个宽敞的背景

bg_image.jpg

  1. 找一些奖品图标

2x4.png

  1. 找一些障碍物图片,树作为普通障碍物,大石头作为高能障碍物

1x2.png

代码走起
  1. 加载背景并使之可以滚动
//游戏背景
this.bg = new utils.BgMap(BeginGame.stageW,BeginGame.stageH);//创建可滚动的背景
this.bg.width = BeginGame.stageW;
this.addChild(this.bg);
this.bg.start();
  1. 随机降落奖品幸运物
//幸运物
this.Score = new match.Score();
//随机出现位置
this.Score.x = Math.random() * (BeginGame.stageW - this.Score.width);
this.Score.y = -this.Score.height - Math.random() * 300;
this.addChildAt(this.Score,this.numChildren - 1);
this.Scores.push(this.Score);
  1. 随机增加一些树木
//树木
this.Tree = new match.Tree(BeginGame.stageW,BeginGame.stageH);
this.Tree.x = Math.random() * (BeginGame.stageW - this.Tree.width);
this.Tree.y = -this.Tree.height - Math.random() * 300 ;
this.addChildAt(this.Tree,this.numChildren - 1);
this.Trees.push(this.Tree);
  1. 定时增加一些滚落的大石头
this.storn = utils.createBitmapByName("shitou");
this.storn.width = stageW / 2 - SnowmanW;
this.storn.x = Math.random() * (stageW - this.storn.width);
this.storn.y = -this.storn.height;
// 速度和偏移量可以自行设置
this.storn.y += speed * speedOffset;

石头的宽度设定为屏幕的一般并减去雪人的宽度,以便人物一定能躲闪过,初始y左边设置导屏幕外边。

  1. 增加碰撞检测
/**基于矩形的碰撞检测*/
public static hitTest(obj1:egret.DisplayObject,obj2:egret.DisplayObject):boolean {
    var rect1:egret.Rectangle = obj1.getBounds();
    rect1.height = rect1.height - 10;
    rect1.width = rect1.width -10;
    var rect2:egret.Rectangle = obj2.getBounds();
    rect2.height = rect2.height - 10;
    rect2.width = rect2.width -10;
    rect1.x = obj1.x - 5;
    rect1.y = obj1.y - 5;
    rect2.x = obj2.x - 5;
    rect2.y = obj2.y - 5;
    return rect1.intersects(rect2);
}
  1. 添加一个雪人
//滑雪者
this.Snowman = new match.Snowman(BeginGame.stageW, BeginGame.stageH);
this.Snowman.x = (BeginGame.stageW - this.Snowman.width) / 2;//居中定位
this.Snowman.y = (BeginGame.stageH - this.Snowman.height) / 1.2;//居中定位
this.addChild(this.Snowman);
// 雪人移动事件监听
this.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchHandler,this);
  1. 添加贞动画监听时间
this.addEventListener(egret.Event.ENTER_FRAME,this.gameViewUpdate,this);
  1. 游戏运行效果

微信截图_20220109164834.png

注意:背景的滚动本质上就是一张图不停的循环播放,所以应该尽量找一个带条纹的,这样可以更明显的看到滚动效果。幸运物,树木,大石头都应放到帧动画回调里。

未完成工作

  • 增加音乐
  • 增加不同难度的模式
  • 增加游戏结束界面

总结

总体来说用Egret来做一些简单的游戏,还是挺容易上手的,应付一些简单的运营活动也是搓搓有余的。而且最新的版本也支持发布到不同的小程序上,一套代码就支持h5和小程序,老板,还不加薪么?