Skip to content

Latest commit

 

History

History
189 lines (133 loc) · 6.17 KB

File metadata and controls

189 lines (133 loc) · 6.17 KB

Build a Space Game Part 5: Scoring and Lives

Pre-Lecture Quiz

Pre-lecture quiz

In this lesson, you'll learn how to add scoring to a game and calculate lives.

Draw text on the screen

To be able to display a game score on the screen, you'll need to know how to place text on the screen. The answer is using the fillText() method on the canvas object. You can also control other aspects like what font to use, the color of the text and even its alignment (left, right, center). Below is some code drawing some text on the screen.

ctx.font = "30px Arial";
ctx.fillStyle = "red";
ctx.textAlign = "right";
ctx.fillText("show this on the screen", 0, 0);

✅ Read more about how to add text to a canvas, and feel free to make yours look fancier!

Life, as a game concept

The concept of having a life in a game is only a number. In the context of a space game it's common to assign a set of lives that get deducted one by one when your ship takes damage. It's nice if you can show a graphical representation of this like miniships or hearts instead of a number.

What to build

Let's add the following to your game:

  • Game score: For every enemy ship that is destroyed, the hero should be awarded some points, we suggest a 100 points per ship. The game score should be shown in the bottom left.
  • Life: Your ship has three lives. You lose a life every time an enemy ship collides with you. A life score should be displayed at the bottom right and be made out of the following graphic life image.

Recommended steps

Locate the files that have been created for you in the your-work sub folder. It should contain the following:

-| assets
  -| enemyShip.png
  -| player.png
  -| laserRed.png
-| index.html
-| app.js
-| package.json

You start your project the your_work folder by typing:

cd your-work
npm start

The above will start a HTTP Server on address http://localhost:5000. Open up a browser and input that address, right now it should render the hero and all the enemies, and as you hit your left and right arrows, the hero moves and can shoot down enemies.

Add code

  1. Copy over the needed assets from the solution/assets/ folder into your-work folder; you will add a life.png asset. Add the lifeImg to the window.onload function:

    lifeImg = await loadTexture("assets/life.png");
  2. Add the lifeImg to the list of assets:

    let heroImg,
    ...
    lifeImg,
    ...
    eventEmitter = new EventEmitter();
  3. Add variables. Add code that represents your total score (0) and lives left (3), display these scores on a screen.

  4. Extend updateGameObjects() function. Extend the updateGameObjects() function to handle enemy collisions:

    enemies.forEach(enemy => {
        const heroRect = hero.rectFromGameObject();
        if (intersectRect(heroRect, enemy.rectFromGameObject())) {
          eventEmitter.emit(Messages.COLLISION_ENEMY_HERO, { enemy });
        }
      })
  5. Add life and points.

    1. Initialize variables. Under this.cooldown = 0 in the Hero class, set life and points:

      this.life = 3;
      this.points = 0;
    2. Draw variables on screen. Draw these values to screen:

      function drawLife() {
        // TODO, 35, 27
        const START_POS = canvas.width - 180;
        for(let i=0; i < hero.life; i++ ) {
          ctx.drawImage(
            lifeImg, 
            START_POS + (45 * (i+1) ), 
            canvas.height - 37);
        }
      }
      
      function drawPoints() {
        ctx.font = "30px Arial";
        ctx.fillStyle = "red";
        ctx.textAlign = "left";
        drawText("Points: " + hero.points, 10, canvas.height-20);
      }
      
      function drawText(message, x, y) {
        ctx.fillText(message, x, y);
      }
    3. Add methods to Game loop. Make sure you add these functions to your window.onload function under updateGameObjects():

      drawPoints();
      drawLife();
  6. Implement game rules. Implement the following game rules:

    1. For every hero and enemy collision, deduct a life.

      Extend the Hero class to do this deduction:

      decrementLife() {
        this.life--;
        if (this.life === 0) {
          this.dead = true;
        }
      }
    2. For every laser that hits an enemy, increase game score with a 100 points.

      Extend the Hero class to do this increment:

        incrementPoints() {
          this.points += 100;
        }

      Add these functions to your Collision Event Emitters:

      eventEmitter.on(Messages.COLLISION_ENEMY_LASER, (_, { first, second }) => {
         first.dead = true;
         second.dead = true;
         hero.incrementPoints();
      })
      
      eventEmitter.on(Messages.COLLISION_ENEMY_HERO, (_, { enemy }) => {
         enemy.dead = true;
         hero.decrementLife();
      });

✅ Do a little research to discover other games that are created using JavaScript/Canvas. What are their common traits?

By the end of this work, you should see the small 'life' ships at the bottom right, points at the bottom left, and you should see your life count decrement as you collide with enemies and your points increment when you shoot enemies. Well done! Your game is almost complete.


🚀 Challenge

Your code is almost complete. Can you envision your next steps?

Post-Lecture Quiz

Post-lecture quiz

Review & Self Study

Research some ways that you can increment and decrement game scores and lives. There are some interesting game engines like PlayFab. How could using one of these would enhance your game?

Assignment

Build a Scoring Game