|
| 1 | +const canvas = document.getElementById("gameCanvas"); |
| 2 | +const ctx = canvas.getContext("2d"); |
| 3 | +canvas.width = window.innerWidth; |
| 4 | +canvas.height = window.innerHeight; |
| 5 | + |
| 6 | +let angleX = 0; |
| 7 | +let angleY = 0; |
| 8 | +let velocity = {x: 0, y: 0}; |
| 9 | +let marble = {x: 50, y: 50, r: 10}; |
| 10 | +const startPoint = {x: 50, y: 50}; |
| 11 | +const goal = {x: canvas.width - 60, y: canvas.height - 60, r: 20}; |
| 12 | +const holes = [ |
| 13 | + {x: 150, y: 150, r: 15}, |
| 14 | + {x: 300, y: 200, r: 15}, |
| 15 | + {x: 400, y: 400, r: 15} |
| 16 | +]; |
| 17 | +const friction = 0.98; |
| 18 | +const sensitivity = 0.5; |
| 19 | + |
| 20 | +const keys = {}; |
| 21 | +document.addEventListener('keydown', e => keys[e.key.toLowerCase()] = true); |
| 22 | +document.addEventListener('keyup', e => keys[e.key.toLowerCase()] = false); |
| 23 | + |
| 24 | +// Mobile tilt support |
| 25 | +if (window.DeviceMotionEvent) { |
| 26 | + window.addEventListener('devicemotion', e => { |
| 27 | + if (e.accelerationIncludingGravity) { |
| 28 | + angleX = e.accelerationIncludingGravity.x; |
| 29 | + angleY = -e.accelerationIncludingGravity.y; |
| 30 | + } |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +function restartGame() { |
| 35 | + marble.x = startPoint.x; |
| 36 | + marble.y = startPoint.y; |
| 37 | + velocity = {x: 0, y: 0}; |
| 38 | +} |
| 39 | + |
| 40 | +function draw() { |
| 41 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 42 | + |
| 43 | + // Goal |
| 44 | + ctx.fillStyle = "green"; |
| 45 | + ctx.beginPath(); |
| 46 | + ctx.arc(goal.x, goal.y, goal.r, 0, Math.PI * 2); |
| 47 | + ctx.fill(); |
| 48 | + |
| 49 | + // Holes |
| 50 | + ctx.fillStyle = "black"; |
| 51 | + for (const hole of holes) { |
| 52 | + ctx.beginPath(); |
| 53 | + ctx.arc(hole.x, hole.y, hole.r, 0, Math.PI * 2); |
| 54 | + ctx.fill(); |
| 55 | + } |
| 56 | + |
| 57 | + // Marble |
| 58 | + ctx.fillStyle = "blue"; |
| 59 | + ctx.beginPath(); |
| 60 | + ctx.arc(marble.x, marble.y, marble.r, 0, Math.PI * 2); |
| 61 | + ctx.fill(); |
| 62 | + |
| 63 | + // Debug |
| 64 | + ctx.fillStyle = "black"; |
| 65 | + ctx.font = "14px sans-serif"; |
| 66 | + ctx.fillText(`angleX: ${angleX.toFixed(2)} angleY: ${angleY.toFixed(2)}`, 10, 20); |
| 67 | +} |
| 68 | + |
| 69 | +function update() { |
| 70 | + // WASD simulation |
| 71 | + let keyboardAngleX = 0; |
| 72 | + let keyboardAngleY = 0; |
| 73 | + if (keys['w']) keyboardAngleY -= 1; |
| 74 | + if (keys['s']) keyboardAngleY += 1; |
| 75 | + if (keys['a']) keyboardAngleX -= 1; |
| 76 | + if (keys['d']) keyboardAngleX += 1; |
| 77 | + |
| 78 | + // Kombiniere Tastatur und DeviceMotion |
| 79 | + const totalAngleX = angleX + keyboardAngleX; |
| 80 | + const totalAngleY = angleY + keyboardAngleY; |
| 81 | + |
| 82 | + velocity.x += totalAngleX * sensitivity; |
| 83 | + velocity.y += totalAngleY * sensitivity; |
| 84 | + velocity.x *= friction; |
| 85 | + velocity.y *= friction; |
| 86 | + |
| 87 | + marble.x += velocity.x; |
| 88 | + marble.y += velocity.y; |
| 89 | + |
| 90 | + // Wall clamp |
| 91 | + marble.x = Math.max(marble.r, Math.min(canvas.width - marble.r, marble.x)); |
| 92 | + marble.y = Math.max(marble.r, Math.min(canvas.height - marble.r, marble.y)); |
| 93 | + |
| 94 | + // Hole check |
| 95 | + for (const hole of holes) { |
| 96 | + const dx = marble.x - hole.x; |
| 97 | + const dy = marble.y - hole.y; |
| 98 | + const dist = Math.sqrt(dx * dx + dy * dy); |
| 99 | + if (dist < marble.r + hole.r) { |
| 100 | + restartGame(); |
| 101 | + return; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Goal check |
| 106 | + const dx = marble.x - goal.x; |
| 107 | + const dy = marble.y - goal.y; |
| 108 | + const dist = Math.sqrt(dx * dx + dy * dy); |
| 109 | + if (dist < marble.r + goal.r) { |
| 110 | + alert("You reached the goal!"); |
| 111 | + restartGame(); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +function gameLoop() { |
| 116 | + update(); |
| 117 | + draw(); |
| 118 | + requestAnimationFrame(gameLoop); |
| 119 | +} |
| 120 | + |
| 121 | +gameLoop(); |
0 commit comments