JavaScript制作猜数字小游戏

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字小游戏</title>
    <link rel="stylesheet" type="text/css" href="./样式.css"> 
</head>
<body>
    <p>请输入1到100之间的数字</p>
    <p style="color: aqua; font-size: 30px; font-weight: bolder;">进入数字游戏</p>
    <div id="info"></div>
    <input id="guess" type="number" placeholder="请输入1到100之间的数字"/><br>
    <button id="start">猜数字</button>
    <div id="again" style="display: none;">再来一局</div>
    <div id="show" style="cursor: default;">历史战绩</div>
    <script>
        var num, i, ks,arr;
        var info = document.getElementById('info');
        var guess = document.getElementById('guess');
        var start = document.getElementById('start');
        var again = document.getElementById('again');
        var show = document.getElementById('show');
        
        const initNum = function() {
            num = Math.floor(Math.random() * 100) + 1; // 产生1~100之间的数字
            i = 0;
            again.style.display = "none";
            info.innerHTML = "";
            start.disabled = false;
            ks = new Date();
        }
        initNum();

        again.onclick = function() {
            initNum();
        }
        guess.onfocus = function() {
            guess.select();
        }
        guess.onchange = function() {
            checknum();
        }
        
        function showH() {  
    show.innerHTML = "<div>历史战绩</div>";  
  
    if (localStorage.getItem("arr")) {  
        arr = localStorage.getItem("arr").split(",");   //读取 arr 项
        arr = arr.filter(function(item) {  
            return item.trim() !== ""; // 移除空字符串  
        });  
        var n = arr.length - 1;  
        var j = 1;  
        while (n >= 0 && j <= 6) { // 如果你想要显示所有元素,可以移除j <= 6的条件  
            show.innerHTML += "<div>" + arr[n] + "</div>";  
            --n;  
            ++j;  
        }  
    } else {  
        show.innerHTML += "<div>没有历史战绩</div>";  
    }  
}  
  
document.addEventListener("DOMContentLoaded", function() {  
    show.onclick = function() {  
        showH();  
    };  
});
        
        var arr = [];
        const checknum = function() {
            var myguess = guess.value - 0;
            ++i;
            if (myguess == num) {
                info.innerHTML = "😀,恭喜您,猜对了,幸运数字是:" + num;
                var over = new Date().getTime();
                var m = Math.floor((over - ks.getTime()) / 1000);
                info.innerHTML += "<br><br>共输入" + i + "次,用时" + m + "秒<br><br>Game Over";
                var t = ks.getFullYear() + "-" + (ks.getMonth() + 1) + "-" + ks.getDate() + " ";
                t += ks.getHours() + ":" + ks.getMinutes() + ":" + ks.getSeconds();
                var his = t + "开始-输入" + i + "次-用时" + m + "秒";
                if (localStorage.getItem("arr")) {
                    arr = localStorage.getItem("arr").split(",");
                }
                arr.push(his);
                localStorage.setItem("arr", arr.join(","));
                again.style.display = "block";
                return;
            }
            if (myguess < num) {
                info.innerHTML = "😀,第" + i + "次输入,您猜的数字" + myguess + "有点小";
            } else {
                info.innerHTML = "😀,第" + i + "次输入,您猜的数字" + myguess + "有点大";
            }
            if (i >= 10) {
                info.innerHTML += "您已经没有机会了,真遗憾!";
                start.disabled = true;
                again.style.display = "block";
            }
        }
        
        // 添加猜数字按钮点击事件处理程序
        start.onclick = function() {
            checknum();
        }
    </script>
</body>
</html>

/* styles.css */
body {
    font-family: Arial, sans-serif;
    background: linear-gradient(to right, #8EC5FC, #E0C3FC);
}

.game-container {
    background-color: white;
    border-radius: 10px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    padding: 2rem;
    width: 80%;
    max-width: 600px;
    text-align: center;
}

.input-group {
    position: relative;
    margin-bottom: 1rem;
}

input[type="number"] {
    width: 15%;
    padding: 0.8rem 1rem;
    border: 1px solid #ddd;
    border-radius: 5px;
    transition: all 0.3s ease;
}

input:focus {
    outline: none;
    border-color: #64B5F6;
}

button {
    background-color: #64B5F6;
    color: white;
    border: none;
    padding: 0.8rem 1.5rem;
    border-radius: 5px;
    cursor: pointer;
    transition: all 0.3s ease;
}

button:hover {
    background-color: #2196F3;
}

#info {
    margin-bottom: 1rem;
}

#again, #show {
    margin-top: 1rem;
    padding: 0.5rem 1rem;
    background-color:#E0C3FC;
    color: white;
    border-radius: 5px;
    cursor: pointer;
    width: 300px;
    transition: all 0.3s ease;
}

#show:hover, #again:hover {
    background-color: #E0C3FC
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值