|
| 1 | +--- |
| 2 | +layout: bootstrap |
| 3 | +title: Road |
| 4 | +description: Road Game |
| 5 | +permalink: /road |
| 6 | +Author: Aarush |
| 7 | +--- |
| 8 | + |
| 9 | +<script> |
| 10 | +// filepath: /home/kasm-user/nighthawk/GenomeGamersFrontend/navigation/Worlds/world0.md |
| 11 | +// ...existing code... |
| 12 | + |
| 13 | +// --- Background Music --- |
| 14 | +const music = new Audio('{{site.baseurl}}/assets/audio/18moonviewhighway.mp3'); // Change path as needed |
| 15 | +music.loop = true; |
| 16 | +music.volume = 0.5; |
| 17 | + |
| 18 | +// Play music after first user interaction (required by browsers) |
| 19 | +function startMusicOnce() { |
| 20 | + music.play().catch(() => {}); |
| 21 | + window.removeEventListener('click', startMusicOnce); |
| 22 | + window.removeEventListener('keydown', startMusicOnce); |
| 23 | +} |
| 24 | +window.addEventListener('click', startMusicOnce); |
| 25 | +window.addEventListener('keydown', startMusicOnce); |
| 26 | +</script> |
| 27 | + |
| 28 | +<h2>Crossy Road Minigame</h2> |
| 29 | +<canvas id="crossyCanvas" width="400" height="500" style="border:1px solid #333; background:#b3e6b3"></canvas> |
| 30 | +<p id="crossyScore"></p> |
| 31 | +<script> |
| 32 | +const canvas = document.getElementById('crossyCanvas'); |
| 33 | +const ctx = canvas.getContext('2d'); |
| 34 | + |
| 35 | +const ROWS = 10; |
| 36 | +const COLS = 8; |
| 37 | +const CELL = 50; |
| 38 | +const PLAYER_SIZE = 40; |
| 39 | +const CAR_HEIGHT = 40; |
| 40 | +const CAR_WIDTH = 60; |
| 41 | + |
| 42 | +let player = { x: Math.floor(COLS/2), y: ROWS-1 }; |
| 43 | +let score = 0; |
| 44 | +let gameOver = false; |
| 45 | + |
| 46 | +function randomCarRow() { |
| 47 | + // Cars only on rows 1-8 (not start/end) |
| 48 | + let rows = []; |
| 49 | + for (let i = 1; i < ROWS-1; i++) rows.push(i); |
| 50 | + return rows; |
| 51 | +} |
| 52 | + |
| 53 | +let cars = []; |
| 54 | +function addCarRowAtTop() { |
| 55 | + // Randomly decide to add a car or not in each lane |
| 56 | + for (let col = 0; col < COLS; col++) { |
| 57 | + if (Math.random() < 0.5) { |
| 58 | + let dir = Math.random() > 0.5 ? 1 : -1; |
| 59 | + let speed = 1 + Math.random() * 2; |
| 60 | + let x = dir === 1 ? -CAR_WIDTH : canvas.width; |
| 61 | + cars.push({ x, y: 1, dir, speed, col }); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +function resetCars() { |
| 66 | + cars = []; |
| 67 | + // Fill initial cars except for row 0 and last row |
| 68 | + for (let row = 1; row < ROWS-1; row++) { |
| 69 | + for (let col = 0; col < COLS; col++) { |
| 70 | + if (Math.random() < 0.5) { |
| 71 | + let dir = Math.random() > 0.5 ? 1 : -1; |
| 72 | + let speed = 1 + Math.random() * 2; |
| 73 | + let x = dir === 1 ? -CAR_WIDTH : canvas.width; |
| 74 | + cars.push({ x, y: row, dir, speed, col }); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +resetCars(); |
| 80 | + |
| 81 | +function drawPlayer() { |
| 82 | + ctx.save(); |
| 83 | + ctx.fillStyle = "#fff"; |
| 84 | + ctx.strokeStyle = "#222"; |
| 85 | + ctx.beginPath(); |
| 86 | + ctx.arc( |
| 87 | + player.x * CELL + CELL/2, |
| 88 | + player.y * CELL + CELL/2, |
| 89 | + PLAYER_SIZE/2, 0, 2*Math.PI |
| 90 | + ); |
| 91 | + ctx.fill(); |
| 92 | + ctx.stroke(); |
| 93 | + // Draw beak |
| 94 | + ctx.beginPath(); |
| 95 | + ctx.moveTo(player.x*CELL+CELL/2, player.y*CELL+CELL/2); |
| 96 | + ctx.lineTo(player.x*CELL+CELL/2+10, player.y*CELL+CELL/2-5); |
| 97 | + ctx.lineTo(player.x*CELL+CELL/2+10, player.y*CELL+CELL/2+5); |
| 98 | + ctx.closePath(); |
| 99 | + ctx.fillStyle = "orange"; |
| 100 | + ctx.fill(); |
| 101 | + ctx.restore(); |
| 102 | +} |
| 103 | + |
| 104 | +function drawCars() { |
| 105 | + for (let car of cars) { |
| 106 | + ctx.save(); |
| 107 | + ctx.fillStyle = "#f55"; |
| 108 | + ctx.fillRect(car.x, car.y*CELL + (CELL-CAR_HEIGHT)/2, CAR_WIDTH, CAR_HEIGHT); |
| 109 | + ctx.strokeRect(car.x, car.y*CELL + (CELL-CAR_HEIGHT)/2, CAR_WIDTH, CAR_HEIGHT); |
| 110 | + ctx.restore(); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +function moveCars() { |
| 115 | + for (let car of cars) { |
| 116 | + car.x += car.dir * car.speed; |
| 117 | + // Loop cars horizontally |
| 118 | + if (car.dir === 1 && car.x > canvas.width) car.x = -CAR_WIDTH; |
| 119 | + if (car.dir === -1 && car.x < -CAR_WIDTH) car.x = canvas.width; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function checkCollision() { |
| 124 | + for (let car of cars) { |
| 125 | + let px = player.x * CELL + CELL/2; |
| 126 | + let py = player.y * CELL + CELL/2; |
| 127 | + let cx = car.x + CAR_WIDTH/2; |
| 128 | + let cy = car.y*CELL + CELL/2; |
| 129 | + if ( |
| 130 | + Math.abs(px - cx) < (CAR_WIDTH/2 + PLAYER_SIZE/2 - 10) && |
| 131 | + Math.abs(py - cy) < (CAR_HEIGHT/2 + PLAYER_SIZE/2 - 10) |
| 132 | + ) { |
| 133 | + return true; |
| 134 | + } |
| 135 | + } |
| 136 | + return false; |
| 137 | +} |
| 138 | + |
| 139 | +function drawGoal() { |
| 140 | + ctx.save(); |
| 141 | + ctx.fillStyle = "#ffe066"; |
| 142 | + ctx.fillRect(0, 0, canvas.width, CELL); |
| 143 | + ctx.restore(); |
| 144 | +} |
| 145 | + |
| 146 | +function draw() { |
| 147 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 148 | + drawGoal(); |
| 149 | + drawCars(); |
| 150 | + drawPlayer(); |
| 151 | +} |
| 152 | + |
| 153 | +function update() { |
| 154 | + if (gameOver) return; |
| 155 | + moveCars(); |
| 156 | + if (checkCollision()) { |
| 157 | + gameOver = true; |
| 158 | + document.getElementById('crossyScore').textContent = "Game Over! Score: " + score; |
| 159 | + return; |
| 160 | + } |
| 161 | + if (player.y === 0) { |
| 162 | + score++; |
| 163 | + // Move everything down by one row |
| 164 | + player.y++; |
| 165 | + for (let car of cars) { |
| 166 | + car.y++; |
| 167 | + } |
| 168 | + // Remove cars that go off the bottom |
| 169 | + cars = cars.filter(car => car.y < ROWS-1); |
| 170 | + // Add new car row at the top |
| 171 | + addCarRowAtTop(); |
| 172 | + document.getElementById('crossyScore').textContent = "Score: " + score; |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +function gameLoop() { |
| 177 | + update(); |
| 178 | + draw(); |
| 179 | + if (!gameOver) requestAnimationFrame(gameLoop); |
| 180 | +} |
| 181 | +gameLoop(); |
| 182 | + |
| 183 | +document.addEventListener('keydown', function(e) { |
| 184 | + if (gameOver) return; |
| 185 | + if (e.key === "ArrowUp" && player.y > 0) player.y--; |
| 186 | + if (e.key === "ArrowDown" && player.y < ROWS-1) player.y++; |
| 187 | + if (e.key === "ArrowLeft" && player.x > 0) player.x--; |
| 188 | + if (e.key === "ArrowRight" && player.x < COLS-1) player.x++; |
| 189 | +}); |
| 190 | + |
| 191 | +document.getElementById('crossyScore').textContent = "Score: 0"; |
| 192 | +</script> |
| 193 | + |
| 194 | + |
0 commit comments