鱼C论坛

 找回密码
 立即注册
查看: 2297|回复: 0

[作品展示] 【HTML】极速计算!

[复制链接]
发表于 2022-1-27 16:08:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
以前,我在学校看了《最强大脑》,里面的计算题让我吃惊!
现在,我正愁于没有灵感,突然,我想到了《最强大脑》……
就做了一个“大众版”《最强大脑》——《极速计算》!

规则:
电脑会出 5 道加减法题目,并要求玩家在 15 秒之内完成。

由于我懒得做美术,所以我懒得做美术。(???
由于网页比较丑,所以网页比较丑。(?????

废话少说,上代码!

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="UTF-8" />
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7.     <title>Document</title>
  8.     <style>
  9.       * {
  10.         margin: 0;
  11.         padding: 0;
  12.       }

  13.       body,
  14.       #game {
  15.         display: flex;
  16.         justify-content: center;
  17.         align-items: center;
  18.         flex-direction: column;
  19.       }

  20.       body {
  21.         min-height: 100vh;
  22.       }

  23.       p {
  24.         margin-bottom: 1vh;
  25.       }

  26.       #game {
  27.         position: relative;
  28.         width: 75vw;
  29.         height: 75vh;
  30.         border: 1px solid #000;
  31.       }

  32.       #start,
  33.       #restart {
  34.         font-size: 1.2rem;
  35.         width: 100px;
  36.         height: 50px;
  37.       }

  38.       #restart {
  39.         display: none;
  40.       }

  41.       #count-down,
  42.       #ready {
  43.         position: absolute;
  44.         top: 20%;
  45.         left: 50%;
  46.         transform: translate(-50%, -50%);
  47.         display: none;
  48.       }
  49.       #count-down {
  50.         top: 30%;
  51.       }

  52.       .question,
  53.       .question-input {
  54.         font-size: 1.2rem;
  55.       }
  56.       .question-input {
  57.         width: 10vw;
  58.       }

  59.       @keyframes fade-out {
  60.         0% {
  61.           opacity: 1;
  62.         }
  63.         100% {
  64.           opacity: 0;
  65.         }
  66.       }
  67.     </style>
  68.   </head>
  69.   <body>
  70.     <h1>极速计算!</h1>
  71.     <p>By Ckblt</p>
  72.     <div id="game">
  73.       <button id="start">开始!</button>
  74.       <h1 id="ready"></h1>
  75.       <h1 id="count-down"></h1>
  76.       <ol id="questions"></ol>
  77.       <button id="restart">再来</button>
  78.     </div>
  79.     <script>
  80.       const sleep = (timeout) =>
  81.         new Promise((resolve) => setTimeout(resolve, timeout))

  82.       const random = (min, max) =>
  83.         Math.floor(Math.random() * (max - min + 1)) + min

  84.       const startButtonE = document.getElementById('start')
  85.       const restartButtonE = document.getElementById('restart')
  86.       const readyE = document.getElementById('ready')
  87.       const countDownE = document.getElementById('count-down')
  88.       const questionsE = document.getElementById('questions')

  89.       const click = () => {
  90.         startButtonE.style.display = 'none'
  91.         restartButtonE.style.display = 'none'
  92.         readyE.style = ''
  93.         countDownE.style = ''
  94.         questionsE.style = ''
  95.         questionsE.innerHTML = ''

  96.         // 开始倒计时
  97.         readyE.style.display = 'block'
  98.         readyE.innerHTML = '3'
  99.         const prom = sleep(1000)

  100.         const questions = []

  101.         // 出题
  102.         for (let i = 0; i < 5; i++) {
  103.           const question = [
  104.             random(0, 1) ? 'plus' : 'minus',
  105.             random(5, 30),
  106.             random(5, 30)
  107.           ]
  108.           if (question[0] === 'minus') {
  109.             question[2] = random(1, question[1])
  110.           }
  111.           question.push(
  112.             question[0] === 'plus'
  113.               ? question[1] + question[2]
  114.               : question[1] - question[2]
  115.           )
  116.           questions.push(question)
  117.         }

  118.         prom
  119.           .then(() => {
  120.             readyE.innerHTML = '2'
  121.             return sleep(1000)
  122.           })
  123.           .then(() => {
  124.             readyE.innerHTML = '1'
  125.             return sleep(1000)
  126.           })
  127.           .then(() => {
  128.             readyE.innerHTML = 'GO!'
  129.             readyE.style.animation = 'fade-out 1s forwards'
  130.             setTimeout(() => {
  131.               readyE.style.display = 'none'
  132.             }, 1000)
  133.             countDownE.innerHTML = '15'
  134.             countDownE.style.display = 'block'

  135.             // 显示题目
  136.             questions.forEach((question) => {
  137.               const questionE = document.createElement('li')
  138.               questionE.classList.add('question')
  139.               questionE.innerHTML =
  140.                 question[1].toString() +
  141.                 (question[0] === 'plus' ? '+' : '-') +
  142.                 question[2].toString() +
  143.                 '=<input type="input" class="question-input" /><span class="question-span"></span>'

  144.               questionsE.appendChild(questionE)
  145.             })
  146.           }) // 倒计时
  147.           .then(() => {
  148.             countDownE.innerHTML = '14'
  149.             return sleep(1000)
  150.           })
  151.           .then(() => {
  152.             countDownE.innerHTML = '13'
  153.             return sleep(1000)
  154.           })
  155.           .then(() => {
  156.             countDownE.innerHTML = '12'
  157.             return sleep(1000)
  158.           })
  159.           .then(() => {
  160.             countDownE.innerHTML = '11'
  161.             return sleep(1000)
  162.           })
  163.           .then(() => {
  164.             countDownE.innerHTML = '10'
  165.             return sleep(1000)
  166.           })
  167.           .then(() => {
  168.             countDownE.innerHTML = '9'
  169.             return sleep(1000)
  170.           })
  171.           .then(() => {
  172.             countDownE.innerHTML = '8'
  173.             return sleep(1000)
  174.           })
  175.           .then(() => {
  176.             countDownE.innerHTML = '7'
  177.             return sleep(1000)
  178.           })
  179.           .then(() => {
  180.             countDownE.innerHTML = '6'
  181.             return sleep(1000)
  182.           })
  183.           .then(() => {
  184.             countDownE.innerHTML = '5'
  185.             return sleep(1000)
  186.           })
  187.           .then(() => {
  188.             countDownE.innerHTML = '4'
  189.             return sleep(1000)
  190.           })
  191.           .then(() => {
  192.             countDownE.innerHTML = '3'
  193.             return sleep(1000)
  194.           })
  195.           .then(() => {
  196.             countDownE.innerHTML = '2'
  197.             return sleep(1000)
  198.           })
  199.           .then(() => {
  200.             countDownE.innerHTML = '1'
  201.             return sleep(1000)
  202.           })
  203.           .then(() => {
  204.             countDownE.innerHTML = '时间到!'
  205.             Array.from(
  206.               document.getElementsByClassName('question-input')
  207.             ).forEach((e) => {
  208.               e.disabled = true
  209.             })
  210.             return sleep(2000)
  211.           })
  212.           .then(() => {
  213.             restartButtonE.style.display = 'block'
  214.             for (let i = 0; i < questions.length; i++) {
  215.               const question = questions[i]
  216.               const questionE = document.getElementsByClassName('question')[i]
  217.               const questionInputE =
  218.                 document.getElementsByClassName('question-input')[i]
  219.               const questionSpanE =
  220.                 document.getElementsByClassName('question-span')[i]

  221.               // 检查题目是否做错
  222.               if (questionInputE.value !== question[3].toString()) {
  223.                 questionE.style.color = '#d22'
  224.                 questionInputE.style.color = '#d22'
  225.                 questionSpanE.style.color = '#2d2'
  226.                 questionSpanE.innerHTML = question[3]
  227.               } else {
  228.                 questionInputE.style.color = '#2d2'
  229.               }
  230.             }
  231.           })
  232.       }

  233.       // 当开始按钮被点击时,开始游戏
  234.       startButtonE.addEventListener('click', click)
  235.       restartButtonE.addEventListener('click', click)
  236.     </script>
  237.   </body>
  238. </html>
复制代码


如果有问题,请回复我哦~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 16:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表