|
|
好的,我会在代码中添加具体的注释,指导你如何添加图片。确保将图片文件放在与HTML、CSS和JavaScript文件相同的目录下。
### HTML (`index.html`)
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>贪吃蛇游戏</title>
- <link rel="stylesheet" href="style.css">
- </head>
- <body>
- <div id="game-container">
- <canvas id="game-board"></canvas>
- <div id="controls">
- <button id="start-button">开始游戏</button>
- <div id="game-over" class="hidden">游戏结束</div>
- </div>
- </div>
- <script src="script.js"></script>
- </body>
- </html>
复制代码
### CSS (`style.css`)
- body {
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- margin: 0;
- background-image: url('background.jpg'); /* 背景图片 */
- background-size: cover;
- }
- #game-container {
- display: flex;
- flex-direction: column;
- align-items: center;
- }
- #game-board {
- border: 1px solid #000;
- background-color: rgba(255, 255, 255, 0.6);
- }
- #controls {
- margin-top: 20px;
- text-align: center;
- }
- button {
- padding: 10px 20px;
- font-size: 16px;
- cursor: pointer;
- }
- .hidden {
- display: none;
- }
- #game-over {
- font-size: 24px;
- color: red;
- margin-top: 20px;
- }
复制代码
### JavaScript (`script.js`)
- const canvas = document.getElementById('game-board');
- const context = canvas.getContext('2d');
- const startButton = document.getElementById('start-button');
- const gameOverText = document.getElementById('game-over');
- canvas.width = 400;
- canvas.height = 400;
- const cellSize = 20;
- let snake = [{ x: 2 * cellSize, y: 2 * cellSize }]; // 初始位置
- let direction = { x: 0, y: 0 };
- let food = {};
- let gameInterval;
- // 加载蛇头和蛇身图像
- const snakeHeadImg = new Image();
- snakeHeadImg.src = 'snake-head.png'; // 蛇头图片位置
- const snakeBodyImg = new Image();
- snakeBodyImg.src = 'snake-body.png'; // 蛇身图片位置
- // 加载食物图像
- const foodImg = new Image();
- foodImg.src = 'food.png'; // 食物图片位置
- function drawBackground() {
- context.fillStyle = 'rgba(0, 128, 128, 0.5)';
- context.fillRect(0, 0, canvas.width, canvas.height);
- }
- function drawSnake() {
- for (let i = 0; i < snake.length; i++) {
- const part = snake[i];
- if (i === 0) { // 画蛇头
- context.drawImage(snakeHeadImg, part.x, part.y, cellSize, cellSize);
- } else { // 画蛇身
- context.drawImage(snakeBodyImg, part.x, part.y, cellSize, cellSize);
- }
- }
- }
- function spawnFood() {
- food.x = Math.floor(Math.random() * canvas.width / cellSize) * cellSize;
- food.y = Math.floor(Math.random() * canvas.height / cellSize) * cellSize;
- }
- function drawFood() {
- context.drawImage(foodImg, food.x, food.y, cellSize, cellSize);
- }
- function updateSnake() {
- const head = { x: snake[0].x + direction.x * cellSize, y: snake[0].y + direction.y * cellSize };
- snake.unshift(head);
- if (head.x === food.x && head.y === food.y) {
- spawnFood();
- } else {
- snake.pop();
- }
- if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height || snake.slice(1).some(segment => segment.x === head.x && segment.y === head.y)) {
- gameOver();
- }
- }
- function gameOver() {
- clearInterval(gameInterval);
- gameOverText.classList.remove('hidden');
- }
- function gameLoop() {
- drawBackground();
- drawSnake();
- drawFood();
- updateSnake();
- }
- document.addEventListener('keydown', event => {
- if (event.key === 'ArrowUp' && direction.y === 0) {
- direction = { x: 0, y: -1 };
- } else if (event.key === 'ArrowDown' && direction.y === 0) {
- direction = { x: 0, y: 1 };
- } else if (event.key === 'ArrowLeft' && direction.x === 0) {
- direction = { x: -1, y: 0 };
- } else if (event.key === 'ArrowRight' && direction.x === 0) {
- direction = { x: 1, y: 0 };
- }
- });
- startButton.addEventListener('click', () => {
- snake = [{ x: 2 * cellSize, y: 2 * cellSize }];
- direction = { x: 0, y: 0 };
- spawnFood();
- gameOverText.classList.add('hidden');
- gameInterval = setInterval(gameLoop, 100);
- });
复制代码
### 补充资源
请确保以下图片文件(背景、蛇头、蛇身、食物)与你的 HTML、CSS 和 JavaScript 文件位于同一目录下:
1. **背景图片:** `background.jpg`
2. **蛇头图片:** `snake-head.png`
3. **蛇身图片:** `snake-body.png`
4. **食物图片:** `food.png`
以上是完整的代码,确保所有图片都放置在正确的位置后,打开 `index.html` 文件即可运行具有生动蛇头和蛇身的贪吃蛇游戏。希望你喜欢这个游戏!如果有任何问题或者进一步的需求,请随时告诉我。 |
|