|
|
你好!我会帮你设计一个简单的贪吃蛇游戏网页,使用HTML、CSS和JavaScript,让我们一步步来吧!
### 第一步:设置HTML结构
首先,我们需要创建基本的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>
复制代码
### 第二步:设计游戏样式 (CSS)
然后,我们需要创建一个CSS文件来简单设置一下游戏的样式,比如背景颜色和边框。
- /* styles.css */
- body {
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- margin: 0;
- background-color: #f0f0f0;
- }
- #gameCanvas {
- background-color: #fff;
- border: 1px solid #000;
- }
复制代码
### 第三步:实现游戏逻辑 (JavaScript)
最后一步,我们用JavaScript来实现游戏的基本逻辑,比如蛇的移动和食物的生成。
- // script.js
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- let snake = [
- { x: 200, y: 200 },
- { x: 190, y: 200 },
- { x: 180, y: 200 },
- ];
- let dx = 10;
- let dy = 0;
- let foodX;
- let foodY;
- function createFood() {
- foodX = Math.floor(Math.random() * 40) * 10;
- foodY = Math.floor(Math.random() * 40) * 10;
- }
- function drawSnakePart(snakePart) {
- ctx.fillStyle = 'lightgreen';
- ctx.strokestyle = 'darkgreen';
- ctx.fillRect(snakePart.x, snakePart.y, 10, 10);
- ctx.strokeRect(snakePart.x, snakePart.y, 10, 10);
- }
- function drawSnake() {
- snake.forEach(drawSnakePart);
- }
- function clearCanvas() {
- ctx.fillStyle = 'white';
- ctx.strokeStyle = 'black';
- ctx.fillRect(0, 0, canvas.width, canvas.height);
- ctx.strokeRect(0, 0, canvas.width, canvas.height);
- }
- function main() {
- if (gameOver()) return;
- setTimeout(function onTick() {
- clearCanvas();
- drawFood();
- moveSnake();
- drawSnake();
- main();
- }, 100);
- }
- function gameOver() {
- for (let i = 4; i < snake.length; i++) {
- if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) return true;
- }
-
- const hitLeftWall = snake[0].x < 0;
- const hitRightWall = snake[0].x > canvas.width - 10;
- const hitTopWall = snake[0].y < 0;
- const hitBottomWall = snake[0].y > canvas.height - 10;
- return hitLeftWall || hitRightWall || hitTopWall || hitBottomWall;
- }
- function drawFood() {
- ctx.fillStyle = 'red';
- ctx.strokestyle = 'darkred';
- ctx.fillRect(foodX, foodY, 10, 10);
- ctx.strokeRect(foodX, foodY, 10, 10);
- }
- function moveSnake() {
- const head = { x: snake[0].x + dx, y: snake[0].y + dy };
- snake.unshift(head);
- const isEatingFood = snake[0].x === foodX && snake[0].y === foodY;
- if (isEatingFood) {
- createFood();
- } else {
- snake.pop();
- }
- }
- function changeDirection(event) {
- const LEFT_KEY = 37;
- const RIGHT_KEY = 39;
- const UP_KEY = 38;
- const DOWN_KEY = 40;
- const keyPressed = event.keyCode;
- const goingUp = dy === -10;
- const goingDown = dy === 10;
- const goingRight = dx === 10;
- const goingLeft = dx === -10;
- if (keyPressed === LEFT_KEY && !goingRight) {
- dx = -10;
- dy = 0;
- }
- if (keyPressed === UP_KEY && !goingDown) {
- dx = 0;
- dy = -10;
- }
- if (keyPressed === RIGHT_KEY && !goingLeft) {
- dx = 10;
- dy = 0;
- }
- if (keyPressed === DOWN_KEY && !goingUp) {
- dx = 0;
- dy = 10;
- }
- }
- document.addEventListener('keydown', changeDirection);
- createFood();
- main();
复制代码
### 解释一下代码:
1. **HTML部分**:创建一个画布`<canvas>`用于绘制游戏。
2. **CSS部分**:简单设置了整个页面居中以及画布的样式。
3. **JavaScript部分**:
- 初始化蛇的位置和方向。
- 使用键盘事件控制蛇的移动方向。
- 检测蛇是否吃到食物,如果吃到就增加蛇的长度并随机生成新的食物位置。
- 控制游戏结束条件,当蛇撞到墙壁或自身时游戏结束。
希望这段代码能帮助你理解如何用HTML、CSS和JavaScript创建一个简单的贪吃蛇游戏!如果有任何问题或需要进一步解释的地方,请告诉我哦~ |
|