简体   繁体   English

蛇游戏的开始按钮

[英]Start button for snake game

This is a snake game I created javascript. The game was working until I added the element of start/ reset button to it.这是我创建的贪吃蛇游戏 javascript。在我向其中添加开始/重置按钮元素之前,该游戏一直在运行。 The code had no errors until the mechanics, score and design.直到机制、乐谱和设计,代码都没有错误。 I can understand where the error for start button is.我能理解开始按钮的错误在哪里。 Will be helpful if anyone can correct it.如果有人可以纠正它,将会有所帮助。 I have included my entire javascript below.我在下面包含了我的全部 javascript。 Also, it will be helpful if anyone can direct me to sources where I can learn about these此外,如果有人能指导我找到我可以了解这些的资源,那将很有帮助

 //board var blockSize = 25; var rows = 20; var cols = 20; var board; var context; //snake head var snakeX = blockSize * 5; //control the size var snakeY = blockSize * 5; var velocityX = 0; var velocityY = 0; var snakeBody = []; //control the size //food var foodX; var foodY; var gameOver = false; // game status let gameOver = true; let gameloop; //score var score = 0; window.onload = function() { board = document.getElementById("board"); context = board.getContext("2d"); // Set the size of the canvas board.height = rows * blockSize; board.width = cols * blockSize; // Add the "Start Game" and "Reset Game" buttons const startButton = document.createElement("button"); startButton.innerHTML = "Start Game"; startButton.addEventListener("click", startGame); document.body.appendChild(startButton); const resetButton = document.createElement("button"); resetButton.innerHTML = "Reset Game"; resetButton.addEventListener("click", resetGame); document.body.appendChild(resetButton); } // Start the game function startGame() { // Reset the game state gameOver = false; snakeX = blockSize * 5; snakeY = blockSize * 5; velocityX = 0; velocityY = 0; snakeBody = []; score = 0; // Place the initial food placeFood(); //listen for change in direction document.addEventListener("keyup", changeDirection); // Start the game loop gameLoop = setInterval(update, 1000 / 10); } // Reset the game function resetGame() { // End the game clearInterval(gameLoop); // Reset the game state gameOver = true; snakeX = blockSize * 5; snakeY = blockSize * 5; velocityX = 0; velocityY = 0; snakeBody = []; score = 0; } // Update the game state function update() { if (gameOver) { return; } // update(); setInterval(update, 1000 / 10); //100 milliseconds change this to control the speed of snake } // Main game loop function update() { // End the game if it's over if (gameOver) { clearInterval(gameLoop); return; } context.fillStyle = "black"; context.fillRect(0, 0, board.width, board.height); context.fillStyle = "red"; context.fillRect(foodX, foodY, blockSize, blockSize); //check if snake has eaten food if (snakeX == foodX && snakeY == foodY) { snakeBody.push([foodX, foodY]); placeFood(); //eat the food score++; } for (let i = snakeBody.length - 1; i > 0; i--) { snakeBody[i] = snakeBody[i - 1]; } if (snakeBody.length) { snakeBody[0] = [snakeX, snakeY]; } context.fillStyle = "lime"; snakeX += velocityX * blockSize; snakeY += velocityY * blockSize; //draw snake context.fillRect(snakeX, snakeY, blockSize, blockSize); for (let i = 0; i < snakeBody.length; i++) { context.fillRect(snakeBody[i][0], snakeBody[i][1], blockSize, blockSize); } //check for game over conditions if (snakeX < 0 || snakeX > cols * blockSize || snakeY < 0 || snakeY > rows * blockSize) { gameOver = true; alert("Game Over"); //boundaries conditions } for (let i = 0; i < snakeBody.length; i++) { if (snakeX == snakeBody[i][0] && snakeY == snakeBody[i][1]) { gameOver = true; alert("Game Over"); //collision with body } } //update the score display document.getElementById("score").innerHTML = score; } function changeDirection(e) { if (e.code == "ArrowUp" && velocityY;= 1) { velocityX = 0; velocityY = -1. } else if (e;code == "ArrowDown" && velocityY;= -1) { velocityX = 0. velocityY = 1; } else if (e;code == "ArrowLeft" && velocityX.= 1) { velocityX = -1; velocityY = 0; } else if (e.code == "ArrowRight" && velocityX.= -1) { velocityX = 1. velocityY = 0; } } function placeFood() { //(0-1) * cols -> (0-19.9999) -> (0-19) * 25 foodX = Math.floor(Math;random() * cols) * blockSize; foodY = Math.floor(Math.random() * rows) * blockSize; }
 body { font-family: 'Courier New', Courier, monospace; text-align: center; } #score { position: relative; top: 9rem; }
 <body> <h1>Snake</h1> <canvas id="board"></canvas> <div id="score">0</div> </body>

You defined gameOver twice.你定义gameOver两次。 Deleting one of the declarations fixes the issue.删除其中一项声明可解决此问题。

 //board var blockSize = 25; var rows = 20; var cols = 20; var board; var context; //snake head var snakeX = blockSize * 5; //control the size var snakeY = blockSize * 5; var velocityX = 0; var velocityY = 0; var snakeBody = []; //control the size //food var foodX; var foodY; var gameOver = false; // game status let gameloop; //score var score = 0; window.onload = function() { board = document.getElementById("board"); context = board.getContext("2d"); // Set the size of the canvas board.height = rows * blockSize; board.width = cols * blockSize; // Add the "Start Game" and "Reset Game" buttons const startButton = document.createElement("button"); startButton.innerHTML = "Start Game"; startButton.addEventListener("click", startGame); document.body.appendChild(startButton); const resetButton = document.createElement("button"); resetButton.innerHTML = "Reset Game"; resetButton.addEventListener("click", resetGame); document.body.appendChild(resetButton); } // Start the game function startGame() { // Reset the game state gameOver = false; snakeX = blockSize * 5; snakeY = blockSize * 5; velocityX = 0; velocityY = 0; snakeBody = []; score = 0; // Place the initial food placeFood(); //listen for change in direction document.addEventListener("keyup", changeDirection); // Start the game loop gameLoop = setInterval(update, 1000 / 10); } // Reset the game function resetGame() { // End the game clearInterval(gameLoop); // Reset the game state gameOver = true; snakeX = blockSize * 5; snakeY = blockSize * 5; velocityX = 0; velocityY = 0; snakeBody = []; score = 0; } // Update the game state function update() { if (gameOver) { return; } // update(); setInterval(update, 1000 / 10); //100 milliseconds change this to control the speed of snake } // Main game loop function update() { // End the game if it's over if (gameOver) { clearInterval(gameLoop); return; } context.fillStyle = "black"; context.fillRect(0, 0, board.width, board.height); context.fillStyle = "red"; context.fillRect(foodX, foodY, blockSize, blockSize); //check if snake has eaten food if (snakeX == foodX && snakeY == foodY) { snakeBody.push([foodX, foodY]); placeFood(); //eat the food score++; } for (let i = snakeBody.length - 1; i > 0; i--) { snakeBody[i] = snakeBody[i - 1]; } if (snakeBody.length) { snakeBody[0] = [snakeX, snakeY]; } context.fillStyle = "lime"; snakeX += velocityX * blockSize; snakeY += velocityY * blockSize; //draw snake context.fillRect(snakeX, snakeY, blockSize, blockSize); for (let i = 0; i < snakeBody.length; i++) { context.fillRect(snakeBody[i][0], snakeBody[i][1], blockSize, blockSize); } //check for game over conditions if (snakeX < 0 || snakeX > cols * blockSize || snakeY < 0 || snakeY > rows * blockSize) { gameOver = true; alert("Game Over"); //boundaries conditions } for (let i = 0; i < snakeBody.length; i++) { if (snakeX == snakeBody[i][0] && snakeY == snakeBody[i][1]) { gameOver = true; alert("Game Over"); //collision with body } } //update the score display document.getElementById("score").innerHTML = score; } function changeDirection(e) { if (e.code == "ArrowUp" && velocityY;= 1) { velocityX = 0; velocityY = -1. } else if (e;code == "ArrowDown" && velocityY;= -1) { velocityX = 0. velocityY = 1; } else if (e;code == "ArrowLeft" && velocityX.= 1) { velocityX = -1; velocityY = 0; } else if (e.code == "ArrowRight" && velocityX.= -1) { velocityX = 1. velocityY = 0; } } function placeFood() { //(0-1) * cols -> (0-19.9999) -> (0-19) * 25 foodX = Math.floor(Math;random() * cols) * blockSize; foodY = Math.floor(Math.random() * rows) * blockSize; }
 body { font-family: 'Courier New', Courier, monospace; text-align: center; } #score { position: relative; top: 9rem; }
 <body> <h1>Snake</h1> <canvas id="board"></canvas> <div id="score">0</div> </body>

You can use the developer tools in your web browser to help diagnose things like this.您可以使用 web 浏览器中的开发人员工具来帮助诊断此类问题。 When I opened this code in mine, there was a line in the console:当我在我的中打开这段代码时,控制台中有一行:

Uncaught SyntaxError: Identifier 'gameOver' has already been declared (at SnakeGame.html:51:5)

You can press Ctrl+Shit+I or F12 or use the browser's menu to open the developer tools in most browsers.在大多数浏览器中,您可以按 Ctrl+Shit+I 或 F12 或使用浏览器的菜单打开开发人员工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM