|
|
抱歉让你感到沮丧!我理解代码可能没有按你的预期工作,我会尽力帮助你修复这个问题。以下是一个完整、简化的多人贪吃蛇游戏示例,确保能够正常运行。
### 完整的多人贪吃蛇代码
#### 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;">
- <h2>游戏结束!</h2>
- <button id="restartButton">重新开始游戏</button>
- <p id="score"></p>
- </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 scores = [];
- let gameInterval;
- // 初始化玩家和食物
- function initGame() {
- snakes = [
- [{ x: 4 * box, y: 4 * box }],
- [{ x: 10 * box, y: 10 * box }]
- ];
- scores = [0, 0];
- foods = [];
- spawnFood();
- gameOverDiv.style.display = "none";
- gameInterval = setInterval(draw, 100);
- }
- // 随机生成食物
- function spawnFood() {
- const food = {
- x: Math.floor(Math.random() * columns) * box,
- y: Math.floor(Math.random() * rows) * box
- };
- foods.push(food);
- }
- // 开始游戏
- function startGame() {
- initGame();
- }
- // 控制方向
- document.addEventListener("keydown", changeDirection);
- let directions = [
- { x: 1, y: 0 }, // 玩家1初始右
- { x: 0, y: 1 } // 玩家2初始下
- ];
- function changeDirection(event) {
- if (event.key === 'w' && directions[0].y !== 1) {
- directions[0] = { x: 0, y: -1 }; // 玩家1上
- } else if (event.key === 's' && directions[0].y !== -1) {
- directions[0] = { x: 0, y: 1 }; // 玩家1下
- } else if (event.key === 'a' && directions[0].x !== 1) {
- directions[0] = { x: -1, y: 0 }; // 玩家1左
- } else if (event.key === 'd' && directions[0].x !== -1) {
- directions[0] = { x: 1, y: 0 }; // 玩家1右
- }
-
- if (event.key === 'ArrowUp' && directions[1].y !== 1) {
- directions[1] = { x: 0, y: -1 }; // 玩家2上
- } else if (event.key === 'ArrowDown' && directions[1].y !== -1) {
- directions[1] = { x: 0, y: 1 }; // 玩家2下
- } else if (event.key === 'ArrowLeft' && directions[1].x !== 1) {
- directions[1] = { x: -1, y: 0 }; // 玩家2左
- } else if (event.key === 'ArrowRight' && directions[1].x !== -1) {
- directions[1] = { x: 1, y: 0 }; // 玩家2右
- }
- }
- function draw() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- // 画食物
- for (let food of foods) {
- ctx.fillStyle = "red";
- ctx.fillRect(food.x, food.y, box, box);
- }
- 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;
- }
- const newHead = { x: headX, y: headY };
- snake.unshift(newHead); // 添加新头
- // 检测食物
- if (foods.length > 0 && headX === foods[0].x && headY === foods[0].y) {
- scores[i] += 10;
- foods.shift(); // 吃掉食物
- spawnFood(); // 生成新食物
- } else {
- snake.pop(); // 移除尾部
- }
- // 绘制蛇
- ctx.fillStyle = i === 0 ? "green" : "blue"; // 玩家1绿色,玩家2蓝色
- for (let segment of snake) {
- ctx.fillRect(segment.x, segment.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 += ` 游戏结束!`;
- }
- // 重新开始游戏
- restartButton.addEventListener("click", startGame);
- // 开始游戏
- startGame();
复制代码
### 说明:
1. **控制**:
- 玩家1使用WASD键来控制。
- 玩家2使用箭头键来控制。
2. **食物**:
- 食物会随机生成,玩家每吃一个食物得10分。
3. **边界和碰撞检测**:
- 如果蛇碰到边界或自身,游戏结束。
4. **得分显示**:
- 游戏页面的下方会显示每个玩家的得分。
### 使用说明:
1. 将以上代码分别保存到三个不同的文件(`index.html`、`styles.css` 和 `script.js`)中。
2. 确保这些文件都在同一目录下,打开 `index.html` 文件,即可在浏览器中运行游戏。
如果遇到任何问题或错误,请告诉我,我会尽快帮你解决! |
|