【原】用javascript实现一个锻炼注意力的小游戏
游戏说明:
选择框内文字正确的字体颜色,累积计分。本游戏可以锻炼反应能力,并且对集中注意力起到一定的训练效果。
运行效果(未作UI修饰,界面一般):
请将以下代码直接复制到记事本中,把后缀改为*.html运行
<html> <head> <style> #gamepanel{ width:200px; height:300px; border:solid black 2px; float:left; } #uppanel{ height:70px; border-bottom:solid 1px black; } #colorbrick{ border:solid 1px black; margin-top:20px; margin-left:40px; width:100px; height:30px; text-align:center; color:#3f8f3e; font-weight:bold; font-size:25px; } #score{ width:100px; height:70px; border: solid black 2px; border-left:none; float:left; } li{ font-size:20px; margin-bottom:10px; border:1px solid grey; width:100px; } li:hover{ backgroud-color:#324ddf; } #restart{ margin-top:50px; cursor:hand; font-size:30px; background-color:red; } </style> <script> window.onload=reloadBrick; function reloadBrick() { var brick=document.getElementById("colorbrick"); var textcolors=["green","black","purple","red","blue"]; var textIndex = Math.floor(Math.random()*5); brick.innerHTML=textcolors[textIndex]; var fontcolorIndex=Math.floor(Math.random()*5); brick.style.color=textcolors[fontcolorIndex]; } myscore=0; total=0; function option_click(color) { total +=1; var correctColor=document.getElementById("colorbrick").style.color; if(correctColor==color) { myscore+=1; } document.getElementById("total").innerHTML="Total:"+total; document.getElementById("myscore").innerHTML="My Score:"+myscore; reloadBrick(); } </script> </head> <body> <div id="gamepanel"> <div id="uppanel"> <div id="colorbrick">ggggg</div> </div> <div id="downpanel"> <div id="options"> <p>Select the font-color: </p></br> <ul> <li onclick="option_click('black')" onmouseover="this.style.cursor='hand'; this.style.backgroundColor='#dfeeef'" onmouseout="this.style.backgroundColor='white'">black</li> <li onclick="option_click('red')" onmouseover="this.style.cursor='hand'; this.style.backgroundColor='#dfeeef'" onmouseout="this.style.backgroundColor='white'">red</li> <li onclick="option_click('green')" onmouseover="this.style.cursor='hand'; this.style.backgroundColor='#dfeeef'" onmouseout="this.style.backgroundColor='white'">green</li> <li onclick="option_click('blue')" onmouseover="this.style.cursor='hand'; this.style.backgroundColor='#dfeeef'" onmouseout="this.style.backgroundColor='white'">blue</li> <li onclick="option_click('purple')" onmouseover="this.style.cursor='hand'; this.style.backgroundColor='#dfeeef'" onmouseout="this.style.backgroundColor='white'">purple</li> </ul> </div> </div> </div> <div id="score"> <div id="total">Total:0</div> <div id="myscore">My Score:0</div> <div id="restart"onclick=" location.reload()">Restart</div> </div> </body> </html>
本文出处:http://www.cnblogs.com/ytaozhao 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库