|
|
非常抱歉,我提供的代码没有完整实现应该显示的蛇。以下是完整的多人贪吃蛇游戏代码,包括创建蛇、食物、方向控制及碰撞检测等功能。
我们将创建一个简单的贪吃蛇游戏,其中有两个玩家,每个玩家都有自己的蛇,场地也会比较大,食物数量也相对较多。
### HTML文件(index.html):
- <!DOCTYPE html>
- <html lang="zh">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="styles.css">
- <title>多人贪吃蛇</title>
- </head>
- <body>
- <h1>多人贪吃蛇</h1>
- <canvas id="gameCanvas" width="800" height="600"></canvas>
- <div id="gameOver" style="display: none;">
- <p id="score">得分: </p>
- <button id="restartButton">重新开始游戏</button>
- </div>
- <script src="script.js"></script>
- </body>
- </html>
复制代码
### CSS文件(styles.css):
- body {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- height: 100vh;
- margin: 0;
- background-color: #f0f0f0;
- font-family: Arial, sans-serif;
- }
- h1 {
- color: #333;
- }
- canvas {
- border: 1px solid #333;
- }
复制代码
### JavaScript文件(script.js):
- const canvas = document.getElementById("gameCanvas");
- const ctx = canvas.getContext("2d");
- const restartButton = document.getElementById("restartButton");
- const gameOverDiv = document.getElementById("gameOver");
- const scoreDisplay = document.getElementById("score");
- const box = 20;
- const rows = canvas.height / box;
- const columns = canvas.width / box;
- let snakes = [];
- let foods = [];
- let directions = [];
- let scores = [];
- let gameInterval;
- // 初始化玩家
- function initPlayers(num) {
- for (let i = 0; i < num; i++) {
- snakes[i] = [{ x: Math.floor(columns / 2) * box, y: Math.floor(rows / 2) * box }];
- directions[i] = { x: 0, y: 0 };
- scores[i] = 0;
- spawnFood();
- }
- }
- // 随机生成食物
- function spawnFood() {
- const food = {
- x: Math.floor(Math.random() * columns) * box,
- y: Math.floor(Math.random() * rows) * box
- };
- foods.push(food);
- }
- // 开始游戏
- function startGame() {
- initPlayers(2); // 初始化两个玩家
- gameOverDiv.style.display = "none";
- gameInterval = setInterval(draw, 100);
- }
- document.addEventListener("keydown", changeDirection);
- restartButton.addEventListener("click", startGame);
- function changeDirection(event) {
- // 控制第一个玩家(WASD)
- if (event.key === 'w' && directions[0].y !== 1) {
- directions[0] = { x: 0, y: -1 };
- } else if (event.key === 's' && directions[0].y !== -1) {
- directions[0] = { x: 0, y: 1 };
- } else if (event.key === 'a' && directions[0].x !== 1) {
- directions[0] = { x: -1, y: 0 };
- } else if (event.key === 'd' && directions[0].x !== -1) {
- directions[0] = { x: 1, y: 0 };
- }
- // 控制第二个玩家(箭头键)
- if (event.key === 'ArrowUp' && directions[1].y !== 1) {
- directions[1] = { x: 0, y: -1 };
- } else if (event.key === 'ArrowDown' && directions[1].y !== -1) {
- directions[1] = { x: 0, y: 1 };
- } else if (event.key === 'ArrowLeft' && directions[1].x !== 1) {
- directions[1] = { x: -1, y: 0 };
- } else if (event.key === 'ArrowRight' && directions[1].x !== -1) {
- directions[1] = { x: 1, y: 0 };
- }
- }
- function draw() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- for (let i = 0; i < snakes.length; i++) {
- const snake = snakes[i];
- const headX = snake[0].x + directions[i].x * box;
- const headY = snake[0].y + directions[i].y * box;
-
- // 检测边界和碰撞
- if (headX < 0 || headX >= canvas.width || headY < 0 || headY >= canvas.height || collision(headX, headY, snake)) {
- endGame();
- return;
- }
- snake.unshift({ x: headX, y: headY });
-
- if (foods.length > 0 && headX === foods[0].x && headY === foods[0].y) {
- scores[i] += 10;
- foods.shift();
- spawnFood();
- } else {
- snake.pop();
- }
- // 绘制蛇
- for (let segment of snake) {
- ctx.fillStyle = i === 0 ? "green" : "blue"; // 第一个玩家为绿色,第二个玩家为蓝色
- ctx.fillRect(segment.x, segment.y, box, box);
- }
- }
- // 绘制食物
- for (let food of foods) {
- ctx.fillStyle = "red";
- ctx.fillRect(food.x, food.y, box, box);
- }
- // 更新得分显示
- scoreDisplay.textContent = `得分: 玩家1 - ${scores[0]}, 玩家2 - ${scores[1]}`;
- }
- function collision(x, y, snake) {
- return snake.some(part => part.x === x && part.y === y);
- }
- function endGame() {
- clearInterval(gameInterval);
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- gameOverDiv.style.display = "block";
- scoreDisplay.textContent += `\n游戏结束!`;
- }
- // 开始游戏
- startGame();
复制代码
### 代码说明:
- **玩家控制**:玩家1用WSAD控制,玩家2用箭头键控制。
- **随机食物**:每当一个玩家吃掉一个食物时,都会随机生成一个新的食物。
- **碰撞检测**:包括墙壁和自身的碰撞检测。
- **分数系统**:会显示每个玩家的得分。
### 使用说明:
1. 将以上代码分别保存到三个不同的文件(`index.html`、`styles.css` 和 `script.js`)中。
2. 确保这些文件都在同一目录下,打开 `index.html` 文件,即可在浏览器中运行游戏。
现在,你可以享受到一个能够同时支持两个玩家的贪吃蛇游戏。如果有任何问题或需要进一步的功能,也请随时告诉我! |
|