上一篇我們介紹了如何用 HTML5 實現一輛可以移動的小坦克,本文則帶大家正式進入坦克大戰,喜歡 HTML5 的朋友可別錯過。
首先是 tank.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body onkeydown="getCommand();">
<h1>html5-經典的坦克大戰</h1>
<!--坦克大戰的戰場-->
<canvas id="tankMap" width="400px" height="300px" style="background-color:black"></canvas>
<span id="aa">數據</span>
<script type="text/javascript" src="tank.js"></script>
<script type="text/javascript">
//得到畫布
var canvas1=document.getElementById("tankMap");
//得到繪圖上下文(可以理解為畫筆)
var cxt=canvas1.getContext("2d");
//我的坦克 hero,參數為橫座標、縱座標、方向
var hero=new Hero(140,140,0,heroColor);
//定義一顆空子彈
var heroBullet=null;
//定義敵人的坦克,放在陣列中便於管理
var enemyTanks=new Array();
//先生成 3 個敵人坦克,後續可把數量做成變量
//0->上, 1->右, 2->下, 3->左
for(var i=0;i<3;i++){
var enemyTank=new EnemyTank((i+1)*50,0,2,enmeyColor);
enemyTanks[i]=enemyTank;
}
//先調用一次繪製
flashTankMap();
//定時刷新作戰區,把要在畫面出現的元素(我方坦克、敵方坦克、子彈、炸彈、障礙物等)繪製出來
function flashTankMap(){
cxt.clearRect(0,0,400,300);
drawTank(hero);
drawHeroBullet();
for(var i=0;i<3;i++){
drawTank(enemyTanks[i]);
}
}
//接受用戶按鍵的函數
function getCommand(){
var code=event.keyCode;
switch(code){
case 87://上
hero.moveUp();
break;
case 68:
hero.moveRight();
break;
case 83:
hero.moveDown();
break;
case 65:
hero.moveLeft();
break;
case 74:
hero.shotEnemy();
break;
}
flashTankMap();
}
//每隔 100 毫秒刷新一次作戰區
window.setInterval("flashTankMap()",100);
</script>
</body>
</html>
其次是 tank.js:
//定義兩個顏色陣列
var heroColor=new Array("#BA9658","#FEF26E");
var enmeyColor=new Array("#00A2B5","#00FEFE");
//子彈類
function Bullet(x,y,direct,speed){
this.x=x;
this.y=y;
this.direct=direct;
this.speed=speed;
this.timer=null;
this.isLive=true;
this.run=function run(){
//更新座標前先判斷是否到達邊界
if(this.x<=0||this.x>=400||this.y<=0||this.y>=300){
window.clearInterval(this.timer);
this.isLive=false;
}else{
switch(this.direct){
case 0:
this.y-=this.speed;
break;
case 1:
this.x+=this.speed;
break;
case 2:
this.y+=this.speed;
break;
case 3:
this.x-=this.speed;
break;
}
}
document.getElementById("aa").innerText="子彈x="+this.x+" 子彈y="+this.y;
}
}
//Tank 類
function Tank(x,y,direct,color){
this.x=x;
this.y=y;
this.speed=1;
this.direct=direct;
this.color=color;
this.moveUp=function(){
this.y-=this.speed;
this.direct=0;
}
this.moveRight=function(){
this.x+=this.speed;
this.direct=1;
}
this.moveDown=function(){
this.y+=this.speed;
this.direct=2;
}
this.moveLeft=function(){
this.x-=this.speed;
this.direct=3;
}
}
//Hero 類,透過對象冒充達到繼承效果
function Hero(x,y,direct,color){
this.tank=Tank;
this.tank(x,y,direct,color);
//射擊敵人坦克,子彈位置與方向有關
this.shotEnemy=function(){
switch(this.direct){
case 0:
heroBullet=new Bullet(this.x+9,this.y,this.direct,1);
break;
case 1:
heroBullet=new Bullet(this.x+30,this.y+9,this.direct,1);
break;
case 2:
heroBullet=new Bullet(this.x+9,this.y+30,this.direct,1);
break;
case 3:
heroBullet=new Bullet(this.x,this.y+9,this.direct,1);
break;
}
var timer=window.setInterval("heroBullet.run()",50);
heroBullet.timer=timer;
}
}
//EnemyTank 類,同樣透過對象冒充繼承 Tank
function EnemyTank (x,y,direct,color){
this.tank=Tank;
this.tank(x,y,direct,color);
}
//繪製我方子彈
function drawHeroBullet(){
if(heroBullet!=null&&heroBullet.isLive){
cxt.fillStyle="#FEF26E";
cxt.fillRect(heroBullet.x,heroBullet.y,2,2);
}
}
//繪製坦克,根據方向分情況處理
function drawTank(tank){
switch(tank.direct){
case 0: //上
case 2:// 下
cxt.fillStyle=tank.color[0];
cxt.fillRect(tank.x,tank.y,5,30);
cxt.fillRect(tank.x+15,tank.y,5,30);
cxt.fillRect(tank.x+6,tank.y+5,8,20);
cxt.fillStyle=tank.color[1];
cxt.arc(tank.x+10,tank.y+15,4,0,360,true);
cxt.fill();
cxt.strokeStyle=tank.color[1];
cxt.lineWidth=1.5;
cxt.beginPath();
cxt.moveTo(tank.x+10,tank.y+15);
if(tank.direct==0){
cxt.lineTo(tank.x+10,tank.y);
}else if(tank.direct==2){
cxt.lineTo(tank.x+10,tank.y+30);
}
cxt.closePath();
cxt.stroke();
break;
case 1: //右和左
case 3:
cxt.fillStyle=tank.color[0];
cxt.fillRect(tank.x,tank.y,30,5);
cxt.fillRect(tank.x,tank.y+15,30,5);
cxt.fillRect(tank.x+5,tank.y+6,20,8);
cxt.fillStyle=tank.color[1];
cxt.arc(tank.x+15,tank.y+10,4,0,360,true);
cxt.fill();
cxt.strokeStyle=tank.color[1];
cxt.lineWidth=1.5;
cxt.beginPath();
cxt.moveTo(tank.x+15,tank.y+10);
if(tank.direct==1){
cxt.lineTo(tank.x+30,tank.y+10);
}else if(tank.direct==3){
cxt.lineTo(tank.x,tank.y+10);
}
cxt.closePath();
cxt.stroke();
break;
}
}
這裡的核心思路是:用 setInterval 定時重化作戰區,只要每次刷新時子彈座標發生變化,肉眼看上去子彈就像在飛了。坦克則用基本的矩形、圓弧和直線繪製出來,並根據方向切換不同的繪製分支。