Skip to content

Commit fd36164

Browse files
authored
update 3d game to new kaboom version (#272)
1 parent 3026086 commit fd36164

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed
37.2 KB
Loading
259 KB
Loading

tutorials/25-build-3d-game-with-kaboom.md

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,46 @@ Head over to [Replit](https://replit.com) and create a new repl. Choose **Kaboom
2323

2424
![New repl](/images/tutorials/25-3d-game-kaboom/new-repl.png)
2525

26-
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.
2727

2828
## Setting up the Kaboom environment
2929

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).
3131

3232
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.
3333

34-
![Uploading sprites](/images/tutorials/25-3d-game-kaboom/upload-sprites.gif)
34+
<img src="/images/tutorials/25-3d-game-kaboom/upload-sprites.gif"
35+
alt="Uploading sprites"
36+
style="width: 50% !important;"/>
3537

36-
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".
3738

38-
![Set up Kaboom environment](/images/tutorials/25-3d-game-kaboom/setup.gif)
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+
import kaboom from "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.
3966

4067
## Creating the interface layers
4168

@@ -81,6 +108,7 @@ function spawnAlien(){
81108
sprite("alien"),
82109
pos(x,y),
83110
scale(0.2),
111+
area(),
84112
rotate(0),
85113
{
86114
xpos: rand(-1*SCREEN_WIDTH/2, SCREEN_WIDTH/2),
@@ -194,9 +222,12 @@ onUpdate(()=>{
194222

195223
if (x>= 0 && x<= SCREEN_WIDTH && y>=0 && y<= SCREEN_HEIGHT) {
196224
const scaled_z = star.zpos * 0.0005;
197-
const intensity = 1 - scaled_z;
225+
const intensity = (1 - scaled_z) * 255;
198226

199-
drawRect(vec2(x,y), 1, 1, {
227+
drawRect({
228+
width: 1,
229+
height: 1,
230+
pos: vec2(x,y),
200231
color: rgb(intensity, intensity, intensity)
201232
});
202233
}
@@ -215,7 +246,7 @@ Now take a look at the [`onUpdate`](https://kaboomjs.com/doc#onUpdate) event han
215246
- 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.
216247
- 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.
217248
- 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.
219250
- 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.
220251

221252
## Adding the spaceship cockpit
@@ -274,25 +305,25 @@ Now we can add some event handlers for keyboard input:
274305
const delta = MOVE_DELTA * dt();
275306
shiftAliens(delta, 0);
276307
shiftStars(delta*0.01, 0);
277-
camRot(0.1);
308+
camRot(5.7);
278309
});
279310

280311
onKeyDown("right", () => {
281312
const delta = -1 * MOVE_DELTA * dt();
282313
shiftAliens(delta, 0);
283314
shiftStars(delta*0.01, 0);
284-
camRot(-0.1);
315+
camRot(-5.7);
285316
});
286317

287318

288319
onKeyDown("up", () => {
289-
const delta = -1 * MOVE_DELTA * dt();
320+
const delta = MOVE_DELTA * dt();
290321
shiftAliens(0, delta);
291322
shiftStars(0,delta*0.01);
292323
});
293324

294325
onKeyDown("down", () => {
295-
const delta = MOVE_DELTA * dt();
326+
const delta = -1 * MOVE_DELTA * dt();
296327
shiftAliens(0, delta);
297328
shiftStars(0, delta*0.01);
298329
});
@@ -324,15 +355,15 @@ const vertical_crosshair = add([
324355
rect(1, 10),
325356
origin('center'),
326357
pos(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.33),
327-
color(0, 1, 1),
358+
color(0, 255, 255),
328359
layer("ui")
329360
]);
330361

331362
const horizontal_crosshair = add([
332363
rect(10, 1),
333364
origin('center'),
334365
pos(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.33),
335-
color(0, 1, 1),
366+
color(0, 255, 255),
336367
layer("ui")
337368
]);
338369

@@ -355,7 +386,8 @@ function spawnBullet() {
355386
add([
356387
rect(1, 1),
357388
pos(BULLET_ORIGIN_LEFT),
358-
color(1, 0, 0),
389+
area(),
390+
color(255, 0, 0),
359391
"bullet",
360392
{
361393
bulletSpeed: BULLET_SPEED ,
@@ -366,7 +398,7 @@ function spawnBullet() {
366398
add([
367399
rect(1, 1),
368400
pos(BULLET_ORIGIN_RIGHT),
369-
color(1, 0, 0),
401+
color(255, 0, 0),
370402
"bullet",
371403
{
372404
bulletSpeed: -1*BULLET_SPEED,
@@ -385,7 +417,7 @@ When the player fires, we call the `spawnBullet` function to create a new set of
385417

386418
Then we calculate where we want the bullets to end up. This is the same position as the cross hairs.
387419

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.
389421

390422
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.
391423

@@ -541,7 +573,7 @@ Then we can modify the `onUpdate("alien",....)` event handler that we added earl
541573
alien.pos.x <= STRIKE_ZONE.x2 &&
542574
alien.pos.y >= STRIKE_ZONE.y1 &&
543575
alien.pos.y <= STRIKE_ZONE.y2){
544-
camShake(20);
576+
shake(20);
545577
makeExplosion(alien.pos, 10, 10, 10);
546578
}
547579
destroyAlien(alien);
@@ -550,7 +582,7 @@ Then we can modify the `onUpdate("alien",....)` event handler that we added earl
550582

551583
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.
552584

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.
554586

555587
![Colliding](/images/tutorials/25-3d-game-kaboom/colliding.gif)
556588

@@ -575,4 +607,5 @@ The spaceship cockpit was made by Ritza.
575607

576608
Thank you to all the creators for putting their assets up with a Creative Commons license and allowing us to use them.
577609

578-
<iframe height="400px" width="100%" src="https://replit.com/@ritza/3d-space-shooter?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>
610+
<iframe height="400px" width="100%" src="https://replit.com/@ritza/3d-space-shooter-new?embed=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>
611+

0 commit comments

Comments
 (0)