WEBLEB
Startseite
Editor
Anmelden
Pro
Deutsch
English
Français
Español
Português
Deutsch
Italiano
हिंदी
2444
Andev.web
Im Editor öffnen
Veröffentlichen Sie Ihren Code
Brauchen Sie eine Website?
Empfohlen
28 October 2024
Profilkartenentwurf
HTML
Copy
Andev web
Score: 0
Highest score: 0
CSS
Copy
@import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;500;600;700&display=swap'); *{ margin: 0; padding: 0; box-sizing: border-box; font-family: 'Open Sans', sans-serif; } body{ display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #e3f2fd; } .wrapper{ width: 65vmin; height: 70vmin; display: flex; overflow: hidden; flex-direction: column; justify-content: center; background: #293447; border-radius: 5px; box-shadow: 0 20px 40px rgba(52, 87, 220, 0.2); } .game-details{ color: #b8c6dc; font-weight: 500; font-size: 1.2rem; padding: 20px 27px; display: flex; justify-content: space-between; } .play-board{ height: 100%; width: 100%; display: grid; background: #212837; grid-template: repeat(30, 1fr) / repeat(30, 1fr); } .play-board .food{ background: #ff003d; } .play-board .head{ background: #60ff70; } .controls{ display: none; justify-content: space-between; } .controls i{ padding: 25px 0; text-align: center; font-size: 1.3rem; color: #b8c6dc; width: calc(100% / 4); cursor: pointer; border-right: 1px solid #171b26; } @media screen and (max-width: 800px) { .wrapper{ width: 90vmin; height: 115vmin; } .game-details{ font-size: 1rem; padding: 15px 27px; } .controls{ display: flex; } .controls i{ padding: 15px 0; font-size: 1rem; } }
JS
Copy
const playBoard = document.querySelector(".play-board"); const scoreElement = document.querySelector(".score"); const highScoreElement = document.querySelector(".high-score"); const controls = document.querySelectorAll(".controls i"); let gameOver = false; let foodX, foodY; let snakeX = 5, snakeY = 5; let velocityX = 0, velocityY = 0; let snakeBody = []; let setIntervalId; let score = 0; // Get high score from local storage let highScore = localStorage.getItem("Puntuación Más Alta") || 0; highScoreElement.innerText = `Puntuación Más Alta: ${highScore}`; // Pass a random between 1 and 30 as food position const updateFoodPosition = () => { foodX = Math.floor(Math.random() * 30) + 1; foodY = Math.floor(Math.random() * 30) + 1; } const handleGameOver = () => { clearInterval(setIntervalId); alert("Game Over! Press OK to replay..."); location.reload(); } // Change velocity value based on key press const changeDirection = e => { if (e.key === "ArrowUp" && velocityY != 1) { velocityX = 0; velocityY = -1; } else if (e.key === "ArrowDown" && velocityY != -1) { velocityX = 0; velocityY = 1; } else if (e.key === "ArrowLeft" && velocityX != 1) { velocityX = -1; velocityY = 0; } else if (e.key === "ArrowRight" && velocityX != -1) { velocityX = 1; velocityY = 0; } } // Change Direction on each key click controls.forEach(button => button.addEventListener("click", () => changeDirection({ key: button.dataset.key }))); const initGame = () => { if (gameOver) return handleGameOver(); let html = `<div class="food" style="grid-area: ${foodY} / ${foodX}"></div>`; // When snake eat food if (snakeX === foodX && snakeY === foodY) { updateFoodPosition(); snakeBody.push([foodY, foodX]); //Add food to snake body array score++; highScore = score >= highScore ? score : highScore; // if score > high score => high score = score localStorage.setItem("Puntuación Más Alta", highScore); scoreElement.innerText = `Puntuación: ${score}`; highScoreElement.innerText = `Puntuación Más Alta: ${highScore}`; } // Update Snake Head snakeX += velocityX; snakeY += velocityY; // Shifthing forward values of elements in snake body by one for (let i = snakeBody.length - 1; i > 0; i--) { snakeBody[i] = snakeBody[i - 1]; } snakeBody[0] = [snakeX, snakeY]; // Check snake body is out of wall or no if (snakeX <= 0 || snakeX > 30 || snakeY <= 0 || snakeY > 30) { return gameOver = true; } // Add div for each part of snake body for (let i = 0; i < snakeBody.length; i++) { html += `<div class="head" style="grid-area: ${snakeBody[i][1]} / ${snakeBody[i][0]}"></div>`; // Check snake head hit body or no if (i !== 0 && snakeBody[0][1] === snakeBody[i][1] && snakeBody[0][0] === snakeBody[i][0]) { gameOver = true; } } playBoard.innerHTML = html; } updateFoodPosition(); setIntervalId = setInterval(initGame, 100); document.addEventListener("keyup", changeDirection);