Advertisement
Guest User

4.11.R16

a guest
Nov 4th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.88 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <meta charset=”utf-8? />
  5. <meta name=”format-detection” content=”telephone=no” />
  6. <meta name=”msapplication-tap-highlight” content=”no” />
  7. <meta name=”viewport” content=”user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width/>
  8. <meta http-equiv=”Content-Security-Policy” content=”default-src * ‘unsafe-inline’; style-src ‘self’ ‘unsafe-inline’; media-src *” />
  9. <link rel="icon" href="icon.png">
  10. <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.css">
  11. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  12. <script src="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.js"></script>
  13. <title>Interactive Page</title>
  14.  
  15. </head>
  16. <style>
  17.     body{
  18.         font-size: 200%;
  19.     }
  20.     img {
  21.         max-width:100%;
  22.         height:auto;
  23.     }
  24.     .game{
  25.         height: 30vw;
  26.         width: 30vw;
  27.     }
  28.    
  29.     table, td, tr {
  30.         border: 2px solid black;
  31.     }
  32.    
  33.     .clickable{
  34.         background-color: red;
  35.     }
  36.    
  37.     .ui-btn{
  38.         font-size: 200%;
  39.     }
  40.    
  41. </style>
  42.  
  43. <body>
  44.     <center>
  45.     <div>
  46.         <h1>Awesome Game</h1>
  47.         <p>Your score: <text id="myScore">0</text></p>
  48.     </div>
  49.  
  50.     <div style="padding-top: 30px">
  51.         <table>
  52.             <tr><td id="1" class="game"></td><td id="2" class="game"></td><td id="3" class="game"></td></tr>
  53.             <tr><td id="4" class="game"></td><td id="5" class="game"></td><td id="6" class="game"></td></tr>
  54.             <tr><td id="7" class="game"></td><td id="8" class="game"></td><td id="9" class="game"></td></tr>
  55.         </table>
  56.        
  57.     </div>
  58.         <div style="height:20vh">
  59.         <button id="startGameBtn" class="ui-btn">Start game!</button>
  60.     </div>
  61.    
  62.     </center>
  63. </body>
  64.  
  65. <script>
  66.     var score = 0;
  67.     var gameStarted = false;
  68.     var lastColoredSquare = -1;
  69.    
  70.     function getRandomNumber(intFrom, intTo){
  71.         return Math.floor((Math.random() * intTo) + intFrom);
  72.     }
  73.    
  74.     function addScore(){
  75.         score = score + 1;
  76.         $("#myScore").html(score);
  77.     }
  78.    
  79.     function colorRandomSquare(){
  80.         var idToColor = getRandomNumber(1,9);
  81.         if(idToColor != lastColoredSquare){
  82.             lastColoredSquare = idToColor;
  83.             $("#" + idToColor).addClass("clickable");
  84.         }else{
  85.             colorRandomSquare();
  86.         }
  87.        
  88.     }
  89.    
  90.     $("#startGameBtn").click(
  91.         function(){
  92.             if(!gameStarted){
  93.                 gameStarted = true;
  94.                 colorRandomSquare();
  95.                 $("#startGameBtn").html("End game!");
  96.             }else{
  97.                 gameStarted = false;
  98.                 score = 0;
  99.                 lastColoredSquare = -1;
  100.                 $("#myScore").html(score);
  101.                 $("#startGameBtn").html("Start game!");
  102.                 for(var squareID = 1; squareID <= 9; squareID++){
  103.                     $("#" + squareID).removeClass("clickable");
  104.                 }
  105.             }
  106.            
  107.         }
  108.     );
  109.    
  110.     $(".game").click(
  111.         function(){
  112.             console.log();
  113.             if($(this).attr("class") == "game clickable"){
  114.                 addScore();
  115.                 $(this).removeClass("clickable");
  116.                 colorRandomSquare();
  117.             }
  118.         }
  119.     );
  120.    
  121.    
  122. </script>
  123.  
  124. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement