这是编写完打开之后的图片
代码给你们咯(拿完代码不要忘记点赞哟))
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>猜密码游戏</title>
<style>
body {
font-family: "Comic Sans MS", cursive, sans-serif;
background-color: #fdeff9;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
border-radius: 20px;
padding: 20px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
width: 320px;
text-align: center;
}
h1 {
margin-top: 0;
color: #ff0099;
font-size: 30px;
text-shadow: 2px 2px 2px #ff99cc;
}
.clues-container {
margin-bottom: 20px;
}
.form-container {
display: flex;
flex-direction: column;
align-items: center;
}
.password-input {
width: 200px;
padding: 10px;
margin-bottom: 20px;
border: 2px solid #ff99cc;
border-radius: 20px;
font-size: 18px;
}
.submit-button {
padding: 10px 30px;
background-color: #ff0099;
color: #fff;
border: none;
cursor: pointer;
border-radius: 20px;
font-size: 20px;
text-shadow: 1px 1px 1px #ff99cc;
}
.submit-button:hover {
background-color: #ff33cc;
}
.error-message {
color: #ff0000;
margin-top: 10px;
font-size: 16px;
}
</style>
</head>
<body>
<div class="container">
<h1>猜密码游戏</h1>
<div id="clues" class="clues-container">
<!-- 线索将显示在这里 -->
</div>
<div id="passwordForm" class="form-container">
<label for="passwordInput">请输入4位数字密码:</label>
<input type="text" id="passwordInput" class="password-input" pattern="[0-9]{4}" maxlength="4" autocomplete="off" required>
<button onclick="checkPassword()" class="submit-button">提交</button>
<div id="error" class="error-message" aria-live="assertive"></div>
</div>
</div>
<script>
const password = Math.floor(1000 + Math.random() * 9000).toString(); // 生成一个随机的4位数字密码
const cluesDiv = document.getElementById('clues');
const errorDiv = document.getElementById('error');
function checkPassword() {
const guess = document.getElementById('passwordInput').value;
if (guess.length !== 4 || isNaN(guess)) {
showError('请输入有效的4位数字。');
return;
}
let correctDigits = 0;
for (let i = 0; i < 4; i++) {
if (guess[i] === password[i]) {
correctDigits++;
}
}
if (correctDigits === 4) {
showSuccess('恭喜!您猜对了密码:' + password);
} else {
showClue(`您的猜测 ${guess} 有 ${correctDigits} 个数字正确。`);
}
}
function showError(message) {
errorDiv.textContent = message;
errorDiv.style.display = 'block';
}
function showSuccess(message) {
alert(message);
}
function showClue(clue) {
const clueParagraph = document.createElement('p');
clueParagraph.textContent = clue;
cluesDiv.appendChild(clueParagraph);
}
</script>
</body>
</html>