例子:web版坦克大战1.0
2015-09-22 13:39
537 查看
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>坦克大战</title> </head> <body onkeydown="hero.move(event)"> <div id="filed" style="background:#000;width:500px;height:400px;position:absolute;"></div> <div id="mytank" style="background:url(w.png) no-repeat;width:40px;height:40px;position:absolute;"></div> <input type="button" value="上"> <input type="button" value="右" > <input type="button" value="下" > <input type="button" value="左"> <script type="text/javascript"> // function change(obj){ // if(obj.value=="上"){ // tank.style.backgroundPositionY="0px"; // }else if(obj.value=="右"){ // tank.style.backgroundPositionY="-40px"; // } // else if(obj.value=="下"){ // tank.style.backgroundPositionY="-80px"; // } // else if(obj.value=="左"){ // tank.style.backgroundPositionY="-120px"; // } // } //用面向对象的方法开发,web版本的坦克大战,可以通过asdw键来控制坦克的走向 function MyTank(x,y,direct){ this.x=x;//坦克的横坐标 this.y=y;//坦克的纵坐标 this.speed=3;//速度 //初始化 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 switch (event.keyCode){ case 65: //a表示左 this.x-=this.speed; this.direct=3; mytank.style.backgroundPositionY="-120px"; break; case 83: //S表示向下 2 this.y+=this.speed; this.direct=2; mytank.style.backgroundPositionY="-80px"; break; case 68: //d表示右 1 this.x+=this.speed; this.direct=1; mytank.style.backgroundPositionY="-40px"; break; case 87: //w表示上 0 this.y-=this.speed; this.direct=0; mytank.style.backgroundPositionY="0px"; break; } //统一改变位置 mytank.style.left=this.x+"px"; mytank.style.top=this.y+"px"; } } //创建坦克 var hero=new MyTank(200,360,0) </script> </body> </html>
相关文章推荐
- [9-13]Shell系列6――取算数运算数值
- VS 2005 创建DLL工程,并进行调用与调试例子
- Python脚本07 —— 替换模板中的七牛域名
- GNU C __attribute__
- hadoop集群模式下导入数据到hbase上报错
- 哈希表
- C语言与C++中点运算符与箭头运算符的区别
- Effective C++——条款51(第8章)
- ElasticSearch分片分布规则配置
- 【用户界面:Android-ConvenientBanner】功能演示
- (乙)1006. 换个格式输出整数
- spring thymeleaf 中文乱码
- spring mvc学习笔记(一)web.xml文件配置的一点重要信息
- Python脚本06 —— 批量上传图片到七牛服务器
- qml学习-------------Slider(滑块)
- No EditorOptionDefinition Export Found Error的问题
- 三维气象要素场--Unity3d篇
- Nginx负载均衡配置实例详解
- IIS建立.net framework4 应用程序池HTTP 错误 500.21 - Internal Server Error处理程序“NickLeeCallbackHandler”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- iOS 集成银联支付(绕过文档的坑,快速集成)-转