napisz mi prostągre snake. waz ma głowe png, jedzenie to png. gra ma być prosta 8 bit…
- User
napisz mi prostągre snake. waz ma głowe png, jedzenie to png. gra ma być prosta 8 bit napisama w html. cała gra ma byc w ramce sterowana strzalkami
Assistant
Oto prosta 8-bitowa gra Snake w HTML, z wężem z głową PNG i jedzeniem PNG, w ramce, sterowana strzałkami:
<!DOCTYPE html> <html lang="pl"> <head> <meta charset="UTF-8"> <title>Snake 8-bit</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #222; margin: 0; font-family: 'Courier New', monospace; } .game-container { border: 10px solid #555; background-color: #000; padding: 20px; border-radius: 10px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.5); } #game-canvas { border: 4px solid #888; background-color: #222; display: block; } .score { color: #fff; font-size: 24px; text-align: center; margin: 10px 0; } .title { color: #0f0; text-align: center; font-size: 32px; margin-bottom: 10px; text-transform: uppercase; letter-spacing: 3px; } </style> </head> <body> <div class="game-container"> <div class="title">SNAKE</div> <canvas id="game-canvas" width="400" height="400"></canvas> <div class="score">Wynik: <span id="score-value">0</span></div> </div> <script> // Referencje do elementów DOM const canvas = document.getElementById('game-canvas'); const ctx = canvas.getContext('2d'); const scoreElement = document.getElementById('score-value'); // Ustawienia gry const gridSize = 20; const gridWidth = canvas.width / gridSize; const gridHeight = canvas.height / gridSize; let score = 0; let gameSpeed = 120; let gameOver = false; // Inicjalizacja węża let snake = [ {x: 5, y: 5}, {x: 4, y: 5}, {x: 3, y: 5} ]; // Kierunek ruchu let direction = 'right'; let nextDirection = 'right'; // Pozycja jedzenia let food = {}; // Obrazy const snakeHeadImg = new Image(); const foodImg = new Image(); // Ustaw ścieżki do obrazów (należy zastąpić swoimi rzeczywistymi ścieżkami) snakeHeadImg.src = 'snake-head.png'; // Zastąp swoim obrazem głowy węża foodImg.src = 'food.png'; // Zastąp swoim obrazem jedzenia // Funkcja do generowania nowej pozycji jedzenia function generateFood() { food = { x: Math.floor(Math.random() * gridWidth), y: Math.floor(Math.random() * gridHeight) }; // Sprawdź czy jedzenie nie jest na wężu for (let cell of snake) { if (cell.x === food.x && cell.y === food.y) { return generateFood(); } } } // Inicjalizacja jedzenia generateFood(); // Obsługa klawiatury document.addEventListener('keydown', function(event) { switch(event.key) { case 'ArrowUp': if (direction !== 'down') nextDirection = 'up'; break; case 'ArrowDown': if (direction !== 'up') nextDirection = 'down'; break; case 'ArrowLeft': if (direction !== 'right') nextDirection = 'left'; break; case 'ArrowRight': if (direction !== 'left') nextDirection = 'right'; break; case 'Enter': if (gameOver) resetGame(); break; } }); // Rysowanie gry function draw() { // Wyczyść canvas ctx.fillStyle = '#222'; ctx.fillRect(0, 0, canvas.width, canvas.height); // Narysuj siatkę w stylu 8-bit ctx.strokeStyle = '#333'; ctx.lineWidth = 1; for (let x = 0; x < canvas.width; x += gridSize) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, canvas.height); ctx.stroke(); } for (let y = 0; y < canvas.height; y += gridSize) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(canvas.width, y); ctx.stroke(); } // Narysuj węża snake.forEach((cell, index) => { if (index === 0) { // Głowa węża (obraz PNG) ctx.save(); ctx.translate(cell.x * gridSize + gridSize / 2, cell.y * gridSize + gridSize / 2); // Obróć głowę w zależności od kierunku switch(direction) { case 'up': ctx.rotate(-Math.PI/2); break; case 'down': ctx.rotate(Math.PI/2); break; case 'left': ctx.rotate(Math.PI); break; case 'right': ctx.rotate(0); break; } ctx.drawImage(snakeHeadImg, -gridSize/2, -gridSize/2, gridSize, gridSize); ctx.restore(); } else { // Ciało węża ctx.fillStyle = '#0f0'; ctx.fillRect(cell.x * gridSize, cell.y * gridSize, gridSize, gridSize); // Dodaj efekt 8-bit (pixelowy) ctx.strokeStyle = '#0c0'; ctx.lineWidth = 1; ctx.strokeRect(cell.x * gridSize, cell.y * gridSize, gridSize, gridSize); } }); // Narysuj jedzenie (obraz PNG) ctx.drawImage(foodImg, food.x * gridSize, food.y * gridSize, gridSize, gridSize); // Wyświetl komunikat o końcu gry if (gameOver) { ctx.fillStyle = 'rgba(0, 0, 0, 0.7)'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = '#f00'; ctx.font = '30px "Press Start 2P", monospace'; ctx.textAlign = 'center'; ctx.fillText('GAME OVER', canvas.width / 2, canvas.height / 2 - 20); ctx.fillStyle = '#fff'; ctx.font = '16px "Press Start 2P", monospace'; ctx.fillText('Naciśnij ENTER, aby zagrać ponownie', canvas.width / 2, canvas.height / 2 + 20); } } // Aktualizacja stanu gry function update() { if (gameOver) return; // Aktualizuj kierunek direction = nextDirection; // Oblicz nową pozycję głowy const head = {...snake[0]}; switch(direction) { case 'up': head.y--; break; case 'down': head.y++; break; case 'left': head.x--; break; case 'right': head.x++; break; } // Sprawdź kolizję ze ścianą if (head.x < 0 || head.x >= gridWidth || head.y < 0 || head.y >= gridHeight) { gameOver = true; return; } // Sprawdź kolizję z własnym ciałem for (let i = 0; i < snake.length; i++) { if (snake[i].x === head.x && snake[i].y === head.y) { gameOver = true; return; } } // Dodaj nową głowę do węża snake.unshift(head); // Sprawdź, czy wąż zjadł jedzenie if (head.x === food.x && head.y === food.y) { score++; scoreElement.textContent = score; generateFood(); // Zwiększ prędkość co 5 punktów if (score % 5 === 0 && gameSpeed > 50) { gameSpeed -= 10; } } else { // Usuń ogon, jeśli wąż nie zjadł jedzenia snake.pop(); } } // Reset gry function resetGame() { snake = [ {x: 5, y: 5}, {x: 4, y: 5}, {x: 3, y: 5} ]; direction = 'right'; nextDirection = 'right'; score = 0; scoreElement.textContent = score; gameSpeed = 120; gameOver = false; generateFood(); } // Pętla gry function gameLoop() { update(); draw(); setTimeout(gameLoop, gameSpeed); } // Obsługa błędów ładowania obrazów snakeHeadImg.onerror = function() { // Zastępczy kształt, jeśli obrazek się nie załaduje snakeHeadImg.onload = null; console.warn("Nie można załadować obrazu głowy węża, używam zastępczego kształtu."); }; foodImg.onerror = function() { // Zastępczy kształt, jeśli obrazek się nie załaduje foodImg.onload = null; console.warn("Nie można załadować obrazu jedzenia, używam zastępczego kształtu."); }; // Rozpocznij grę po załadowaniu obrazów let imagesLoaded = 0; function imageLoaded() { imagesLoaded++; if (imagesLoaded === 2) { gameLoop(); } } snakeHeadImg.onload = imageLoaded; foodImg.onload = imageLoaded; // Jeśli obrazy nie załadują się w ciągu 2 sekund, rozpocznij grę mimo to setTimeout(function() { if (imagesLoaded < 2) { console.warn("Nie wszystkie obrazy się załadowały, rozpoczynam grę mimo to."); gameLoop(); } }, 2000); </script> </body> </html>Czy chciałbyś, żebym wyjaśnił jak działa ten kod lub jak go dostosować do twoich potrzeb?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.