HTML5坦克大战(2)绘制坦克复习
html代码:
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>坦克大战</title>
- <script src="tank.js"></script>
- </head>
- <body onkeydown="moveTank(hero)">
- <canvas id="canvas" width="" height="" style="border:1px solid red; display:block; margin: 50px auto; background-color:black;"></canvas>
- <script type="text/javascript">
- var canvas = document.getElementById("canvas");
- var context = canvas.getContext("2d");
- var hero = new Tank(, , , );
- //创建hero对象
- var hero = new Tank(, , , );
- function moveTank(tank) {
- //上下左右移动坦克
- switch (event.keyCode) {
- case ://左
- tank.direct = ;
- tank.moveLeft();
- break;
- case ://右
- tank.direct = ;
- tank.moveRight();
- break;
- case ://上
- tank.direct = ;
- tank.moveUp();
- break;
- case ://下
- tank.direct = ;
- tank.moveDown();
- break;
- default:
- }
- drawTank(hero);
- }
- drawTank(hero);
- </script>
- </body>
- </html>
JavaScript代码:
- 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;
- }
- }
- function drawTank(tank) {
- //画坦克
- switch (tank.direct) {
- case :
- case :
- //向上,向下
- //清屏
- context.clearRect(, , canvas.width, canvas.height);
- //画坦克
- //画轮子和身体
- context.beginPath();
- context.fillStyle = "red";
- context.fillRect(tank.x, tank.y, , );//左轮
- context.fillRect(tank.x + , tank.y + , , );//身体
- context.fillRect(tank.x + , tank.y, , );//右轮
- context.fill();
- context.closePath();
- //画脑袋
- context.beginPath();
- context.fillStyle = "blue";
- context.arc(tank.x + , tank.y + , , , * Math.PI);
- context.fill();
- context.closePath();
- //画炮筒
- context.beginPath();
- context.strokeStyle = "yellow";
- context.lineWidth = ;
- context.moveTo(tank.x + , tank.y + );
- if (tank.direct == ) {
- context.lineTo(tank.x + , tank.y);
- } else if (tank.direct == ) {
- context.lineTo(tank.x + , tank.y + );
- }
- context.stroke();
- context.fill();
- context.closePath();
- break;
- case :
- case :
- //向左,向右
- //清屏
- context.clearRect(, , canvas.width, canvas.height);
- //画坦克
- //画轮子和身体
- context.beginPath();
- context.fillStyle = "red";
- context.fillRect(tank.x, tank.y, , );//左轮
- context.fillRect(tank.x + , tank.y + , , );//身体
- context.fillRect(tank.x, tank.y + , , );//右轮
- context.fill();
- context.closePath();
- //画脑袋
- context.beginPath();
- context.fillStyle = "blue";
- context.arc(tank.x + , tank.y + , , , * Math.PI);
- context.fill();
- context.closePath();
- //画炮筒
- context.beginPath();
- context.strokeStyle = "yellow";
- context.lineWidth = ;
- context.moveTo(tank.x + , tank.y + );
- if (tank.direct == ) {
- context.lineTo(tank.x + , tank.y + );
- } else if (tank.direct == ) {
- context.lineTo(tank.x, tank.y + );
- }
- context.stroke();
- context.fill();
- context.closePath();
- break;
- default:
- }
- }
HTML5坦克大战(2)绘制坦克复习的更多相关文章
- 【 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坦克大战(1)绘制坦克
坦克尺寸如下: <!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初学者来说, ...
随机推荐
- 理解PHP的变量,值与引用的关系
--- title: 理解PHP的变量,值与引用的关系 createdDate: 2015-03-11 category: php --- PHP的变量与C++中的变量是两种截然不容的概念.如果没有理 ...
- arithmetic-slices
https://leetcode.com/problems/arithmetic-slices/ public class Solution { public int numberOfArithmet ...
- FPGA作为从机与STM32进行SPI协议通信---Verilog实现
一.SPI协议简要介绍 SPI,是英语Serial Peripheral Interface的缩写,顾名思义就是串行外围设备接口.SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用 ...
- 安装64位office时提示已安装32位的office
运行 regedit,进入到HKEY_CLASSES_ROOT\Installer\Products下,删除0000510开头的项,没有00005我把00002....的删了也可以
- HTTP状态码及说明
- scala类型系统 type关键字
和c里的type有点像. scala里的类型,除了在定义class,trait,object时会产生类型,还可以通过type关键字来声明类型. type相当于声明一个类型别名: scala> t ...
- zabbix_LAMP源码安装
Zabbix源码包安装 Cenos5.3 Basic server 安装顺序 Libxml2 Libmcrypt Zlib Libpng Jpeg:需要创建目录jpeg /bin /lib /incl ...
- html中锚点的应用【本页面跳转】
设置锚点 <a name="top"></a> 同页跳转 <a href="#top">返回顶部</a> 不同页 ...
- PHP上传文件功代码练习(单文件)
前端: <html> <head><title>upload file</title> <meta http-equiv="Conten ...
- Android File类 根据官方文档理解
File有四个构造函数 public File(File dir,String name) 参数为File和String,File制定构造的新的File对象的路径 ...