HTML5+javascript韩顺平坦克大战游戏教程笔记

//css设置 body{padding: 0;margin:0;} .main{width: 510px;height: 510px;margin: 20px auto;position: relative;} #canvas{box-shadow: 2px 2px 2px 3px blue;} #aa{position: absolute;right: -200px;} //html结构
您的浏览器不支持html5 数据
        坦克类的构造,属性有坐标xy,方向direct,行驶速度speed,车身颜色color,考虑到坦克控制,可以把按键运动函数封装到坦克类中,所以可以构造出一个给玩家坦克和敌人坦克共同的父类Tank,减少代码重复
        该类中,坦克运动则根据按键,分别改变坦克坐标,然后分别给方向direct赋值0--3

//抽象一个Tank类
function Tank(x,y,direct,color){
this.x=x;
this.y=y;
this.direct=direct;
this.speed=1;
this.color=color;

    this.moveUp=function(){
        this.y-=this.speed;
        this.direct=0;
    }
    this.moveRight=function(){
        this.x+=this.speed; 
        this.direct=1;
    }
    this.moveDown=function(){
        this.y+=this.speed;
        this.direct=2;
    }
    this.moveLeft=function(){
        this.x-=this.speed;
        this.direct=3;
    }

}
        既然出现Tank父类,那么玩家坦克类Hero就要继承Tank,javascript完成继承机制,采用对象冒充实现。
        简单例子

//父类Animal
function Animal(age,name){
this.age=age;
this.name=name;
}
//子类dog
function Dog(age,name){
//对象冒充
this.Animal_Dog=Animal;
t

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值