小游戏html代码
时间: 2023-06-30 13:15:57 浏览: 43
以下是一个简单的猜数字小游戏的 HTML 代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>猜数字小游戏</title>
</head>
<body>
<h1>猜数字小游戏</h1>
<p>在 1 到 100 之间猜一个数字:</p>
<input type="number" id="guess" min="1" max="100">
<button onclick="checkGuess()">提交</button>
<p id="message"></p>
<script>
var answer = Math.floor(Math.random() * 100) + 1;
var attempts = 0;
function checkGuess() {
var guess = parseInt(document.getElementById("guess").value);
if (isNaN(guess) || guess < 1 || guess > 100) {
document.getElementById("message").innerHTML = "请输入1到100之间的数字。";
} else if (guess == answer) {
attempts++;
document.getElementById("message").innerHTML = "恭喜你!你猜对了,你用了 " + attempts + " 次机会。";
} else if (guess < answer) {
attempts++;
document.getElementById("message").innerHTML = "你猜的数字太小了,请再试一次。";
} else {
attempts++;
document.getElementById("message").innerHTML = "你猜的数字太大了,请再试一次。";
}
}
</script>
</body>
</html>
```
该代码使用了 HTML、CSS 和 JavaScript 来创建一个简单的猜数字游戏。用户可以在输入框中输入猜测的数字,然后点击提交按钮,游戏会根据用户的猜测给出相应的提示,直到用户猜对为止。