css
body, html { width: 100%; height: 100%; } .wrapper { width: 400px; margin: 20px auto; } div { font-size: 100px; font-weight: 900; text-align: center; } input{ width: 400px; margin: 20px auto; }
js
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' var text = document.getElementById('text') var ipt = document.getElementById('ipt') var res; var newRes; var random = Math.round(Math.random() * 25); var count = 0 window.onkeyup = function (e) { var random = Math.round(Math.random() * 25); newRes = str[random] res = text.innerHTML text.innerHTML = newRes if(e.keyCode == res.charCodeAt(0)){ count++; ipt.value = ''; }else{ alert('game over' + ' ' +'您的得分是'+ ':' + count) count = 0; ipt.value = ''; } }
以上就是关于“如何用JavaScript模拟实现打字小游戏”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注创新互联行业资讯频道。
Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号