javascript 制作坦克大战游戏初步 图片与代码
学了一阵子的javascript,该做点东西,虽然东西还是东拼西凑,见笑。方向键控制坦克移动,回车键发射炸弹。其他的功能敌方坦克那些还没写。
javascript坦克游戏初步|阿会楠练习作品
body{
background:black;
}
#tanke{
position:absolute;
left:500px;
top:200px;
}
var i = 0;
var bombLeftArray = new Array(100);//存放炸弹信息x
var bombTopArray = new Array(100);//存放炸弹信息y
var bombDirctionArray = new Array(100);//存放炸弹方向
var t = new Array(100);//炸弹加速度
for(var t_ = 0;t_ 0){
allBomb[j_].style.top = bombTopArray[j_] - bombsetup*t[j_];
t[j_] ++;
}
else
{
allBomb[j_].style.display = "none";
}
break;
case "zbottom.gif":
bombLeftArray[j_] = allBomb[j_].offsetLeft;
bombTopArray[j_] = allBomb[j_].offsetTop;
if(bombTopArray[j_] 0)
{
allBomb[j_].style.left = bombLeftArray[j_] - bombsetup*t[j_];
t[j_] ++;
}
else
{
allBomb[j_].style.display = "none";
}
break;
case "zright.gif":
bombLeftArray[j_] = allBomb[j_].offsetLeft;
bombTopArray[j_] = allBomb[j_].offsetTop;
if(bombLeftArray[j_] ";
break;
//向右
case 39:
$("tanke").style.left = current_x + document.body.scrollLeft + step + "px";
$("tanke").innerHTML = "";
break;
//向上
case 38:
$("tanke").innerHTML = "";
$("tanke").style.top = current_y + document.body.scrollTop - step + "px";
break;
//向下
case 40:
$("tanke").innerHTML = "";
$("tanke").style.top = current_y + document.body.scrollTop + step + "px";
break;
//发出攻击
case 13:
//新子弹
var createDiv = document.createElement("div");
createDiv.setAttribute("id","bomb" + i);
i ++;
//createDiv.style.overflow = "hidden";
createDiv.style.position = "absolute";
var reg = /[a-z]{1,}\.gif/;//匹配只要找到图片的名字就可以了
var current_decoration = $("tanke").innerHTML.match(reg);
switch(current_decoration[0])//按下回车发弹
{
//炮筒对着左边
case "left.gif":
createDiv.style.left = current_x - 5 + "px";
createDiv.style.top = current_y + (tanke_h/2) - 3 + "px";
createDiv.innerHTML = ""
break;
//炮筒对着右边
case "right.gif":
createDiv.style.left = current_x + tanke_w - 7 + "px";
createDiv.style.top = current_y + (tanke_h/2) - 1 + "px";
createDiv.innerHTML = "";
break;
//炮筒对着上边
case "top.gif":
createDiv.style.left = current_x + (tanke_w/2) - 5 + "px";
createDiv.style.top = current_y - 5 + "px";
createDiv.innerHTML = "";
break;
//炮筒对着下边
case "bottom.gif":
createDiv.style.left = current_x + (tanke_w/2) - 7 + "px";
createDiv.style.top = current_y + tanke_h - 7 + "px";
createDiv.innerHTML = "";
break;
}
document.body.appendChild(createDiv);
break;
}
}
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]