diff --git a/games/Dino game.js b/games/Dino game.js new file mode 100644 index 0000000000..630aa49163 --- /dev/null +++ b/games/Dino game.js @@ -0,0 +1,149 @@ +const player = bitmap` +................ +................ +...000000....... +...0....0....... +...000000....... +.....00......... +...000000....... +..0..00..0...... +..0..00..0...... +.....00......... +....0..0........ +....0..0........ +....0..0........ +...00..00....... +................ +................`; + +const cactus = bitmap` +................ +......33........ +.....3333....... +..3..3333..3.... +..3..3333..3.... +..333333..3..... +...333333....... +.....33......... +.....33......... +.....33......... +.....33......... +.....33......... +.....33......... +....3333........ +................ +................`; + +const ground = bitmap` +................ +................ +................ +................ +................ +................ +................ +................ +................ +................ +................ +................ +................ +................ +LLLLLLLLLLLLLLLL +LLLLLLLLLLLLLLLL`; + +// Map legend +setLegend( + [ "p", player ], + [ "c", cactus ], + [ "g", ground ] +); + +// Map layout (16 rows total: index 0 to 15) +setMap(map` +.................... +.................... +.................... +.................... +.................... +.................... +.................... +.................... +.................... +.................... +.................... +.................... +.................... +..p................c +gggggggggggggggggggg +gggggggggggggggggggg +`); + +// Retrieve sprite references from map +const p = getFirst("p"); +const c = getFirst("c"); + +// Game state variables +let playerY = 13; +let cactusX = 19; +let isJumping = false; +let jumpStep = 0; +let score = 0; +let gameOver = false; + +// Controls +onInput("w", () => jump()); +onInput("i", () => jump()); +onInput("j", () => jump()); + +function jump() { + if (!isJumping && !gameOver) { + isJumping = true; + jumpStep = 0; + } +} + +// Initial score log +console.log("Score: " + score); + +// Game loop +setInterval(() => { + if (gameOver) return; + + // Jump animation + if (isJumping) { + if (jumpStep < 3) { + playerY -= 1; + } else if (jumpStep < 6) { + playerY += 1; + } else { + isJumping = false; + playerY = 13; + } + jumpStep++; + } + + // Move cactus left + cactusX -= 1; + if (cactusX < 0) { + cactusX = 19; + score += 1; + console.log("Score: " + score); + } + + // Collision check (only triggers if player is on ground level) + if (cactusX === 2 && playerY === 13) { + gameOver = true; + console.log("===================="); + console.log(" GAME OVER! "); + console.log(" Final Score: " + score); + console.log("===================="); + return; + } + + // Update positions safely + if (p && c) { + p.y = playerY; + c.x = cactusX; + } +}, 100); \ No newline at end of file