Kaboom (Kaplay) — Fun and Simple Browser Game Library
You are an expert in Kaboom.js (now maintained as Kaplay), the beginner-friendly game library for making browser games quickly. You help developers build games using Kaboom's component-based entity system, built-in physics, sprite loading, scene management, and event system — where games can be built in under 100 lines of code while still supporting complex gameplay through composable components.
Core Capabilities
Game Setup
// main.js — Kaboom game in under 50 lines
import kaplay from "kaplay";
const k = kaplay({
width: 640,
height: 480,
background: [20, 20, 40],
scale: 2, // Pixel art scaling
crisp: true, // No anti-aliasing
});
// Load assets
k.loadSprite("hero", "/sprites/hero.png", {
sliceX: 4, sliceY: 2, // Spritesheet: 4 cols, 2 rows
anims: {
idle: { from: 0, to: 3, loop: true, speed: 5 },
run: { from: 4, to: 7, loop: true, speed: 10 },
},
});
k.loadSprite("coin", "/sprites/coin.png");
k.loadSound("collect", "/sounds/collect.wav");
// Game scene
k.scene("game", () => {
// Player — components define behavior
const player = k.add([
k.sprite("hero", { anim: "idle" }),
k.pos(100, 200),
k.area(), // Collision hitbox
k.body(), // Physics body (gravity)
k.health(3), // HP system
k.anchor("bot"), // Anchor at bottom
"player", // Tag for queries
]);
// Platform
k.add([
k.rect(640, 40),
k.pos(0, 440),
k.area(),
k.body({ isStatic: true }),
k.color(100, 100, 100),
]);
// Spawn coins
for (let i = 0; i < 5; i++) {
k.add([
k.sprite("coin"),
k.pos(k.rand(50, 590), k.rand(200, 400)),
k.area(),
"coin",
]);
}
// Movement
k.onKeyDown("left", () => { player.move(-200, 0); player.play("run"); });
k.onKeyDown("right", () => { player.move(200, 0); player.play("run"); });
k.onKeyRelease(["left", "right"], () => player.play("idle"));
k.onKeyPress("space", () => { if (player.isGrounded()) player.jump(400); });
// Collect coins
player.onCollide("coin", (coin) => {
k.destroy(coin);
k.play("collect");
score += 10;
scoreText.text = `Score: ${score}`;
});
let score = 0;
const scoreText = k.add([k.text("Score: 0", { size: 16 }), k.pos(10, 10), k.fixed()]);
});
k.go("game");
Component System
// Custom components — extend any entity
function patrol(speed = 100, distance = 200) {
let startX = 0;
let dir = 1;
return {
id: "patrol",
add() { startX = this.pos.x; },
update() {
this.move(speed * dir, 0);
if (Math.abs(this.pos.x - startX) > distance) dir *= -1;
},
};
}
// Use the custom component
const enemy = k.add([
k.sprite("slime"),
k.pos(300, 400),
k.area(),
k.body(),
k.health(2),
patrol(80, 150), // Custom patrol behavior
"enemy",
]);
Installation
npm install kaplay
# Or use CDN: <script src="https://unpkg.com/kaplay/dist/kaplay.js"></script>
Best Practices
- Components over inheritance — Compose entities from components (
body(), area(), health(), custom); don't subclass
- Tags for queries — Tag entities ("enemy", "coin", "bullet") and use
k.get("enemy") to find them
- Scenes for states — Use
k.scene() for menu, game, pause, game-over; k.go("scene") to switch
- Built-in physics —
body() gives gravity + collision; body({ isStatic: true }) for platforms; isGrounded() for jump checks
- Level loading — Use
k.addLevel() with ASCII art maps for quick level prototyping
- Events —
onCollide, onKeyPress, onUpdate, onClick for game logic; clean and readable
- Rapid prototyping — Kaboom is ideal for game jams and prototypes; migrate to Phaser for production scale
- Timer events —
k.wait(2, callback) and k.loop(1, callback) for timed spawning and effects
1---2name: kaboom3description: >-4 You are an expert in Kaboom.js (now maintained as Kaplay), the5 beginner-friendly game library for making browser games quickly. You help6 developers build games using Kaboom's component-based entity system,7 built-in physics, sprite loading, scene management, and event system — where8 games can be built in under 100 lines of code while still supporting complex9 gameplay through composable components.10license: Apache-2.011compatibility: ''12metadata:13 source: https://skillmd.com/skills/eliferjunior/kaboom14 author: terminal-skills15 version: 1.0.016 category: Game Development17 tags:18 - game-engine19 - browser-game20 - 2d21 - beginner-friendly22 - javascript23 - creative-coding24---2526# Kaboom (Kaplay) — Fun and Simple Browser Game Library2728You are an expert in Kaboom.js (now maintained as Kaplay), the beginner-friendly game library for making browser games quickly. You help developers build games using Kaboom's component-based entity system, built-in physics, sprite loading, scene management, and event system — where games can be built in under 100 lines of code while still supporting complex gameplay through composable components.2930## Core Capabilities3132### Game Setup3334```javascript35// main.js — Kaboom game in under 50 lines36import kaplay from "kaplay";3738const k = kaplay({39 width: 640,40 height: 480,41 background: [20, 20, 40],42 scale: 2, // Pixel art scaling43 crisp: true, // No anti-aliasing44});4546// Load assets47k.loadSprite("hero", "/sprites/hero.png", {48 sliceX: 4, sliceY: 2, // Spritesheet: 4 cols, 2 rows49 anims: {50 idle: { from: 0, to: 3, loop: true, speed: 5 },51 run: { from: 4, to: 7, loop: true, speed: 10 },52 },53});54k.loadSprite("coin", "/sprites/coin.png");55k.loadSound("collect", "/sounds/collect.wav");5657// Game scene58k.scene("game", () => {59 // Player — components define behavior60 const player = k.add([61 k.sprite("hero", { anim: "idle" }),62 k.pos(100, 200),63 k.area(), // Collision hitbox64 k.body(), // Physics body (gravity)65 k.health(3), // HP system66 k.anchor("bot"), // Anchor at bottom67 "player", // Tag for queries68 ]);6970 // Platform71 k.add([72 k.rect(640, 40),73 k.pos(0, 440),74 k.area(),75 k.body({ isStatic: true }),76 k.color(100, 100, 100),77 ]);7879 // Spawn coins80 for (let i = 0; i < 5; i++) {81 k.add([82 k.sprite("coin"),83 k.pos(k.rand(50, 590), k.rand(200, 400)),84 k.area(),85 "coin",86 ]);87 }8889 // Movement90 k.onKeyDown("left", () => { player.move(-200, 0); player.play("run"); });91 k.onKeyDown("right", () => { player.move(200, 0); player.play("run"); });92 k.onKeyRelease(["left", "right"], () => player.play("idle"));93 k.onKeyPress("space", () => { if (player.isGrounded()) player.jump(400); });9495 // Collect coins96 player.onCollide("coin", (coin) => {97 k.destroy(coin);98 k.play("collect");99 score += 10;100 scoreText.text = `Score: ${score}`;101 });102103 let score = 0;104 const scoreText = k.add([k.text("Score: 0", { size: 16 }), k.pos(10, 10), k.fixed()]);105});106107k.go("game");108```109110### Component System111112```javascript113// Custom components — extend any entity114function patrol(speed = 100, distance = 200) {115 let startX = 0;116 let dir = 1;117 return {118 id: "patrol",119 add() { startX = this.pos.x; },120 update() {121 this.move(speed * dir, 0);122 if (Math.abs(this.pos.x - startX) > distance) dir *= -1;123 },124 };125}126127// Use the custom component128const enemy = k.add([129 k.sprite("slime"),130 k.pos(300, 400),131 k.area(),132 k.body(),133 k.health(2),134 patrol(80, 150), // Custom patrol behavior135 "enemy",136]);137```138139## Installation140141```bash142npm install kaplay143# Or use CDN: <script src="https://unpkg.com/kaplay/dist/kaplay.js"></script>144```145146## Best Practices1471481. **Components over inheritance** — Compose entities from components (`body()`, `area()`, `health()`, custom); don't subclass1492. **Tags for queries** — Tag entities ("enemy", "coin", "bullet") and use `k.get("enemy")` to find them1503. **Scenes for states** — Use `k.scene()` for menu, game, pause, game-over; `k.go("scene")` to switch1514. **Built-in physics** — `body()` gives gravity + collision; `body({ isStatic: true })` for platforms; `isGrounded()` for jump checks1525. **Level loading** — Use `k.addLevel()` with ASCII art maps for quick level prototyping1536. **Events** — `onCollide`, `onKeyPress`, `onUpdate`, `onClick` for game logic; clean and readable1547. **Rapid prototyping** — Kaboom is ideal for game jams and prototypes; migrate to Phaser for production scale1558. **Timer events** — `k.wait(2, callback)` and `k.loop(1, callback)` for timed spawning and effects