littlejsengine 1.6.0 → 1.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +133 -33
  2. package/build/littlejs.d.ts +13 -5
  3. package/build/littlejs.esm.js +106 -95
  4. package/build/littlejs.esm.min.js +1 -1
  5. package/build/littlejs.js +98 -93
  6. package/build/littlejs.min.js +1 -1
  7. package/build/littlejs.release.js +91 -91
  8. package/examples/breakout/game.js +5 -0
  9. package/examples/breakout/gameObjects.js +41 -48
  10. package/examples/breakout/index.html +3 -3
  11. package/examples/breakoutTutorial/README.md +514 -0
  12. package/examples/breakoutTutorial/game.js +181 -0
  13. package/examples/breakoutTutorial/images/1.png +0 -0
  14. package/examples/breakoutTutorial/images/10.png +0 -0
  15. package/examples/breakoutTutorial/images/11.png +0 -0
  16. package/examples/breakoutTutorial/images/2.png +0 -0
  17. package/examples/breakoutTutorial/images/3.png +0 -0
  18. package/examples/breakoutTutorial/images/4.png +0 -0
  19. package/examples/breakoutTutorial/images/5.png +0 -0
  20. package/examples/breakoutTutorial/images/6.png +0 -0
  21. package/examples/breakoutTutorial/images/7.png +0 -0
  22. package/examples/breakoutTutorial/images/8.png +0 -0
  23. package/examples/breakoutTutorial/images/9.png +0 -0
  24. package/examples/breakoutTutorial/index.html +10 -0
  25. package/examples/empty/game.js +10 -0
  26. package/examples/module/index.html +1 -1
  27. package/examples/particles/index.html +1 -1
  28. package/examples/platformer/gameLevel.js +1 -1
  29. package/examples/platformer/gamePlayer.js +1 -1
  30. package/examples/platformer/index.html +6 -6
  31. package/examples/puzzle/index.html +2 -2
  32. package/examples/starter/build.bat +3 -2
  33. package/examples/starter/game.js +9 -9
  34. package/examples/starter/index.html +13 -13
  35. package/examples/stress/index.html +1 -1
  36. package/examples/typescript/game.js +89 -89
  37. package/examples/typescript/game.ts +10 -10
  38. package/examples/typescript/index.html +1 -1
  39. package/package.json +3 -3
  40. package/src/engine.js +1 -2
  41. package/src/engineAudio.js +11 -11
  42. package/src/engineBuild.bat +3 -1
  43. package/src/engineDebug.js +8 -2
  44. package/src/engineDraw.js +19 -15
  45. package/src/engineExport.js +8 -2
  46. package/src/engineInput.js +51 -55
  47. package/src/engineObject.js +4 -4
  48. package/src/engineRelease.js +1 -0
  49. package/src/engineUtilities.js +4 -4
@@ -5,31 +5,36 @@
5
5
  'use strict';
6
6
 
7
7
  ///////////////////////////////////////////////////////////////////////////////
8
- class Paddle extends EngineObject
8
+ class Wall extends EngineObject
9
+ {
10
+ constructor(pos, size)
11
+ {
12
+ super(pos, size);
13
+
14
+ this.mass = 0; // make object have static physics
15
+ this.setCollision(); // make object collide
16
+ this.color = new Color(0,0,0,0); // make object invisible
17
+ }
18
+ }
19
+
20
+ ///////////////////////////////////////////////////////////////////////////////
21
+ class Paddle extends EngineObject
9
22
  {
10
23
  constructor(pos)
11
24
  {
12
25
  super(pos, vec2(5,.5));
13
-
14
- // set to collide
15
- this.setCollision();
16
- this.mass = 0;
26
+
27
+ this.mass = 0; // make object have static physics
28
+ this.setCollision(); // make object collide
17
29
  }
18
30
 
19
31
  update()
20
32
  {
21
- if (isUsingGamepad)
22
- {
23
- // control with gamepad
24
- this.pos.x += gamepadStick(0).x;
25
- }
26
- else
27
- {
28
- // move to mouse
29
- this.pos.x = mousePos.x;
30
- }
33
+ // control with gamepad or mouse
34
+ this.pos.x = isUsingGamepad ? this.pos.x + gamepadStick(0).x : mousePos.x;
35
+
36
+ // keep paddle in bounds of level
31
37
  this.pos.x = clamp(this.pos.x, this.size.x/2, levelSize.x - this.size.x/2);
32
- super.update();
33
38
  }
34
39
  }
35
40
 
@@ -39,11 +44,10 @@ class Brick extends EngineObject
39
44
  constructor(pos)
40
45
  {
41
46
  super(pos, vec2(2,1), 1, vec2(32,16), 0, randColor());
42
-
43
- // set to collide
44
- this.setCollision();
45
- this.mass = 0;
46
47
  ++brickCount;
48
+
49
+ this.mass = 0; // make object have static physics
50
+ this.setCollision(); // make object collide
47
51
  }
48
52
 
49
53
  collideWithObject(o)
@@ -54,6 +58,7 @@ class Brick extends EngineObject
54
58
  --brickCount;
55
59
  sound_break.play(this.pos);
56
60
 
61
+ // make explosion effect
57
62
  const color1 = this.color;
58
63
  const color2 = color1.lerp(new Color, .5);
59
64
  new ParticleEmitter(
@@ -62,7 +67,7 @@ class Brick extends EngineObject
62
67
  0, vec2(16), // tileIndex, tileSize
63
68
  color1, color2, // colorStartA, colorStartB
64
69
  color1.scale(1,0), color2.scale(1,0), // colorEndA, colorEndB
65
- .2, .5, 1, .05, .05,// time, sizeStart, sizeEnd, speed, angleSpeed
70
+ .1, .3, 1, .05, .05,// time, sizeStart, sizeEnd, speed, angleSpeed
66
71
  .99, .95, .4, PI, // damping, angleDamping, gravityScale, cone
67
72
  .1, .5, 0, 1 // fadeRate, randomness, collide, additive
68
73
  );
@@ -82,55 +87,43 @@ class Ball extends EngineObject
82
87
  this.setCollision();
83
88
  this.velocity = vec2(randSign(), -1).scale(.1);
84
89
  this.elasticity = 1;
85
- this.damping = 1;
86
90
  }
87
91
 
88
92
  update()
89
93
  {
90
- if (this.pos.y < 0)
94
+ if (this.pos.y < -1)
91
95
  {
92
96
  // destroy ball if it goes below the level
93
97
  ball = 0;
94
98
  this.destroy();
95
99
  }
96
100
 
97
- // bounce on sides and top
98
- const nextPos = this.pos.x + this.velocity.x;
99
- if (nextPos - this.size.x/2 < 0 || nextPos + this.size.x/2 > levelSize.x)
100
- {
101
- this.velocity.x *= -1;
102
- this.bounce();
103
- }
104
- if (this.pos.y + this.velocity.y > levelSize.y)
105
- {
106
- this.velocity.y *= -1;
107
- this.bounce();
108
- }
109
-
110
101
  // update physics
111
102
  super.update();
112
103
  }
113
104
 
114
105
  collideWithObject(o)
115
106
  {
116
- if (o == paddle && this.velocity.y < 0)
117
- {
118
- // put english on the ball when it collides with paddle
119
- this.velocity = this.velocity.rotate(.2 * (this.pos.x - o.pos.x));
120
- this.velocity.y = max(-this.velocity.y, .2);
121
- this.bounce();
107
+ // prevent colliding with paddle if moving upwards
108
+ if (o == paddle && this.velocity.y > 0)
122
109
  return 0;
123
- }
124
- return 1;
125
- }
126
110
 
127
- bounce()
128
- {
129
111
  // speed up
130
- const speed = min(1.05*this.velocity.length(), .5);
112
+ const speed = min(1.04*this.velocity.length(), .5);
131
113
  this.velocity = this.velocity.normalize(speed);
132
114
 
133
115
  // scale bounce sound pitch by speed
134
116
  sound_bounce.play(this.pos, 1, speed);
117
+
118
+ if (o == paddle)
119
+ {
120
+ // put english on the ball when it collides with paddle
121
+ this.velocity = this.velocity.rotate(.2 * (this.pos.x - o.pos.x));
122
+ this.velocity.y = max(-this.velocity.y, .2);
123
+ return 0;
124
+ }
125
+
126
+ // prevent default collision with paddle
127
+ return 1;
135
128
  }
136
129
  }
@@ -6,6 +6,6 @@
6
6
  <link rel=icon type=image/png href=../favicon.png>
7
7
  </head><body>
8
8
 
9
- <script src=../../build/littlejs.js?125></script>
10
- <script src=gameObjects.js?125></script>
11
- <script src=game.js?125></script>
9
+ <script src=../../build/littlejs.js?126></script>
10
+ <script src=gameObjects.js?126></script>
11
+ <script src=game.js?126></script>
@@ -0,0 +1,514 @@
1
+ # LittleJS Breakout Tutorial
2
+
3
+ For this tutorial we will start with the empty project in the LittleJS examples folder. This is a blank template to start a new project.
4
+
5
+ The empty project has only 3 files: one for html, one for js, and one image. The index.html and image files will not need to be changed for this example. The
6
+ [game.js](https://github.com/KilledByAPixel/LittleJS/blob/main/examples/empty/game.js) file contains a stubbed out project with all the JavaScript code and is the only file we will be working with today.
7
+
8
+ You can get the [LittleJS code using GitHub](https://github.com/KilledByAPixel/LittleJS) or call ```npm install littlejsengine```
9
+
10
+ In this tutorial we will make a breakout style game with a player controllable paddle, a ball that bounces, and bricks that break when hit. This is a great introduction to LittleJS and takes only around 30 minutes to complete.
11
+
12
+ ## [You can play the result of this tutorial here.](https://killedbyapixel.github.io/LittleJS/examples/breakoutTutorial/)
13
+
14
+ ![LittleJS Screenshot](images/9.png)
15
+
16
+ ## Create the Bricks
17
+
18
+ The first step is to make a 2D grid of brick objects in gameInit. As a placeholder we will create an EngineObject for each cell in the grid and set it to have a random color.
19
+
20
+ ```javascript
21
+ // create bricks
22
+ for(let x=0; x<=20; x++)
23
+ for(let y=0; y<=20; y++)
24
+ {
25
+ const brick = new EngineObject(vec2(x,y)); // create a brick
26
+ brick.color = randColor(); // give brick a random color
27
+ }
28
+ ```
29
+
30
+ That will make a square of 20×20 engine objects, we can adjust it as we go. It also assigns the color property of each brick to a random color.
31
+
32
+ ![LittleJS Screenshot](images/1.png)
33
+
34
+ It is completely off center and may not look like much yet, but it’s a start!
35
+
36
+ ## Improve the Bricks
37
+
38
+ Let’s make the bricks more rectangular by passing vec2(2,1) to the size parameter of Engine Object. We will need to adjust the loop to add 2 for each x iteration. Also we can use a Vector2 to describe the size of the level.
39
+
40
+ ```javascript
41
+ // create bricks
42
+ const levelSize = vec2(20, 20);
43
+ for(let x=0; x<=levelSize.x; x+=2)
44
+ for(let y=0; y<=levelSize.y; y+=1)
45
+ {
46
+ const brick = new EngineObject(vec2(x,y), vec2(2,1)); // create a brick
47
+ brick.color = randColor(); // give brick a random color
48
+ }
49
+ ```
50
+
51
+ ## Move the Camera
52
+
53
+ Let’s move the camera to the center of our level by setting the cameraPos variable to half the level size.
54
+
55
+ ```javascript
56
+ cameraPos = levelSize.scale(.5); // center camera in level
57
+ ```
58
+
59
+ We’ll also start with a blank slate by commenting out the default “Hello World!” text in gameRenderPost.
60
+
61
+ ```javascript
62
+ // drawTextScreen('Hello World!', mainCanvasSize.scale(.5), 80);
63
+ ```
64
+
65
+ ![LittleJS Screenshot](images/2.png)
66
+
67
+ It’s getting closer! The camera is now centered and blocks are rectangular.
68
+
69
+ ## Modify the Level Size
70
+
71
+ For this example we will use a fixed size canvas. This will let the drawing canvas always be the same resolution, even if the window size changes. Black space is added on the sides to compensate. It’s a great option for any game that uses a single screen like this.
72
+
73
+ To enable it we will set canvasFixedSize in gameInit to use 720p resolution.
74
+
75
+ ```javascript
76
+ canvasFixedSize = vec2(1280, 720); // use a 720p fixed size canvas
77
+ ```
78
+
79
+ Before we tweak the level size, let’s add this bit of code to gameRender to show the size of the level. This will cause some rects to be drawn each frame before the engine objects. To create color objects we pass in RGB values between 0 and 1.
80
+
81
+ ```javascript
82
+ drawRect(cameraPos, vec2(100), new Color(.5,.5,.5)); // draw background
83
+ drawRect(cameraPos, levelSize, new Color(.1,.1,.1)); // draw level boundary
84
+ ```
85
+
86
+ We also need to make levelSize a global by moving it to the top so it can be accessed from other functions.
87
+
88
+ ```javascript
89
+ const levelSize = vec2(38, 20); // size of play area
90
+ ```
91
+
92
+ Now we can see the boundaries of our level and tweak the size to fill the canvas. I found that a level size of vec2(38, 20) works well here.
93
+
94
+ We can also adjust the for loop where the bricks are created to make them only fill the top middle part of the level.
95
+
96
+ ```javascript
97
+ for(let x=2; x<=levelSize.x-2; x+=2)
98
+ for(let y=12; y<=levelSize.y-2; y+=1)
99
+ {
100
+ const brick = new EngineObject(vec2(x,y), vec2(2,1)); // create a brick
101
+ brick.color = randColor(); // give brick a random color
102
+ }
103
+ ```
104
+
105
+ ![LittleJS Screenshot](images/3.png)
106
+
107
+ Now that looks almost like a breakout game!
108
+
109
+ ## Create the Player Paddle
110
+
111
+ For the player’s paddle, let’s create a new class of object that extends the built in EngineObject. This will allow us to control movement with the mouse. In this Paddle class we will just add some new code to the update function which is automatically called each frame by the engine.
112
+
113
+ ```javascript
114
+ class Paddle extends EngineObject
115
+ {
116
+ update()
117
+ {
118
+ this.pos.x = mousePos.x; // move paddle to mouse
119
+ }
120
+ }
121
+ ```
122
+
123
+ Then we just need to create the paddle in gameInit.
124
+
125
+ ```javascript
126
+ new Paddle; // create player's paddle
127
+ ```
128
+
129
+ There is now a square paddle that moves along the bottom of the screen.
130
+
131
+ Let’s make the paddle wider and move it up a smidge by adding a constructor to the Paddle class.
132
+
133
+ ```javascript
134
+ constructor()
135
+ {
136
+ super(vec2(0,1), vec2(6,.5)); // set object position and size
137
+ }
138
+ ```
139
+
140
+ ![LittleJS Screenshot](images/4.png)
141
+
142
+ Now there is a player controllable paddle that moves along the bottom of the screen.
143
+
144
+ You can also add this code to the paddle update that will keep the paddle from following the mouse offscreen, though it is not required.
145
+
146
+ ```javascript
147
+ // clamp paddle to level size
148
+ this.pos.x = clamp(this.pos.x, this.size.x/2, levelSize.x - this.size.x/2);
149
+ ```
150
+
151
+ ## Create the Ball
152
+ The final missing piece is of course that ball that moves around and bounces off of stuff. We will leverage the built in physics solver to handle all the ball’s physics and collision math.
153
+
154
+ We can create a Ball class the same way we created the paddle. In this case we will also apply some velocity to ball so it starts moving right away.
155
+
156
+ ```javascript
157
+ class Ball extends EngineObject
158
+ {
159
+ constructor(pos)
160
+ {
161
+ super(pos); // set object position
162
+
163
+ this.velocity = vec2(-.1, -.1); // give ball some movement
164
+ }
165
+ }
166
+ ```
167
+
168
+ Then we just need to create the ball in gameInit, passing in the camera position for it’s location.
169
+
170
+ ```javascript
171
+ new Ball(cameraPos); // create a ball
172
+ ```
173
+
174
+ ## Make the Ball Collide
175
+
176
+ You may have noticed that the ball goes right through the paddle and bricks. To enable collision we just need to call setCollision on the ball and the paddle by adding a call in both of their constructors.
177
+
178
+ ```javascript
179
+ this.setCollision(); // make object collide
180
+ ```
181
+
182
+ Now the ball collides but unfortunately also pushes the paddle away, not quite what we want. To prevent the paddle from being moved by physics we will make it use static physics by setting it’s mass to 0 in the paddle constructor.
183
+
184
+ ```javascript
185
+ this.mass = 0; // make object have static physics
186
+ ```
187
+
188
+ Getting closer, the paddle is not pushed away but the ball doesn’t bounce either. To make it bounce, we need to set the ball’s elasticity which controls how much it will bounce.
189
+
190
+ ```javascript
191
+ this.elasticity = 1; // make object bounce
192
+ ```
193
+
194
+ ![LittleJS Screenshot](images/5.png)
195
+
196
+ It works! The ball now bounces off the paddle, still not the walls or blocks yet though.
197
+
198
+ Before we continue, let’s make the ball a bit smaller by changing the super call in it’s constructor.
199
+
200
+ ```javascript
201
+ super(pos, vec2(.5)); // set object position and size
202
+ ```
203
+
204
+ ## Make the Ball Bounce Off Walls
205
+
206
+ Next, let’s make the ball bounce when it hits the top or sides of the screen. To do this, we will make a Wall object class that works similar to the paddle but without an update function.
207
+
208
+ ```javascript
209
+ class Wall extends EngineObject
210
+ {
211
+ constructor(pos, size)
212
+ {
213
+ super(pos, size); // set object position and size
214
+
215
+ this.setCollision(); // make object collide
216
+ this.mass = 0; // make object have static physics
217
+ }
218
+ }
219
+ ```
220
+
221
+ We can create walls on the sides and top by adding a few more lines of code to the gameInit function.
222
+
223
+ ```javascript
224
+ // create walls
225
+ new Wall(vec2(-.5,levelSize.y/2), vec2(1,100)) // top
226
+ new Wall(vec2(levelSize.x+.5,levelSize.y/2), vec2(1,100)) // left
227
+ new Wall(vec2(levelSize.x/2,levelSize.y+.5), vec2(100,1)) // right
228
+ ```
229
+
230
+ ![LittleJS Screenshot](images/6.png)
231
+
232
+ Now we have white walls around the outside that block the ball. These walls should be invisible so we will set their color to be transparent by adding another line to the Wall constructor.
233
+
234
+ ```javascript
235
+ this.color = new Color(0,0,0,0); // make object invisible
236
+ ```
237
+
238
+ ## Debug Display
239
+
240
+ This is a good time to try opening up the debug info by pressing ~. This feature will allow for a selection of debug overlays while also showing all game objects with more info displayed for the object closest to the mouse. It can be really useful when trying to diagnose bugs and understand what is going on.
241
+
242
+ ![LittleJS Screenshot](images/7.png)
243
+
244
+ As you can see, the wall objects are still present even though they are now invisible.
245
+
246
+ ## Respawn the Ball
247
+
248
+ We should respawn the ball when it goes below the screen so players can keep playing.
249
+
250
+ To do this, we will need to make a global ball object.
251
+
252
+ ```javascript
253
+ let ball; // keep track of ball object
254
+ ```
255
+
256
+ We can remove the old code that was creating a new ball in gameInit, and instead create the ball in gameUpdate, only when it is needed.
257
+
258
+ ```javascript
259
+ // if there is no ball or ball is below level
260
+ if (!ball || ball.pos.y < -1)
261
+ {
262
+ // destroy old ball
263
+ if (ball)
264
+ ball.destroy();
265
+
266
+ // create a ball
267
+ ball = new Ball(cameraPos);
268
+ }
269
+ ```
270
+
271
+ Don’t forget to destroy the old ball before creating the new one! Though it doesn’t really matter for this simple example, it would cause the old balls to continue existing while being updated and rendered. That could really slow things in bigger games with thousands of objects.
272
+
273
+ ## Break the Bricks
274
+
275
+ Still, the ball doesn’t collide with the bricks much less breaks them and it wouldn’t be breakout without that. So we need to make the brick collide with the ball, just like the walls, so let’s start with Brick class that is the same as Wall.
276
+
277
+ ```javascript
278
+ class Brick extends EngineObject
279
+ {
280
+ constructor(pos, size)
281
+ {
282
+ super(pos, size);
283
+
284
+ this.setCollision(); // make object collide
285
+ this.mass = 0; // make object have static physics
286
+ }
287
+ }
288
+ ```
289
+
290
+ And we will just change the for loop in gameInit to use the Brick class.
291
+
292
+ ```javascript
293
+ const brick = new Brick(vec2(x,y), vec2(2,1)); // create a brick
294
+ ```
295
+
296
+ Now the ball will bounce off the bricks, but we want it to break the bricks. To do this we can override the brick’s collideWithObject function which is called by the engine when the ball hits it. Instead of the default collide behavior, we just destroy the brick.
297
+
298
+ ```javascript
299
+ collideWithObject(o)
300
+ {
301
+ this.destroy(); // destroy block when hit
302
+ }
303
+ ```
304
+
305
+ But wait! This will cause the ball to careen through the bricks without bouncing. Maybe fun for a special powerup, but not our goal here. This function should return a Boolean value to indicate if the collision needs to be resolved, if it returns false then the ball will not bounce. So the answer is to just add another line of code to the collideWithObject function that returns a truthy value indicating that the collision should occur.
306
+
307
+ ```javascript
308
+ return 1; // allow object to collide
309
+ ```
310
+
311
+ ![LittleJS Screenshot](images/8.png)
312
+
313
+ Success, we have breakout! All the core components are here though there are a few more changes we can make that will greatly increase the playability.
314
+
315
+ ## Track the Player’s Score
316
+
317
+ We can easily add a score tracker to the game by making a global score variable called score.
318
+
319
+ ```javascript
320
+ let score = 0; // start score at 0
321
+ ```
322
+
323
+ Then increment it in the Brick.collideWithObject function.
324
+
325
+ ```javascript
326
+ ++score; // award a point for each brick broke
327
+ ```
328
+
329
+ And change that drawTextScreen code we had commented out in gameRenderPost to display the score. We can adjust the position and size of the text to fit at the top of the screen.
330
+
331
+ ```javascript
332
+ drawTextScreen("Score " + score, vec2(mainCanvasSize.x/2, 70), 50); // show score
333
+ ```
334
+
335
+ ![LittleJS Screenshot](images/9.png)
336
+
337
+ ## Add Sound Effects
338
+
339
+ So far the game is completely silent. Let’s add sounds using ZzFX. This is a tiny sound effect generator that works in conjunction with LittleJS and can be used on it’s own too.
340
+
341
+ We will start with the ball bounce sound. [You can use the official ZzFX sound designer to create your own sound.](https://killedbyapixel.github.io/ZzFX/) I recommend using the Blip preset for this one.
342
+
343
+ To make a sound effect with this system we will create a global object for each sound.
344
+
345
+ ```javascript
346
+ const sound_bounce = new Sound([,,1e3,,.03,.02,1,2,,,940,.03,,,,,.2,.6,,.06], 0);
347
+ ```
348
+
349
+ You can use the sound I chose or copy the code for your own sound from ZzFX. There is a checkbox for LittleJS style sounds to make exporting a little easier.
350
+
351
+ To play the sound we will override collideWithObject for the Ball class. Also remember we must return 1 for the collision to occur.
352
+
353
+ ```javascript
354
+ collideWithObject(o)
355
+ {
356
+ sound_bounce.play(); // play bounce sound
357
+ return 1; // allow object to collide
358
+ }
359
+ ```
360
+ Let’s also make a brick break sound. You can use the ZzFX sound designer again to create your own sound, for this one try the Hit preset.
361
+
362
+ Create a global object for this sound the same way we did for the bounce sound. You can replace it with your own generated sound or use mine.
363
+
364
+ ```javascript
365
+ const sound_break = new Sound([,,90,,.01,.03,4,,,,,,,9,50,.2,,.2,.01], 0);
366
+ ```
367
+
368
+ Then in the Brick’s collideWithObject function just add some code to play that sound.
369
+
370
+ ```javascript
371
+ sound_break.play(); // play brick break sound
372
+ ```
373
+
374
+ Now you should hear sound as the ball bounces around and breaks bricks! Of course it’s easy to swap out these sounds with other ones, feel free to experiment.
375
+
376
+ ## Add Click To Start Interaction
377
+
378
+ Let’s improve the gameplay by letting the player click to start, instead of creating the ball right away. This will also ensure that the player has clicked which is requirement for audio to play in most browsers.
379
+
380
+ We can also add a special sound that plays on startup. Try making an interesting start from the ZzFX sound designer, maybe using the Powerup preset.
381
+
382
+ ```javascript
383
+ const sound_start = new Sound([,0,500,,.04,.3,1,2,,,570,.02,.02,,,,.04]);
384
+ ```
385
+
386
+ We can modify the gameUpdate to wait for player input from the mouse and play the sound when the ball is created.
387
+
388
+ ```javascript
389
+ if (ball && ball.pos.y < -1) // if ball is below level
390
+ {
391
+ // destroy old ball
392
+ ball.destroy();
393
+ ball = 0;
394
+ }
395
+ if (!ball && mouseWasPressed(0)) // if there is no ball and left mouse is pressed
396
+ {
397
+ ball = new Ball(cameraPos); // create the ball
398
+ sound_start.play(); // play start sound
399
+ }
400
+ ```
401
+
402
+ ## Add Particle Effects
403
+
404
+ Let’s add some effects when the bricks break. You can use the LittleJS particle system designer to create an explosion effect.
405
+
406
+ This will be a one shot type of effect, not a continuous emitter so change emitTime to something small like .1 for this example. Now before you play with it too much, let’s test it by pasting in the code it generated into the Brick’s collideWithObject function.
407
+
408
+ Make sure you replace the first parameter, vec2(), with this.pos so the effect appears wherever the brick is, otherwise it will spawn at the world origin.
409
+
410
+ ```javascript
411
+ // create explosion effect
412
+ new ParticleEmitter(this.pos, 0, 0, 0.1, 100, 3.14, -1, vec2(16, 16), new Color(1, 1, 1, 1), new Color(1, 1, 1, 1), new Color(1, 1, 1, 0), new Color(1, 1, 1, 0), 0.5, 0.1, 1, 0.1, 0.05, 1, 1, 0, 3.14, 0.1, 0.2, 0, 0, 1);
413
+ ```
414
+
415
+ Now you should see this simple particle effect play wherever a brick breaks. You can continue tweaking the parameters to make your own effect or use the one I made which also uses this.color to change the particle’s color so it matches the brick.
416
+
417
+ ```javascript
418
+ // create explosion effect
419
+ const color = this.color;
420
+ new ParticleEmitter(
421
+ this.pos, 0, // pos, angle
422
+ this.size, .1, 200, PI, // emitSize, emitTime, emitRate, emiteCone
423
+ -1, vec2(16), // tileIndex, tileSize
424
+ color, color, // colorStartA, colorStartB
425
+ color.scale(1,0), color.scale(1,0), // colorEndA, colorEndB
426
+ .2, .5, 1, .1, .1, // time, sizeStart, sizeEnd, speed, angleSpeed
427
+ .99, .95, .4, PI, // damping, angleDamping, gravityScale, cone
428
+ .1, .5, 0, 1 // fadeRate, randomness, collide, additive
429
+ );
430
+ ```
431
+
432
+ ![LittleJS Screenshot](images/10.png)
433
+
434
+ ## Improve the Gameplay
435
+
436
+ Last but certainly not least, let’s make a few small improvements to the gameplay. It would help to give the player some more control of where the ball goes when it hits the paddle. We can do this by adding some code to the ball’s collideWithObject function.
437
+
438
+ This code will use the rotate function to modify the bounce angle based on the distance from the center of the paddle.
439
+
440
+ We will also slightly increase the speed of the ball each time it hits the paddle to make the difficulty of the game ramp up over time.
441
+
442
+ ```javascript
443
+ collideWithObject(o)
444
+ {
445
+ // prevent colliding with paddle if moving upwards
446
+ if (o == paddle && this.velocity.y > 0)
447
+ return 0;
448
+
449
+ sound_bounce.play(); // play bounce sound
450
+
451
+ if (o == paddle)
452
+ {
453
+ // control bounce angle when ball collides with paddle
454
+ const deltaX = this.pos.x - o.pos.x;
455
+ this.velocity = this.velocity.rotate(.3 * deltaX);
456
+
457
+ // make sure ball is moving upwards with a minimum speed
458
+ this.velocity.y = max(-this.velocity.y, .2);
459
+
460
+ // prevent default collision code
461
+ return 0;
462
+ }
463
+
464
+ return 1; // allow object to collide
465
+ }
466
+ ```
467
+
468
+ Also we will need to make the paddle a global object like the ball.
469
+
470
+ ```javascript
471
+ let paddle; // keep track of player's paddle
472
+ ```
473
+
474
+ And assign that paddle object when it is created in gameInit.
475
+
476
+ ```javascript
477
+ paddle = new Paddle; // create player's paddle
478
+ ```
479
+
480
+ While we are working in the ball’s collideWithObject function, let’s also make it speed up a little bit each time it bounces.
481
+
482
+ ```javascript
483
+ // speed up the ball
484
+ const speed = min(1.04*this.velocity.length(), .5);
485
+ this.velocity = this.velocity.normalize(speed);
486
+ ```
487
+
488
+ ## Improve the Sound
489
+
490
+ Lets make the sound a bit more interesting by using positional audio. This lets the volume and panning be adjusted based on the sound’s on screen location. To enable this, just pass a position to the sound’s play function.
491
+
492
+ ```javascript
493
+ sound_break.play(this.pos); // play brick break sound
494
+ ```
495
+
496
+ For the ball bounce sound, let’s also tweak the pitch by how fast the ball is moving. For the sound play function, the second parameter takes a scalar for volume and the third parameter takes a scalar for pitch.
497
+
498
+ ```javascript
499
+ sound_bounce.play(this.pos, 1, speed); // play bounce sound with pitch scaled by speed
500
+ ```
501
+
502
+ ## Congratulations on Completing the Breakout Tutorial
503
+
504
+ Now you know the basics of how to use the LittleJS engine! [The final result with all the code from this tutorial is playable here.](https://killedbyapixel.github.io/LittleJS/examples/breakoutTutorial/)
505
+
506
+ If you need a little help completing the tutoiral, [join us on the LittleJS discord](https://discord.gg/zb7hcGkyZe).
507
+
508
+ Feel free to continue building on this tutorial to make a more interesting breakout game. What kind of ideas would you like to include?
509
+
510
+ You can also check out [the breakout example game](https://killedbyapixel.github.io/LittleJS/examples/breakout/) which extends this tutorial to show textured objects, post processing, and image fonts.
511
+
512
+ ![LittleJS Screenshot](images/11.png)
513
+
514
+ There are several other example games you can use as a starting point for your own creations, or use a new blank project like we did for this example. What will you build with LittleJS? 🚂