首页 > 语言 > JavaScript > 正文

javascript制作坦克大战全纪录(2)

2024-05-06 14:49:54
字体:
来源:转载
供稿:网友

2.   完善地图

    我们的地图中有空地,墙,钢,草丛,水,总部等障碍物。 我们可以把这些全部设计为对象。

2.1  创建障碍物对象群

    对象群保存各种地图上的对象,我们通过对象的属性来判断对象是否可以被穿过或被攻击。
 
Barrier.js:
 
代码如下:
 // 障碍物基类对象,继承自TankObject
 Barrier = function () {
     this.DefenVal = 1;  // 防御力
     this.CanBeAttacked = true;  // 是否可以被攻击
 }
 Barrier.prototype = new TankObject();
 // 墙
 WallB = function () { }
 WallB.prototype = new Barrier();
 // 空地
 EmptyB = function () {
     this.CanAcross = true;  // 可被穿过
 }
 EmptyB.prototype = new Barrier();
 // 河流
 RiverB = function () {
     this.DefenVal = 0;
     this.CanBeAttacked = false; // 优先取对象的成员,继承自父类的会被覆盖。
 }
 RiverB.prototype = new Barrier();
 // 钢
 SteelB = function () {
     this.DefenVal = 3;
 }
 SteelB.prototype = new Barrier();
 // 草丛对象
 TodB = function () {
     this.CanBeAttacked = false;
     this.DefenVal = 0;
     this.CanAcross = true;
 }
 TodB.prototype = new Barrier();
 // 总部
 PodiumB = function () {
     this.DefenVal = 5;
 }
 PodiumB.prototype = new Barrier();

2.2    写入地图的数据。

在Common.js 中添加以下代码:
 
代码如下:
 //地图元素类型枚举
 /*
 0:空地   
 1:墙   
 2:钢   
 3:树丛       
 4:河       
 5:总部   
 */
 var EnumMapCellType = {
     Empty: "0"
     , Wall: "1"
     , Steel: "2"
     , Tod: "3"
     , River: "4"
     , Podium: "5"
 };
 // 每个地形对应的样式名称
 var ArrayCss = ['empty', 'wall', 'steel', 'tod', 'river', 'podium'];
 // 关卡地图
 /*关卡*/
 var str = '0000000000000';
 str += ',0011100111010';
 str += ',1000010000200';

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选