w html stwórz mi gre węża.
- User
w html stwórz mi gre węża.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
w html stwórz mi gre węża.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Stworzenie gry "Wąż" (Snake) w HTML wymaga użycia HTML, CSS i JavaScript. Poniżej znajdziesz kompletny kod dla tej gry. Gra będzie działać w przeglądarce internetowej.
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gra Wąż</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #000;
color: white;
font-family: Arial, sans-serif;
}
canvas {
border: 2px solid white;
background-color: #222;
}
.game-over {
position: absolute;
font-size: 2rem;
color: red;
display: none;
}
</style>
</head>
<body>
<div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div class="game-over" id="gameOver">KONIEC GRY! Naciśnij SPACJĘ, aby zagrać ponownie.</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const boxSize = 20; // Rozmiar jednego pola
let snake = [{ x: 10, y: 10 }]; // Początkowa pozycja węża
let food = { x: 5, y: 5 }; // Początkowa pozycja jedzenia
let direction = { x: 0, y: 0 }; // Kierunek ruchu węża
let gameOver = false;
// Nasłuchiwanie klawiszy
document.addEventListener('keydown', changeDirection);
function changeDirection(event) {
if (event.key === 'ArrowUp' && direction.y === 0) {
direction = { x: 0, y: -1 };
} else if (event.key === 'ArrowDown' && direction.y === 0) {
direction = { x: 0, y: 1 };
} else if (event.key === 'ArrowLeft' && direction.x === 0) {
direction = { x: -1, y: 0 };
} else if (event.key === 'ArrowRight' && direction.x === 0) {
direction = { x: 1, y: 0 };
} else if (event.key === ' ') {
resetGame();
}
}
function resetGame() {
snake = [{ x: 10, y: 10 }];
food = { x: 5, y: 5 };
direction = { x: 0, y: 0 };
gameOver = false;
document.getElementById('gameOver').style.display = 'none';
}
function drawRect(x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x * boxSize, y * boxSize, boxSize, boxSize);
}
function drawSnake() {
snake.forEach(segment => drawRect(segment.x, segment.y, 'lime'));
}
function drawFood() {
drawRect(food.x, food.y, 'red');
}
function updateSnake() {
const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
// Sprawdzenie kolizji ze ścianami lub samym sobą
if (
head.x < 0 || head.x >= canvas.width / boxSize ||
head.y < 0 || head.y >= canvas.height / boxSize ||
snake.some(segment => segment.x === head.x && segment.y === head.y)
) {
gameOver = true;
document.getElementById('gameOver').style.display = 'block';
return;
}
snake.unshift(head);
// Sprawdzenie, czy wąż zjadł jedzenie
if (head.x === food.x && head.y === food.y) {
generateFood();
} else {
snake.pop();
}
}
function generateFood() {
food = {
x: Math.floor(Math.random() * (canvas.width / boxSize)),
y: Math.floor(Math.random() * (canvas.height / boxSize))
};
// Upewnij się, że jedzenie nie pojawia się na wężu
while (snake.some(segment => segment.x === food.x && segment.y === food.y)) {
food = {
x: Math.floor(Math.random() * (canvas.width / boxSize)),
y: Math.floor(Math.random() * (canvas.height / boxSize))
};
}
}
function gameLoop() {
if (gameOver) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawFood();
updateSnake();
drawSnake();
setTimeout(gameLoop, 100); // Szybkość gry
}
gameLoop();
</script>
</body>
</html>
canvas) o wymiarach 400x400 pikseli.↑, ↓, ←, →).SPACE).snake.html).Jeśli masz pytania lub potrzebujesz dodatkowych funkcji, daj mi znać! 🚀