Created
June 27, 2024 08:45
-
-
Save appsdevpk/27d199c654e9bd2d89f3c226d126b224 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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