Created
February 7, 2024 21:34
-
-
Save aelphias/83374c98c63499b9d492f9da6994cb88 to your computer and use it in GitHub Desktop.
snake javascript game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Snake Game</title> | |
<link rel="stylesheet" href="styles.css"> | |
</head> | |
<body> | |
<div class="game-container"> | |
<div id="snake" class="snake"></div> | |
<div id="food" class="food"></div> | |
</div> | |
<script src="script.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let snake = [{ x: 200, y: 200 }]; | |
let food = { x: 0, y: 0 }; | |
let dx = 0; | |
let dy = 0; | |
let score = 0; | |
function drawSnake() { | |
const gameContainer = document.querySelector('.game-container'); | |
gameContainer.innerHTML = ''; | |
snake.forEach((segment) => { | |
const snakeElement = document.createElement('div'); | |
snakeElement.style.gridRowStart = segment.y / 20; | |
snakeElement.style.gridColumnStart = segment.x / 20; | |
snakeElement.classList.add('snake'); | |
gameContainer.appendChild(snakeElement); | |
}); | |
} | |
function drawFood() { | |
const gameContainer = document.querySelector('.game-container'); | |
const foodElement = document.createElement('div'); | |
foodElement.style.gridRowStart = food.y / 20; | |
foodElement.style.gridColumnStart = food.x / 20; | |
foodElement.classList.add('food'); | |
gameContainer.appendChild(foodElement); | |
} | |
function randomFoodPosition() { | |
food.x = Math.floor(Math.random() * 20) * 20; | |
food.y = Math.floor(Math.random() * 20) * 20; | |
} | |
function main() { | |
randomFoodPosition(); | |
drawFood(); | |
document.addEventListener('keydown', (event) => { | |
switch (event.key) { | |
case 'ArrowUp': | |
dx = 0; | |
dy = -20; | |
break; | |
case 'ArrowDown': | |
dx = 0; | |
dy = 20; | |
break; | |
case 'ArrowLeft': | |
dx = -20; | |
dy = 0; | |
break; | |
case 'ArrowRight': | |
dx = 20; | |
dy = 0; | |
break; | |
} | |
}); | |
function gameLoop() { | |
snake[0].x += dx; | |
snake[0].y += dy; | |
if (snake[0].x === food.x && snake[0].y === food.y) { | |
score++; | |
randomFoodPosition(); | |
drawFood(); | |
snake.push({ ...snake[snake.length - 1] }); | |
} | |
drawSnake(); | |
setTimeout(gameLoop, 100); | |
} | |
gameLoop(); | |
} | |
main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
height: 100vh; | |
margin: 0; | |
} | |
.game-container { | |
position: relative; | |
width: 400px; | |
height: 400px; | |
border: 1px solid #ccc; | |
} | |
.snake, | |
.food { | |
position: absolute; | |
width: 20px; | |
height: 20px; | |
background-color: green; | |
} | |
.food { | |
background-color: red; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment