napisz mi prostą gre w węża w html
- User
napisz mi prostą gre w węża 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ą gre w węża w html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oto prosta gra w węża napisana w HTML, CSS i JavaScript. Możesz skopiować ten kod i zapisać go w pliku snake.html, a następnie otworzyć w przeglądarce.
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gra w Węża</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #222;
}
canvas {
border: 1px solid #fff;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const gridSize = 20;
const canvasSize = 400;
let snake = [
{ x: 10, y: 10 }
];
let direction = { x: 0, y: 0 };
let food = { x: 15, y: 15 };
let score = 0;
function draw() {
ctx.clearRect(0, 0, canvasSize, canvasSize);
// Rysuj węża
ctx.fillStyle = 'lime';
snake.forEach(segment => {
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
});
// Rysuj jedzenie
ctx.fillStyle = 'red';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
// Rysuj wynik
ctx.fillStyle = 'white';
ctx.font = '20px Arial';
ctx.fillText(`Wynik: ${score}`, 10, 30);
}
function move() {
const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
// Sprawdź kolizję z ścianami
if (head.x < 0 || head.x >= canvasSize / gridSize || head.y < 0 || head.y >= canvasSize / gridSize) {
clearInterval(gameInterval);
alert('Koniec gry! Twój wynik: ' + score);
return;
}
// Sprawdź kolizję z samym sobą
if (snake.some(segment => segment.x === head.x && segment.y === head.y)) {
clearInterval(gameInterval);
alert('Koniec gry! Twój wynik: ' + score);
return;
}
// Sprawdź czy wąż zjadł jedzenie
if (head.x === food.x && head.y === food.y) {
score++;
placeFood();
} else {
snake.pop();
}
snake.unshift(head);
draw();
}
function placeFood() {
food = {
x: Math.floor(Math.random() * (canvasSize / gridSize)),
y: Math.floor(Math.random() * (canvasSize / gridSize))
};
}
function changeDirection(event) {
const keyPressed = event.keyCode;
const up = 38;
const down = 40;
const left = 37;
const right = 39;
const goingUp = direction.y === -1;
const goingDown = direction.y === 1;
const goingLeft = direction.x === -1;
const goingRight = direction.x === 1;
if (keyPressed === up && !goingDown) {
direction = { x: 0, y: -1 };
}
if (keyPressed === down && !goingUp) {
direction = { x: 0, y: 1 };
}
if (keyPressed === left && !goingRight) {
direction = { x: -1, y: 0 };
}
if (keyPressed === right && !goingLeft) {
direction = { x: 1, y: 0 };
}
}
document.addEventListener('keydown', changeDirection);
const gameInterval = setInterval(move, 100);
placeFood();
draw();
</script>
</body>
</html>
Otwórz plik w przeglądarce i zacznij grać! 🐍