You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After the repl has booted up, you should see a `main.js` file under the "Scenes" section. This is where we'll start coding.
26
+
After the repl has booted up, you should see a `main.js` file under the "Code" section. This is where we'll start coding.
27
27
28
28
## Setting up the Kaboom environment
29
29
30
-
The Kaboom interface on Replit is specialised for game-making. Besides the Space Invader icon, you'll notice a few special folders in the file try, like "Scenes", '"Sprites", and "Sounds". These special folders take care of loading up assets, and all the necessary code to start scenes and direct the game. You can read up more about the Kaboom interface [here](https://docs.replit.com/tutorials/kaboom).
30
+
The Kaboom interface on Replit is specialised for game-making. Besides the Space Invader icon, you'll notice a few special folders in the file try, like "Code", '"Sprites", and "Sounds". These special folders take care of loading up assets, and all the necessary code to start scenes and direct the game. You can read up more about the Kaboom interface [here](https://docs.replit.com/tutorials/kaboom).
31
31
32
32
If you haven't already, download this [zip file](/tutorial-files/3d-game-kaboom/3d-game-resources.zip) containing all the sprites and sounds for the game. Extract the file on your computer, then add the sprites to the "Sprites" folder, and the sounds to the "Sounds" folder.
To set up the game play environment, click the dropdown next to the Kaboom menu. Uncheck "Full Screen", and set the Width to 320 and Height to 200. Then choose dark blue or black as the "Clear Color".
37
38
38
-

39
+
To set up the game play environment, we need to set up Kaboom with the screen size and colors we want for the game window. Replace the code in `main.js` with the code below:
40
+
41
+
```javascript
42
+
importkaboomfrom"kaboom";
43
+
44
+
kaboom({
45
+
background: [0, 0, 0],
46
+
width:320,
47
+
height:200,
48
+
scale:2
49
+
});
50
+
```
51
+
52
+
Here we import the kaboom library, and then initialize the context by calling `kaboom({ ... })`. We set the size of the view to 320x200 pixels and `scale` to make the background twice the size on screen.
53
+
Now, let's load up the sprites and sounds we will need in this game. The code below loads each of the graphic elements we'll use, and gives them a name, so we can refer to them when we build the game characters:
54
+
55
+
```javascript
56
+
loadRoot("sprites/")
57
+
loadSprite("cockpit", "cockpit.png")
58
+
loadSprite("alien", "alien.png")
59
+
60
+
loadRoot("sounds/")
61
+
loadSound("shoot","shoot.wav")
62
+
loadSound("explosion","explosion.wav")
63
+
```
64
+
65
+
The first line, [`loadRoot`](https://kaboomjs.com/#loadRoot), specifies which folder to load all the sprites and game elements from, so we don't have to keep typing it in for each sprite. Then each line loads a game sprite and gives it a name so that we can refer to it in code later. We use similar code to load the sounds we will need in this game, but instead of [`loadSprite`](https://kaboomjs.com/#loadSprite) we use [`loadSound`](https://kaboomjs.com/#loadSound) to load sounds.
39
66
40
67
## Creating the interface layers
41
68
@@ -81,6 +108,7 @@ function spawnAlien(){
81
108
sprite("alien"),
82
109
pos(x,y),
83
110
scale(0.2),
111
+
area(),
84
112
rotate(0),
85
113
{
86
114
xpos:rand(-1*SCREEN_WIDTH/2, SCREEN_WIDTH/2),
@@ -194,9 +222,12 @@ onUpdate(()=>{
194
222
195
223
if (x>=0&& x<=SCREEN_WIDTH&& y>=0&& y<=SCREEN_HEIGHT) {
196
224
constscaled_z=star.zpos*0.0005;
197
-
constintensity=1- scaled_z;
225
+
constintensity=(1- scaled_z) *255;
198
226
199
-
drawRect(vec2(x,y), 1, 1, {
227
+
drawRect({
228
+
width:1,
229
+
height:1,
230
+
pos:vec2(x,y),
200
231
color:rgb(intensity, intensity, intensity)
201
232
});
202
233
}
@@ -215,7 +246,7 @@ Now take a look at the [`onUpdate`](https://kaboomjs.com/doc#onUpdate) event han
215
246
- We don't use an object filter to look for the stars, as we didn't create them as Kaboom objects. Instead, we just cycle through each star in the `stars` array.
216
247
- Instead of destroying the star and removing it from the array when it reaches the 'front' of the screen, we recycle it by resetting its `z-pos` back to 1000.
217
248
- We also check if the star is out of the screen view. If it is, we don't draw it, to save a bit of overhead.
218
-
- Instead of using the `z-pos` to calculate a value by which to scale the star, we use it to calculate the star's intensity, or brightness. Kaboom uses color values in the range 0-1. So we first scale the `z-pos` down to below 1. Then we subtract it from 1 (the maximum color value, i.e. brightest value) to create an inverse relationship between `z-pos` and intensity. In other words, stars further away from us have higher `zpos` values, giving us lower color intensity. This makes stars far away glow dimly, while those closer to our view look brighter.
249
+
- Instead of using the `z-pos` to calculate a value by which to scale the star, we use it to calculate the star's intensity, or brightness. Kaboom uses color values in the range 0-255. So we first scale the `z-pos` down to below 1. Then we subtract it from 1 to create an inverse relationship between `z-pos` and intensity. We then multiply the intensity value by 255 to scale it to a value that is within the range 0-255. In other words, stars further away from us have higher `zpos` values, giving us lower color intensity. This makes stars far away glow dimly, while those closer to our view look brighter.
219
250
- Finally, we use the Kaboom's [drawRect](https://kaboomjs.com/doc#drawRect) method to directly draw the star to the screen. As there is no pixel level drawing function in Kaboom, we create a rectangle of size 1 to draw just one pixel.
220
251
221
252
## Adding the spaceship cockpit
@@ -274,25 +305,25 @@ Now we can add some event handlers for keyboard input:
@@ -385,7 +417,7 @@ When the player fires, we call the `spawnBullet` function to create a new set of
385
417
386
418
Then we calculate where we want the bullets to end up. This is the same position as the cross hairs.
387
419
388
-
Using these values, we create 2 bullet objects - simple 1 pixel objects with the tag `bullet` and color set to red `(1,0,0)` so they look menacing. We also add custom properties to the object: A speed for the bullet to move at, and the vanishing point where its course ends.
420
+
Using these values, we create 2 bullet objects - simple 1 pixel objects with the tag `bullet` and color set to red `(255,0,0)` so they look menacing. We also add custom properties to the object: A speed for the bullet to move at, and the vanishing point where its course ends.
389
421
390
422
As a final detail, we set our "shoot" sound to play as each bullet is created, adjusting the volume and applying a randomly generated detune value so that the pitch of the sound is slightly different each time it's played.
391
423
@@ -541,7 +573,7 @@ Then we can modify the `onUpdate("alien",....)` event handler that we added earl
541
573
alien.pos.x<=STRIKE_ZONE.x2&&
542
574
alien.pos.y>=STRIKE_ZONE.y1&&
543
575
alien.pos.y<=STRIKE_ZONE.y2){
544
-
camShake(20);
576
+
shake(20);
545
577
makeExplosion(alien.pos, 10, 10, 10);
546
578
}
547
579
destroyAlien(alien);
@@ -550,7 +582,7 @@ Then we can modify the `onUpdate("alien",....)` event handler that we added earl
550
582
551
583
We've modified the code to check if the alien is really close to us (`alien.zpos < 1 `), and if it is, we check if it is within the bounds of the `STRIKE_ZONE` area. The strike zone is a rectangle - you could implement more complex shapes if you wanted to be more accurate about where the alien can hit. However, a rectangle approximation is OK for this game.
552
584
553
-
If the alien is close enough, and within our strike zone, we use the [`camShake`](https://kaboomjs.com/doc#camShake) effect to make it "feel" like we've been hit. Then we create an explosion at the point of impact for some visual effects.
585
+
If the alien is close enough, and within our strike zone, we use the [`shake`](https://kaboomjs.com#shake) effect to make it "feel" like we've been hit. Then we create an explosion at the point of impact for some visual effects.
0 commit comments