|
|
要编写一个简单的贪吃蛇小游戏,你可以使用HTML、CSS和JavaScript来实现。以下是一个基本的示例代码,可以为你创建一个贪吃蛇小游戏的网页:
### 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>
- <h1>贪吃蛇小游戏</h1>
- <canvas id="gameCanvas" width="400" height="400"></canvas>
- <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 {
- margin-bottom: 20px;
- }
- #gameCanvas {
- border: 1px solid #000;
- background-color: #fff;
- }
复制代码
### JavaScript部分 (script.js)
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- // Game settings
- const gridSize = 20;
- const canvasSize = 400;
- const rows = canvasSize / gridSize;
- const cols = canvasSize / gridSize;
- // Snake settings
- let snake = [{ x: 10, y: 10 }];
- let direction = { x: 0, y: 0 };
- let food = { x: 15, y: 15 };
- let score = 0;
- function drawBlock(x, y, color) {
- ctx.fillStyle = color;
- ctx.fillRect(x * gridSize, y * gridSize, gridSize, gridSize);
- }
- function draw() {
- ctx.clearRect(0, 0, canvasSize, canvasSize);
- // Draw snake
- snake.forEach(segment => drawBlock(segment.x, segment.y, 'green'));
- // Draw food
- drawBlock(food.x, food.y, 'red');
- // Draw score
- ctx.fillStyle = 'black';
- ctx.font = '16px Arial';
- ctx.fillText(`Score: ${score}`, 10, 20);
- }
- function update() {
- const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
- // Check for collisions with walls
- if (head.x < 0 || head.x >= cols || head.y < 0 || head.y >= rows) {
- resetGame();
- }
- // Check for collisions with self
- snake.forEach(segment => {
- if (segment.x === head.x && segment.y === head.y) {
- resetGame();
- }
- });
- // Move the snake
- snake.unshift(head);
- // Check for food collision
- if (head.x === food.x && head.y === food.y) {
- score++;
- placeFood();
- } else {
- snake.pop();
- }
- }
- function placeFood() {
- food = {
- x: Math.floor(Math.random() * cols),
- y: Math.floor(Math.random() * rows)
- };
- // Ensure food doesn't spawn on the snake
- snake.forEach(segment => {
- if (segment.x === food.x && segment.y === food.y) {
- placeFood();
- }
- });
- }
- function resetGame() {
- snake = [{ x: 10, y: 10 }];
- direction = { x: 0, y: 0 };
- score = 0;
- placeFood();
- }
- document.addEventListener('keydown', event => {
- switch (event.key) {
- case 'ArrowUp':
- if (direction.y === 0) direction = { x: 0, y: -1 };
- break;
- case 'ArrowDown':
- if (direction.y === 0) direction = { x: 0, y: 1 };
- break;
- case 'ArrowLeft':
- if (direction.x === 0) direction = { x: -1, y: 0 };
- break;
- case 'ArrowRight':
- if (direction.x === 0) direction = { x: 1, y: 0 };
- break;
- }
- });
- function gameLoop() {
- update();
- draw();
- setTimeout(gameLoop, 100);
- }
- resetGame();
- gameLoop();
复制代码
将上述内容分别保存为`index.html`、`styles.css`和`script.js`文件,然后将它们全部放在同一个目录下。打开`index.html`文件,即可运行一个简单的贪吃蛇游戏。你可以通过方向键控制蛇的移动。 |
|