坦克大战

用面向对象的方法设计坦克大战,以下只是一个小框架,实现一些基本功能。

<!DOCTYPE html>
<html>
<head>
</head>
<body onkeydown="dosomething(event)"> 
<div id="filed" style="background-color:black;width:500px;height:400px;position:absolute">
<div id="mytank" style="background-position-y:-40px;background-image:url('itank.gif');width:40px;height:40px;positon:absolute"></div>
</div>
<script language="javascript" type="text/javascript">
//用面向对象的方法开发坦克大战
function MyTank(x,y,direct){
    this.x=x; //坦克的横坐标
    this.y=y; //坦克的纵坐标
    this.direct=direct;//方向
    this.speed=1;//速度
    //初始化
    mytank.style.left=this.x+"px";
    mytank.style.top=this.y+"px";
    mytank.style.backgroundPositionY="0px";
    //event表示按键事件
    this.move=function move(event){
        //a表示向左 3
        //s表示向下 2
        //d表示向右 1
        //w表示向上 0
        switvh(event.keyCode){
            case 65:
                //a表示左
                this.x-=this.speed;
                this.direct=3;
                mytank.style.backgroundPositionY="40px";
                break;
            case 83:
                //s表示下
                this.y+=this.speed;
                this.direct=2;
                mytank.style.backgroundPositionY="80px";
                break;
            case 68:
                //d表示右
                this.x+=this.speed;
                this.direct=1;
                mytank.style.backgroundPositionY="120px";
                break;
            case 87:
                //w表示上
                this.y-=this.speed;
                this.direct=0;
                mytank.style.backgroundPositionY="0px";
                break;              
        }
        //统一改变位置
        mytank.style.left=this.x+"px";
        mytank.style.top=this.y+"px";       
    }

}
//判断用户想干什么
        function dosomething(event){
                if(event.keyCode==65||event.key==68||event.keyCode==83||event.keyCode==87)
                    hero.move(event);
        }
//创建坦克
var hero=new MyTank(200,360,0);
</script>
</body>
</html>

























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值