js仿3366小游戲選字游戲
本文實(shí)例為大家分享了js仿3366小游戲中“你是色盲嗎”游戲,大家先來(lái)挑戰(zhàn)一下
游戲目標(biāo): 按畫(huà)面中出現(xiàn)的文字的顏色來(lái)選擇顏色,千萬(wàn)不要被顏色的困局打擾,眼睛一定要放亮哦,游戲開(kāi)始時(shí)會(huì)有10分,每答對(duì)一題得一分,總共有10分,時(shí)間用完游戲會(huì)結(jié)束。
操作說(shuō)明: 鼠標(biāo)點(diǎn)擊選擇顏色
1、效果圖:
原圖:
模仿:
代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .wrap { width: 400px; height: 600px; border: 1px solid black; margin: 0 auto; } .head { width: 100%; height: 50px; overflow: hidden; } .time { float: left; width: 150px; height: 100%; line-height: 50px; font-size: 20px; text-align: center; } .score { width: 150px; height: 100%; float: right; line-height: 50px; font-size: 20px; /*text-align: center;*/ } .middle { width: 100%; height: 450px; } .text { width: 100%; height: 300px; font-size: 200px; text-align: center; line-height: 300px; } .alert { width: 80%; height: 150px; margin: 0 auto; text-indent: 2em; font-size: 25px; } .bottom { width: 100%; height: 100px; overflow: hidden; } .bottomText { width: 20%; height: 100px; float: left; text-align: center; line-height: 100px; font-size: 70px; cursor: pointer; } </style> </head> <body> <div class="wrap"> <div class="head"> <div class="time">時(shí)間:10s</div> <div class="score">分?jǐn)?shù) :0</div> </div> <div class="middle"> <div class="text">藍(lán)</div> <div class="alert">根據(jù)上面的字的顏色從下面選擇正確的字,選擇正確自動(dòng)開(kāi)始</div> </div> <div class="bottom"> <div class="bottomText">紅</div> <div class="bottomText">綠</div> <div class="bottomText">黑</div> <div class="bottomText">藍(lán)</div> <div class="bottomText">黃</div> </div> </div> </body> <script type="text/javascript"> //變化的核心 獲得不重復(fù)的亂序數(shù)組(數(shù)組中下標(biāo)值) function random(min, max) { return parseInt(Math.random() * (max - min + 1)) + min; } //不重復(fù)的數(shù)組 function randomArr() { var arr = []; while (arr.length < 5) { var temp = random(0, 4); if (arr.indexOf(temp) == -1) { arr.push(temp); } } return arr; } function fresh() { //中間字 變化 var textIndex = random(0, 4); colorIndex = random(0, 4); textDiv.innerHTML = textArr[textIndex]; textDiv.style.color = colorArr[colorIndex]; //獲取亂序下標(biāo)數(shù)組 var textRandoms = randomArr(); var colorRandoms = randomArr(); for (var i = 0; i < bottomDivs.length; i++) { //通過(guò)亂序下標(biāo)獲取文本,賦值給div bottomDivs[i].innerHTML = textArr[textRandoms[i]]; bottomDivs[i].style.color = colorArr[colorRandoms[i]]; //保存亂序下標(biāo) bottomDivs[i].index = textRandoms[i]; } } var textDiv = document.querySelector(".text"); var bottomDivs = document.querySelectorAll(".bottomText"); var timeDiv = document.querySelector(".time"); var scoreDiv = document.querySelector(".score"); var alertDiv = document.querySelector(".alert"); var textArr = ["紅", "綠", "藍(lán)", "黃", "黑"]; var colorArr = ["red", "green", "blue", "yellow", "black"]; var colorIndex=0; var timer = null; var isplaying = false; var countDown = 10; var score = 0; fresh(); for (var i = 0; i < bottomDivs.length; i++) { bottomDivs[i].onclick = function(){ //判斷 if(colorIndex == this.index &&countDown!=0 ){ //刷新 score ++; isplaying =true; //分?jǐn)?shù)增加 fresh(); scoreDiv.innerHTML = "分?jǐn)?shù): "+score ; alertDiv.style.opacity = 0; }else if(colorIndex != this.index &&isplaying){ //點(diǎn)錯(cuò)時(shí)間減小 countDown --; //更新時(shí)間變化 timeDiv.innerHTML = "時(shí)間: " + countDown +"s"; //判斷清理定時(shí)器 if(countDown <= 0){ clearInterval(timer); isplaying = false; } } } } //定時(shí)器,監(jiān)聽(tīng)游戲進(jìn)行 timer = setInterval(function(){ if(isplaying){ countDown --; timeDiv.innerHTML = "時(shí)間: " + countDown +"s"; if(countDown <= 0){ clearInterval(timer); isplaying =false; alert("game over!!"); } //停止游戲 }else{ } },1000); </script> </html>
以上就是本文的全部?jī)?nèi)容,希望大家能夠挑戰(zhàn)成功。
- js貪吃蛇游戲?qū)崿F(xiàn)思路和源碼
- 基于javascript實(shí)現(xiàn)泡泡大冒險(xiǎn)網(wǎng)頁(yè)版小游戲
- 基于javascript實(shí)現(xiàn)句子翻牌網(wǎng)頁(yè)版小游戲
- 基于javascript制作經(jīng)典傳統(tǒng)的拼圖游戲
- JavaScript實(shí)現(xiàn)斗地主游戲的思路
- javascript結(jié)合Flexbox簡(jiǎn)單實(shí)現(xiàn)滑動(dòng)拼圖游戲
- 分享自己用JS做的掃雷小游戲
- js編寫(xiě)貪吃蛇的小游戲
- 使用Javascript寫(xiě)的2048小游戲
- javascript實(shí)現(xiàn)的猜數(shù)小游戲完整實(shí)例代碼
相關(guān)文章
layui使用templet格式化表格數(shù)據(jù)的方法
今天小編就為大家分享一篇layui使用templet格式化表格數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09JavaScript 雙向鏈表操作實(shí)例分析【創(chuàng)建、增加、查找、刪除等】
這篇文章主要介紹了JavaScript 雙向鏈表操作,結(jié)合實(shí)例形式分析了JavaScript雙向鏈表的創(chuàng)建、增加、查找、刪除等相關(guān)操作技巧,需要的朋友可以參考下2020-04-04JavaScript面試必備之垃圾回收機(jī)制和內(nèi)存泄漏詳解
垃圾回收機(jī)制和內(nèi)存泄漏是JavaScript面試時(shí)常常問(wèn)到的問(wèn)題,這篇文章就為大家詳細(xì)整理了他們的相關(guān)知識(shí),感興趣的小伙伴可以跟隨小編一起了解一下2023-05-05Echarts中X軸/Y軸坐標(biāo)標(biāo)簽顯示不下的問(wèn)題解決
本文主要介紹了Echarts中X軸/Y軸坐標(biāo)標(biāo)簽顯示不下的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09canvas實(shí)現(xiàn)十二星座星空?qǐng)D
本文主要分享了canvas實(shí)現(xiàn)十二星座星空?qǐng)D的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02