napisz mi grę wężyk w html
- User
napisz mi 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 grę wężyk w html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oto prosta implementacja gry "Wężyk" w HTML, CSS i JavaScript:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Wężyk</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script src="script.js"></script>
</body>
</html>
style.css
body {
background-color: #f0f0f0;
}
#canvas {
border: 1px solid #000;
}
script.js
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const scale = 20;
let score = 0;
// Początkowe położenie węża
let snake = [
{x: scale * 5, y: scale * 5},
{x: scale * 4, y: scale * 5},
{x: scale * 3, y: scale * 5}
];
// Początkowy kierunek węża
let direction = 'right';
// Pozycja jedzenia
let food = {x: Math.floor(Math.random() * (canvas.width / scale)) * scale, y: Math.floor(Math.random() * (canvas.height / scale)) * scale};
// Rysuj planszę
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Rysuj węża
ctx.fillStyle = '#0f0';
snake.forEach((part) => {
ctx.fillRect(part.x, part.y, scale, scale);
});
// Rysuj jedzenie
ctx.fillStyle = '#f00';
ctx.fillRect(food.x, food.y, scale, scale);
// Rysuj wynik
ctx.font = '24px Arial';
ctx.fillStyle = '#000';
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
ctx.fillText(`Wynik: ${score}`, 10, 10);
}
// Aktualizuj grę
function update() {
// Przesuń węża
let head = snake[0];
let newHead = head;
switch (direction) {
case 'right':
newHead = {x: head.x + scale, y: head.y};
break;
case 'left':
newHead = {x: head.x - scale, y: head.y};
break;
case 'up':
newHead = {x: head.x, y: head.y - scale};
break;
case 'down':
newHead = {x: head.x, y: head.y + scale};
break;
}
snake.unshift(newHead);
// Sprawdź czy wąż zjadł jedzenie
if (snake[0].x === food.x && snake[0].y === food.y) {
score++;
food = {x: Math.floor(Math.random() * (canvas.width / scale)) * scale, y: Math.floor(Math.random() * (canvas.height / scale)) * scale};
} else {
snake.pop();
}
// Sprawdź czy wąż uderzył w ścianę lub samego siebie
if (snake[0].x < 0 || snake[0].x >= canvas.width || snake[0].y < 0 || snake[0].y >= canvas.height || snake.slice(1).some((part) => part.x === snake[0].x && part.y === snake[0].y)) {
alert(`Game Over! Twój wynik: ${score}`);
score = 0;
snake = [
{x: scale * 5, y: scale * 5},
{x: scale * 4, y: scale * 5},
{x: scale * 3, y: scale * 5}
];
direction = 'right';
}
}
// Obsłuż klawiaturę
document.addEventListener('keydown', (e) => {
switch (e.key) {
case 'ArrowUp':
if (direction !== 'down') direction = 'up';
break;
case 'ArrowDown':
if (direction !== 'up') direction = 'down';
break;
case 'ArrowLeft':
if (direction !== 'right') direction = 'left';
break;
case 'ArrowRight':
if (direction !== 'left') direction = 'right';
break;
}
});
// Główna pętla gry
setInterval(() => {
update();
draw();
}, 100);
To jest prosta implementacja gry "Wężyk". Możesz ją uruchomić, kopiując kod do plików index.html, style.css i script.js i otwierając index.html w przeglądarce.
Gra działa w następujący sposób:
Miłej zabawy!