<!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 {
margin: 0;
padding: 0;
background: #2c3e50;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: Arial, sans-serif;
}
#gameContainer {
background: #34495e;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0,0,0,0.5);
}
#gameBoard {
display: grid;
grid-template-columns: repeat(8, 60px);
grid-template-rows: repeat(6, 60px);
gap: 5px;
background: #1a252f;
padding: 10px;
border-radius: 5px;
}
.glass {
background: linear-gradient(135deg, #87CEEB 0%, #4682B4 100%);
border: 2px solid #5F9EA0;
border-radius: 5px;
cursor: pointer;
position: relative;
transition: all 0.3s ease;
box-shadow: inset 0 0 10px rgba(255,255,255,0.3);
}
.glass:hover {
transform: scale(1.05);
box-shadow: 0 0 15px rgba(135, 206, 235, 0.8);
}
.glass.cracked {
background: linear-gradient(135deg, #B0C4DE 0%, #708090 100%);
animation: crack 0.5s ease-out;
}
.glass.broken {
background: transparent;
border: 2px dashed #7f8c8d;
pointer-events: none;
}
@keyframes crack {
0% { transform: scale(1); }
50% { transform: scale(1.1) rotate(5deg); }
100% { transform: scale(1); }
}
.crack-lines {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.crack-lines::before,
.crack-lines::after {
content: '';
position: absolute;
background: #2c3e50;
opacity: 0.7;
}
.crack-lines::before {
width: 2px;
height: 80%;
top: 10%;
left: 30%;
transform: rotate(45deg);
}
.crack-lines::after {
width: 2px;
height: 60%;
top: 20%;
left: 60%;
transform: rotate(-30deg);
}
#scoreBoard {
text-align: center;
color: white;
margin-bottom: 20px;
}
#score {
font-size: 24px;
font-weight: bold;
color: #3498db;
}
#message {
font-size: 18px;
margin-top: 10px;
height: 30px;
}
button {
background: #3498db;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
margin-top: 10px;
transition: background 0.3s ease;
}
button:hover {
background: #2980b9;
}
.hammer {
position: absolute;
width: 40px;
height: 40px;
pointer-events: none;
z-index: 1000;
display: none;
}
.hammer::before {
content: '🔨';
font-size: 30px;
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="scoreBoard">
<h1>敲玻璃游戏</h1>
<div id="score">得分: 0</div>
<div id="message"></div>
<button onclick="resetGame()">重新开始</button>
</div>
<div id="gameBoard"></div>
</div>
<div class="hammer" id="hammer"></div>
<script>
let score = 0;
let gameBoard = document.getElementById('gameBoard');
let scoreDisplay = document.getElementById('score');
let messageDisplay = document.getElementById('message');
let hammer = document.getElementById('hammer');
let glasses = [];
// 创建游戏板
function createBoard() {
gameBoard.innerHTML = '';
glasses = [];
for (let i = 0; i < 48; i++) {
let glass = document.createElement('div');
glass.className = 'glass';
glass.dataset.index = i;
glass.dataset.state = 'intact';
glass.addEventListener('click', hitGlass);
glass.addEventListener('mouseenter', showHammer);
glass.addEventListener('mouseleave', hideHammer);
gameBoard.appendChild(glass);
glasses.push(glass);
}
}
// 敲玻璃
function hitGlass(e) {
let glass = e.target;
let state = glass.dataset.state;
if (state === 'broken') return;
if (state === 'intact') {
glass.dataset.state = 'cracked';
glass.classList.add('cracked');
let crackLines = document.createElement('div');
crackLines.className = 'crack-lines';
glass.appendChild(crackLines);
score += 10;
showMessage('裂开了!+10分');
// 添加破碎音效(使用Web Audio API模拟)
playBreakSound();
} else if (state === 'cracked') {
glass.dataset.state = 'broken';
glass.classList.add('broken');
glass.innerHTML = '';
score += 20;
showMessage('破碎了!+20分');
playShatterSound();
}
updateScore();
checkWin();
}
// 显示锤子光标
function showHammer(e) {
hammer.style.display = 'block';
hammer.style.left = e.pageX - 20 + 'px';
hammer.style.top = e.pageY - 20 + 'px';
}
// 隐藏锤子光标
function hideHammer() {
hammer.style.display = 'none';
}
// 鼠标移动时更新锤子位置
document.addEventListener('mousemove', (e) => {
if (hammer.style.display === 'block') {
hammer.style.left = e.pageX - 20 + 'px';
hammer.style.top = e.pageY - 20 + 'px';
}
});
// 更新分数
function updateScore() {
scoreDisplay.textContent = `得分: ${score}`;
}
// 显示消息
function showMessage(msg) {
messageDisplay.textContent = msg;
setTimeout(() => {
messageDisplay.textContent = '';
}, 1000);
}
// 检查是否获胜
function checkWin() {
let brokenCount = glasses.filter(g => g.dataset.state === 'broken').length;
if (brokenCount === glasses.length) {
showMessage('恭喜!所有玻璃都被敲碎了!');
setTimeout(() => {
alert(`游戏结束!最终得分:${score}`);
}, 500);
}
}
// 重置游戏
function resetGame() {
score = 0;
updateScore();
createBoard();
showMessage('游戏重新开始!');
}
// 模拟破碎音效
function playBreakSound() {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.frequency.value = 200;
oscillator.type = 'square';
gainNode.gain.setValueAtTime(0.1, audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + 0.1);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.1);
}
// 模拟粉碎音效
function playShatterSound() {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
for (let i = 0; i < 3; i++) {
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.frequency.value = 100 + Math.random() * 200;
oscillator.type = 'sawtooth';
gainNode.gain.setValueAtTime(0.1, audioContext.currentTime + i * 0.05);
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + i * 0.05 + 0.1);
oscillator.start(audioContext.currentTime + i * 0.05);
oscillator.stop(audioContext.currentTime + i * 0.05 + 0.1);
}
}
// 初始化游戏
createBoard();
</script>
</body>
</html>