Advertisement
Jorell_Ramos_Sinaga

Untitled

Oct 2nd, 2021
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.13 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>Find the buried treasure!</title>
  6. </head>
  7. <body>
  8.     <h1 id="heading">Find the buried treasure!</h1>
  9.     <img id="map" src="https://1.bp.blogspot.com/-_vs-lb9vWBo/YVgYdJwECAI/AAAAAAAAAcY/q2hG8yXU7qg-5yN1q9cZwEYqoghCGc0KgCLcBGAsYHQ/s353/treasuremap.jpg" width=400 height=400>
  10.     <p id="distance"></p>
  11.     <p id="limit"></p>
  12.     <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  13.     <script type="text/javascript">
  14.         //Game code goes here ...
  15.  
  16.         //Get a random number from 0 to x
  17.         function getRandomNumber(size) {
  18.             return Math.floor(Math.random() * size);
  19.         }
  20.  
  21.         //Calculate distance between click and target
  22.         function getDistance(event, target) {
  23.             let dx = event.offsetX - target.x;
  24.             let dy = event.offsetY - target.y;
  25.  
  26.             return Math.sqrt( (dx * dx) + (dy * dy) );
  27.         }
  28.  
  29.  
  30.         //Get a message to represent the distance
  31.         function getDistanceMessage(distance) {
  32.             if (distance < 10){
  33.                 return "Boiling hot!";
  34.             } else if (distance < 20){
  35.                 return "Really hot!";
  36.             } else if (distance < 40){
  37.                 return "Hot!";
  38.             } else if (distance < 80){
  39.                 return "Warm!";
  40.             } else if (distance < 160){
  41.                 return "Cold";
  42.             } else if (distance < 320){
  43.                 return "Really Cold";
  44.             } else {
  45.                 return "Freezing!"
  46.             }
  47.         }
  48.  
  49.         function setting_up_target (){
  50.             return {
  51.                 x : getRandomNumber(width),
  52.                 y : getRandomNumber(height)
  53.             };
  54.         }
  55.  
  56.         function reset_game(){
  57.             limits = 30;
  58.             clicks = 0;
  59.             target = setting_up_target();
  60.  
  61.         }
  62.  
  63.         let width = 400;
  64.         let height = 400;
  65.         let clicks = 0;
  66.         let limits = 30;
  67.  
  68.         let target = setting_up_target()
  69.  
  70.         $('#map').click(function (event) {
  71.             clicks ++;
  72.             limits--;
  73.             let distance = getDistance(event, target);
  74.             let message = getDistanceMessage(distance);
  75.  
  76.             $('#distance').text(message);
  77.             $('#limit').text(`Your Limit is ${limits}.`)
  78.  
  79.             if (limits === 0){
  80.                 alert(`Game Over , go start again..`);
  81.                 reset_game();
  82.             }
  83.  
  84.             if (distance < 5){
  85.                 alert(`Found the treasure in ${clicks} clicks.`);
  86.                 reset_game();
  87.             };
  88.         });
  89.  
  90.  
  91.     </script>
  92. </body>
  93. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement