자바스크립트 기반 디지털 게임 코드 맞추기 실례

환경
  • vscode 1.46
  • Microsoft Edge 83
  • 예제
    다음은 JavaScript에 대한 이해와 숙지입니다. 이해하지 않고 복사하여 붙여넣고 정확하게 실행하면 됩니다.
    새 웹 템플릿
    vscode에 index를 새로 만듭니다.html 파일을 입력하고 다음 내용을 입력하십시오.
    
    <!DOCTYPE html>
    <html>
     <head>
      <meta charset="utf-8">
    
      <title> </title>
    
      <style>
       html {
        font-family: sans-serif;
       }
    
       body {
        width: 50%;
        max-width: 800px;
        min-width: 480px;
        margin: 0 auto;
       }
    
       .lastResult {
        color: white;
        padding: 3px;
       }
      </style>
     </head>
    
     <body>
      <h1> </h1>
    
      <p> 100 。  10  。 。</p>
    
      <div class="form">
       <label for="guessField"> : </label>
       <input type="text" id="guessField" class="guessField">
       <input type="submit" value=" " class="guessSubmit">
      </div>
    
      <div class="resultParas">
       <p class="guesses"></p>
       <p class="lastResult"></p>
       <p class="lowOrHi"></p>
      </div>
    
      <script>
    
       //   JavaScript  
    
      </script>
     </body>
    </html>
    변수 정의
    우선 사용해야 할 변수를 모두 정의합니다.
    
    let randomNumber = Math.floor(Math.random() * 100) + 1;
    
    const guesses = document.querySelector('.guesses');
    const lastResult = document.querySelector('.lastResult');
    const lowOrHi = document.querySelector('.lowOrHi');
    
    const guessSubmit = document.querySelector('.guessSubmit');
    const guessField = document.querySelector('.guessField');
    
    let guessCount = 1;
    let resetButton;
    숫자 맞추기 주 논리 정의
    
    function checkGuess() {
     let userGuess = Number(guessField.value);
     if (guessCount === 1) {
      guesses.textContent = ' :';
     }
     guesses.textContent += userGuess + ' ';
     
     if (userGuess === randomNumber) {
      lastResult.textContent = ' ! ';
      lastResult.style.backgroundColor = 'green';
      lowOrHi.textContent = '';
      setGameOver();
     } else if (guessCount === 10) {
      lastResult.textContent = '!!!GAME OVER!!!';
      setGameOver();
     } else {
      lastResult.textContent = ' !';
      lastResult.style.backgroundColor = 'red';
      if(userGuess < randomNumber) {
       lowOrHi.textContent = ' !';
      } else if(userGuess > randomNumber) {
       lowOrHi.textContent = ' ';
      }
     }
     
     guessCount++;
     guessField.value = '';
     guessField.focus();
    }
    게임 종료
    
    function setGameOver() {
     guessField.disabled = true;
     guessSubmit.disabled = true;
     resetButton = document.createElement('button');
     resetButton.textContent = ' ';
     document.body.appendChild(resetButton);
     resetButton.addEventListener('click', resetGame);
    }
    게임 리셋
    
    function resetGame() {
     guessCount = 1;
    
     const resetParas = document.querySelectorAll('.resultParas p');
     for (let i = 0 ; i < resetParas.length; i++) {
      resetParas[i].textContent = '';
     }
    
     resetButton.parentNode.removeChild(resetButton);
    
     guessField.disabled = false;
     guessSubmit.disabled = false;
     guessField.value = '';
     guessField.focus();
    
     lastResult.style.backgroundColor = 'white';
    
     randomNumber = Math.floor(Math.random() * 100) + 1;
    }
    브라우저 열기
    브라우저로 방금 작성한 프로그램을 열어 게임이 정상적으로 실행될 수 있는지 시험해 보세요.
    부록
    원본 코드
    
    <!DOCTYPE html>
    <html>
    
    <head>
     <meta charset="utf-8">
     <title> </title>
     <style>
      html {
       font-family: sans-serif;
      }
    
      body {
       width: 50%;
       max-width: 800px;
       min-width: 480px;
       margin: 0 auto;
      }
    
      .lastResult {
       color: white;
       padding: 3px;
      }
     </style>
    </head>
    
    <body>
     <h1> </h1>
    
     <p> 100 。  10  。 。</p>
    
     <div class="form">
      <label for="guessField"> : </label>
      <input type="text" id="guessField" class="guessField">
      <input type="submit" value=" " class="guessSubmit">
     </div>
    
     <div class="resultParas">
      <p class="guesses"></p>
      <p class="lastResult"></p>
      <p class="lowOrHi"></p>
     </div>
    
     <script>
    
      let randomNumber = Math.floor(Math.random() * 100) + 1;
    
      const guesses = document.querySelector('.guesses');
      const lastResult = document.querySelector('.lastResult');
      const lowOrHi = document.querySelector('.lowOrHi');
    
      const guessSubmit = document.querySelector('.guessSubmit');
      const guessField = document.querySelector('.guessField');
    
      let guessCount = 1;
      let resetButton;
    
      function checkGuess() {
       let userGuess = Number(guessField.value);
       if (guessCount === 1) {
        guesses.textContent = ' :';
       }
       guesses.textContent += userGuess + ' ';
    
       if (userGuess === randomNumber) {
        lastResult.textContent = ' ! ';
        lastResult.style.backgroundColor = 'green';
        lowOrHi.textContent = '';
        setGameOver();
       } else if (guessCount === 10) {
        lastResult.textContent = '!!!GAME OVER!!!';
        setGameOver();
       } else {
        lastResult.textContent = ' !';
        lastResult.style.backgroundColor = 'red';
        if (userGuess < randomNumber) {
         lowOrHi.textContent = ' !';
        } else if (userGuess > randomNumber) {
         lowOrHi.textContent = ' ';
        }
       }
    
       guessCount++;
       guessField.value = '';
       guessField.focus();
      }
    
      guessSubmit.addEventListener('click', checkGuess);
    
      function setGameOver() {
       guessField.disabled = true;
       guessSubmit.disabled = true;
       resetButton = document.createElement('button');
       resetButton.textContent = ' ';
       document.body.appendChild(resetButton);
       resetButton.addEventListener('click', resetGame);
      }
    
      function resetGame() {
       guessCount = 1;
    
       const resetParas = document.querySelectorAll('.resultParas p');
       for (let i = 0; i < resetParas.length; i++) {
        resetParas[i].textContent = '';
       }
    
       resetButton.parentNode.removeChild(resetButton);
    
       guessField.disabled = false;
       guessSubmit.disabled = false;
       guessField.value = '';
       guessField.focus();
    
       lastResult.style.backgroundColor = 'white';
    
       randomNumber = Math.floor(Math.random() * 100) + 1;
      }
    
     </script>
    </body>
    </html>
    이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

    좋은 웹페이지 즐겨찾기