HTML5坦克大战(1)绘制坦克
坦克尺寸如下:
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>坦克大战</title>
- </head>
- <body onkeydown="tankMove()">
- <canvas id="canvas" width="" height="" style="border:1px solid red; display:block; margin:50px auto;">浏览器不支持</canvas>
- <script type="text/javascript">
- var canvas = document.getElementById("canvas");
- var context = canvas.getContext("2d");
- //构造方法,创建一个坦克类
- function Tank(x, y, direct, speed) {
- this.x = x;
- this.y = y;
- this.direct = direct;
- this.speed = speed;
- this.moveUp = function () {
- this.y -= this.speed;
- }
- this.moveDown = function () {
- this.y += this.speed;
- }
- this.moveLeft = function () {
- this.x -= this.speed;
- }
- this.moveRight = function () {
- this.x += this.speed;
- }
- }
- var hero = new Tank(, , , );
- function drawTank(tank) {
- switch (tank.direct) {
- case :
- case :
- //向左,向右
- //清空画布
- context.clearRect(, , canvas.width, canvas.height);
- //开始画坦克
- //上轮
- context.fillStyle = "red";
- context.fillRect(hero.x, hero.y, , );
- context.fill();
- //下轮
- context.fillRect(hero.x, hero.y + , , );
- context.fill();
- //中间
- context.fillStyle = "green";
- context.fillRect(hero.x + , hero.y + , , );
- context.fill();
- //盖子
- context.beginPath()//加个开始,不然炮筒会粘连
- context.fillStyle = "blue";
- context.arc(hero.x + , hero.y + , , , * Math.PI);
- context.fill();
- context.closePath();
- //炮筒
- context.beginPath();
- context.strokeStyle = "black";
- context.lineWidth = "0.5";
- context.moveTo(hero.x + , hero.y + );
- if (tank.direct == ) {
- context.lineTo(hero.x, hero.y + );
- } else if (tank.direct == ) {
- context.lineTo(hero.x + , hero.y + );
- }
- context.stroke();
- context.closePath();
- break;
- case :
- case :
- //向上,向下
- //清空画布
- context.clearRect(, , canvas.width, canvas.height);
- //开始画坦克
- //左轮
- context.fillStyle = "red";
- context.fillRect(hero.x, hero.y, , );
- context.fill();
- //右轮
- context.fillRect(hero.x + , hero.y, , );
- context.fill();
- //中间
- context.fillStyle = "green";
- context.fillRect(hero.x + , hero.y + , , );
- context.fill();
- //盖子
- context.beginPath()//加个开始,不然炮筒会粘连
- context.fillStyle = "blue";
- context.arc(hero.x + , hero.y + , , , * Math.PI);
- context.fill();
- //炮筒
- context.beginPath();
- context.strokeStyle = "black";
- context.lineWidth = "0.5";
- context.moveTo(hero.x + , hero.y + );
- if (tank.direct == ) {
- context.lineTo(hero.x + , hero.y);
- } else if (tank.direct == ) {
- context.lineTo(hero.x + , hero.y + );
- }
- context.stroke();
- break;
- default:
- }
- }
- function tankMove() {
- switch (event.keyCode) {
- case ://左A
- hero.direct = ;
- hero.moveLeft();
- break;//不要忘记break!
- case ://右D
- hero.direct = ;
- hero.moveRight();
- break;
- case ://上W
- hero.direct = ;
- hero.moveUp();
- break;
- case ://下S
- hero.direct = ;
- hero.moveDown();
- break;
- //68 87 83
- default:
- }
- drawTank(hero);
- //alert(event.keyCode);
- }
- drawTank(hero);
- </script>
- </body>
- </html>
HTML5坦克大战(1)绘制坦克的更多相关文章
- 【 java版坦克大战--事件处理】 坦克动起来了
折腾了这么久,坦克总算能动了.只贴代码编辑不给上首页,花了半个小时的时间写了n多注释. 再顺便把绘图的原理发在这里: 绘图原理 Component类提供了两个和绘图有关的重要方法: ① paint ...
- cocos2d-x游戏开发系列教程-坦克大战游戏之坦克和地图碰撞的检测下
上篇我们完成了地图的信息获取和碰撞检测,这篇我们整合到程序中. 在这之前我们改造一下Tank类,使它更加模块化,共容易理解: 1.改造后的Tank类声明如下: class Tank : public ...
- cocos2d-x游戏开发系列教程-坦克大战游戏之坦克的显示
1.先定义坦克的一些属性 class Tank : public CCSprite { public : Tank(); ~Tank(); static Tank* createTankWithTan ...
- HTML坦克大战学习02---坦克动起来
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...
- Java坦克大战 (七) 之图片版
本文来自:小易博客专栏.转载请注明出处:http://blog.csdn.net/oldinaction 在此小易将坦克大战这个项目分为几个版本,以此对J2SE的知识进行回顾和总结,希望这样也能给刚学 ...
- HTML5-坦克大战一完成坦克上下左右移动的功能(一)
坦克大战一完成坦克上下左右移动的功能 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- HTML5坦克大战(2)绘制坦克复习
html代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head&g ...
- 小强的HTML5移动开发之路(7)——坦克大战游戏1
来自:http://blog.csdn.net/dawanganban/article/details/17693145 上一篇中我们介绍了关于Canvas的基础知识,用Canvas绘制各种图形和图片 ...
- HTML5坦克大战(韩顺平版本)
HTML5坦克大战(韩顺平版本) 2017-3-22 22:46:22 by SemiconductorKING 去年暑假学习了一下HTML5实现简单的坦克大战,觉得对JavaScript初学者来说, ...
随机推荐
- Tile based Rendering 二 tbr and tbdr 优化建议tiled based deferred rendering
http://www.seas.upenn.edu/~pcozzi/OpenGLInsights/OpenGLInsights-TileBasedArchitectures.pdf tbr 和tbdr ...
- humanoid ik unity 配件 animation的问题
遇到这样一个问题 想给角色设置ik 以实现代码控制的 更为自然的 角色动作 比如角色头跟随点击转动 身体也有相应扭转 https://docs.unity3d.com/Manual/InverseKi ...
- [置顶] (奇迹冬瓜)坦克大战[MFC框架]
经过二次整合 重新放出MFC框架下的坦克大战 采用小窗口 多线程 双缓冲 动画帧化 碰撞检测 接口封装 混音 事件延迟等 力求做到代码与美工的双向化 开场动画 界面一 界面二 游戏界面 结束动画 零积 ...
- 使用kubeadm部署Kubernetes v1.13.3
kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具. 这个工具能通过两条指令完成一个kubernetes集群的部署: 1. 安装要求 在开始之前,部署Kubernetes集群 ...
- [HTML5] Build Flexible HTML with HTMLTemplates using Slots and Web Components
HTMLTemplates are part of the web components specification. In this lesson we will learn what are HT ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-为什么无法打开官方范例的项目,打开tszip文件时提示尝试越过结尾怎么办
打开新的解决方案,找到tszip文件 提示错误Advanced Setting时越过结尾 到这里一般VS会卡死 但是我们已经可以得到解压出来的文件夹,其中包含PLC的完整项目文件夹,可以新 ...
- kindeditor 图片上传插件
富文本编辑器,kindeditor是比较好用的一款.需要的功能都有,文档.demo也详细.有什么功能去官网看一眼就好. 官网:http://kindeditor.net/ 一些好用的如图片上传,kin ...
- wamp server php环境绑定域名
思路: 用花生壳做动态域名解析 用wamp server 在本机上做一个 php web server; 这样就可以让一台内网电脑做一个测试用的服务器: 一:wamp server php环境绑定域名 ...
- 【DB2】If 'db2' is not a typo you can run the following command to lookup the package that contains the binary: command-not-found db2 bash: db2: command not found
数据库安装以后,db2报错如下: If 'db2' is not a typo you can run the following command to lookup the package that ...
- Spring MVC坑汇总+Stackoverflow巧解答
1.http://stackoverflow.com/questions/25598406/spring-annotaion-autowired-inside-methods Q: Autowire ...