o3-2025-04-16· OpenAI
LMArena Human Preference 140K
Siema, napisz mi grę w węża w html
- User
Siema, napisz mi grę w węża w html
- Assistant
Hej! Poniżej masz kompletny, samodzielny plik HTML z klasycznym „Wężem”.
Wystarczy skopiować kod do pliku (np. index.html), otworzyć w przeglądarce i grać 🚀<!DOCTYPE html> <html lang="pl"> <head> <meta charset="UTF-8" /> <title>Snake</title> <style> /* Styl gry */ * { box-sizing: border-box; } body { margin: 0; background:#222; color:#fff; font-family: sans-serif; display:flex; justify-content:center; align-items:center; height:100vh; } canvas { background:#000; } #score { position:fixed; top:10px; left:50%; transform:translateX(-50%); font-size:24px; font-weight:700; } </style> </head> <body> <div id="score">Wynik: 0</div> <canvas id="game" width="400" height="400"></canvas> <script> /* --- PARAMETRY PLANSZY --- */ const canvas = document.getElementById('game'); const ctx = canvas.getContext('2d'); const tile = 20; // rozmiar pojedynczego „kwadracika” const tiles = canvas.width / tile; // ile kwadratów mieści się w rzędzie /* --- STAN GRY --- */ let snake = [{x:10, y:10}]; // startowa długość węża let dx = 0, dy = 0; // aktualny kierunek ruchu let food = randomPos(); // pozycja jedzenia let score = 0; // licznik punktów let loop; // identyfikator setInterval /* --- OBSŁUGA KLAWIATURY --- */ document.addEventListener('keydown', e => { switch (e.key) { case 'ArrowUp' : if (dy === 0) { dx = 0; dy = -1; } break; case 'ArrowDown' : if (dy === 0) { dx = 0; dy = 1; } break; case 'ArrowLeft' : if (dx === 0) { dx = -1; dy = 0; } break; case 'ArrowRight': if (dx === 0) { dx = 1; dy = 0; } break; } }); /* --- LOSOWANIE POZYCJI JEDZENIA --- */ function randomPos() { return { x: Math.floor(Math.random() * tiles), y: Math.floor(Math.random() * tiles) }; } /* --- GŁÓWNA PĘTLA GRY --- */ function gameLoop() { const head = { x: snake[0].x + dx, y: snake[0].y + dy }; /* 1. Sprawdź kolizje ze ścianą lub z samym sobą */ if ( head.x < 0 || head.x >= tiles || head.y < 0 || head.y >= tiles || snake.some(seg => seg.x === head.x && seg.y === head.y) ) return gameOver(); /* 2. Przesuń węża */ snake.unshift(head); /* 3. Zjedzenie jedzenia */ if (head.x === food.x && head.y === food.y) { score++; document.getElementById('score').textContent = 'Wynik: ' + score; food = randomPos(); } else { snake.pop(); // usuń ogon jeśli bez jedzenia } /* 4. Rysowanie planszy */ ctx.fillStyle = '#000'; ctx.fillRect(0, 0, canvas.width, canvas.height); // jedzenie ctx.fillStyle = 'red'; ctx.fillRect(food.x * tile, food.y * tile, tile, tile); // wąż ctx.fillStyle = 'lime'; snake.forEach(seg => { ctx.fillRect(seg.x * tile, seg.y * tile, tile - 1, tile - 1); // -1 dla delikatnej siatki }); } /* --- START GRY --- */ loop = setInterval(gameLoop, 100); // 100 ms ≈ 10 FPS /* --- KONIEC GRY --- */ function gameOver() { clearInterval(loop); ctx.fillStyle = 'rgba(0,0,0,0.5)'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = '#fff'; ctx.textAlign = 'center'; ctx.font = '30px sans-serif'; ctx.fillText('Koniec gry!', canvas.width / 2, canvas.height / 2 - 10); ctx.font = '20px sans-serif'; ctx.fillText('Wynik: ' + score, canvas.width / 2, canvas.height / 2 + 20); ctx.fillText('Spacja = nowa gra', canvas.width / 2, canvas.height / 2 + 50); document.addEventListener('keydown', restart, { once: true }); } /* --- RESTART --- */ function restart(e) { if (e.code !== 'Space') return document.addEventListener('keydown', restart, { once: true }); snake = [{x:10, y:10}]; dx = dy = 0; food = randomPos(); score = 0; document.getElementById('score').textContent = 'Wynik: 0'; loop = setInterval(gameLoop, 100); } </script> </body> </html>Co możesz łatwo zmienić/rozbudować:
- Zwiększ prędkość → zmień wartość
setInterval(np.80,60ms). - Inny rozmiar planszy → zmień atrybuty
width,heightcanvasu +tile. - Dodaj poziomy trudności czy dodatkowe przeszkody - wystarczy dorzucić tablicę „ścian” i sprawdzać kolizję.
- Stylizacja – pobaw się CSS-em lub kolorami w js.
Miłej zabawy! 🎮
- Zwiększ prędkość → zmień wartość
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.