[jscript]坦克大战游戏方向的实现和思路
这个代码是使用jscript面向对象编程的思路实现了坦克的上下左右移动和视觉效果
主要是一个gif动态图片封了四个上下左右的坦克 利用css实现 这个很简单就不说了
再相应键盘的事件进行执行程序
我们可以在此model进行延伸可以让坦克发送子弹,子弹碰到边界就会消失以及显示几个敌人的坦克,击中敌人的坦克 坦克爆炸(贞图片迅速切换后)敌人目标消失
//面向对象
function Wall(){
}
function Bullet(){
}
function Water(){
}
function Hero(x,y){
this.x=x;
this.y=y;
this.speed=1;
this.direct=0;//0 -》上 1->右 2->下 3->左
this.move_up=function(){
this.y-=this.speed;
myhero.style.backgroundPositionY="0px";
myhero.style.top=this.y+"px";
}
this.move_down=function(){
this.y+=this.speed;
myhero.style.backgroundPositionY="80px";
myhero.style.top=this.y+"px";
}
this.move_left=function(){
//首先让坦克转向
this.x-=this.speed;
myhero.style.backgroundPositionY="40px";
myhero.style.left=this.x+"px";
}
this.move_right=function(){
this.x+=this.speed;
myhero.style.backgroundPositionY="120px";
myhero.style.left=this.x+"px";
}
}
//创建自己的坦克
var hero=new Hero(100,250);
function sendCommand(){
switch(window.event.keyCode){
case 65:
hero.move_left(); //自己坦克向左
break;
case 68:
hero.move_right();
break;
case 83:
hero.move_down();
break;
case 87:
hero.move_up();
break;
//响应用户按j这个键
case 74:
//发送子弹的行为封装到fire
hero.fire();
break;
default:
break;
}
}