1
- # KAPLAY.js, a JavaScript game library
1
+ # 🎮 KAPLAY.js — A JavaScript & TypeScript Game Library
2
2
3
- ![ #KAPLAY] ( /kaplay.webp )
3
+ <div align =" center " >
4
+ <img src =" ./kaplay.webp " >
5
+ </div >
4
6
5
- [ ** KAPLAY** ] ( https://kaplayjs.com/ ) is a JavaScript library that helps you make
6
- games fast and fun!
7
+ KAPLAY is the ** fun-first** , 2D game library for ** JavaScript** and
8
+ ** TypeScript** . It’s made to ** feel like a game** while you're making games.
9
+ Simple. Fast. Powerful.
7
10
8
- Start playing around with it in the [ KAPLAYGROUND] ( https://play.kaplayjs.com/ )
11
+ ✨ Whether you’re a beginner or an experienced dev, ** KAPLAY** comes with its
12
+ own ** web-based editor** — the [ KAPLAYGROUND] ( https://play.kaplayjs.com ) — so
13
+ you can try code instantly, and learn with more than ** 90 examples** !
9
14
10
- ## Examples
15
+ ## 🎲 Quick Overview
11
16
12
17
``` js
13
- // initialize context
14
- kaplay ();
15
-
16
- // define gravity
17
- setGravity (2400 );
18
+ // Start a game
19
+ kaplay ({
20
+ background: " #6d80fa" ,
21
+ });
18
22
19
- // load a sprite called "bean"
20
- loadSprite (" bean" , " sprites /bean.png" );
23
+ // Load an image
24
+ loadSprite (" bean" , " https://play.kaplayjs.com /bean.png" );
21
25
22
- // compose the player game object from multiple components and add it to the game
23
- const bean = add ([
24
- sprite (" bean" ),
25
- pos (80 , 40 ),
26
- area (),
27
- body (),
26
+ // Add a sprite to the scene
27
+ add ([
28
+ sprite (" bean" ), // it renders as a sprite
28
29
]);
29
-
30
- // press space to jump
31
- onKeyPress (" space" , () => {
32
- // this method is provided by the "body" component above
33
- bean .jump ();
34
- });
35
30
```
36
31
37
- KAPLAY uses a powerful component system to compose game objects and behaviors.
32
+ Game objects are composed from simple, powerful components:
38
33
39
34
``` js
40
- // add a game obj to the scene from a list of component
35
+ // Add a Game Obj to the scene from a list of component
41
36
const player = add ([
42
- // it renders as a sprite
43
- sprite (" bean" ),
44
- // it has a position
45
- pos (100 , 200 ),
46
- // it has a collider
47
- area (),
48
- // it is a physical body which will respond to physics
49
- body (),
50
- // it has 8 of health
51
- health (8 ),
52
- // or give it tags for easier group behaviors
53
- " player" ,
37
+ rect (40 , 40 ), // it renders as a rectangle
38
+ pos (100 , 200 ), // it has a position (coordinates)
39
+ area (), // it has a collider
40
+ body (), // it is a physical body which will respond to physics
41
+ health (8 ), // it has 8 health points
42
+ // Give it tags for easier group behaviors
54
43
" friendly" ,
55
- // plain objects fields are directly assigned to the game obj
44
+ // Give plain objects fields for associated data
56
45
{
57
46
dir: vec2 (- 1 , 0 ),
58
47
dead: false ,
@@ -74,18 +63,12 @@ player.onCollide("enemy", () => {
74
63
player .onUpdate (() => {
75
64
if (player .pos .y >= height ()) {
76
65
destroy (player);
77
- gameOver ();
78
66
}
79
67
});
80
68
81
- // if 'player' onCollide with any object with tag "enemy", run the callback
82
- player .onCollide (" enemy" , () => {
83
- player .hp -= 1 ;
84
- });
85
-
86
- // all objects with tag "enemy" will move towards 'player' every frame
87
- onUpdate (" enemy" , (e ) => {
88
- e .move (player .pos .sub (e .pos ).unit ().scale (e .speed ));
69
+ // All objects with tag "enemy" will move to the left
70
+ onUpdate (" enemy" , (enemy ) => {
71
+ enemy .move (- 400 , 0 );
89
72
});
90
73
91
74
// move up 100 pixels per second every frame when "w" key is held down
@@ -94,97 +77,122 @@ onKeyDown("w", () => {
94
77
});
95
78
```
96
79
97
- ## Usage
80
+ ## 🖥️ Installation
98
81
99
- ### Start a Project With ` create-kaplay `
82
+ ### 🚀 Using ` create-kaplay `
100
83
101
- The fastest way to start a KAPLAY game is with
102
- [ ` create-kaplay ` ] ( https://github.com/kaplayjs/create-kaplay )
84
+ The fastest way to get started:
103
85
104
86
``` sh
105
- $ npx create-kaplay mygame
87
+ npx create-kaplay my-game
106
88
```
107
89
108
- This will create a directory called ` mygame ` for you, containing all the files
109
- we need
90
+ Then open [ http://localhost:5173 ] ( http://localhost:5173 ) and edit ` src/game.js ` .
91
+
92
+ ### 📦 Install with package manager
110
93
111
94
``` sh
112
- $ cd mygame
113
- $ npm run dev
95
+ npm install kaplay
114
96
```
115
97
116
- Then open http://localhost:5173 and edit ` src/game.js `
98
+ ``` sh
99
+ yarn add kaplay
100
+ ```
117
101
118
- ### Install as NPM Package
102
+ ``` sh
103
+ pnpm add kaplay
104
+ ```
119
105
120
106
``` sh
121
- $ npm install kaplay
107
+ bun add kaplay
122
108
```
123
109
124
- ``` js
125
- import kaplay from " kaplay" ;
110
+ > You will need a bundler like [ Vite] ( https://vitejs.dev/ ) or
111
+ > [ ESBuild] ( https://esbuild.github.io/ ) to use KAPLAY in your project. Learn how
112
+ > to setup ESbuild
113
+ > [ here] ( https://kaplayjs.com/guides/install/#setup-your-own-nodejs-environment ) .
126
114
127
- kaplay ();
115
+ ### 🌐 Use in Browser
128
116
129
- add ([
130
- text ( " oh hi " ),
131
- pos ( 80 , 40 ),
132
- ]);
117
+ Include via CDN:
118
+
119
+ ``` html
120
+ < script src = " https://unpkg.com/kaplay@3001.0.12/dist/kaplay.js " ></ script >
133
121
```
134
122
135
- also works with CommonJS
123
+ ### 📜 TypeScript Global Types
136
124
137
- ``` js
138
- const kaplay = require ( " kaplay " );
139
- ```
125
+ If you're using ** TypeScript ** , you used ` create-kaplay ` or installed with a
126
+ package manager and you want ** global types ** , you can load them using the
127
+ following directive:
140
128
141
- Note that you'll need to use a bundler like ` esbuild ` or ` webpack ` to use KAPLAY
142
- with NPM
129
+ ``` ts
130
+ import " kaplay/global " ;
143
131
144
- ### Browser CDN
132
+ vec2 (10 , 10 ); // typed!
133
+ ```
145
134
146
- This exports a global ` kaplay ` function
135
+ But it's recommended to use ` tsconfig.json ` to include the types:
147
136
148
- ``` html
149
- <script src =" https://unpkg.com/kaplay@3001.0.9/dist/kaplay.js" ></script >
150
- <script >
151
- kaplay ()
152
- </script >
137
+ ``` json
138
+ {
139
+ "compilerOptions" : {
140
+ "types" : [" ./node_modules/kaplay/dist/declaration/global.d.ts" ]
141
+ }
142
+ }
153
143
```
154
144
155
- or use with es modules
145
+ > If you are publishing a game (and not testing/learning) maybe you don't want
146
+ > to use globals,
147
+ > [ see why] ( https://kaplayjs.com/guides/optimization/#avoid-global-namespace ) .
156
148
157
- ``` html
158
- <script type =" module" >
159
- import kaplay from " https://unpkg.com/kaplay@3001.0.9/dist/kaplay.mjs"
160
- kaplay ()
161
- </script >
149
+ You can also use all ** KAPLAY** source types importing them:
150
+
151
+ ``` js
152
+ import type { TextCompOpt } from " kaplay"
153
+ import type * as KA from " kaplay" // if you prefer a namespace-like import
154
+
155
+ interface MyTextCompOpt extends KA .TextCompOpt {
156
+ fallback: string;
157
+ }
162
158
```
163
159
164
- works all CDNs that supports NPM packages, e.g. jsdelivr, skypack
160
+ ## 📚 Resources
161
+
162
+ ### 📖 Docs
165
163
166
- ## Documentation
164
+ - [ KAPLAY Official Docs] ( https://kaplayjs.com/docs/ )
165
+ - [ KAPLAYGROUND] ( https://play.kaplayjs.com )
167
166
168
- - ** v4000** : https://v4000.kaplayjs.com/
169
- - ** v3001** : https://kaplayjs.com/
167
+ ### 📺 Tutorials
170
168
171
- ## Community
169
+ - 🎥
170
+ [ KAPLAY Library Crash Course by JSLegend ⚔️] ( https://www.youtube.com/watch?v=FdEYxGoy5_c )
171
+ - 📖
172
+ [ Learn JavaScript basics and KAPLAY to make games quickly] ( https://jslegenddev.substack.com/p/learn-the-basics-of-javascript-and )
173
+
174
+ ### 💬 Community
172
175
173
176
- [ Discord Server] ( https://discord.gg/aQ6RuQm3TF )
174
177
- [ GitHub Discussions] ( https://github.com/kaplayjs/kaplay/discussions )
175
- - [ Twitter] ( https://twitter.com/kaplayjs )
178
+ - [ Twitter] ( https://twitter.com/Kaboomjs )
176
179
177
- ### Games
180
+ ## 🎮 Games
178
181
179
- Collections of games made with KAPLAY (and Kaboom) , selected by KAPLAY:
182
+ Collections of games made with KAPLAY, selected by KAPLAY:
180
183
181
184
- [ Itch.io] ( https://itch.io/c/4494863/kag-collection )
182
185
- [ Newgrounds.com] ( https://www.newgrounds.com/playlist/379920/kaplay-games )
183
186
184
- ## Credits
187
+ ## 🙌 Credits
188
+
189
+ KAPLAY is an open-source project, maintained by the
190
+ [ KAPLAY Team and core contributors] ( https://github.com/kaplayjs/kaplay/wiki/Development-Team )
191
+ and with the support of many
192
+ [ other amazing contributors] ( https://github.com/kaplayjs/kaplay/graphs/contributors ) .
193
+
194
+ ### 🏆 Recognitions
185
195
186
- - Thanks to [ tga] ( https://space55.xyz ) for all his work on the original
187
- Kaboom.js
188
196
- Thanks to [ mulfok] ( https://twitter.com/MulfoK ) for the awesome
189
197
[ mulfok32] ( https://lospec.com/palette-list/mulfok32 ) color palette, used in
190
198
KAPLAY sprites and art
0 commit comments