闲暇之余写的坦克小游戏,源码贴出来,有意见和疑问请留言,共同进步
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
var missleno=0;
var energy=100;
var enemytimer;
function document.onkeydown()
{
if (window.event.keyCode==37) // 方向键 <-
{
var temp=document.getElementById("sg");
temp.style.pixelLeft-=10;
}
else if (window.event.keyCode==39) // 方向键 ->
{
var temp=document.getElementById("sg");
temp.style.pixelLeft+=10;
}
else if (window.event.keyCode==40){
var temp=document.getElementById("sg");
temp.style.pixelTop+=10;
// temp.style.height=66px;
}
else if(window.event.keyCode==38){ // up ^
//alert(window.event.keyCode);
var temp=document.getElementById("sg");
temp.style.pixelTop-=10;
}
else if(window.event.keyCode==32){
fire(missleno);
missleno++;
}
// else{
// var temp=document.getElementById("missle1000");
// if(temp==null){
// alert('null');
// }
//}
}
function fire(mno){
var no=mno;
var temp=document.getElementById("sg");
tt=document.createElement("div");
cont=document.createTextNode("=");
tt.appendChild(cont);
tt.id="missle"+no;
// missleno++;
tt.style.position="absolute";
// var obj=eval("sg"+num);
tt.style.pixelTop=temp.style.pixelTop+15;
tt.style.pixelLeft=temp.style.pixelLeft+60;
document.body.appendChild(tt);
move(no);
}
function move(mno){
var no=mno;
var temp=document.getElementById("missle"+no);
// var temphn=document.getElementById("hn");
temp.style.pixelLeft+=5;
var result=monitor_missle(no); //监控子弹是否打中
//alert(result);
if(result!='stop'){
if(temp.style.pixelLeft!=1000){
setTimeout("move("+no+")",20);
}
else if(temp.style.pixelLeft==1000){ //消除子弹
temp.parentNode.removeChild(temp);
}
}
}
function showpos(){
var temp=document.getElementById("hn");
alert(temp.style.pixelLeft);
}
function flush(){
var temp=document.getElementById("hn");
//temp.parentNode.removeChild(temp);
temp.style.display="none";
temp=document.getElementById("missle"+missleno);
temp.parentNode.removeChild(temp);
}
function initEnemy(){
tt=document.createElement("div");
cont=document.createTextNode("enemy");
tt.appendChild(cont);
tt.id="enemy";
tt.style.position="absolute";
// var obj=eval("sg"+num);
tt.style.pixelTop=390;
tt.style.pixelLeft=900;
tt.style.border="dashed thick #008022";
tt.style.width="36px";
tt.style.padding="2px 4px 0px 4px";
tt.style.background="#ff3303";
tt.style.color="#FFFFEE";
document.body.appendChild(tt);
enemy_move();
}
function monitor(){
var enemy=document.getElementById("enemy");
for(var t=0;t<missleno;t++){
var temp=document.getElementById("missle"+t);
if(temp==null)
continue;
else{
if(((enemy.style.pixelTop-10)<temp.style.pixelTop) &&((enemy.style.pixelTop+10)>temp.style.pixelTop) && (temp.style.pixelLeft>=enemy.style.pixelLeft)){
energy--;
temp.parentNode.removeChild(temp);
alert(energy);
}
}
}
}
function monitor_missle(mno){
var t=mno;
var enemy=document.getElementById("enemy");
var temp=document.getElementById("missle"+t);
if((temp!=null)&&(enemy!=null)){
if(((enemy.style.pixelTop-10)<temp.style.pixelTop) &&((enemy.style.pixelTop+10)>temp.style.pixelTop) && (temp.style.pixelLeft>=enemy.style.pixelLeft)){
energy--;
var blood=document.getElementById("bld");
blood.innerHTML=energy;
temp.parentNode.removeChild(temp);
if(energy==0){
enemy.parentNode.removeChild(enemy);
clearTimeout(enemytimer);
// enemy.style.display="none";
alert("YOU WIN,CONGRATULATIONS!");
}
return 'stop';
}
}
return 'continue';
}
function enemy_move(){
var enemy=document.getElementById("enemy");
enemy.style.pixelLeft-=10;
enemytimer=setTimeout("enemy_move()",1000);
}
//-->
</SCRIPT>
<BODY>
<div style="border: dashed thick #008000; width:36px; text-align:center; padding:2px 4px 0px 4px; background:#ff3300; color:#FFFFFF;position:absolute" id="sg" >sugo</div>
<!-- <div style="border: dashed thick #008000; width:36px; text-align:center; padding:2px 4px 0px 4px; background:#ff3300; color:#FFFFFF;position:absolute;left=1000;top=500" id="hn">hanna</div> -->
<SCRIPT LANGUAGE="JavaScript">
<!--
initEnemy();
// setInterval("monitor()",500);
//-->
</SCRIPT>
<div id="bld"></div>
</BODY>
</HTML>