日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php 代码坦克,HTML5实现经典坦克大战坦克乱走还能发出一个子弹_html5教程技巧

發(fā)布時(shí)間:2023/12/10 php 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 代码坦克,HTML5实现经典坦克大战坦克乱走还能发出一个子弹_html5教程技巧 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

//為了編程方便,我們定義兩個(gè)顏色數(shù)組

var heroColor=new Array("#BA9658","#FEF26E");

var enmeyColor=new Array("#00A2B5","#00FEFE");

//其它的敵人坦克,這里的擴(kuò)展性,還是不錯(cuò)的.

//子彈類

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

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

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

//子彈要停止.

window.clearInterval(this.timer);

//子彈死亡

this.isLive=false;

}else{

//這個(gè)可以去修改坐標(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;

}

}

//這是一個(gè)Tank類

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

this.x=x;

this.y=y;

this.speed=1;

this.direct=direct;

//一個(gè)坦克,需要兩個(gè)顏色.

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;

}

}

//定義一個(gè)Hero類

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

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

//下面兩句話的作用是通過對(duì)象冒充,達(dá)到繼承的效果

this.tank=Tank;

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

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

this.shotEnemy=function(){

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

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

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 是老師多次測(cè)試得到的一個(gè)結(jié)論.

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

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

heroBullet.timer=timer;

}

}

//定義一個(gè)EnemyTank類

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

//也通過對(duì)象冒充,來繼承Tank

this.tank=Tank;

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

}

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

function drawHeroBullet(){

//這里,我們加入了一句話,但是要知道這里加,是需要對(duì)整個(gè)程序有把握

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é)者最好用這個(gè)方法)

//先畫出左面的矩形

cxt.fillRect(tank.x,tank.y,5,30);

//畫出右邊的矩形(這時(shí)請(qǐng)大家思路->一定要一個(gè)參照點(diǎn))

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é)者最好用這個(gè)方法)

//先畫出左面的矩形

cxt.fillRect(tank.x,tank.y,30,5);

//畫出右邊的矩形(這時(shí)請(qǐng)大家思路->一定要一個(gè)參照點(diǎn))

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;

}

}

總結(jié)

以上是生活随笔為你收集整理的php 代码坦克,HTML5实现经典坦克大战坦克乱走还能发出一个子弹_html5教程技巧的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。