JavaScript實(shí)現(xiàn)打字游戲
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)打字游戲的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
需求分析:
1、在char這個(gè)div里面顯示要輸入的字母,大寫
2、在result這個(gè)div里面時(shí)時(shí)顯示正確率,比如98%
3、給文檔綁定按鍵事件
4、如果輸入的內(nèi)容和char里面一致,顯示正確動畫:animated zoomIn,并更換輸入的字母
5、如果輸入的內(nèi)容和char里面不一致,顯示錯(cuò)誤動畫:animated shake error
6、不管是正確還是錯(cuò)誤都時(shí)時(shí)更新result里面的正確率
源代碼:
HTML部分
<mian> <div id="char">A</div> <div id="result">請?jiān)诎存I上按下屏幕上顯示的字母</div> </mian>
css部分
1.為了實(shí)現(xiàn)一些特效,先創(chuàng)建一個(gè)animate.css文件,在封裝一些動畫效果放里面
/*animate.css*/ .animated { -webkit-animation-duration: 1s; animation-duration: 1s; -webkit-animation-fill-mode: both; animation-fill-mode: both; } .animated.infinite { -webkit-animation-iteration-count: infinite; animation-iteration-count: infinite; } .animated.hinge { -webkit-animation-duration: 2s; animation-duration: 2s; } .animated.flipOutX, .animated.flipOutY, .animated.bounceIn, .animated.bounceOut { -webkit-animation-duration: .75s; animation-duration: .75s; } @-webkit-keyframes zoomIn { from { opacity: 0; -webkit-transform: scale3d(.3, .3, .3); transform: scale3d(.3, .3, .3); } 50% { opacity: 1; } } @keyframes zoomIn { from { opacity: 0; -webkit-transform: scale3d(.3, .3, .3); transform: scale3d(.3, .3, .3); } 50% { opacity: 1; } } .zoomIn { -webkit-animation-name: zoomIn; animation-name: zoomIn; } .animated { -webkit-animation-duration: 1s; animation-duration: 1s; -webkit-animation-fill-mode: both; animation-fill-mode: both; } @-webkit-keyframes shake { from, to { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } 10%, 30%, 50%, 70%, 90% { -webkit-transform: translate3d(-10px, 0, 0); transform: translate3d(-10px, 0, 0); } 20%, 40%, 60%, 80% { -webkit-transform: translate3d(10px, 0, 0); transform: translate3d(10px, 0, 0); } } @keyframes shake { from, to { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } 10%, 30%, 50%, 70%, 90% { -webkit-transform: translate3d(-10px, 0, 0); transform: translate3d(-10px, 0, 0); } 20%, 40%, 60%, 80% { -webkit-transform: translate3d(10px, 0, 0); transform: translate3d(10px, 0, 0); } } .shake { -webkit-animation-name: shake; animation-name: shake; }
2.css主體代碼,無動畫特效版
<style> body { margin: 0; /*開啟彈性布局,并讓彈性布局中的子元素 水平居中對齊,垂直居中對齊*/ display: flex; justify-content: center; align-items: center; /*文字居中*/ text-align: center; /*設(shè)置背景顏色的經(jīng)像漸變*/ background: radial-gradient(circle, #444, #111, #000); } #char { font-size: 400px; color: lightgreen; /*設(shè)置文字陰影*/ /*text-shadow: 水平位置 垂直位置 模糊距離 陰影顏色*/ /*位置可以為負(fù)值*/ text-shadow: 0 0 50px #666; } #result { font-size: 20px; color: #888; } /*找到id為char及類名為error的div元素*/ #char.error { color: red; } </style>
JavaScript部分
1.為了簡化代碼,先封裝一些常用的自定義構(gòu)造函數(shù)
<script> // 定義一個(gè)函數(shù):rand // 參數(shù):最小整數(shù),最大整數(shù) // 返回:兩個(gè)整數(shù)之間的一個(gè)隨機(jī)整數(shù) function rand(min, max) { return parseInt(Math.random() * (max - min + 1)) + min; } </script>
2.js主體部分,需要用到封裝的函數(shù),調(diào)用即可
<script> // 獲取相關(guān)元素 var charDiv = document.getElementById('char'); var resultDiv = document.getElementById('result'); // code用于記錄頁面上的字母的編碼,使用全局變量,到處都可以使用 var code, tirme; var rightNum = 0;//正確次數(shù) var wrongNum = 0;//錯(cuò)誤次數(shù) // 1 在char這個(gè)div里面顯示要輸入的字母,大寫 showChar(); // 3 給文檔綁定按鍵事件 document.onkeyup = function (e) { // 事件對象 e = window.event || e; // 獲取按鍵編碼 var keyCode = e.keyCode || e.which; // 4 如果輸入的內(nèi)容和char里面一致 if (keyCode == code) { // 顯示正確動畫:animated zoomIn charDiv.className = "animated zoomIn"; rightNum++; showChar() } // 5 如果輸入的內(nèi)容和char里面不一致 else { // 顯示錯(cuò)誤動畫:animated shake error charDiv.className = "animated shake error"; wrongNum++ } // 為了下一次有動畫,在本次動畫完后要移除類名 setTimeout(function () { charDiv.className = ""; }, 500) // 6 不管是正確還是錯(cuò)誤都時(shí)時(shí)更新result里面的正確率 // 正確率 = 正確次/總次數(shù) resultDiv.innerHTML = "正確率:" + parseInt(rightNum / (rightNum + wrongNum) * 100) + "%" } // 函數(shù)功能:在char這個(gè)div里面隨機(jī)顯示要輸入的字母:大寫 function showChar() { // 先隨機(jī)出一個(gè)字母編碼 code = rand(65, 90); // 變成一個(gè)字母 var char = String.fromCharCode(code); // 顯示在char這個(gè)div里面 charDiv.innerHTML = char; } </script>
總代碼
<html> <head> <meta charset="utf-8"> <title>打字游戲</title> <!--引入animate.css動畫庫--> <link rel="stylesheet" href="animate.css" > <style> body { margin: 0; /*開啟彈性布局,并讓彈性布局中的子元素 水平居中對齊,垂直居中對齊*/ display: flex; justify-content: center; align-items: center; /*文字居中*/ text-align: center; /*設(shè)置背景顏色的經(jīng)像漸變*/ background: radial-gradient(circle, #444, #111, #000); } #char { font-size: 400px; color: lightgreen; /*設(shè)置文字陰影*/ /*text-shadow: 水平位置 垂直位置 模糊距離 陰影顏色*/ /*位置可以為負(fù)值*/ text-shadow: 0 0 50px #666; } #result { font-size: 20px; color: #888; } /*找到id為char及類名為error的div元素*/ #char.error { color: red; } </style> </head> <body> <mian> <div id="char">A</div> <div id="result">請?jiān)诎存I上按下屏幕上顯示的字母</div> </mian> </body> </html> <script> // 定義一個(gè)函數(shù):rand // 參數(shù):最小整數(shù),最大整數(shù) // 返回:兩個(gè)整數(shù)之間的一個(gè)隨機(jī)整數(shù) function rand(min, max) { return parseInt(Math.random() * (max - min + 1)) + min; } </script> <script> // 獲取相關(guān)元素 var charDiv = document.getElementById('char'); var resultDiv = document.getElementById('result'); // code用于記錄頁面上的字母的編碼,使用全局變量,到處都可以使用 var code, tirme; var rightNum = 0;//正確次數(shù) var wrongNum = 0;//錯(cuò)誤次數(shù) // 1 在char這個(gè)div里面顯示要輸入的字母,大寫 showChar(); // 3 給文檔綁定按鍵事件 document.onkeyup = function (e) { // 事件對象 e = window.event || e; // 獲取按鍵編碼 var keyCode = e.keyCode || e.which; // 4 如果輸入的內(nèi)容和char里面一致 if (keyCode == code) { // 顯示正確動畫:animated zoomIn charDiv.className = "animated zoomIn"; rightNum++; showChar() } // 5 如果輸入的內(nèi)容和char里面不一致 else { // 顯示錯(cuò)誤動畫:animated shake error charDiv.className = "animated shake error"; wrongNum++ } // 為了下一次有動畫,在本次動畫完后要移除類名 setTimeout(function () { charDiv.className = ""; }, 500) // 6 不管是正確還是錯(cuò)誤都時(shí)時(shí)更新result里面的正確率 // 正確率 = 正確次/總次數(shù) resultDiv.innerHTML = "正確率:" + parseInt(rightNum / (rightNum + wrongNum) * 100) + "%" } // 函數(shù)功能:在char這個(gè)div里面隨機(jī)顯示要輸入的字母:大寫 function showChar() { // 先隨機(jī)出一個(gè)字母編碼 code = rand(65, 90); // 變成一個(gè)字母 var char = String.fromCharCode(code); // 顯示在char這個(gè)div里面 charDiv.innerHTML = char; } </script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jquery $(document).ready()和window.onload的區(qū)別淺析
這篇文章主要介紹了jquery $(document).ready()和 window.onload的區(qū)別淺析,本文總結(jié)了執(zhí)行時(shí)間、編寫個(gè)數(shù)不同、簡化寫法等不同的地方,需要的朋友可以參考下2015-02-02Storage、cookie的用途和優(yōu)缺點(diǎn)比較
cookie的大小是受限制的,并且每次請求cookie都會被發(fā)送,浪費(fèi)寬帶,cookie還需要指定作用域,不可以跨域調(diào)用。cookie的作用是與服務(wù)器進(jìn)行交互,作為http規(guī)范的一部分存在,而webstorage僅僅是為了本地“存儲”數(shù)據(jù)而生。2023-07-07uni-app小程序中父組件和子組件傳值的實(shí)現(xiàn)實(shí)例
uniapp父子組件引用傳值,和vue的一樣,沒有小程序那樣的麻煩,下面這篇文章主要給大家介紹了關(guān)于uni-app小程序中父組件和子組件傳值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08JS實(shí)現(xiàn)移動端整屏滑動的實(shí)例代碼
本文通過實(shí)例代碼給大家分享了基于js 實(shí)現(xiàn)移動端整屏滑動效果,基本思路是檢測手指滑動方向,獲取手指抬起時(shí)的位置,減去手指按下時(shí)的位置,得正即為向下滑動了,具體實(shí)現(xiàn)代碼大家參考下本文2017-11-11javascript實(shí)現(xiàn)當(dāng)前頁導(dǎo)航激活的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)當(dāng)前頁導(dǎo)航激活的方法,涉及javascript操作css的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02