1 //定义敌人和我们自己的坦克的颜色
2 var enemyColor = new Array("#0BB","#0FF");3 var heroColor = new Array("#dc0","#ff5");4 //封装一个公用的坦克父类
5 functionTank(x,y,direct){6 this.x =x;7 this.y =y;8 this.speed = 3;9 this.direct =direct;10 this.moveUp = function(){11 if (hero.y>0) {12 hero.y -=hero.speed;13 }14 hero.direct = 0;15 }16 this.moveRight = function(){17 if (hero.x+30<500) {18 hero.x +=hero.speed;19 }20 hero.direct = 1;21 }22 this.moveBottom = function(){23 if (hero.y+30<300) {24 hero.y +=hero.speed;25 }26 hero.direct = 2;27 }28 this.moveLeft = function(){29 if (hero.x>0) {30 hero.x -=hero.speed;31 }32 hero.direct = 3;33 }34 }35
36 //英雄坦克类
37 functionHero(x,y,direct,color){38 //将坦克类的构造方法赋给hero
39 this.hero =Tank;40 //调用,拥有坦克类的所有的属性和方法
41 this.hero(x,y,direct);42 this.color =color;43 this.direct =direct;44 this.isLive = true;45 this.shotEnemy = function(){46 switch(this.direct){47 case 0:48 heroBullet = new Bullet(this.x+9,this.y,this.direct);49 break;50 case 1:51 heroBullet = new Bullet(this.x+30,this.y+9,this.direct);52 break;53 case 2:54 heroBullet = new Bullet(this.x+9,this.y+30,this.direct);55 break;56 case 3:57 heroBullet = new Bullet(this.x,this.y+9,this.direct);58 break;59 }60 heroBullets.push(heroBullet);61 heroBullets[heroBullets.length-1].timer = window.setInterval("