点击「开始游戏」挑战100个城市!
欢迎来到《猜地名挑战》!这是一个考验你地理知识的快节奏游戏。
祝你好运,地理大师!
开发这个增强版游戏需要掌握以下核心技术:
@keyframes)实现反馈信息的淡入和倒计时警告的脉冲效果。setInterval和clearInterval精确控制双重倒计时(每题倒计时和总游戏倒计时)。isPlaying)来控制游戏的开始、进行和结束状态。按照以下步骤,可以有条不紊地开发出这个游戏:
startGame():重置游戏状态,开始倒计时,显示第一题。displayNextQuestion():随机选择并显示下一题。checkAnswer():验证用户答案,更新分数,并处理后续逻辑。endGame():结束游戏,显示最终结果。以下是构成此页面的完整HTML、CSS和JavaScript代码。点击标题可展开/折叠查看。
<!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>
/* --- 此处省略了完整的CSS样式,实际代码中包含 --- */
</style>
</head>
<body>
<div class="container">
<!-- 此处省略了游戏区域的HTML,实际代码中包含 -->
</div>
<div class="container">
<!-- 此处省略了文档区域的HTML,实际代码中包含 -->
</div>
<script>
// --- 此处省略了完整的JavaScript代码,实际代码中包含 ---
</script>
</body>
</html>
/* --- 游戏和页面共用样式 --- */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
body {
background-color: #f0f2f5;
color: #333;
padding: 20px;
line-height: 1.6;
}
.container {
max-width: 900px;
margin: 0 auto;
background-color: #fff;
padding: 30px 40px;
border-radius: 12px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
margin-bottom: 30px;
}
h1, h2, h3 {
color: #1a73e8;
margin-bottom: 20px;
}
/* --- 此处省略了大量CSS样式,实际代码中包含 --- */
/* --- 代码块样式 --- */
.code-section {
background-color: #f8f9fa;
border-radius: 8px;
overflow: hidden;
margin: 20px 0;
}
.code-header {
background-color: #e8f0fe;
padding: 12px 15px;
font-weight: bold;
color: #1a73e8;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
}
.code-header::after {
content: "+ 显示代码";
font-size: 0.9em;
color: #5f6368;
}
.code-header.open::after {
content: "- 隐藏代码";
}
.code-content {
padding: 0;
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease, padding 0.3s ease;
}
.code-section.open .code-content {
padding: 15px;
max-height: 2000px;
}
pre {
white-space: pre-wrap;
word-break: break-all;
color: #24292e;
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
font-size: 0.95em;
}
// 1. 100个城市题库(涵盖国内外主要城市)
const cities = [
{ name: "北京", clue: "中国首都,拥有故宫、长城,2008年奥运会举办地" },
{ name: "上海", clue: "中国第一大城市,东方明珠塔是其标志性建筑" },
// ... (此处省略了98个城市的数据)
{ name: "惠灵顿", clue: "新西兰首都,风之城" }
];
// 2. 游戏状态变量
let score = 0;
let currentQuestionIndex = 0;
let isPlaying = false;
let totalTimeLeft = 120; // 总游戏时间:2分钟(120秒)
let questionTimeLeft = 10; // 每题答题时间:10秒
let totalTimer; // 总倒计时定时器
let questionTimer; // 每题倒计时定时器
let usedQuestions = []; // 已使用的题目索引(避免重复)
let totalQuestions = 10; // 每轮游戏题目数
// 3. 获取 DOM 元素
const clueElement = document.getElementById('clue');
const answerInputElement = document.getElementById('answer-input');
const startButtonElement = document.getElementById('start-btn');
const submitButtonElement = document.getElementById('submit-btn');
const feedbackElement = document.getElementById('feedback');
const scoreElement = document.getElementById('score');
const totalTimeElement = document.getElementById('total-time');
const questionProgressElement = document.getElementById('question-progress');
const gameOverElement = document.getElementById('game-over');
// 4. 工具函数
function formatTime(seconds) { /* ... */ }
function getRandomQuestion() { /* ... */ }
// 5. 游戏核心函数
function startGame() { /* ... */ }
function displayNextQuestion() { /* ... */ }
function resetQuestionTimer() { /* ... */ }
function startTotalTimer() { /* ... */ }
function handleTimeout() { /* ... */ }
function getCurrentQuestion() { /* ... */ }
function checkAnswer() { /* ... */ }
function endGame(message) { /* ... */ }
// 6. 绑定事件监听器
startButtonElement.addEventListener('click', startGame);
submitButtonElement.addEventListener('click', checkAnswer);
answerInputElement.addEventListener('keydown', function(event) { /* ... */ });
// 初始化页面显示
totalTimeElement.textContent = formatTime(totalTimeLeft);
questionProgressElement.textContent = `0/${totalQuestions}`;