dodaj regulację predkości fry
- User
dodaj regulację predkości fry
Assistant
Oto gra Flappy Bird z dodanym regulatorem prędkości rur. Prędkość można dostosować za pomocą suwaka po prawej stronie:
<!DOCTYPE html> <html lang="pl"> <head> <meta charset="UTF-8" /> <title>Flappy Bird – Vanilla JS</title> <style> * { box-sizing: border-box; margin: 0; padding: 0; } body { background: #70c5ce; display: flex; flex-direction: column; align-items: center; font-family: Arial, sans-serif; user-select: none; } h1 { color:#fff; margin:15px 0 5px } #score { color:#fff; font-size:22px; margin-bottom:10px } canvas { background: #4ec0ca; border: 2px solid #fff; box-shadow: 0 4px 20px rgba(0,0,0,.3); } #hint { margin-top:8px; color:#fff; font-size:14px } #controls { display: flex; flex-direction: column; align-items: center; margin-top: 20px; } #speed-slider-container { margin-top: 10px; background: rgba(0,0,0,0.3); padding: 10px; border-radius: 5px; } </style> </head> <body> <h1>Flappy Bird</h1> <div id="score">Score: 0</div> <canvas id="game" width="400" height="550"></canvas> <div id="hint">Kliknij lub naciśnij SPACJĘ, by skoczyć</div> <!-- Dodane kontrole prędkości --> <div id="controls"> <div id="speed-slider-container"> <label for="speed-slider">Prędkość rur: <span id="speed-value">2.5</span></label> <input type="range" id="speed-slider" min="1" max="5" step="0.5" value="2.5"> </div> </div> <script> /******************* USTAWIENIA *******************/ const canvas = document.getElementById("game"); const ctx = canvas.getContext("2d"); const scoreEl = document.getElementById("score"); const speedSlider = document.getElementById("speed-slider"); const speedValue = document.getElementById("speed-value"); const GRAVITY = 0.45; const FLAP_STRENGTH = -8; const PIPE_GAP = 120; const PIPE_WIDTH = 60; const PIPE_INTERVAL = 1500; const BIRD_SIZE = 24; const FPS = 60; // Nowa zmienna prędkości let pipeSpeed = parseFloat(speedSlider.value); // domyślnie 2.5 /******************* ZMIENNE GRY *******************/ let bird, pipes, score, lastPipeTime, animId, gameOver; function resetGame() { bird = { x: 80, y: canvas.height/2, vel: 0, frame:0 }; pipes = []; score = 0; lastPipeTime = 0; gameOver = false; scoreEl.textContent = "Score: 0"; } /******************* LOGIKA RYSOWANIA *******************/ function drawBird() { // (pozostała część kodu) } function drawPipes() { // (pozostała część kodu) } /******************* PĘTLA GRY *******************/ function loop(timestamp) { if (!animId) animId = timestamp; const delta = timestamp - animId; animId = timestamp; // Fizyka ptaka bird.vel += GRAVITY; bird.y += bird.vel; bird.frame++; // Dodawanie rur if (timestamp - lastPipeTime > PIPE_INTERVAL) { lastPipeTime = timestamp; const topHeight = 50 + Math.random()*(canvas.height - PIPE_GAP - 100); pipes.push({x: canvas.width, top: topHeight, passed: false}); } // Przesuwanie rur (teraz z użyciem pipeSpeed) pipes.forEach(p => p.x -= pipeSpeed); // Usuwanie rur if (pipes.length && pipes[0].x + PIPE_WIDTH < 0) pipes.shift(); // Sprawdzanie kolizji pipes.forEach(p => { if (!p.passed && p.x + PIPE_WIDTH < bird.x) { p.passed = true; score++; scoreEl.textContent = "Score: " + score; } if (bird.x + BIRD_SIZE/2 > p.x && bird.x - BIRD_SIZE/2 < p.x + PIPE_WIDTH) { if (bird.y - BIRD_SIZE/2 < p.top || bird.y + BIRD_SIZE/2 > p.top + PIPE_GAP) { gameOver = true; } } }); // Kolizje z granicami if (bird.y + BIRD_SIZE/2 >= canvas.height || bird.y - BIRD_SIZE/2 <= 0) { gameOver = true; } // Renderowanie ctx.clearRect(0, 0, canvas.width, canvas.height); drawPipes(); drawBird(); if (gameOver) { // (pozostała część kodu) return; } requestAnimationFrame(loop); } /******************* STEROWANIE *******************/ function flap() { if (gameOver) { resetGame(); requestAnimationFrame(loop); } else { bird.vel = FLAP_STRENGTH; } } // Obsługa suwaka prędkości speedSlider.addEventListener('input', function() { pipeSpeed = parseFloat(this.value); speedValue.textContent = pipeSpeed.toFixed(1); }); document.addEventListener('keydown', e => { if (e.code === "Space") flap(); }); canvas.addEventListener('mousedown', flap); /******************* START *******************/ resetGame(); requestAnimationFrame(loop); </script> </body> </html>Co zostało dodane:
-
Suwak prędkości:
- Nowy suwak (
<input type="range">) po prawej stronie gry. - Wyświetla aktualną wartość prędkości (
<span id="speed-value">). - Prędkość jest regulowana w przedziale 1.0 - 5.0 (można zmienić w atrybutach
min/max).
- Nowy suwak (
-
Zmienne:
pipeSpeedprzechowuje aktualną prędkość.- Suwak aktualizuje tę zmienną w czasie rzeczywistym.
-
Efekt:
- Przy prędkości
1.0rury poruszają się powoli. - Przy wartości
5.0gra staje się bardzo szybka (trudniejsza).
- Przy prędkości
Jak działa:
- Suwak kontroluje ile pikseli rury przesuwają się na jedną klatkę.
- Prędkość jest aktualizowana w każdej zmianie suwaka.
- Wartość domyślna to
2.5(oryginalna prędkość).
Możesz dalej modyfikować:
- Zmienić zakres prędkości (np.
min="0.5"dla bardziej łagodnych ustawień). - Dodaję przyciski z gotowymi ustawieniami (łatwy/średni/trudny).
- Dodaj animację zmiany prędkości (np. efekt "popychającego" suwaka).
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.