搜索
查看: 8059|回复: 1

js游戏——射击(源码)

[复制链接]
发表于 2012-8-21 22:50:33 | 显示全部楼层 |阅读模式

<SCRIPT LANGUAGE="JavaScript">


// ---- Make array
function MakeArray( n){
     this.length = n;
     for (var i = 1; i <= n; i++) {
         this = 0
     }
     return this
}

// ----- poor man's random number
var d0 = new Date();
var r0 = d0.getSeconds();
function poor_rand(){
     d1 = new Date();
     r0 = (r0 * r0 + r0 + d1.getSeconds()) % 3721 ;
     return r0 % width_n;
}

// ---- Parameters of game
var height_n = 8;
var width_n =5;

// ---- state of game
var enemy = new MakeArray( width_n); // enemy positions
var wtime; // wait time
var score; // points
var gameover; // in case of gameover set to true

// ----- Make the battlefield.
document.write( "<FORM NAME='fm1'>");
document.write( "<INPUT TYPE='text' NAME='message' SIZE=20>");
document.write( "</FORM>");
document.write( "<FORM NAME='fm2'><TABLE>");
for( var i=0; i<height_n; i++){
     document.write( "<TR>");
     for( var j=0;j<width_n; j++){
         document.write( "<TD><CENTER><INPUT TYPE='radio'></CENTER></TD>");
     }
     document.write( "</TR>");
}
document.write( "<TR>");
for( var j=0;j<width_n; j++){
     document.write( "<TD><INPUT TYPE='button' VALUE='A' onClick='fire("
         + j + ")'></TD>");
}
document.write( "</TR>");
document.write( "</TABLE>");
document.write( "<INPUT TYPE='button' VALUE='START' onClick='game_start()'>");
document.write( "</FORM>");

// ----- game main timer event
// -----     enemy come one step
function come(){
     var n = poor_rand();
     document.fm2.elements[ width_n * enemy[n+1] + n].checked = true;
     enemy[n+1]++;
     if( enemy[n+1] < height_n){
         setTimeout("come()", wtime);
     }else{
       gameover = true;
       document.fm1.message.value =  "GAME OVER:" + score ;
     }
}

// ----- Fire the n th beam
function fire( n){
     if( gameover ) return;
     for( var i=0; i<enemy[n+1]; i++){ // clear enemy
         document.fm2.elements[ width_n * i + n].checked = false;
     }
     score += enemy[ n+1];
     document.fm1.message.value = "Points:" + score;
     enemy[ n+1] = 0;
     if( wtime > 50){ wtime -= 10};
}

// ---- initialize & start game
function game_start(){
    for( var n=0; n<width_n; n++){
         for( var i=0; i<enemy[n+1]; i++){
             document.fm2.elements[ width_n * i + n].checked = false;
         }
         enemy[n+1] = 0;
     }
     wtime = 150;
     score = 0;
     gameover = false;
     document.fm1.message.value = "Points:" + score;
     setTimeout("come()", wtime);
}

// -->

</SCRIPT>
<BODY >


射击.html
http://kuai.xunlei.com/d/OYOPHNDOVCOM
回复

使用道具 举报

发表于 2012-8-26 20:33:43 | 显示全部楼层
恩 不错  很简单的小游戏
回复

使用道具 举报

联系我们(Contact)|手机版|萝卜头IT论坛 ( 苏ICP备15050961号-1 )

GMT+8, 2024-12-23 09:10 , Processed in 0.085872 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表