Advertisement
asimryu

word game json

Apr 16th, 2023
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.73 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Word Game</title>
  6.     <style>
  7.         * {
  8.             box-sizing: border-box;
  9.         }
  10.         html, body {
  11.             width: 100%;
  12.             height: 100%;
  13.         }
  14.         body {
  15.             margin: 0;
  16.             padding: 0;
  17.            
  18.         }
  19.         #wordspace {
  20.             width: 100%;
  21.             height: 100%;
  22.             font-size: 1.5rem;
  23.             font-weight: bold;
  24.             background-color: black;
  25.             color: white;
  26.             padding: 0 50px;
  27.         }
  28.         #footer {
  29.             position: fixed;
  30.             bottom: 0;
  31.             left: 0;
  32.             width: 100%;
  33.             height: 50px;
  34.             background-color: #333;
  35.             display: flex;
  36.             justify-content: center;
  37.             align-items: center;
  38.             flex-direction: row;
  39.             align-items: stretch;
  40.         }
  41.         #footer > div {
  42.             flex: 1;
  43.             display: flex;
  44.             justify-content: center;
  45.             align-items: stretch;              
  46.         }
  47.         #footer .btns {
  48.             justify-content: start;
  49.         }
  50.         #footer .typeinput input {
  51.             width: 100%;
  52.             font-size: 1.2rem;
  53.         }
  54.         #footer .score {
  55.             justify-content: end;
  56.             line-height: 50px;
  57.             padding-right: 20px;
  58.             color: yellow;
  59.             font-size: 1.2rem;
  60.         }
  61.         #level {
  62.             color: red;
  63.             margin-right: 20px;
  64.         }
  65.     </style>
  66. </head>
  67. <body>
  68.     <div id="wordspace"></div>
  69.     <div id="footer">
  70.         <div class="btns">     
  71.             <button id="start">Start</button>
  72.             <button id="stop">Stop</button>
  73.             <button id="clear">Clear</button>
  74.         </div>
  75.         <div class="typeinput">
  76.             <input type="text" id="typeinput" autofocus>
  77.         </div>
  78.         <div class="score">
  79.             <span>Level: </span>
  80.             <span id="level">1</span>
  81.             <span>Score: </span>
  82.             <span id="score">0</span>
  83.         </div>
  84.     </div>
  85.     <script>
  86.         let level = 0;
  87.         let score = 0;
  88.         let wordinterval = [4000, 3500, 3000, 2500, 2000];
  89.         let movewordpixels = [10, 20, 30, 40, 50];
  90.         let timer4words = null;
  91.         let timer4drop = null;
  92.         let words = [];
  93.         let totalwords = 0;
  94.  
  95.         const getWordsFromJson = async () => {
  96.             const response = await fetch('https://assets.codepen.io/147406/words.json');
  97.             const words = await response.json();
  98.             return words;
  99.         }
  100.  
  101.         getWordsFromJson().then((data) => {
  102.             for( let i=0; i<data.words.length; i++) {
  103.                words.push(data.words[i]);
  104.            }
  105.        });
  106.  
  107.         document.getElementById('start').addEventListener('click', () => {
  108.             showWords();
  109.             timer4words = setInterval(() => {
  110.                 showWords();
  111.             }, wordinterval[level]);
  112.             timer4drop = setInterval(() => {
  113.                 moveWords();
  114.             }, 1000);
  115.             document.getElementById('typeinput').focus();
  116.         });
  117.         document.getElementById('stop').addEventListener('click', () => {
  118.             clearInterval(timer4words);
  119.             clearInterval(timer4drop);
  120.         });
  121.  
  122.         document.getElementById('clear').addEventListener('click', () => {
  123.             let words = document.getElementsByClassName('word');
  124.             for (let i = 0; i < words.length; i++) {
  125.                 let word = words[i];
  126.                 word.remove();
  127.             }
  128.             document.getElementById('stop').click();
  129.             level = 0;
  130.             score = 0;
  131.             document.getElementById('level').innerText = level + 1;
  132.             document.getElementById('score').innerText = score;
  133.         });
  134.  
  135.         document.getElementById('typeinput').addEventListener('keyup', (e) => {
  136.             if (e.keyCode === 13) {
  137.                 let words = document.getElementsByClassName('word');
  138.                 for (let i = 0; i < words.length; i++) {
  139.                     let word = words[i];
  140.                     if (word.innerText === e.target.value) {
  141.                         word.remove();
  142.                         score++;
  143.                         document.getElementById('score').innerText = score;
  144.                         document.getElementById('level').innerText = level + 1;
  145.                         if( score % 10 === 0) {
  146.                             level++;
  147.                             if( level > 3) {
  148.                                 level = 3;
  149.                             }
  150.                             document.getElementById('stop').click();
  151.                             document.getElementById('start').click();
  152.                         }
  153.                     }
  154.                     if( word.style.top >= window.innerHeight-50) {
  155.                         word.remove();
  156.                     }
  157.                 }
  158.                 e.target.value = '';
  159.                 console.log(e.target.value);
  160.             }
  161.         });
  162.  
  163.         const showWords = async () => {
  164.             if( words.length === 0) {
  165.                 document.getElementById('stop').click();
  166.                 return;
  167.             }
  168.             words.sort(() => Math.random() - 0.5);
  169.             const word = words.pop();
  170.             let span = document.createElement('span');
  171.             span.classList.add('word');
  172.             span.style.position = 'absolute';
  173.             span.style.left = Math.floor(((window.innerWidth-200) - 200) * Math.random() + 200)  + "px";
  174.             span.style.top = 0;
  175.             span.style.color = "white";
  176.             span.innerText = word;
  177.             document.getElementById('wordspace').appendChild(span);
  178.         }
  179.         const moveWords = () => {
  180.             let words = document.getElementsByClassName('word');
  181.             for (let i = 0; i < words.length; i++) {
  182.                 let word = words[i];
  183.                 let top = parseInt(word.style.top);
  184.                 word.style.top = `${top + movewordpixels[level]}px`;
  185.             }
  186.         }
  187.  
  188.  
  189.        
  190.     </script>
  191. </body>
  192. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement