heli.htm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <html>
  2. <head>
  3. <script language="JavaScript">
  4. var speed = 2;
  5. var scroll_speed = 2;
  6. var clicked = false;
  7. var run = false;
  8. var blocks = new Array('block_0', 'block_1', 'block_2', 'block_3');
  9. var crash = false;
  10. var score = 0;
  11. var highscore = 0;
  12. var acc = 1;
  13. var block_w = 30;
  14. var block_max_h = 100;
  15. var block_min_h = 10;
  16. var table_x = 200;
  17. var table_y = 100;
  18. var table_w = 460;
  19. var table_h = 360;
  20. var heli_x = table_x + table_w / 2;
  21. var heli_y = table_y + table_h / 2;
  22. var heli_w = 50;
  23. var heli_h = 25;
  24. var heli_border = 3;
  25. var border = 40;
  26. function init()
  27. {
  28. for(i = 0; i < blocks.length; i++)
  29. {
  30. document.getElementById(blocks[i]).x = table_x + (table_w / blocks.length * i);
  31. document.getElementById(blocks[i]).y = table_y;
  32. document.getElementById(blocks[i]).w = block_w;
  33. document.getElementById(blocks[i]).h = 0;
  34. document.getElementById(blocks[i]).style.backgroundColor = "#00ff00";
  35. }
  36. document.getElementById('table').style.left = table_x;
  37. document.getElementById('table').style.top = table_y;
  38. document.getElementById('table').style.width = table_w;
  39. document.getElementById('table').style.height = table_h;
  40. document.getElementById('border').style.left = table_x-border/2.;
  41. document.getElementById('border').style.top = table_y-border/2.;
  42. document.getElementById('border').style.width = table_w-border;
  43. document.getElementById('border').style.height = table_h-border;
  44. document.getElementById('border').style.borderWidth = border;
  45. document.getElementById('score').style.left = table_x + border;
  46. document.getElementById('score').style.top = table_y + border/2.;
  47. document.getElementById('highscore').style.left = table_x + border;
  48. document.getElementById('highscore').style.top = table_y + border/2. + 15;
  49. if(highscore < score)
  50. highscore = score;
  51. document.getElementById('highscore').innerHTML = "Highscore: " + highscore;
  52. score = 0;
  53. heli_x = table_x + table_w / 2;
  54. heli_y = table_y + table_h / 2;
  55. document.getElementById('heli').style.left = heli_x;
  56. document.getElementById('heli').style.top = heli_y;
  57. }
  58. function main_loop()
  59. {
  60. if(clicked)
  61. heli_y -= speed * acc;
  62. else
  63. heli_y += speed * acc;
  64. for(i = 0; i < blocks.length; i++)
  65. {
  66. if(document.getElementById(blocks[i]).x < table_x)
  67. {
  68. document.getElementById(blocks[i]).x = (table_w + table_x) - document.getElementById(blocks[i]).w;
  69. document.getElementById(blocks[i]).y = (Math.random() * table_h) + table_y;
  70. document.getElementById(blocks[i]).h = Math.random() * block_max_h;
  71. if(document.getElementById(blocks[i]).h < block_min_h)
  72. document.getElementById(blocks[i]).h = block_min_h;
  73. if((document.getElementById(blocks[i]).h + document.getElementById(blocks[i]).y) > (table_h + table_y))
  74. document.getElementById(blocks[i]).h = (table_h + table_y) - document.getElementById(blocks[i]).y;
  75. }
  76. else
  77. document.getElementById(blocks[i]).x -= scroll_speed;
  78. document.getElementById(blocks[i]).style.top = document.getElementById(blocks[i]).y;
  79. document.getElementById(blocks[i]).style.left = document.getElementById(blocks[i]).x;
  80. document.getElementById(blocks[i]).style.height = document.getElementById(blocks[i]).h;
  81. document.getElementById(blocks[i]).style.width = document.getElementById(blocks[i]).w;
  82. }
  83. document.getElementById('heli').style.top = heli_y;
  84. document.getElementById('heli').style.left = heli_x;
  85. if(check_collision())
  86. {
  87. run = false;
  88. crash = true;
  89. }
  90. else
  91. {
  92. run = true;
  93. score++;
  94. if(acc < 2)
  95. acc += 0.10;
  96. document.getElementById('score').innerHTML = "Score: "+score;
  97. window.setTimeout("main_loop()", 16);
  98. }
  99. }
  100. function check_collision()
  101. {
  102. if(heli_y < table_y || (heli_y + heli_h) >= (table_y + table_h))
  103. return true
  104. for(i = 0; i < blocks.length; i++)
  105. {
  106. if((heli_x + heli_w - heli_border) >= document.getElementById(blocks[i]).x && heli_x + heli_border <= (document.getElementById(blocks[i]).x+document.getElementById(blocks[i]).w))
  107. x_collision = true;
  108. else
  109. x_collision = false;
  110. if((heli_y + heli_h - heli_border) >= document.getElementById(blocks[i]).y && heli_y + heli_border <= (document.getElementById(blocks[i]).y+document.getElementById(blocks[i]).h))
  111. y_collision = true;
  112. else
  113. y_collision = false;
  114. if(x_collision && y_collision)
  115. {
  116. document.getElementById(blocks[i]).style.backgroundColor = "#ff0000";
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. function fly_up(event)
  123. {
  124. if(document.all)
  125. {
  126. if(window.event.keyCode == 27 && !run)
  127. {
  128. init();
  129. run = true;
  130. crash = false;
  131. main_loop();
  132. }
  133. }
  134. else
  135. {
  136. if(event.which == 27 && !run)
  137. {
  138. init();
  139. run = true;
  140. crash = false;
  141. main_loop();
  142. }
  143. }
  144. if(!clicked)
  145. {
  146. clicked = true;
  147. acc = 0;
  148. }
  149. if(!run && !crash)
  150. main_loop();
  151. }
  152. function fly_down()
  153. {
  154. acc = 0;
  155. clicked = false;
  156. }
  157. document.onmousedown = fly_up;
  158. document.onmouseup = fly_down;
  159. document.onkeydown = fly_up;
  160. document.onkeyup = fly_down;
  161. </script>
  162. </head>
  163. <body bgcolor="#ffffff" onload="init();">
  164. Press any key to fly up, release to drop down. <br/>
  165. ESC resets the game
  166. <div id="table" style="position: absolute; background-color: #000000;"> </div>
  167. <div id="heli" style="position: absolute;"><img src="heli.gif"></div>
  168. <div id="block_0" style="position: absolute; overflow: hidden; background-color: #00ff00;"></div>
  169. <div id="block_1" style="position: absolute; overflow: hidden; background-color: #00ff00;"></div>
  170. <div id="block_2" style="position: absolute; overflow: hidden; background-color: #00ff00;"></div>
  171. <div id="block_3" style="position: absolute; overflow: hidden; background-color: #00ff00;"></div>
  172. <div id="score" style="position: absolute; color: #000000; background-color: #0000ff;">Score: 0</div>
  173. <div id="highscore" style="position: absolute; color: #000000; background-color: #0000ff;">Highscore: 0</div>
  174. <div id="border" style="position: absolute; background-color: transparent; border-color: gray; border-style: solid;"></div>
  175. </body>
  176. </html>