stworz prosta gre w wezyka w html
- User
stworz prosta gre w wezyka w html
Assistant
Oto przykładowa prosta gra "Snake" w HTML/JS. Możesz skopiować kod do pliku HTML i otworzyć w przeglądarce:
<!DOCTYPE html> <html> <head> <title>Snake Game</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: #f0f0f0; font-family: Arial, sans-serif; } canvas { background: #000; border: 2px solid #333; } #score { position: absolute; top: 20px; color: #333; font-size: 24px; } </style> </head> <body> <div id="score">Score: 0</div> <canvas id="game" width="400" height="400"></canvas> <script> const canvas = document.getElementById('game'); const ctx = canvas.getContext('2d'); const scoreElement = document.getElementById('score'); const gridSize = 20; let snake = [ {x: 10, y: 10} ]; let food = generateFood(); let dx = 1; let dy = 0; let score = 0; let gameSpeed = 100; // Game loop setInterval(game, gameSpeed); function game() { update(); draw(); } function update() { // Move snake const head = {x: snake[0].x + dx, y: snake[0].y + dy}; snake.unshift(head); // Check food collision if(head.x === food.x && head.y === food.y) { score += 10; scoreElement.textContent = `Score: ${score}`; food = generateFood(); if(gameSpeed > 50) gameSpeed -= 5; // Speed up } else { snake.pop(); } // Check wall collision if(head.x < 0 || head.x >= canvas.width/gridSize || head.y < 0 || head.y >= canvas.height/gridSize) { alert(`Game Over! Score: ${score}`); resetGame(); } // Check self collision for(let i = 1; i < snake.length; i++) { if(head.x === snake[i].x && head.y === snake[i].y) { alert(`Game Over! Score: ${score}`); resetGame(); } } } function draw() { // Clear canvas ctx.fillStyle = '#000'; ctx.fillRect(0, 0, canvas.width, canvas.height); // Draw snake ctx.fillStyle = '#0f0'; snake.forEach((segment, index) => { ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize-2, gridSize-2); }); // Draw food ctx.fillStyle = '#f00'; ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize-2, gridSize-2); } function generateFood() { return { x: Math.floor(Math.random() * (canvas.width/gridSize)), y: Math.floor(Math.random() * (canvas.height/gridSize)) }; } function resetGame() { snake = [{x: 10, y: 10}]; dx = 1; dy = 0; score = 0; gameSpeed = 100; scoreElement.textContent = `Score: 0`; } // Controls document.addEventListener('keydown', (e) => { switch(e.key) { case 'ArrowUp': if(dy !== 1) { dx = 0; dy = -1; } break; case 'ArrowDown': if(dy !== -1) { dx = 0; dy = 1; } break; case 'ArrowLeft': if(dx !== 1) { dx = -1; dy = 0; } break; case 'ArrowRight': if(dx !== -1) { dx = 1; dy = 0; } break; } }); </script> </body> </html>Główne cechy gry:
- Sterowanie strzałkami
- Rosnąca długość węża po zjedzeniu czerwonej jedzenia
- System punktacji
- Kolizje ze ścianami i własnym ciałem
- Postępująco zwiększająca się prędkość
- Wykrywanie końca gry
Aby gra działała:
- Zapisz kod jako plik .html
- Otwórz w nowoczesnej przeglądarce
- Steruj strzałkami na klawiaturze
- Unikaj kolizji ze ścianami i własnym ciałem
Gra ma podstawowe elementy:
- Plansza 20x20 kratek
- System punktacji
- Mechanika rosnącego węża
- Wykrywanie kolizji
- Proste UI z wynikiem
- Animowany ruch
Możesz modyfikować wartości w kodzie (rozmiary, prędkość, kolor) aby dostosować grę do własnych potrzeb.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.