napisz mi prostą grę wężyk w html
- User
napisz mi prostą 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 prostą grę wężyk w html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Stworzę dla Ciebie prostą, ale w pełni funkcjonalną grę węża, która działa w przeglądarce. Gra zawiera:
Oto kompletny kod:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prosta 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);
overflow: hidden;
}
.game-container {
position: relative;
width: 100%;
max-width: 500px;
text-align: center;
padding: 20px;
}
.header {
margin-bottom: 20px;
color: white;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}
.header h1 {
font-size: 2.8rem;
margin-bottom: 5px;
color: #ffde59;
}
.score-container {
display: flex;
justify-content: center;
gap: 30px;
margin-top: 15px;
}
.score-box {
background: rgba(0, 0, 0, 0.3);
padding: 10px 20px;
border-radius: 10px;
min-width: 120px;
}
#score, #high-score {
font-size: 1.8rem;
font-weight: bold;
color: #4cff8f;
}
.game-board {
background: rgba(0, 20, 40, 0.8);
border-radius: 10px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
overflow: hidden;
position: relative;
}
canvas {
display: block;
margin: 0 auto;
}
.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, 20, 40, 0.9);
color: white;
padding: 20px;
text-align: center;
z-index: 10;
}
.screen h2 {
font-size: 2.5rem;
margin-bottom: 20px;
color: #ffde59;
}
.screen p {
font-size: 1.2rem;
margin-bottom: 25px;
max-width: 80%;
line-height: 1.6;
}
#game-over {
display: none;
}
.btn {
background: linear-gradient(to right, #ff8c00, #ffde59);
border: none;
color: #1a2a6c;
padding: 12px 30px;
font-size: 1.2rem;
font-weight: bold;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s ease;
margin-top: 15px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}
.btn:hover {
transform: translateY(-3px);
box-shadow: 0 6px 15px rgba(0, 0, 0, 0.4);
}
.btn:active {
transform: translateY(1px);
}
.controls {
margin-top: 20px;
color: white;
background: rgba(0, 0, 0, 0.3);
padding: 15px;
border-radius: 10px;
font-size: 0.9rem;
}
.instructions {
color: #a0d2ff;
margin-top: 20px;
font-size: 1rem;
line-height: 1.5;
max-width: 80%;
margin-left: auto;
margin-right: auto;
}
@media (max-width: 500px) {
.header h1 {
font-size: 2.2rem;
}
.score-container {
flex-direction: column;
gap: 10px;
}
.screen h2 {
font-size: 2rem;
}
}
</style>
</head>
<body>
<div class="game-container">
<div class="header">
<h1>WĄŻ</h1>
<div class="score-container">
<div class="score-box">
<div>WYNIK</div>
<div id="score">0</div>
</div>
<div class="score-box">
<div>REKORD</div>
<div id="high-score">0</div>
</div>
</div>
</div>
<div class="game-board">
<canvas id="game-canvas" width="400" height="400"></canvas>
<div id="start-screen" class="screen">
<h2>Witaj w Grze Wąż!</h2>
<p>Steruj wężem za pomocą strzałek na klawiaturze. Zbieraj jedzenie, aby rosnąć i zdobywać punkty.</p>
<p>Uważaj, aby nie uderzyć w ściany ani we własny ogon!</p>
<button id="start-btn" class="btn">ROZPOCZNIJ GRĘ</button>
<div class="instructions">
<p>Sterowanie: ↑ ↓ ← →</p>
</div>
</div>
<div id="game-over" class="screen">
<h2>KONIEC GRY!</h2>
<p>Twój wynik: <span id="final-score">0</span></p>
<p>Najlepszy wynik: <span id="best-score">0</span></p>
<button id="restart-btn" class="btn">ZAGRAJ PONOWNIE</button>
</div>
</div>
</div>
<script>
// Elementy DOM
const canvas = document.getElementById('game-canvas');
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');
const finalScoreElement = document.getElementById('final-score');
const bestScoreElement = document.getElementById('best-score');
const startBtn = document.getElementById('start-btn');
const restartBtn = document.getElementById('restart-btn');
// Ustawienia gry
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let speed = 8;
// Stan gry
let snake = [];
let food = {};
let dx = 0;
let dy = 0;
let score = 0;
let highScore = localStorage.getItem('snakeHighScore') || 0;
let gameRunning = false;
let gameLoop;
// Inicjalizacja gry
function initGame() {
// Ustaw węża na środku planszy
snake = [
{x: 10, y: 10},
{x: 9, y: 10},
{x: 8, y: 10}
];
// Wygeneruj jedzenie
generateFood();
// Ustaw początkowy kierunek (w prawo)
dx = 1;
dy = 0;
// Resetuj wynik
score = 0;
scoreElement.textContent = score;
highScoreElement.textContent = highScore;
// Ukryj ekrany
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
gameRunning = true;
}
// Generowanie jedzenia
function generateFood() {
// Sprawdź czy nowe jedzenie nie pojawi się na wężu
let onSnake;
do {
onSnake = false;
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
for (let segment of snake) {
if (segment.x === food.x && segment.y === food.y) {
onSnake = true;
break;
}
}
} while (onSnake);
}
// Rysowanie gry
function draw() {
// Wyczyść canvas
ctx.fillStyle = 'rgba(0, 20, 40, 1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Narysuj siatkę
ctx.strokeStyle = 'rgba(30, 70, 120, 0.3)';
ctx.lineWidth = 0.5;
for (let i = 0; i < tileCount; i++) {
ctx.beginPath();
ctx.moveTo(i * gridSize, 0);
ctx.lineTo(i * gridSize, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * gridSize);
ctx.lineTo(canvas.width, i * gridSize);
ctx.stroke();
}
// Narysuj węża
snake.forEach((segment, index) => {
if (index === 0) {
// Głowa węża
ctx.fillStyle = '#4cff8f';
ctx.shadowColor = '#4cff8f';
} else {
// Ciało węża
const colorValue = 150 - (index % 5) * 10;
ctx.fillStyle = `rgb(100, ${colorValue}, 255)`;
ctx.shadowColor = 'rgba(100, 150, 255, 0.5)';
}
ctx.shadowBlur = 10;
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
// Obramowanie segmentu
ctx.strokeStyle = '#0a1a3a';
ctx.lineWidth = 2;
ctx.strokeRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
});
// Narysuj jedzenie
ctx.fillStyle = '#ff5252';
ctx.shadowColor = '#ff5252';
ctx.shadowBlur = 15;
ctx.beginPath();
ctx.arc(
food.x * gridSize + gridSize/2,
food.y * gridSize + gridSize/2,
gridSize/2,
0,
Math.PI * 2
);
ctx.fill();
ctx.shadowBlur = 0;
}
// Aktualizacja stanu gry
function update() {
if (!gameRunning) return;
// Oblicz nową pozycję głowy węża
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
// Sprawdź kolizje ze ścianami
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
gameOver();
return;
}
// Sprawdź kolizję 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;
}
}
// Dodaj nową głowę
snake.unshift(head);
// Sprawdź czy wąż zjadł jedzenie
if (head.x === food.x && head.y === food.y) {
// Zwiększ wynik
score += 10;
scoreElement.textContent = score;
// Generuj nowe jedzenie
generateFood();
// Zwiększ prędkość co 50 punktów
if (score % 50 === 0 && speed < 15) {
speed += 1;
}
} else {
// Usuń ogon jeśli nie zjedzono jedzenia
snake.pop();
}
}
// Koniec gry
function gameOver() {
gameRunning = false;
clearInterval(gameLoop);
// Aktualizuj najlepszy wynik
if (score > highScore) {
highScore = score;
localStorage.setItem('snakeHighScore', highScore);
highScoreElement.textContent = highScore;
}
// Pokaż ekran końca gry
finalScoreElement.textContent = score;
bestScoreElement.textContent = highScore;
gameOverScreen.style.display = 'flex';
}
// Obsługa klawiatury
document.addEventListener('keydown', e => {
// Zapobiegaj przewijaniu strony przy używaniu strzałek
if ([37, 38, 39, 40].includes(e.keyCode)) {
e.preventDefault();
}
// Zmiana kierunku (zapobiega odwróceniu się o 180 stopni)
switch(e.keyCode) {
case 37: // Lewo
if (dx !== 1) {
dx = -1;
dy = 0;
}
break;
case 38: // Góra
if (dy !== 1) {
dx = 0;
dy = -1;
}
break;
case 39: // Prawo
if (dx !== -1) {
dx = 1;
dy = 0;
}
break;
case 40: // Dół
if (dy !== -1) {
dx = 0;
dy = 1;
}
break;
}
});
// Przyciski
startBtn.addEventListener('click', () => {
initGame();
gameLoop = setInterval(() => {
update();
draw();
}, 1000 / speed);
});
restartBtn.addEventListener('click', () => {
initGame();
gameLoop = setInterval(() => {
update();
draw();
}, 1000 / speed);
});
// Inicjalizacja
highScoreElement.textContent = highScore;
draw();
</script>
</body>
</html>
Po prostu skopiuj cały kod do pliku HTML i otwórz w przeglądarce, aby zagrać!