Wygeneruj w HTML kod gry w węża
- User
Wygeneruj w HTML kod gry w 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.
Wygeneruj w HTML kod gry w węża
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Stworzę kompletną, responsywną grę węża (Snake) w jednym pliku HTML. Gra będzie zawierała wszystkie niezbędne elementy: planszę, sterowanie, punktację, ekran startowy i końcowy gry.
<!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>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
padding: 20px;
overflow: hidden;
}
.game-container {
position: relative;
width: 100%;
max-width: 600px;
background-color: rgba(0, 0, 0, 0.7);
border-radius: 20px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
overflow: hidden;
padding: 20px;
}
.header {
text-align: center;
margin-bottom: 20px;
color: white;
text-shadow: 0 0 10px #00ff00;
}
.header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
color: #4cff00;
}
.score-container {
display: flex;
justify-content: space-between;
margin-bottom: 15px;
background: rgba(0, 0, 0, 0.5);
padding: 10px 20px;
border-radius: 10px;
color: white;
}
.score-box {
text-align: center;
}
.score-title {
font-size: 1rem;
margin-bottom: 5px;
color: #aaa;
}
.score-value {
font-size: 1.8rem;
font-weight: bold;
color: #4cff00;
}
#game-board {
background-color: #111;
border-radius: 10px;
border: 2px solid #333;
display: block;
margin: 0 auto;
box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.8);
}
.controls {
display: flex;
justify-content: space-between;
margin-top: 20px;
gap: 10px;
}
button {
flex: 1;
padding: 12px;
font-size: 1rem;
font-weight: bold;
border: none;
border-radius: 8px;
cursor: pointer;
background: linear-gradient(to bottom, #4cff00, #1a7d00);
color: white;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.5);
transition: all 0.2s ease;
box-shadow: 0 4px 0 #0f4d00;
}
button:hover {
background: linear-gradient(to bottom, #5eff1a, #1f9a00);
transform: translateY(-2px);
}
button:active {
transform: translateY(2px);
box-shadow: 0 2px 0 #0f4d00;
}
#pause-btn {
background: linear-gradient(to bottom, #ffcc00, #cc7a00);
box-shadow: 0 4px 0 #995c00;
}
#pause-btn:hover {
background: linear-gradient(to bottom, #ffd633, #e68a00);
}
#restart-btn {
background: linear-gradient(to bottom, #ff3333, #b30000);
box-shadow: 0 4px 0 #800000;
}
#restart-btn:hover {
background: linear-gradient(to bottom, #ff4d4d, #cc0000);
}
.screen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: rgba(0, 0, 0, 0.85);
border-radius: 20px;
z-index: 10;
padding: 20px;
text-align: center;
}
.screen h2 {
font-size: 2.5rem;
color: #4cff00;
margin-bottom: 20px;
text-shadow: 0 0 10px #00ff00;
}
.screen p {
color: white;
font-size: 1.2rem;
margin-bottom: 20px;
max-width: 80%;
line-height: 1.6;
}
#game-over-screen {
display: none;
}
.instructions {
margin-top: 15px;
color: #aaa;
font-size: 0.9rem;
text-align: center;
padding: 10px;
background: rgba(0, 0, 0, 0.3);
border-radius: 8px;
max-width: 500px;
margin: 20px auto 0;
}
.snake-segment {
position: absolute;
width: 20px;
height: 20px;
background-color: #4cff00;
border-radius: 4px;
box-shadow: 0 0 5px #00ff00;
}
.snake-head {
background: linear-gradient(135deg, #4cff00, #00ff88);
border-radius: 6px;
z-index: 2;
}
.food {
position: absolute;
width: 20px;
height: 20px;
background: radial-gradient(circle, #ff3333, #b30000);
border-radius: 50%;
box-shadow: 0 0 8px #ff0000;
animation: pulse 1s infinite;
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.2); }
100% { transform: scale(1); }
}
.mobile-controls {
display: none;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 10px;
margin-top: 20px;
height: 200px;
}
.mobile-btn {
background: rgba(255, 255, 255, 0.2);
border: none;
border-radius: 10px;
color: white;
font-size: 24px;
display: flex;
align-items: center;
justify-content: center;
}
#up-btn {
grid-column: 2;
grid-row: 1;
}
#left-btn {
grid-column: 1;
grid-row: 2;
}
#right-btn {
grid-column: 3;
grid-row: 2;
}
#down-btn {
grid-column: 2;
grid-row: 3;
}
@media (max-width: 600px) {
.mobile-controls {
display: grid;
}
.header h1 {
font-size: 2rem;
}
.score-value {
font-size: 1.5rem;
}
}
</style>
</head>
<body>
<div class="game-container">
<div class="header">
<h1>WĘŻOWA GRA</h1>
</div>
<div class="score-container">
<div class="score-box">
<div class="score-title">WYNIK</div>
<div id="score" class="score-value">0</div>
</div>
<div class="score-box">
<div class="score-title">NAJLEPSZY WYNIK</div>
<div id="high-score" class="score-value">0</div>
</div>
</div>
<canvas id="game-board" width="400" height="400"></canvas>
<div class="instructions">
Sterowanie: użyj klawiszy strzałek lub W/A/S/D. Zbieraj czerwone kropki, aby zdobywać punkty. Unikaj uderzania w ściany i własny ogon!
</div>
<div class="mobile-controls">
<button id="up-btn" class="mobile-btn">↑</button>
<button id="left-btn" class="mobile-btn">←</button>
<button id="right-btn" class="mobile-btn">→</button>
<button id="down-btn" class="mobile-btn">↓</button>
</div>
<div class="controls">
<button id="start-btn">START</button>
<button id="pause-btn">PAUZA</button>
<button id="restart-btn">RESTART</button>
</div>
<div id="start-screen" class="screen">
<h2>GRA W WĘŻA</h2>
<p>Steruj wężem za pomocą klawiszy strzałek lub przycisków na ekranie. Zbieraj czerwone jedzenie, aby zdobywać punkty i rosnąć.</p>
<p>Uważaj, aby nie uderzyć w ściany ani we własny ogon!</p>
<button id="start-game-btn">ROZPOCZNIJ GRĘ</button>
</div>
<div id="game-over-screen" class="screen">
<h2>KONIEC GRY!</h2>
<p>Twój wynik: <span id="final-score">0</span></p>
<p>Najlepszy wynik: <span id="final-high-score">0</span></p>
<button id="play-again-btn">ZAGRAJ PONOWNIE</button>
</div>
</div>
<script>
// Elementy DOM
const canvas = document.getElementById('game-board');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const highScoreElement = document.getElementById('high-score');
const startScreen = document.getElementById('start-screen');
const gameOverScreen = document.getElementById('game-over-screen');
const finalScoreElement = document.getElementById('final-score');
const finalHighScoreElement = document.getElementById('final-high-score');
// Przyciski
const startBtn = document.getElementById('start-btn');
const pauseBtn = document.getElementById('pause-btn');
const restartBtn = document.getElementById('restart-btn');
const startGameBtn = document.getElementById('start-game-btn');
const playAgainBtn = document.getElementById('play-again-btn');
// Przyciski mobilne
const upBtn = document.getElementById('up-btn');
const downBtn = document.getElementById('down-btn');
const leftBtn = document.getElementById('left-btn');
const rightBtn = document.getElementById('right-btn');
// Ustawienia gry
const gridSize = 20;
const gridWidth = canvas.width / gridSize;
const gridHeight = canvas.height / gridSize;
// Zmienne gry
let snake = [];
let food = {};
let direction = 'right';
let nextDirection = 'right';
let score = 0;
let highScore = localStorage.getItem('snakeHighScore') || 0;
let gameSpeed = 120;
let gameRunning = false;
let gamePaused = false;
let gameLoop;
// Inicjalizacja gry
function initGame() {
// Ustawienie węża na środku planszy
snake = [
{x: Math.floor(gridWidth / 2), y: Math.floor(gridHeight / 2)},
{x: Math.floor(gridWidth / 2) - 1, y: Math.floor(gridHeight / 2)},
{x: Math.floor(gridWidth / 2) - 2, y: Math.floor(gridHeight / 2)}
];
// Generowanie jedzenia
generateFood();
// Resetowanie kierunku i wyniku
direction = 'right';
nextDirection = 'right';
score = 0;
scoreElement.textContent = score;
highScoreElement.textContent = highScore;
// Ustawienie prędkości gry
gameSpeed = 120;
}
// Generowanie jedzenia
function generateFood() {
// Sprawdzanie, czy jedzenie nie pojawi się na wężu
let newFood;
let onSnake;
do {
onSnake = false;
newFood = {
x: Math.floor(Math.random() * gridWidth),
y: Math.floor(Math.random() * gridHeight)
};
// Sprawdź, czy nowe jedzenie nie znajduje się na wężu
for (let segment of snake) {
if (segment.x === newFood.x && segment.y === newFood.y) {
onSnake = true;
break;
}
}
} while (onSnake);
food = newFood;
}
// Rysowanie gry
function draw() {
// Czyszczenie canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Rysowanie siatki (opcjonalne)
ctx.strokeStyle = '#222';
for (let i = 0; i < gridWidth; i++) {
ctx.beginPath();
ctx.moveTo(i * gridSize, 0);
ctx.lineTo(i * gridSize, canvas.height);
ctx.stroke();
}
for (let j = 0; j < gridHeight; j++) {
ctx.beginPath();
ctx.moveTo(0, j * gridSize);
ctx.lineTo(canvas.width, j * gridSize);
ctx.stroke();
}
// Rysowanie węża
for (let i = 0; i < snake.length; i++) {
const segment = snake[i];
// Głowa węża
if (i === 0) {
ctx.fillStyle = '#4cff00';
ctx.shadowBlur = 10;
ctx.shadowColor = '#00ff00';
}
// Reszta ciała
else {
const intensity = 200 - Math.min(150, i * 3);
ctx.fillStyle = `rgb(50, ${intensity}, 50)`;
ctx.shadowBlur = 5;
ctx.shadowColor = '#00aa00';
}
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
// Obramowanie segmentu
ctx.strokeStyle = '#000';
ctx.lineWidth = 1;
ctx.strokeRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
}
// Rysowanie jedzenia
ctx.fillStyle = '#ff3333';
ctx.shadowBlur = 10;
ctx.shadowColor = '#ff0000';
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize / 2,
food.y * gridSize + gridSize / 2,
gridSize / 2 - 2,
0,
Math.PI * 2
);
ctx.fill();
// Resetowanie cienia
ctx.shadowBlur = 0;
}
// Aktualizacja stanu gry
function update() {
if (gamePaused) return;
// Aktualizacja kierunku
direction = nextDirection;
// Tworzenie nowej głowy na podstawie kierunku
const head = {...snake[0]};
switch (direction) {
case 'up':
head.y -= 1;
break;
case 'down':
head.y += 1;
break;
case 'left':
head.x -= 1;
break;
case 'right':
head.x += 1;
break;
}
// Sprawdzanie kolizji ze ścianami
if (head.x < 0 || head.x >= gridWidth || head.y < 0 || head.y >= gridHeight) {
gameOver();
return;
}
// Sprawdzanie kolizji z własnym ciałem
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver();
return;
}
}
// Dodawanie nowej głowy
snake.unshift(head);
// Sprawdzanie, czy wąż zjadł jedzenie
if (head.x === food.x && head.y === food.y) {
// Zwiększanie wyniku
score += 10;
scoreElement.textContent = score;
// Generowanie nowego jedzenia
generateFood();
// Przyspieszanie gry co 50 punktów
if (score % 50 === 0 && gameSpeed > 70) {
gameSpeed -= 5;
}
} else {
// Usuwanie ogona jeśli nie zjedzono jedzenia
snake.pop();
}
// Rysowanie gry
draw();
}
// Rozpoczęcie gry
function startGame() {
if (gameRunning) return;
initGame();
gameRunning = true;
gamePaused = false;
// Ukrywanie ekranów
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
// Rozpoczęcie pętli gry
clearInterval(gameLoop);
gameLoop = setInterval(update, gameSpeed);
}
// Pauzowanie gry
function pauseGame() {
if (!gameRunning) return;
gamePaused = !gamePaused;
pauseBtn.textContent = gamePaused ? 'WZNÓW' : 'PAUZA';
}
// Restart gry
function restartGame() {
clearInterval(gameLoop);
gameRunning = false;
startGame();
}
// Koniec gry
function gameOver() {
clearInterval(gameLoop);
gameRunning = false;
// Aktualizacja najlepszego wyniku
if (score > highScore) {
highScore = score;
localStorage.setItem('snakeHighScore', highScore);
highScoreElement.textContent = highScore;
}
// Aktualizacja ekranu końca gry
finalScoreElement.textContent = score;
finalHighScoreElement.textContent = highScore;
// Wyświetlanie ekranu końca gry
gameOverScreen.style.display = 'flex';
}
// Obsługa klawiatury
document.addEventListener('keydown', (e) => {
if (!gameRunning) return;
switch (e.key) {
case 'ArrowUp':
case 'w':
case 'W':
if (direction !== 'down') nextDirection = 'up';
break;
case 'ArrowDown':
case 's':
case 'S':
if (direction !== 'up') nextDirection = 'down';
break;
case 'ArrowLeft':
case 'a':
case 'A':
if (direction !== 'right') nextDirection = 'left';
break;
case 'ArrowRight':
case 'd':
case 'D':
if (direction !== 'left') nextDirection = 'right';
break;
case ' ':
pauseGame();
break;
}
});
// Obsługa przycisków mobilnych
upBtn.addEventListener('click', () => { if (direction !== 'down') nextDirection = 'up'; });
downBtn.addEventListener('click', () => { if (direction !== 'up') nextDirection = 'down'; });
leftBtn.addEventListener('click', () => { if (direction !== 'right') nextDirection = 'left'; });
rightBtn.addEventListener('click', () => { if (direction !== 'left') nextDirection = 'right'; });
// Przypisanie zdarzeń do przycisków
startBtn.addEventListener('click', startGame);
pauseBtn.addEventListener('click', pauseGame);
restartBtn.addEventListener('click', restartGame);
startGameBtn.addEventListener('click', startGame);
playAgainBtn.addEventListener('click', startGame);
// Inicjalizacja najlepszego wyniku
highScoreElement.textContent = highScore;
// Rysowanie ekranu startowego
draw();
</script>
</body>
</html>
Klasyczna rozgrywka węża:
Interfejs użytkownika:
Funkcje dodatkowe:
Responsywność:
Aby rozpocząć grę, wystarczy kliknąć przycisk "ROZPOCZNIJ GRĘ" na ekranie startowym. Miłej zabawy!