|
|
当然可以,我们可以移除音效相关的代码和元素。下面是没有音效的版本。
### HTML部分
贪吃蛇游戏
贪吃蛇游戏
开始游戏
暂停
难度:
简单
普通
困难
地图:
地图1
地图2
地图3
皮肤:
默认
蓝色
红色
最高得分: 0
时间: 0 秒
### CSS部分(style.css)
- body {
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- height: 100vh;
- margin: 0;
- font-family: Arial, sans-serif;
- background-color: #f0f0f0;
- }
- #gameCanvas {
- border: 2px solid #000;
- background-color: #fff;
- }
- .controls {
- margin-bottom: 20px;
- }
- button {
- padding: 10px 20px;
- font-size: 16px;
- margin: 5px;
- cursor: pointer;
- }
复制代码
### JavaScript部分(snake.js)
- const canvas = document.getElementById('gameCanvas');
- const context = canvas.getContext('2d');
- const box = 20; // 每个格子的大小
- let snake, food, direction, score, game, gameInterval, timeCounter;
- let highScore = localStorage.getItem('highScore') || 0; // 从localStorage读取最高得分
- let timeElapsed = 0;
- const difficultySettings = {
- easy: 150,
- normal: 100,
- hard: 50
- };
- const skins = {
- default: { snake: 'black', food: 'red' },
- blue: { snake: 'blue', food: 'lightblue' },
- red: { snake: 'red', food: 'orange' }
- };
- // 定义多个地图的障碍物位置
- const maps = {
- map1: [
- {x: 4 * box, y: 4 * box},
- {x: 5 * box, y: 4 * box},
- {x: 6 * box, y: 4 * box},
- {x: 7 * box, y: 4 * box}
- ],
- map2: [
- {x: 10 * box, y: 10 * box},
- {x: 11 * box, y: 10 * box},
- {x: 12 * box, y: 10 * box}
- ],
- map3: [
- {x: 15 * box, y: 15 * box},
- {x: 16 * box, y: 15 * box}
- ]
- };
- let obstacles = [];
- document.getElementById('startButton').addEventListener('click', startGame);
- document.getElementById('pauseButton').addEventListener('click', pauseGame);
- function startGame() {
- document.getElementById('startButton').style.display = 'none';
- document.getElementById('pauseButton').style.display = 'block';
- document.getElementById('gameCanvas').style.display = 'block';
- resetGame();
- const difficulty = document.getElementById('difficulty').value;
- gameInterval = difficultySettings[difficulty];
- game = setInterval(drawGame, gameInterval);
- timeCounter = setInterval(updateTime, 1000); // 每秒更新一次时间
- }
- function pauseGame() {
- if (game) {
- clearInterval(game);
- clearInterval(timeCounter);
- game = null;
- timeCounter = null;
- document.getElementById('pauseButton').innerText = '继续';
- } else {
- const difficulty = document.getElementById('difficulty').value;
- gameInterval = difficultySettings[difficulty];
- game = setInterval(drawGame, gameInterval);
- timeCounter = setInterval(updateTime, 1000); // 每秒更新一次时间
- document.getElementById('pauseButton').innerText = '暂停';
- }
- }
- function resetGame() {
- snake = [{ x: 9 * box, y: 10 * box }];
- food = generateFood();
- score = 0;
- direction = undefined;
- timeElapsed = 0;
- document.getElementById('time').innerText = timeElapsed;
- loadMap(); // 加载选中的地图
- }
- document.addEventListener('keydown', changeDirection);
- function changeDirection(event) {
- const keyPressed = event.keyCode;
- if (keyPressed === 37 && direction !== 'RIGHT') direction = 'LEFT';
- if (keyPressed === 38 && direction !== 'DOWN') direction = 'UP';
- if (keyPressed === 39 && direction !== 'LEFT') direction = 'RIGHT';
- if (keyPressed === 40 && direction !== 'UP') direction = 'DOWN';
- }
- function drawGame() {
- context.clearRect(0, 0, canvas.width, canvas.height);
- const skin = document.getElementById('skin').value;
- const colors = skins[skin];
- for (let i = 0; i < snake.length; i++) {
- context.fillStyle = i === 0 ? 'green' : colors.snake;
- context.fillRect(snake[i].x, snake[i].y, box, box);
- }
- context.fillStyle = colors.food;
- context.fillRect(food.x, food.y, box, box);
- drawObstacles();
- let snakeX = snake[0].x;
- let snakeY = snake[0].y;
- if (direction === 'LEFT') snakeX -= box;
- if (direction === 'UP') snakeY -= box;
- if (direction === 'RIGHT') snakeX += box;
- if (direction === 'DOWN') snakeY += box;
- if (snakeX === food.x && snakeY === food.y) {
- score++;
- updateHighScore();
- food = generateFood();
- } else {
- snake.pop();
- }
- let newHead = { x: snakeX, y: snakeY };
- if (snakeX < 0) snakeX = canvas.width - box;
- if (snakeY < 0) snakeY = canvas.height - box;
- if (snakeX >= canvas.width) snakeX = 0;
- if (snakeY >= canvas.height) snakeY = 0;
- if (collision(newHead, snake) || collision(newHead, obstacles)) {
- clearInterval(game);
- clearInterval(timeCounter);
- alert('游戏结束');
- return;
- }
- snake.unshift(newHead);
- context.fillStyle = 'black';
- context.font = '20px Arial';
- context.fillText(`得分: ${score}`, 5, canvas.height - 5);
- }
- function collision(head, array) {
- for (let i = 0; i < array.length; i++) {
- if (head.x === array[i].x && head.y === array[i].y) {
- return true;
- }
- }
- return false;
- }
- function updateHighScore() {
- if (score > highScore) {
- highScore = score;
- localStorage.setItem('highScore', highScore); // 保存最高得分到localStorage
- document.getElementById('highScore').innerText = highScore;
- }
- }
- function updateTime() {
- timeElapsed++;
- document.getElementById('time').innerText = timeElapsed;
- }
- function generateFood() {
- let newFood;
- do {
- newFood = {
- x: Math.floor(Math.random() * 19 + 1) * box,
- y: Math.floor(Math.random() * 19 + 1) * box
- };
- } while (collision(newFood, snake) || collision(newFood, obstacles));
- return newFood;
- }
- function loadMap() {
- const selectedMap = document.getElementById('map').value;
- obstacles = maps[selectedMap] ? [...maps[selectedMap]] : [];
- }
- function drawObstacles() {
- context.fillStyle = 'grey';
- for (let i = 0; i < obstacles.length; i++) {
- context.fillRect(obstacles[i].x, obstacles[i].y, box, box);
- }
- }
复制代码
### 运行游戏
1. **创建项目文件**:在一个新文件夹内创建三个文件:`index.html`, `style.css`, 和 `snake.js`。
2. **运行游戏**:用你的浏览器打开 `index.html` 文件,即可运行完整的贪吃蛇游戏。
通过这些步骤,你应该已经能够运行一个具有多地图、皮肤选择而且没有音效的完整贪吃蛇游戏。如果有任何问题或进一步的需求,请随时告诉我!享受编程和游戏的乐趣吧! |
|