Skip to content

Instantly share code, notes, and snippets.

@appsdevpk
Created June 27, 2024 08:45
Show Gist options
  • Save appsdevpk/27d199c654e9bd2d89f3c226d126b224 to your computer and use it in GitHub Desktop.
Save appsdevpk/27d199c654e9bd2d89f3c226d126b224 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<script src="kaplay.js"></script>
<script>
kaplay();
loadSprite("bean", "bean.png");
const SPEED = 320;
function spawnTree() {
add([
rect(48, rand(24, 64)),
area(),
outline(4),
pos(width(), height() - 48),
anchor("botleft"),
color(255, 180, 255),
move(LEFT, 240),
"tree"
]);
wait(rand(2, 3.5), () => {
spawnTree();
});
}
scene("game", () => {
setGravity(1600);
let score = 0;
const scoreLabel = add([text(score), pos(24, 24)]);
onUpdate(() => {
score++;
scoreLabel.text = score;
});
const bean = add([sprite("bean"), pos(80, 250), scale(0.3), area(), body()]);
onKeyPress("space", () => {
if (bean.isGrounded()) {
bean.jump();
}
});
onKeyDown("left", () => {
bean.move(-SPEED, 0);
});
onKeyDown("right", () => {
bean.move(SPEED, 0);
});
add([
rect(width(), 48),
pos(0, height() - 48),
outline(4),
area(),
body({ isStatic: true }),
color(127, 200, 255),
]);
spawnTree();
bean.onCollide("tree", () => {
addKaboom(bean.pos);
shake();
go("lose");
});
});
scene("lose", () => {
add([
sprite("bean"),
pos(width() / 2, height() / 2 - 80),
scale(2),
anchor("center"),
]);
add([text("Game Over"), pos(center()), anchor("center")]);
onKeyPress("space", () => go("game"));
onClick(() => go("game"));
});
go("game");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment