Programmiere ein Doodle Jump Spiel mit JavaSkript, HTML und CSS
- User
Programmiere ein Doodle Jump Spiel mit JavaSkript, HTML und CSS
Assistant
Gerne! Ich werde Ihnen eine einfache Implementierung von Doodle Jump mit JavaScript, HTML und CSS zeigen. Hier ist ein Schritt-für-Schritt-Beispiel:
HTML (index.html):
<!DOCTYPE html> <html lang="de"> <head> <meta charset="UTF-8"> <title>Doodle Jump</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div id="game-container"> <div id="doodler"></div> </div> <div id="score">Score: <span id="score-value">0</span></div> <script src="game.js"></script> </body> </html>CSS (styles.css):
body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } #game-container { width: 400px; height: 600px; border: 2px solid black; position: relative; overflow: hidden; background-color: white; } #doodler { width: 50px; height: 50px; background-color: blue; position: absolute; bottom: 50px; } #score { position: absolute; top: 10px; left: 10px; font-size: 20px; }JavaScript (game.js):
const container = document.getElementById('game-container'); const doodler = document.getElementById('doodler'); const scoreElement = document.getElementById('score-value'); let doodlerLeft = 200; let doodlerBottom = 50; let isGameOver = false; let platformCount = 5; let platforms = []; let score = 0; let gravity = 0.5; let jumpSpeed = 10; class Platform { constructor(newPlatformBottom) { this.bottom = newPlatformBottom; this.left = Math.random() * (container.clientWidth - 85); this.visual = document.createElement('div'); const visual = this.visual; visual.classList.add('platform'); visual.style.left = this.left + 'px'; visual.style.bottom = this.bottom + 'px'; container.appendChild(visual); } } function createPlatforms() { for (let i = 0; i < platformCount; i++) { let platformGap = container.clientHeight / platformCount; let newPlatformBottom = 100 + i * platformGap; let newPlatform = new Platform(newPlatformBottom); platforms.push(newPlatform); } } function movePlatforms() { if (doodlerBottom > 200) { platforms.forEach(platform => { platform.bottom -= 4; platform.visual.style.bottom = platform.bottom + 'px'; if (platform.bottom < 0) { let firstPlatform = platforms[0].visual; firstPlatform.remove(); platforms.shift(); score++; scoreElement.textContent = score; let newPlatform = new Platform(container.clientHeight); platforms.push(newPlatform); } }); } } function jump() { doodlerBottom += jumpSpeed; doodler.style.bottom = doodlerBottom + 'px'; jumpSpeed -= gravity; } function fall() { doodlerBottom -= jumpSpeed; doodler.style.bottom = doodlerBottom + 'px'; jumpSpeed -= gravity; } function control(e) { if (e.key === 'ArrowLeft') { doodlerLeft -= 10; } if (e.key === 'ArrowRight') { doodlerLeft += 10; } doodler.style.left = doodlerLeft + 'px'; } function checkCollision() { platforms.forEach(platform => { if ( (doodlerBottom >= platform.bottom) && (doodlerBottom <= platform.bottom + 15) && (doodlerLeft >= platform.left) && (doodlerLeft <= platform.left + 85) && jumpSpeed < 0 ) { jumpSpeed = 10; } }); if (doodlerBottom <= 0) { gameOver(); } } function gameOver() { isGameOver = true; while (container.firstChild) { container.removeChild(container.firstChild); } container.innerHTML = `Game Over! Score: ${score}`; } function gameLoop() { if (!isGameOver) { checkCollision(); movePlatforms(); if (jumpSpeed <= 0) { fall(); } else { jump(); } requestAnimationFrame(gameLoop); } } function start() { createPlatforms(); document.addEventListener('keydown', control); gameLoop(); } start();Zusätzliche CSS für Plattformen (in styles.css hinzufügen):
.platform { width: 85px; height: 15px; background-color: green; position: absolute; }Dieses Spiel bietet grundlegende Doodle Jump Mechaniken:
- Sprungmechanik
- Plattformen
- Punktezähler
- Steuerung mit Pfeiltasten
- Einfache Kollisionserkennung
Verbesserungsmöglichkeiten:
- Animationen hinzufügen
- Schwierigkeitsgrad erhöhen
- Power-Ups implementieren
- Bessere Grafiken
Möchten Sie, dass ich einen bestimmten Aspekt des Spiels näher erkläre?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.