|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>CK Bird Game</title> |
| 5 | + <style> |
| 6 | + body { |
| 7 | + margin: 0; |
| 8 | + background: skyblue; |
| 9 | + text-align: center; |
| 10 | + font-family: Arial; |
| 11 | + } |
| 12 | + canvas { |
| 13 | + background: #70c5ce; |
| 14 | + display: none; |
| 15 | + margin: auto; |
| 16 | + } |
| 17 | + #menu { |
| 18 | + margin-top: 150px; |
| 19 | + } |
| 20 | + button { |
| 21 | + padding: 15px 30px; |
| 22 | + font-size: 18px; |
| 23 | + margin: 10px; |
| 24 | + cursor: pointer; |
| 25 | + } |
| 26 | + </style> |
| 27 | +</head> |
| 28 | +<body> |
| 29 | + |
| 30 | +<h1>CK</h1> |
| 31 | + |
| 32 | +<div id="menu"> |
| 33 | + <button onclick="startGame()">▶ Play</button><br> |
| 34 | + <button onclick="exitGame()">❌ Exit</button> |
| 35 | +</div> |
| 36 | + |
| 37 | +<canvas id="game" width="400" height="500"></canvas> |
| 38 | + |
| 39 | +<script> |
| 40 | +const canvas = document.getElementById("game"); |
| 41 | +const ctx = canvas.getContext("2d"); |
| 42 | + |
| 43 | +let birdY = 200; |
| 44 | +let birdVelocity = 0; |
| 45 | +let gravity = 0.6; |
| 46 | +let jump = -10; |
| 47 | + |
| 48 | +let pipeX = 400; |
| 49 | +let pipeGap = 140; |
| 50 | +let pipeTopHeight = 150; |
| 51 | + |
| 52 | +function startGame() { |
| 53 | + document.getElementById("menu").style.display = "none"; |
| 54 | + canvas.style.display = "block"; |
| 55 | + requestAnimationFrame(update); |
| 56 | +} |
| 57 | + |
| 58 | +function exitGame() { |
| 59 | + alert("Thanks for playing CK!"); |
| 60 | +} |
| 61 | + |
| 62 | +document.addEventListener("click", () => { |
| 63 | + birdVelocity = jump; |
| 64 | +}); |
| 65 | + |
| 66 | +function update() { |
| 67 | + ctx.clearRect(0,0,400,500); |
| 68 | + |
| 69 | + // Bird |
| 70 | + birdVelocity += gravity; |
| 71 | + birdY += birdVelocity; |
| 72 | + ctx.fillStyle = "yellow"; |
| 73 | + ctx.beginPath(); |
| 74 | + ctx.arc(80, birdY, 15, 0, Math.PI * 2); |
| 75 | + ctx.fill(); |
| 76 | + |
| 77 | + // Pipes |
| 78 | + pipeX -= 2; |
| 79 | + if (pipeX < -60) { |
| 80 | + pipeX = 400; |
| 81 | + pipeTopHeight = Math.random() * 200 + 50; |
| 82 | + } |
| 83 | + |
| 84 | + ctx.fillStyle = "green"; |
| 85 | + ctx.fillRect(pipeX, 0, 50, pipeTopHeight); |
| 86 | + ctx.fillRect(pipeX, pipeTopHeight + pipeGap, 50, 500); |
| 87 | + |
| 88 | + // Collision |
| 89 | + if ( |
| 90 | + birdY < 0 || |
| 91 | + birdY > 500 || |
| 92 | + (80 > pipeX && 80 < pipeX + 50 && |
| 93 | + (birdY < pipeTopHeight || birdY > pipeTopHeight + pipeGap)) |
| 94 | + ) { |
| 95 | + alert("Game Over!"); |
| 96 | + location.reload(); |
| 97 | + } |
| 98 | + |
| 99 | + requestAnimationFrame(update); |
| 100 | +} |
| 101 | +</script> |
| 102 | + |
| 103 | +</body> |
| 104 | +</html> |
0 commit comments