好的,以下是一个简单的贪食蛇和乒乓球组合成的新游戏的代码,基于HTML、CSS和JavaScript编写:
HTML代码:
<!DOCTYPE html>
<html>
<head>
<title>贪食球</title>
<meta charset="utf-8">
<style>
#game-board {
border: 1px solid black;
width: 400px;
height: 400px;
position: relative;
}
#snake {
position: absolute;
top: 0;
left: 0;
background-color: green;
}
#ball {
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
width: 20px;
height: 20px;
background-color: red;
border-radius: 50%;
}
</style>
</head>
<body>
<h1>贪食球</h1>
<p>得分: <span id="score">0</span></p>
<div id="game-board">
<div id="snake"></div>
<div id="ball"></div>
</div>
<script src="game.js"></script>
</body>
</html>
JavaScript代码:
const board = document.getElementById('game-board');
const snake = document.getElementById('snake');
const ball = document.getElementById('ball');
const scoreDisplay = document.getElementById('score');
const boardSize = 400;
const snakeSize = 20;
const ballSize = 20;
const ballSpeed = 5;
const foodInterval = 5000;
const snakeSpeed = 100;
const initialSnakeLength = 3;
let x = 0;
let y = 0;
let dx = 1;
let dy = 1;
let score = 0;
let snakeLength = initialSnakeLength;
let snakeCoords = [];
function init() {
// 设置蛇的初始坐标
for(let i = 1; i <= snakeLength; i++) {
snakeCoords.push([x, y]);
y += snakeSize;
}
}
function moveSnake() {
// 删除蛇尾
snakeCoords.shift();
// 添加蛇头
snakeCoords.push([x, y]);
// 更新蛇的位置
snake.style.top = y + 'px';
snake.style.left = x + 'px';
}
function checkCollision() {
// 如果球撞到了墙壁,x/y变换方向
if(x < 0 || x + ballSize > boardSize) {
dx = -dx;
}
if(y < 0 || y + ballSize > boardSize) {
dy = -dy;
}
// 如果球撞到了蛇身,游戏结束
snakeCoords.forEach(coord => {
const [cx, cy] = coord;
if(x < cx + snakeSize && x + ballSize > cx &&
y < cy + snakeSize && y + ballSize > cy) {
clearInterval(game);
alert('游戏结束,得分:' + score);
}
});
}
function moveBall() {
// 更新球的位置
x += dx * ballSpeed;
y += dy * ballSpeed;
ball.style.top = y + 'px';
ball.style.left = x + 'px';
// 碰撞检测
checkCollision();
// 如果球被吃掉,更新分数和蛇的长度
if(x < 0 || x + ballSize > boardSize ||
y < 0 || y + ballSize > boardSize) {
score++;
scoreDisplay.innerText = score;
snakeLength++;
snakeCoords.push([x, y]);
// 随机生成下一个食物的位置
x = Math.floor(Math.random() * (boardSize - ballSize));
y = Math.floor(Math.random() * (boardSize - ballSize));
ball.style.top = y + 'px';
ball.style.left = x + 'px';
}
}
// 初始化游戏
init();
// 每隔foodInterval毫秒生成一个新的球
setInterval(() => {
x = Math.floor(Math.random() * (boardSize - ballSize));
y = Math.floor(Math.random() * (boardSize - ballSize));
ball.style.top = y + 'px';
ball.style.left = x + 'px';
}, foodInterval);
// 每隔snakeSpeed毫秒更新蛇的位置
const game = setInterval(() => {
moveSnake();
}, snakeSpeed);
// 监听键盘事件,改变蛇的运动方向
document.addEventListener('keydown', (event) => {
switch(event.keyCode) {
// 上
case 38:
dx = 0;
dy = -1;
break;
// 下
case 40:
dx = 0;
dy = 1;
break;
// 左
case 37:
dx = -1;
dy = 0;
break;
// 右
case 39:
dx = 1;
dy = 0;
break;
}
});
CSS代码:
#game-board {
border: 1px solid black;
width: 400px;
height: 400px;
position: relative;
}
#snake {
position: absolute;
top: 0;
left: 0;
background-color: green;
width: 20px;
height: 20px;
}
#ball {
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
width: 20px;
height: 20px;
background-color: red;
border-radius: 50%;
}
这只是一个简单的游戏示例代码,您可以根据自己的需求进行修改和扩展。希望对您有所帮助。