napisz mi prostą grę wężyk w html
- User
napisz mi prostą grę wężyk w html
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
napisz mi prostą grę wężyk w html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
<!DOCTYPE html>
<html>
<head>
<title>Gra Wężyk</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
canvas {
border: 2px solid #333;
background-color: #fff;
}
h1 {
font-family: Arial, sans-serif;
}
#score {
font-family: Arial, sans-serif;
font-size: 18px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<h1>Gra Wężyk</h1>
<div id="score">Wynik: 0</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
// Ustawienia gry
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [];
snake[0] = {
x: 10 * gridSize,
y: 10 * gridSize
};
let food = {
x: Math.floor(Math.random() * tileCount) * gridSize,
y: Math.floor(Math.random() * tileCount) * gridSize
};
let dx = gridSize;
let dy = 0;
let score = 0;
let gameOver = false;
// Obsługa klawiatury
document.addEventListener('keydown', changeDirection);
function changeDirection(event) {
const LEFT_KEY = 37;
const UP_KEY = 38;
const RIGHT_KEY = 39;
const DOWN_KEY = 40;
// Zapobieganie przeciwnemu kierunkowi
if (event.keyCode === LEFT_KEY && dx === 0) {
dx = -gridSize;
dy = 0;
}
if (event.keyCode === UP_KEY && dy === 0) {
dx = 0;
dy = -gridSize;
}
if (event.keyCode === RIGHT_KEY && dx === 0) {
dx = gridSize;
dy = 0;
}
if (event.keyCode === DOWN_KEY && dy === 0) {
dx = 0;
dy = gridSize;
}
}
// Główna funkcja gry
function main() {
if (gameOver) {
ctx.fillStyle = "black";
ctx.font = "50px Arial";
ctx.fillText("Koniec gry!", canvas.width/6, canvas.height/2);
return;
}
setTimeout(function onTick() {
clearCanvas();
drawFood();
moveSnake();
drawSnake();
main();
}, 100);
}
function clearCanvas() {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function drawSnake() {
snake.forEach(drawSnakePart);
}
function drawSnakePart(snakePart) {
ctx.fillStyle = "#4CAF50";
ctx.fillRect(snakePart.x, snakePart.y, gridSize, gridSize);
ctx.strokeStyle = "#006400";
ctx.strokeRect(snakePart.x, snakePart.y, gridSize, gridSize);
}
function drawFood() {
ctx.fillStyle = "red";
ctx.fillRect(food.x, food.y, gridSize, gridSize);
}
function moveSnake() {
const head = {
x: snake[0].x + dx,
y: snake[0].y + dy
};
// Sprawdzanie kolizji ze ścianą
if (head.x < 0 || head.x >= canvas.width ||
head.y < 0 || head.y >= canvas.height) {
gameOver = true;
return;
}
// Sprawdzanie kolizji z własnym ogonem
for (let i = 0; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver = true;
return;
}
}
// Dodanie nowej głowy
snake.unshift(head);
// Sprawdzanie czy wąż zjadł jedzenie
if (head.x === food.x && head.y === food.y) {
// Zwiększenie wyniku
score++;
scoreElement.innerText = "Wynik: " + score;
// Stworzenie nowego jedzenia
food = {
x: Math.floor(Math.random() * tileCount) * gridSize,
y: Math.floor(Math.random() * tileCount) * gridSize
};
} else {
// Usunięcie ogona
snake.pop();
}
}
// Rozpoczęcie gry
main();
</script>
</body>
</html>
Czy chcesz, żebym wyjaśnił lub omówił ten kod?