帮我写一个稍微复杂一点的html游戏
时间: 2023-12-20 17:04:45 浏览: 27
好的,以下是一个简单的猜数字游戏:
<!DOCTYPE html>
<html>
<head>
<title>猜数字游戏</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
}
h1 {
font-size: 36px;
}
input[type="number"] {
padding: 10px;
border: none;
border-radius: 4px;
margin-bottom: 20px;
}
button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h1>猜数字游戏</h1>
<p>我已经想好了一个1到100之间的数字,请猜一下是多少?</p>
<input type="number" id="guess" min="1" max="100">
<button onclick="checkGuess()">猜一下</button>
<p id="result"></p>
<script>
// 生成1到100之间的随机数
var numberToGuess = Math.floor(Math.random() * 100) + 1;
// 检查猜测的数字是否正确
function checkGuess() {
var guess = document.getElementById("guess").value;
if (guess == numberToGuess) {
document.getElementById("result").innerHTML = "恭喜你,猜对了!";
} else if (guess < numberToGuess) {
document.getElementById("result").innerHTML = "不对,再大一点。";
} else {
document.getElementById("result").innerHTML = "不对,再小一点。";
}
}
</script>
</body>
</html>
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)