Zrób grę węża w HTML, CSS i Javascript
- User
Zrób grę węża w HTML, CSS i Javascript
Assistant
Gra Wąż w HTML, CSS i JavaScript
Oto kompletny kod gry "Wąż" napisanej w HTML, CSS i JavaScript. Gra działa w przeglądarce i zawiera wszystkie podstawowe funkcje: sterowanie wężem, zbieranie jedzenia, zwiększanie punktacji i wykrywanie kolizji.
Kod HTML (index.html)
<!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> <link rel="stylesheet" href="style.css"> </head> <body> <div class="game-container"> <h1>Gra Wąż</h1> <div class="score">Wynik: <span id="score">0</span></div> <canvas id="gameCanvas" width="400" height="400"></canvas> <div class="controls"> <p>Sterowanie: strzałki lub W, A, S, D</p> <button id="startBtn">Start</button> <button id="resetBtn">Reset</button> </div> </div> <script src="script.js"></script> </body> </html>Kod CSS (style.css)
body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } .game-container { text-align: center; background-color: white; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } h1 { color: #333; margin-top: 0; } .score { font-size: 1.2em; margin: 10px 0; } canvas { border: 1px solid #333; background-color: #f8f8f8; display: block; margin: 0 auto; } .controls { margin-top: 15px; } button { background-color: #4CAF50; border: none; color: white; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 5px; transition: background-color 0.3s; } button:hover { background-color: #45a049; } #resetBtn { background-color: #f44336; } #resetBtn:hover { background-color: #d32f2f; }Kod JavaScript (script.js)
document.addEventListener('DOMContentLoaded', () => { const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const scoreElement = document.getElementById('score'); const startBtn = document.getElementById('startBtn'); const resetBtn = document.getElementById('resetBtn'); const box = 20; let snake = []; let direction = null; let food = {}; let score = 0; let game = null; let gameSpeed = 150; // Inicjalizacja gry function initGame() { snake = [ {x: 9 * box, y: 10 * box}, {x: 8 * box, y: 10 * box}, {x: 7 * box, y: 10 * box} ]; direction = null; score = 0; scoreElement.textContent = score; createFood(); } // Rysowanie planszy function draw() { // Czyszczenie canvas ctx.fillStyle = '#f8f8f8'; ctx.fillRect(0, 0, canvas.width, canvas.height); // Rysowanie węża for (let i = 0; i < snake.length; i++) { ctx.fillStyle = i === 0 ? '#4CAF50' : '#8BC34A'; ctx.fillRect(snake[i].x, snake[i].y, box, box); ctx.strokeStyle = '#fff'; ctx.strokeRect(snake[i].x, snake[i].y, box, box); } // Rysowanie jedzenia ctx.fillStyle = '#f44336'; ctx.fillRect(food.x, food.y, box, box); // Pozycja głowy węża let snakeX = snake[0].x; let snakeY = snake[0].y; // Zmiana kierunku if (direction === 'LEFT') snakeX -= box; if (direction === 'UP') snakeY -= box; if (direction === 'RIGHT') snakeX += box; if (direction === 'DOWN') snakeY += box; // Sprawdzanie kolizji z jedzeniem if (snakeX === food.x && snakeY === food.y) { score++; scoreElement.textContent = score; createFood(); // Przyspieszanie gry co 5 punktów if (score % 5 === 0 && gameSpeed > 50) { gameSpeed -= 10; clearInterval(game); game = setInterval(gameLoop, gameSpeed); } } else { // Usuwanie ogona jeśli nie zjedzono jedzenia snake.pop(); } // Dodawanie nowej głowy const newHead = {x: snakeX, y: snakeY}; // Sprawdzanie kolizji if ( snakeX < 0 || snakeY < 0 || snakeX >= canvas.width || snakeY >= canvas.height || collision(newHead, snake) ) { clearInterval(game); alert('Game Over! Twój wynik: ' + score); return; } snake.unshift(newHead); } // Sprawdzanie kolizji function collision(head, array) { for (let i = 0; i < array.length; i++) { if (head.x === array[i].x && head.y === array[i].y) { return true; } } return false; } // Tworzenie jedzenia function createFood() { food = { x: Math.floor(Math.random() * (canvas.width / box)) * box, y: Math.floor(Math.random() * (canvas.height / box)) * box }; // Sprawdzanie czy jedzenie nie pojawiło się na wężu for (let i = 0; i < snake.length; i++) { if (food.x === snake[i].x && food.y === snake[i].y) { createFood(); } } } // Obsługa klawiatury function changeDirection(e) { if (e.key === 'ArrowLeft' && direction !== 'RIGHT') direction = 'LEFT'; else if (e.key === 'ArrowUp' && direction !== 'DOWN') direction = 'UP'; else if (e.key === 'ArrowRight' && direction !== 'LEFT') direction = 'RIGHT'; else if (e.key === 'ArrowDown' && direction !== 'UP') direction = 'DOWN'; // Alternatywne sterowanie WASD else if (e.key === 'a' && direction !== 'RIGHT') direction = 'LEFT'; else if (e.key === 'w' && direction !== 'DOWN') direction = 'UP'; else if (e.key === 'd' && direction !== 'LEFT') direction = 'RIGHT'; else if (e.key === 's' && direction !== 'UP') direction = 'DOWN'; } // Główna pętla gry function gameLoop() { if (direction !== null) { draw(); } } // Start gry startBtn.addEventListener('click', () => { if (game) clearInterval(game); initGame(); game = setInterval(gameLoop, gameSpeed); }); // Reset gry resetBtn.addEventListener('click', () => { clearInterval(game); initGame(); gameSpeed = 150; }); // Nasłuchiwanie klawiszy document.addEventListener('keydown', changeDirection); // Inicjalizacja przy pierwszym załadowaniu initGame(); });Jak uruchomić grę
- Utwórz trzy pliki:
index.html,style.cssiscript.jsw tym samym folderze. - Skopiuj odpowiednie kody do każdego pliku.
- Otwórz plik
index.htmlw przeglądarce internetowej. - Kliknij przycisk "Start" aby rozpocząć grę.
Funkcjonalności gry
- Sterowanie wężem za pomocą strzałek lub klawiszy WASD
- Utwórz trzy pliki:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.