//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