🎮 KAPLAY.js — A JavaScript & TypeScript Game Library
KAPLAY is the fun-first, 2D game library for JavaScript and
TypeScript. It’s made to feel like a game while you’re making games.
Simple. Fast. Powerful.
✨ Whether you’re a beginner or an experienced dev, KAPLAY comes with its
own web-based editor — the KAPLAYGROUND — so
you can try code instantly, and learn with more than 90 examples!
🎲 Quick Overview
kaplay({
background: "#6d80fa",
});
loadSprite("bean", "https://play.kaplayjs.com/bean.png");
add([
sprite("bean"),
]);
Game objects are composed from simple, powerful components:
const player = add([
rect(40, 40),
pos(100, 200),
area(),
body(),
health(8),
"friendly",
{
dir: vec2(-1, 0),
dead: false,
speed: 240,
},
]);
Blocky imperative syntax for describing behaviors
player.onCollide("enemy", () => {
player.hp--;
});
player.onUpdate(() => {
if (player.pos.y >= height()) {
destroy(player);
}
});
onUpdate("enemy", (enemy) => {
enemy.move(-400, 0);
});
onKeyDown("w", () => {
player.move(0, 100);
});
🖥️ Installation
🚀 Using create-kaplay
The fastest way to get started:
npx create-kaplay my-game
Then open http://localhost:5173 and edit src/game.js.
📦 Install with package manager
npm install kaplay
yarn add kaplay
pnpm add kaplay
bun add kaplay
You will need a bundler like Vite or
ESBuild to use KAPLAY in your project. Learn how
to setup ESbuild
here.
🌐 Use in Browser
Include via CDN:
<script src="https://unpkg.com/[email protected]/dist/kaplay.js"></script>
📜 TypeScript Global Types
If you’re using TypeScript, you used create-kaplay or installed with a
package manager and you want global types, you can load them using the
following directive:
import "kaplay/global";
vec2(10, 10); // typed!
But it’s recommended to use tsconfig.json to include the types:
{
"compilerOptions": {
"types": ["./node_modules/kaplay/dist/declaration/global.d.ts"]
}
}
[!WARNING]
If you are publishing a game (and not testing/learning) maybe you don’t want
to use globals,
see why.
You can also use all KAPLAY source types importing them:
import type { TextCompOpt } from "kaplay"
import type * as KA from "kaplay"
interface MyTextCompOpt extends KA.TextCompOpt {
fallback: string;
}
📚 Resources
📖 Docs
📺 Tutorials
🎮 Games
Collections of games made with KAPLAY, selected by KAPLAY:
🙌 Credits
KAPLAY is an open-source project, maintained by the
KAPLAY Team and core contributors
and with the support of many
other amazing contributors.
🏆 Recognitions
评论