|
|
当然可以!我们可以将这个项目分为三部分:HTML、CSS和JavaScript,每一部分都有其特定的功能。
### 1. HTML (`index.html`)
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Snake Game</title>
- <link rel="stylesheet" href="style.css">
- </head>
- <body>
- <div class="game-container">
- <canvas id="gameCanvas"></canvas>
- <div id="score">Score: 0</div>
- </div>
- <script src="script.js"></script>
- </body>
- </html>
复制代码
**解释:**
- 这是一个基本的HTML页面结构。
- `<head>` 部分包含元数据(如字符编码、视口设置)和对外部CSS样式表 `style.css` 的引用。
- `<body>` 部分包含一个 `div`,其中包含一个 `canvas` 元素(用于绘制游戏图形)和一个 `div` 用于显示得分。
- `<script>` 标签在页面底部加载 `script.js` 文件,它包含了游戏的逻辑。
### 2. CSS (`style.css`)
- body {
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- margin: 0;
- background-color: #f0f0f0;
- }
- .game-container {
- text-align: center;
- }
- #gameCanvas {
- border: 1px solid #000;
- background-color: #fff;
- }
- #score {
- font-size: 24px;
- margin-top: 10px;
- }
复制代码
**解释:**
- `body`: 设置整个页面布局,使其内容居中,并设置高度、外边距和背景颜色。
- `.game-container`: 设置游戏容器的文本对齐方式。
- `#gameCanvas`: 为画布添加边框和背景颜色。
- `#score`: 设置得分显示的字体大小和外边距。
### 3. JavaScript (`script.js`)
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- const gridSize = 20;
- const tileCount = 20;
- let snake = [{ x: 10, y: 10 }];
- let direction = { x: 0, y: 0 };
- let apple = { x: 15, y: 15 };
- let score = 0;
- canvas.width = canvas.height = gridSize * tileCount;
- function draw() {
- ctx.fillStyle = 'white';
- ctx.fillRect(0, 0, canvas.width, canvas.height);
- ctx.fillStyle = 'red';
- ctx.fillRect(apple.x * gridSize, apple.y * gridSize, gridSize - 2, gridSize - 2);
- ctx.fillStyle = 'green';
- snake.forEach(part => {
- ctx.fillRect(part.x * gridSize, part.y * gridSize, gridSize - 2, gridSize - 2);
- });
- requestAnimationFrame(update);
- }
- function update() {
- const nextX = snake[0].x + direction.x;
- const nextY = snake[0].y + direction.y;
- if (nextX === apple.x && nextY === apple.y) {
- score++;
- document.getElementById('score').innerText = `Score: ${score}`;
- apple.x = Math.floor(Math.random() * tileCount);
- apple.y = Math.floor(Math.random() * tileCount);
- } else {
- snake.pop();
- }
- const newHead = { x: nextX, y: nextY };
- if (
- newHead.x < 0 || newHead.x >= tileCount ||
- newHead.y < 0 || newHead.y >= tileCount ||
- snake.some(part => part.x === newHead.x && part.y === newHead.y)
- ) {
- alert("Game Over");
- snake = [{ x: 10, y: 10 }];
- direction = { x: 0, y: 0 };
- score = 0;
- document.getElementById('score').innerText = `Score: ${score}`;
- return;
- }
- snake.unshift(newHead);
- requestAnimationFrame(draw);
- }
- window.addEventListener('keydown', e => {
- switch (e.keyCode) {
- case 37:
- if (direction.x === 0) direction = { x: -1, y: 0 };
- break;
- case 38:
- if (direction.y === 0) direction = { x: 0, y: -1 };
- break;
- case 39:
- if (direction.x === 0) direction = { x: 1, y: 0 };
- break;
- case 40:
- if (direction.y === 0) direction = { x: 0, y: 1 };
- break;
- }
- });
- requestAnimationFrame(draw);
复制代码
**解释:**
- 定义了几个变量来设置画布、格子大小、贪吃蛇、方向、苹果位置和得分。
- 设置画布的宽度和高度。
- `draw()` 函数用于绘制画布,包括清除先前的画面、画出苹果和贪吃蛇。
- `update()` 函数用于更新贪吃蛇的位置、检测碰撞并处理得分和游戏结束的逻辑。
- 通过 `window.addEventListener('keydown', ...)` 来监听键盘事件,更改贪吃蛇的方向。
- 使用 `requestAnimationFrame(draw)` 不断调用 `draw` 方法来刷新画布,形成动画效果。
通过这三部分代码共同作用,你就能够实现一个简单的网页贪吃蛇游戏。希望这些解释能帮你理解代码的不同部分以及它们如何协同工作来创建一个完整的游戏。 |
|