|
|
好的,下面是一个基本的贪吃蛇游戏,分为三个部分:HTML文件、CSS文件和JavaScript文件。你可以将这些代码分别保存到不同的文件中,然后用浏览器打开HTML文件即可运行游戏。
### 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>贪吃蛇游戏</title>
- <link rel="stylesheet" href="styles.css">
- </head>
- <body>
- <canvas id="gameCanvas" width="400" height="400"></canvas>
- <script src="script.js"></script>
- </body>
- </html>
复制代码
### 2. CSS 文件 (styles.css)
- body {
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- background-color: #f0f0f0;
- margin: 0;
- font-family: Arial, sans-serif;
- }
- canvas {
- border: 2px solid #000;
- background-color: #fff;
- }
复制代码
### 3. JavaScript 文件 (script.js)
- const canvas = document.getElementById("gameCanvas");
- const ctx = canvas.getContext("2d");
- const gridSize = 20;
- let snake = [{ x: 200, y: 200 }];
- let direction = { x: 0, y: 0 };
- let food = { x: 0, y: 0 };
- let score = 0;
- document.addEventListener("keydown", changeDirection);
- function gameLoop() {
- if (checkCollision()) {
- alert("Game Over! Your score: " + score);
- resetGame();
- return;
- }
- moveSnake();
- if (checkFoodCollision()) {
- increaseSnake();
- placeFood();
- score++;
- }
- clearCanvas();
- drawSnake();
- drawFood();
- setTimeout(gameLoop, 100);
- }
- function clearCanvas() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- }
- function drawSnake() {
- ctx.fillStyle = "green";
- snake.forEach(segment => {
- ctx.fillRect(segment.x, segment.y, gridSize, gridSize);
- });
- }
- function moveSnake() {
- const head = { x: snake[0].x + direction.x * gridSize, y: snake[0].y + direction.y * gridSize };
- snake.unshift(head);
- snake.pop();
- }
- function changeDirection(event) {
- switch (event.keyCode) {
- case 37: // left arrow key
- if (direction.x === 0) direction = { x: -1, y: 0 };
- break;
- case 38: // up arrow key
- if (direction.y === 0) direction = { x: 0, y: -1 };
- break;
- case 39: // right arrow key
- if (direction.x === 0) direction = { x: 1, y: 0 };
- break;
- case 40: // down arrow key
- if (direction.y === 0) direction = { x: 0, y: 1 };
- break;
- }
- }
- function checkCollision() {
- const head = snake[0];
- if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height) {
- return true;
- }
- for (let i = 1; i < snake.length; i++) {
- if (snake[i].x === head.x && snake[i].y === head.y) {
- return true;
- }
- }
- return false;
- }
- function checkFoodCollision() {
- return snake[0].x === food.x && snake[0].y === food.y;
- }
- function increaseSnake() {
- const tail = snake[snake.length - 1];
- snake.push({ x: tail.x, y: tail.y });
- }
- function placeFood() {
- food.x = Math.floor(Math.random() * (canvas.width / gridSize)) * gridSize;
- food.y = Math.floor(Math.random() * (canvas.height / gridSize)) * gridSize;
- }
- function drawFood() {
- ctx.fillStyle = "red";
- ctx.fillRect(food.x, food.y, gridSize, gridSize);
- }
- function resetGame() {
- snake = [{ x: 200, y: 200 }];
- direction = { x: 0, y: 0 };
- score = 0;
- placeFood();
- }
- placeFood();
- gameLoop();
复制代码
请按照上面的文件结构分别创建 `index.html`, `styles.css` 和 `script.js` 文件,然后把各自的代码复制进去。这个简单的贪吃蛇游戏就完成了,你可以直接在浏览器中打开 `index.html` 进行游戏。 |
|