const balloon = document.getElementById('balloon'); const scoreDisplay = document.getElementById('score'); let score = 0;
balloon.addEventListener('click', () => {
score++;
scoreDisplay.textContent = score;
balloon.style.left = Math.random() * 80 + 'vw';
balloon.style.top = Math.random() * 80 + 'vh';
});
function popUpMole() {
const holes = document.querySelectorAll('.hole');
const randomHole = holes[Math.floor(Math.random() * holes.length)];
randomHole.classList.add('up');
setTimeout(() => {
randomHole.classList.remove('up');
if(!gameOver) popUpMole();
}, 800);
}
// 画个笑脸 ctx.beginPath(); ctx.arc(100, 100, 50, 0, Math.PI * 2); // 脸 ctx.fillStyle = 'yellow'; ctx.fill();
ctx.beginPath(); ctx.arc(80, 80, 10, 0, Math.PI 2); // 左眼 ctx.arc(120, 80, 10, 0, Math.PI 2); // 右眼 ctx.fillStyle = 'black'; ctx.fill();
ctx.beginPath(); ctx.arc(100, 110, 30, 0, Math.PI); // 微笑的嘴 ctx.lineWidth = 3; ctx.stroke();
class Entity {
constructor() {
this.components = {};
}
addComponent(component) {
this.components[component.name] = component;
component.entity = this;
}
getComponent(name) {
return this.components[name];
}
}
class PositionComponent {
constructor(x, y) {
this.name = 'position';
this.x = x;
this.y = y;
}
}
function moveRight(board) {
let moved = false;
for(let row = 0; row < 4; row++) {
// 移除空格并紧凑排列
let compact = board[row].filter(cell => cell !== 0);
// 合并相邻相同数字
for(let i = compact.length-1; i > 0; i--) {
if(compact[i] === compact[i-1]) {
compact[i] *= 2;
compact[i-1] = 0;
moved = true;
}
}
// 再次紧凑排列
compact = compact.filter(cell => cell !== 0);
// 补齐空格
while(compact.length < 4) compact.unshift(0);
board[row] = compact;
}
return moved;
}
标签: #JavaScript游戏开发 #互动游戏编程 #前端游戏设计 #JavaScript实例教程 #游戏开发初学者指南