Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions games/Dino game.js
Original file line number Diff line number Diff line change
@@ -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);
Loading