HTML5實現(xiàn)經(jīng)典坦克大戰(zhàn)坦克亂走還能發(fā)出一個子彈
來源:易賢網(wǎng) 閱讀:1108 次 日期:2014-07-23 11:52:21
溫馨提示:易賢網(wǎng)小編為您整理了“HTML5實現(xiàn)經(jīng)典坦克大戰(zhàn)坦克亂走還能發(fā)出一個子彈”,方便廣大網(wǎng)友查閱!

代碼如下:

<pre name="code" class="html">tank.html</pre><pre name="code" class="html"><!DOCTYPE html>

<html>

<head>

<meta charset="utf-8"/>

</head>

<body onkeydown="getCommand();">

<h1>hmtl5-經(jīng)典的坦克大戰(zhàn)</h1>

<!--坦克大戰(zhàn)的戰(zhàn)場-->

<canvas id="tankMap" width="400px" height="300px" style="background-color:black"></canvas>

<span id="aa">數(shù)據(jù)</span>

<!--把tankGames.js引入到本頁面-->

<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;

//定義敵人的坦克(敵人的坦克有多少? 思路 : 是單個單個的定義,還是放在數(shù)組中?)

var enemyTanks=new Array();

//先死后活 ,定3個,后面我們把敵人坦克的數(shù)量,作出變量

//0->上, 1->右, 2->下 3->左

for(var i=0;i<3;i++){

//創(chuàng)建一個坦克

var enemyTank=new EnemyTank((i+1)*50,0,2,enmeyColor);

//把這個坦克放入數(shù)組

enemyTanks[i]=enemyTank;

}

//先調(diào)用一次

flashTankMap();

//專門寫一個函數(shù),用于定時刷新我們的作戰(zhàn)區(qū),把要在作戰(zhàn)區(qū)出現(xiàn)的元素(自己坦克,敵人坦克,子彈,炸彈,

//障礙物...)->游戲思想

function flashTankMap(){

//把畫布清理

cxt.clearRect(0,0,400,300);

//我的坦克

drawTank(hero);

//畫出自己的子彈

//子彈飛效果是怎么出現(xiàn)的?[答 : 首先我們應(yīng)該每隔一定時間(setInterval)就去刷新作戰(zhàn)區(qū),如果在刷新的時候,子彈坐標(biāo)變換了,給人的感覺就是子彈在飛!]

drawHeroBullet();

//敵人的坦克

//畫出所有敵人坦克

for(var i=0;i<3;i++){

drawTank(enemyTanks[i]);

}

}

//這是一個接受用戶按鍵函數(shù)

function getCommand(){

//我怎么知道,玩家按下的是什么鍵

//說明當(dāng)按下鍵后 事件--->event對象----->事件處理函數(shù)()

var code=event.keyCode;//對應(yīng)字母的ascii碼->我們看碼表

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;

}

//觸發(fā)這個函數(shù) flashTankMap();

flashTankMap();

//重新繪制所有的敵人的坦克.你可以在這里寫代碼(思想,我們干脆些一個函數(shù),專門用于定時刷新我們的畫布[作戰(zhàn)區(qū)])

}

//每隔100毫秒去刷新一次作戰(zhàn)區(qū)

window.setInterval("flashTankMap()",100);

</script>

</body>

</html></pre>

tank.js

代碼如下:

<pre></pre>

<pre name="code" class="html"><pre name="code" class="javascript">//為了編程方便,我們定義兩個顏色數(shù)組

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(){

//在該表這個子彈的坐標(biāo)時,我們先判斷子彈是否已經(jīng)到邊界

if(this.x<=0||this.x>=400||this.y<=0||this.y>=300){

//子彈要停止.

window.clearInterval(this.timer);

//子彈死亡

this.isLive=false;

}else{

//這個可以去修改坐標(biāo)

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類

//x 表示坦克的 橫坐標(biāo), y 表示縱坐標(biāo), direct 方向

function Hero(x,y,direct,color){

//下面兩句話的作用是通過對象冒充,達到繼承的效果

this.tank=Tank;

this.tank(x,y,direct,color);

//增加一個函數(shù),射擊敵人坦克.

this.shotEnemy=function(){

//創(chuàng)建子彈, 子彈的位置應(yīng)該和hero有關(guān)系,并且和hero的方向有關(guān).!!!

//this.x 就是當(dāng)前hero的橫坐標(biāo),這里我們簡單的處理(細化)

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;

}

//調(diào)用我們的子彈run, 50 是老師多次測試得到的一個結(jié)論.

var timer=window.setInterval("heroBullet.run()",50);

//把這個timer賦給這個子彈(js對象是引用傳遞!)

heroBullet.timer=timer;

}

}

//定義一個EnemyTank類

function EnemyTank (x,y,direct,color){

//也通過對象冒充,來繼承Tank

this.tank=Tank;

this.tank(x,y,direct,color);

}

//畫出自己的子彈,多說一句,你也可以把該函數(shù)封裝到Hero類中

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:// 下

//畫出自己的坦克,使用前面的繪圖技術(shù)

//設(shè)置顏色

cxt.fillStyle=tank.color[0];

//韓老師使用 先死--->后活 (初學(xué)者最好用這個方法)

//先畫出左面的矩形

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];

//設(shè)置線條的寬度

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:

//畫出自己的坦克,使用前面的繪圖技術(shù)

//設(shè)置顏色

cxt.fillStyle=tank.color[0];

//韓老師使用 先死--->后活 (初學(xué)者最好用這個方法)

//先畫出左面的矩形

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];

//設(shè)置線條的寬度

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;

}

}

</pre>

<pre></pre>

</pre>

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)頁制作
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)