Oto zaktualizowany kod z przyciskiem start:
<!DOCTYPE html>
<html>
<head>
<title>Gra Wąż</title>
<style>
#game-board {
border: 1px solid black;
width: 400px;
height: 400px;
position: relative;
}
.snake-part {
position: absolute;
width: 20px;
height: 20px;
background-color: green;
}
#food {
position: absolute;
width: 20px;
height: 20px;
background-color: red;
}
#start-button {
position: absolute;
top: 410px;
left: 150px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="game-board">
<div id="food"></div>
</div>
<button id="start-button">Start</button>
<script>
let snake = [
{x: 100, y: 100},
{x: 80, y: 100},
{x: 60, y: 100},
];
let food = {x: 200, y: 200};
let direction = 'right';
let score = 0;
let gameInterval = null;
// Rysowanie węża
function drawSnake() {
for (let i = 0; i < snake.length; i++) {
let part = document.createElement('div');
part.classList.add('snake-part');
part.style.left = snake[i].x + 'px';
part.style.top = snake[i].y + 'px';
document.getElementById('game-board').appendChild(part);
}
}
// Rysowanie jedzenia
function drawFood() {
document.getElementById('food').style.left = food.x + 'px';
document.getElementById('food').style.top = food.y + 'px';
}
// Generowanie nowego jedzenia
function generateFood() {
food.x = Math.floor(Math.random() * 20) * 20;
food.y = Math.floor(Math.random() * 20) * 20;
}
// Poruszanie wężem
function moveSnake() {
let newHead = {x: snake[0].x, y: snake[0].y};
if (direction === 'right') {
newHead.x += 20;
} else if (direction === 'left') {
newHead.x -= 20;
} else if (direction === 'up') {
newHead.y -= 20;
} else if (direction === 'down') {
newHead.y += 20;
}
snake.unshift(newHead);
if (snake[0].x === food.x && snake[0].y === food.y) {
score++;
generateFood();
} else {
snake.pop();
}
}
// Sprawdzenie kolizji
function checkCollision() {
if (snake[0].x < 0 || snake[0].x >= 400 || snake[0].y < 0 || snake[0].y >= 400) {
alert('Game Over!');
clearInterval(gameInterval);
}
for (let i = 1; i < snake.length; i++) {
if (snake[0].x === snake[i].x && snake[0].y === snake[i].y) {
alert('Game Over!');
clearInterval(gameInterval);
}
}
}
// Główna pętla gry
function gameLoop() {
document.getElementById('game-board').innerHTML = '';
document.getElementById('food').style.backgroundColor = 'red';
moveSnake();
checkCollision();
drawSnake();
drawFood();
}
// Obsługa przycisku start
document.getElementById('start-button').addEventListener('click', function() {
if (gameInterval === null) {
snake = [
{x: 100, y: 100},
{x: 80, y: 100},
{x: 60, y: 100},
];
food = {x: 200, y: 200};
direction = 'right';
score = 0;
gameInterval = setInterval(gameLoop, 100);
}
});
// Obsługa klawiszy
document.addEventListener('keydown', function(event) {
if (event.key === 'ArrowUp' && direction !== 'down') {
direction = 'up';
} else if (event.key === 'ArrowDown' && direction !== 'up') {
direction = 'down';
} else if (event.key === 'ArrowLeft' && direction !== 'right') {
direction = 'left';
} else if (event.key === 'ArrowRight' && direction !== 'left') {
direction = 'right';
}
});
</script>
</body>
</html>