|
|
你好!增加一个“修改贪吃蛇颜色”的下拉菜单是个不错的主意。我们可以通过以下步骤来实现这个功能:
1. **在HTML文件中添加下拉菜单**。
2. **在CSS文件中为下拉菜单添加样式(可选)**。
3. **在JavaScript文件中处理下拉菜单的选择事件**。
### 第一步:在HTML文件中添加下拉菜单
在HTML文件中,找到适合的位置加入以下代码:
- <label for="snakeColor">选择贪吃蛇颜色:</label>
- <select id="snakeColor">
- <option value="lightgreen">绿</option>
- <option value="blue">蓝</option>
- <option value="yellow">黄</option>
- <option value="red">红</option>
- </select>
复制代码
### 第二步:在CSS文件中为下拉菜单添加样式(可选)
在CSS文件中,可以为下拉菜单添加样式,让它看起来更美观。例如:
- #snakeColor {
- margin: 10px;
- padding: 5px;
- font-size: 16px;
- }
复制代码
### 第三步:在JavaScript文件中处理下拉菜单的选择事件
我们需要在JavaScript中处理下拉菜单的变化,确保贪吃蛇的颜色能够根据用户选择进行改变。
- 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 food = { x: 0, y: 0 };
- let dx = 10;
- let dy = 0;
- let score = 0;
- let gameOver = false;
- let snakeColor = 'lightgreen'; // 默认颜色
- // 创建音效对象
- const eatSound = new Audio('eat.mp3');
- // 创建背景音乐对象
- const backgroundMusic = new Audio('background.mp3');
- backgroundMusic.loop = true; // 设置循环播放
- document.addEventListener("keydown", changeDirection);
- document.getElementById('snakeColor').addEventListener('change', function(event) {
- snakeColor = event.target.value; // 更新贪吃蛇颜色
- });
- function randomFoodPosition() {
- const x = Math.floor(Math.random() * (canvas.width / 10)) * 10;
- const y = Math.floor(Math.random() * (canvas.height / 10)) * 10;
- return { x, y };
- }
- function clearCanvas() {
- ctx.fillStyle = "white";
- ctx.fillRect(0, 0, canvas.width, canvas.height);
- }
- function drawSnake() {
- snake.forEach(drawSnakePart);
- }
- function drawSnakePart(snakePart) {
- ctx.fillStyle = snakeColor; // 使用选择的颜色
- ctx.strokeStyle = 'darkgreen';
- ctx.fillRect(snakePart.x, snakePart.y, 10, 10);
- ctx.strokeRect(snakePart.x, snakePart.y, 10, 10);
- }
- function drawFood() {
- ctx.fillStyle = 'lightpink';
- ctx.fillRect(food.x, food.y, 10, 10);
- }
- function moveSnake() {
- const head = { x: snake[0].x + dx, y: snake[0].y + dy };
- // 检测碰壁
- if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height || collision(head)) {
- gameOver = true;
- return;
- }
- snake.unshift(head);
- // 检查是否吃到食物
- if (head.x === food.x && head.y === food.y) {
- score++;
- food = randomFoodPosition();
- playEatSound(); // 播放音效
- } else {
- snake.pop();
- }
- }
- function collision(head) {
- for (let i = 1; i < snake.length; i++) {
- if (snake[i].x === head.x && snake[i].y === head.y) {
- return true;
- }
- }
- return false;
- }
- // 播放吃食物的音效
- function playEatSound() {
- eatSound.play();
- }
- // 播放背景音乐
- function playBackgroundMusic() {
- backgroundMusic.play();
- }
- // 停止背景音乐
- function stopBackgroundMusic() {
- backgroundMusic.pause();
- backgroundMusic.currentTime = 0; // 重置音乐到开头
- }
- function main() {
- if (gameOver) {
- displayGameOver();
- return;
- }
- setTimeout(function onTick() {
- clearCanvas();
- drawFood();
- moveSnake();
- drawSnake();
- main();
- }, 100);
- }
- 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;
- }
- }
- // 显示游戏结束
- function displayGameOver() {
- ctx.fillStyle = "pink";
- ctx.font = "30px Arial";
- // 绘制“游戏结束!”位于中心稍上方
- ctx.fillText("很遗憾,游戏结束!", canvas.width / 2 - 115, canvas.height / 2 - 20);
- // 绘制得分位于中心稍下方
- ctx.fillText("得分: " + score, canvas.width / 2 - 50, canvas.height / 2 + 20);
- document.getElementById("startButton").style.display = "block";
- stopBackgroundMusic(); // 停止背景音乐
- }
- function startGame() {
- snake = [{ x: 200, y: 200 }, { x: 190, y: 200 }, { x: 180, y: 200 }];
- score = 0;
- dx = 10;
- dy = 0;
- gameOver = false;
- food = randomFoodPosition();
- document.getElementById('startButton').style.display = 'none';
- playBackgroundMusic(); // 播放背景音乐
- main();
- }
- food = randomFoodPosition();
- main();
- // 获取按钮和规则内容的DOM元素
- const rulesButton = document.getElementById('rulesButton');
- const rulesContent = document.getElementById('rulesContent');
- // 当鼠标移到按钮上时,显示规则
- rulesButton.addEventListener('mouseover', function () {
- rulesContent.style.display = 'block';
- });
- // 当鼠标离开按钮时,隐藏规则
- rulesButton.addEventListener('mouseout', function () {
- rulesContent.style.display = 'none';
- });
复制代码
在这个例子中,我们在 `HTML` 中添加了一个下拉菜单,并在 `JavaScript` 文件中监听该下拉菜单的变化。当用户选择不同的颜色时,贪吃蛇的颜色会相应地更新。
希望这对你有帮助!如果有任何疑问或进一步的问题,请随时告诉我! |
|