stworz prosta gre w wezyka w html
- User
stworz prosta gre w wezyka 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.
stworz prosta gre w wezyka w html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Oto prosta gra w wężyka stworzona w HTML, CSS i JavaScript:
index.html
<!DOCTYPE html>
<html>
<head>
<title>Gra w wężyka</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="game-canvas" width="400" height="400"></canvas>
<script src="script.js"></script>
</body>
</html>
style.css
body {
background-color: #f0f0f0;
}
#game-canvas {
border: 1px solid #ccc;
}
script.js
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
// Ustawienia gry
const gridSize = 20;
const snakeSpeed = 10;
let snake = [
{ x: gridSize * 5, y: gridSize * 5 },
{ x: gridSize * 4, y: gridSize * 5 },
{ x: gridSize * 3, y: gridSize * 5 }
];
let direction = 'right';
let food = { x: gridSize * 10, y: gridSize * 10 };
let score = 0;
// Funkcja rysowania gry
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Rysowanie węża
snake.forEach((part, index) => {
ctx.fillStyle = index === 0 ? '#000' : '#333';
ctx.fillRect(part.x, part.y, gridSize, gridSize);
});
// Rysowanie jedzenia
ctx.fillStyle = '#f00';
ctx.fillRect(food.x, food.y, gridSize, gridSize);
// Rysowanie punktacji
ctx.font = '24px Arial';
ctx.fillStyle = '#000';
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
ctx.fillText(`Punkty: ${score}`, 10, 10);
}
// Funkcja aktualizująca grę
function update() {
// Przesunięcie węża
const head = snake[0];
let newHead;
switch (direction) {
case 'up':
newHead = { x: head.x, y: head.y - gridSize };
break;
case 'down':
newHead = { x: head.x, y: head.y + gridSize };
break;
case 'left':
newHead = { x: head.x - gridSize, y: head.y };
break;
case 'right':
newHead = { x: head.x + gridSize, y: head.y };
break;
}
snake.unshift(newHead);
// Sprawdzenie kolizji z jedzeniem
if (newHead.x === food.x && newHead.y === food.y) {
score++;
food = {
x: gridSize * Math.floor(Math.random() * (canvas.width / gridSize)),
y: gridSize * Math.floor(Math.random() * (canvas.height / gridSize))
};
} else {
snake.pop();
}
// Sprawdzenie kolizji ze ścianami lub samym sobą
if (
newHead.x < 0 ||
newHead.x >= canvas.width ||
newHead.y < 0 ||
newHead.y >= canvas.height ||
snake.some((part, index) => index > 0 && part.x === newHead.x && part.y === newHead.y)
) {
alert(`Game Over! Twoje punkty: ${score}`);
score = 0;
snake = [
{ x: gridSize * 5, y: gridSize * 5 },
{ x: gridSize * 4, y: gridSize * 5 },
{ x: gridSize * 3, y: gridSize * 5 }
];
direction = 'right';
food = { x: gridSize * 10, y: gridSize * 10 };
}
}
// Pętla gry
setInterval(() => {
update();
draw();
}, 1000 / snakeSpeed);
// Obsługa klawiatury
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;
}
});
To prosta gra w wężyka, w której sterujesz wężem za pomocą strzałek na klawiaturze. Celem gry jest zjedzenie jedzenia (czerwony prostokąt) i uniknięcie kolizji ze ścianami lub samym sobą. Po każdej kolizji gra resetuje się.
Oto jak uruchomić grę:
index.html, style.css i script.js.index.html w przeglądarce internetowej.Gra powinna działać poprawnie. Miłej zabawy!