SHARE
TWEET

lab1

a guest Sep 16th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3.  
  4. <script language="JavaScript" type="text/JavaScript">
  5. <!--
  6.  
  7. var moving_timer, count_down_timer; // Declare the two timers
  8. var score = 0;              // To store the score of the player
  9. var time_remaining = 10;        // To store the time remaining
  10. var monster_position=0;         // To store the position of the monster
  11. var finished=false;         // Indicate whether the game is finished or not
  12. var hitSound, missSound;        // Declare the two sounds
  13. var difficulty;
  14. var difficulty2=1;
  15. var froze=false;
  16.  
  17. var radios;
  18.  
  19. function game_start(){
  20.    
  21. difficulty = document.getElementsByName('diffs');
  22. radios = document.getElementsByName('func2');
  23.  
  24.  
  25.  
  26.     hitSound = document.getElementById("ouch");
  27.     missSound = document.getElementById("boo");
  28.     unfreeze();
  29. }
  30.  
  31. function count_down(){
  32.     time_remaining = time_remaining - 1;
  33.     timer_text.innerHTML = "Time remaining: " + time_remaining + " sec";
  34.     if (time_remaining == 0)
  35.             game_over();    //  do nothing
  36.     else
  37.         count_down_timer = setTimeout("count_down()", 1000);
  38. }
  39.  
  40. function relocate(){
  41.  
  42. for (var i = 0, length = difficulty.length; i < length; i++)
  43. {
  44.  if (difficulty[i].checked)
  45.  {
  46.   // do whatever you want with the checked radio
  47.   difficulty2=difficulty[i].value;
  48.  
  49.   // only one radio can be logically checked, don't check the rest
  50.   break;
  51.  }
  52. }
  53.  
  54.  
  55.     document.images[monster_position].src = 'empty.png';
  56.     monster_position = Math.floor(Math.random()*8)
  57.     document.images[monster_position].src = 'monster.png';
  58.  
  59. switch(difficulty2)
  60.     {
  61.             case '0': moving_timer = setTimeout("relocate()", 1000); break;
  62.             case '1': moving_timer = setTimeout("relocate()", 600); break;
  63.             case '2': moving_timer = setTimeout("relocate()", 200); break;
  64.     }
  65.  
  66.  
  67. for (var i = 0, length = radios.length; i < length; i++)
  68. {
  69.  if (radios[1].checked && froze == true)
  70.  {
  71.     alert(froze)
  72.   unfreeze();
  73.     froze = false;
  74.  
  75.   // only one radio can be logically checked, don't check the rest
  76.   break;
  77.  }
  78.  
  79.  if (radios[0].checked && froze == false)
  80.  {
  81.     alert(froze)
  82.    
  83.   freeze();
  84.     froze=true;
  85.  
  86.   // only one radio can be logically checked, don't check the rest
  87.   break;
  88.  }
  89.  
  90. }
  91.  
  92. }
  93.  
  94. function game_over(){
  95.     finished = true;
  96.     clearTimeout(moving_timer);
  97.     alert("Times up!!!");
  98. }
  99.  
  100. function freeze(){
  101.     clearTimeout(moving_timer);
  102.     clearTimeout(count_down_timer);
  103. }
  104.  
  105. function unfreeze(){
  106.     count_down();
  107.     relocate();
  108. }
  109.  
  110. function keyboard_event(event){
  111.     var pressed_key = String.fromCharCode(event.keyCode);
  112.     var hit_box;
  113.     switch(pressed_key)
  114.     {
  115.             case 'Q': hit_box=0; break;
  116.             case 'W': hit_box=1; break;
  117.             case 'E': hit_box=2; break;
  118.             case 'A': hit_box=3; break;
  119.             case 'S': hit_box=4; break;
  120.             case 'D': hit_box=5; break;
  121.             case 'Z': hit_box=6; break;
  122.             case 'X': hit_box=7; break;
  123.             case 'C': hit_box=8; break;
  124.     }
  125.  
  126.     if (hit_box == monster_position){
  127.         hitSound.play();
  128.         score = score + 1;
  129.         score_text.innerHTML = "Score: " + score;
  130.         clearTimeout(moving_timer);
  131.         relocate();
  132.     }
  133.     else {
  134.         missSound.play();
  135.     }
  136. }
  137.  
  138. -->
  139. </script>
  140. </head>
  141.  
  142. <body onload="game_start()" onKeyDown="if (!finished) keyboard_event(event)">
  143.  
  144. <form action="#n" name="theForm">
  145.  
  146.     <label for="diff">Difficulty: </label>
  147.     <input type="radio" name="diffs" value="2"> Expert
  148.     <input type="radio" name="diffs" value="1" checked="checked" > Normal
  149.     <input type="radio" name="diffs" value="0" > Novice<br><br>
  150. </form>
  151.  
  152.  <label for="func2">Stop: </label>
  153.     <input type="radio" name="func" value="1" checked="checked" > False
  154.     <input type="radio" name="func" value="0"> True<br><br>
  155. </form>
  156.  
  157.     <audio src="./boo.mp3" type="audio/mpeg" id="boo" preload="auto" autobuffer controls> </audio>
  158.     <audio src="./ouch.mp3" type="audio/mpeg"  id="ouch" preload="auto" autobuffer controls > </audio>
  159.  
  160.     <table border="10" width="300">
  161.     <tr>
  162.       <td height="100" width="100"> <img src="empty.png" height="100" width="100"> </td>
  163.       <td height="100" width="100"> <img src="empty.png" height="100" width="100"> </td>
  164.       <td height="100" width="100"> <img src="empty.png" height="100" width="100"> </td>
  165.     </tr>
  166.     <tr>
  167.       <td height="100" width="100"> <img src="empty.png" height="100" width="100"> </td>
  168.       <td height="100" width="100"> <img src="empty.png" height="100" width="100"> </td>
  169.       <td height="100" width="100"> <img src="empty.png" height="100" width="100"> </td>
  170.     </tr>
  171.     <tr>
  172.       <td height="100" width="100"> <img src="empty.png" height="100" width="100"> </td>
  173.       <td height="100" width="100"> <img src="empty.png" height="100" width="100"> </td>
  174.       <td height="100" width="100"> <img src="empty.png" height="100" width="100"> </td>
  175.     </tr>
  176.   </table>
  177.  
  178.   <div id="timer_text" style="font-size:32pt">
  179.     Time remaining: 10 sec
  180.   </div>
  181.  
  182.   <div id="score_text" style="font-size:32pt">
  183.     Score: 0
  184.   </div>
  185.  
  186. </body>
  187.  
  188. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top