简易打字游戏

我们以前刚接触电脑的菜鸟时期应该都有玩过一些打字游戏,一些字母往下掉然后我们按下相应字母键然后这个字母消失,一个很简单的打字小游戏,其中用了animate

body部分

 <div class="box animate__animated ">  </div>

 <p>请在键盘上按下屏幕上显示的字母</p>

css部分

<style>

        * {

            margin: 0;

            padding: 0;

            list-style: none;

            text-decoration: none;

        }

        body {

            background-color: #2d2d2d;

           

        }

        .box {

            width: 300px;

            height: 300px;

            /* background-color: lightblue; */

            margin: 200px auto;

            font-size: 200px;

            text-align: center;

            color: #88df8e;

        }

        p {

            width: 250px;

            height: 20px;

            line-height: 20px;

            text-align: center;

            display: inline-block;

            background-color: coral;

            position: relative;

            bottom: 200px;

            left: calc(50% - 125px);

        }

    </style>

Js部分

 var box = document.querySelector('.box');

        var p = document.querySelector('p');

        var right = 0;

        var wrong = 0;

        function letter() {

            return String.fromCharCode((Math.random() * 25) + 65);

        }

        box.innerHTML = letter();

        window.onkeydown = function (ev) {

            if (ev.keyCode == box.innerHTML.charCodeAt()) {

                box.innerHTML = letter();

                right++;

                box.classList.add('animate__zoomIn');

            } else {

                box.classList.add('animate__shakeX');

                wrong++

            }

        }


 

        window.onkeyup = function (ev) {

            box.classList.remove('animate__zoomIn');

            box.classList.remove('animate__shakeX');

            p.innerHTML = '总计' + (right + wrong) + '次,正确率 ' + ((right / (right + wrong)) * 100).toFixed(2) + '%';

        }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值