Sample Video Frame

20: First Game

In this exercise you will begin to write your very first game. This game will teach you how to use functions and if statements together. I am going to write half of the game, and then you must finish the game using my same style. You can finish the game in any way you want, so long as you have the same number of rooms and those rooms are in functions. It would be a good idea to make sure that you completely understand the game as I have written it before you begin to write your additions.

The Code

Remember that this code is not complete. I start you off with the basic code you need to make the game work, the first room, and how to select random hit points. After that you create your rooms similar to mine.

const readline = require('readline-sync');

const say = (prompt) => {
    console.log(prompt);
}

const die = (message) => {
    say(message);
    process.exit(1);
}

const ask = (hp, prompt) => {
    console.log(`[[You have ${hp} hit points.]]`);
    if(hp <= 0) {
        die("You died!");
    } else {
        return readline.question(prompt + ' ');
    }
}

const door = (hp) => {
    // they have to open the door to get the gold
    // what kind of puzzle will they solve?
}

const spider = (hp) => {
    // they enter here, and the spider takes 10 hit points
    // if they live then they can run away
}

const gold = (hp) => {
    // end of the game they win if they get the gold
}

const rope = (hp) => {
    // they are at the bottom of the well
    // they can go through the door to the gold
    // or go take a wrong turn to the spider
}

const well = (hp) => {
    say("You are walking through the woods and see a well.");
    say("Walking up to it and looking down you see a shiny thing at the bottom.");
    let next = ask(hp, "What do you do?");

    if(next === "climb") {
        say("You climb down the rope.");
        rope(hp);
    } else if(next === "jump") {
        say("Yikes! Let's see if you survive!");
        hp = Math.floor(hp / 2);
        rope(hp);
    } else {
        say("You can't do that here.");
        well(hp);
    }
}

// setup hit points
let hp = Math.floor(Math.random() * 10) + 1;

// this starts the game
well(hp)

What You Should See

Here is how I run this first game with only my code in it.

$ node ex20-first-game/code.js
You are walking through the woods and see a well.
Walking up to it and looking down you see a shiny thing at the bottom.
[[You have 7 hit points.]]
What do you do? climb
You climb down the rope.
$ node ex20-first-game/code.js
You are walking through the woods and see a well.
Walking up to it and looking down you see a shiny thing at the bottom.
[[You have 5 hit points.]]
What do you do? jump
Yikes! Let's see if you survive!
$ node ex20-first-game/code.js
You are walking through the woods and see a well.
Walking up to it and looking down you see a shiny thing at the bottom.
[[You have 2 hit points.]]
What do you do? what?
You can't do that here.
You are walking through the woods and see a well.
Walking up to it and looking down you see a shiny thing at the bottom.
[[You have 2 hit points.]]
What do you do? climb
You climb down the rope.
$

Learn JavaScript Today

Register today for the Early Access course and get the all currently available videos and lessons, plus all future modules for no extra charge.


Still Not Sure? Try the next FREE Lesson!