console
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>中英互译正误判断双人比赛</title>
<style>
body {
font-family: Arial, sans-serif;
}
#container {
max-width: 800px;
margin: 0 auto;
text-align: center;
}
h1 {
font-size: 32px;
margin-bottom: 20px;
}
p {
font-size: 18px;
margin-bottom: 10px;
}
input[type=text] {
padding: 10px;
width: 400px;
font-size: 18px;
margin-right: 10px;
}
button {
padding: 10px;
font-size: 18px;
background-color: #008CBA;
color: #fff;
border: none;
cursor: pointer;
}
button:hover {
background-color: #006d87;
}
.result-correct {
color: green;
font-weight: bold;
}
.result-incorrect {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<div id="container">
<h1>中英互译双人比赛</h1>
<p>游戏规则:系统会随机给出一个中文或英文单词,两名选手轮流进行翻译并进行正误判断,每次翻译正确加1分,错误不得分并扣除1分,时间到后得分高的选手获胜。</p>
<p>倒计时:<span id="countdown">60</span> 秒</p>
<p>选手1得分:<span id="score1">0</span></p>
<p>选手2得分:<span id="score2">0</span></p>
<p id="word" style="display: none;"></p>
<label for="input">请输入翻译:</label>
<input type="text" name="input" id="input">
<button id="submit">提交</button>
<p>翻译结果:<span id="result"></span></p>
<button id="start">开始游戏</button>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
// 用户自定义的翻译方案
const translations = {
'hello': '你好',
'world': '世界',
'good morning': '早上好',
'good afternoon': '下午好',
'good evening': '晚上好',
'how are you': '你好吗',
'thank you': '谢谢',
'excuse me': '请问',
'sorry': '对不起',
'bye': '再见',
'中国': 'China',
'北京': 'Beijing',
'上海': 'Shanghai',
'深圳': 'Shenzhen',
'广州': 'Guangzhou',
'美国': 'America',
'纽约': 'New York',
'加州': 'California',
'得克萨斯州': 'Texas',
'华盛顿州': 'Washington'
};
// 随机单词列表
const words = [
'hello', 'world', 'good morning', 'good afternoon', 'good evening',
'how are you', 'thank you', 'excuse me', 'sorry', 'bye',
'中国', '北京', '上海', '深圳', '广州',
'美国', '纽约', '加州', '得克萨斯州', '华盛顿州'
];
// 初始化得分、计时器和单词
let score1 = 0;
let score2 = 0;
let timer;
let word;
// 获取DOM元素
const $countdown = $('#countdown');
const $score1 = $('#score1');
const $score2 = $('#score2');
const $word = $('#word');
const $input = $('#input');
const $submit = $('#submit');
const $result = $('#result');
// 切换选手
function switchPlayer() {
if (score1 + score2 === 0 || score1 === score2) {
// 第一轮或得分相同,切换到选手1
$word.text('请选手1翻译:');
} else {
// 得分不同,切换到选手2
$word.text('请选手2翻译:');
}
}
// 提交按钮的点击事件
$submit.on('click', function() {
const text = $input.val().trim();
if (text === '') {
alert('请输入翻译');
return;
}
// 清空输入框
$input.val('');
// 判断翻译是否正确,并更新得分、结果和下一个单词
if (text === translations[word]) {
// 翻译正确
if (score1 + score2 === 0 || score1 === score2) {
score1++;
$score1.text(score1);
} else {
score2++;
$score2.text(score2);
}
$result.text('正确').addClass('result-correct').removeClass('result-incorrect');
switchPlayer();
nextWord();
} else {
// 翻译错误
if (score1 + score2 === 0 || score1 === score2) {
score1 = Math.max(score1 - 1, 0);
$score1.text(score1);
} else {
score2 = Math.max(score2 - 1, 0);
$score2.text(score2);
}
$result.text('错误').addClass('result-incorrect').removeClass('result-correct');
}
});
// 下一个单词
function nextWord() {
word = words[Math.floor(Math.random() * words.length)];
$word.text('请翻译:' + word);
}
// 开始游戏
function startGame() {
nextWord();
// 修改部分如下
$word.show();
// 开始计时器
let timeLeft = parseInt($countdown.text());
timer = setInterval(function() {
timeLeft--;
$countdown.text(timeLeft);
if (timeLeft === 0) {
clearInterval(timer);
$submit.prop('disabled', true);
let resultText;
if (score1 === score2) {
resultText = '比赛结束,双方得分相同,平局!';
} else {
const winner = score1 > score2 ? '选手1' : '选手2';
resultText = '比赛结束,' + winner + '获胜!\n\n选手1得分:' + score1 + '\n选手2得分:' + score2;
}
alert(resultText);
}
}, 1000);
}
// 点击开始按钮开始游戏
$('#start').on('click', function() {
startGame();
$('#start').remove();
});
</script>
</body>
</html>