Friday, April 26, 2013

J.S. Project: Choose Your Own Adventure!


Today, we're going to create a 'choose your own adventure' game. Exciting, right? It will have a basic story line, the opportunity for the user to make a couple of decisions, and a happy ending (hopefully).

01 / 07 : Let's use the confirm command to ask your user if they are ready to play.
    HINT - remember, to get the user to confirm something, the syntax looks like this:
        confirm("I understand confirm!");

// Check if the user is ready to play!
confirm("Are you ready to embark on this adventure of fun?");


02 / 07 : However before we start playing, we're going to check to see if the user is old enough to play! This step involves two bits of code. Declare a variable age using prompt. Make that age equal to the answer you prompt the user to give (ie. ask the user 'how old are you?'). Finally, create an if / else statement: if the user is less than 19, use console.log and tel the that they're allowed to play but you take no responsibility. Else, use console.log and give them an encouraging message to play on!
    HINT - recall the syntax for if / else statements:
        if (condition) {
            code code code;
        } else {
            code code code;
        }

var age = prompt("How old are you?");
    if (age < 18) {
        console.log("You're allowed to play, but I take no responsibility.");
    } else {
        console.log("Well, what are you waiting for? Let's get started already!");
    }


03 / 07 : We've gotta set up the scene for our story. Here's a short little introduction that we can use for now: Snow White and Batman were hanging out at the bus stop, waiting to go to the shops. There was a sale on and both needed some new threads. You've never really liked Batman. You walk up to him.
Let's print the intro using console.log. The intro is full of words... remember how words are called 'strings' in JavaScript? Don't forget how to properly use strings!
    HINT - don't forget to put quotes around the intro!

// introduction
console.log("Snow White and Batman were hanging out at the bus stop, waiting to go to the shops. There was a sale on and both needed some new threads. You've never really liked Batman. You walk up to him.");


04 / 07 : Our story is ready to move along! There's a scene and your user is about to talk to batman. Under your existing code, print out the storyline: "Batman glares at you." Then declare a variable userAnswer and make it equal a prompt that asks the user "Are you feeling lucky, punk?" (This will be the question that Batman asks your user).

console.log("Batman glares at you.");
var userAnswer = prompt("Are you feeling lucky, punk?");


05 / 07 : Now we've gotta create different scenarios using the if / else statements! Use an if / else statement to write out the last part of this game. If userAnswer is "yes", print out: "Batman hits you very hard. It's Batman and you're you! Of course Batman wins!" Otherwise, print out: "You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman."
    HINT - Remember: = is for assignment, and === is to check this are equal!

if (userAnswer === "yes") {
    console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
} else {
    console.log "You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
}


06 / 07 : It's probably worthwhile to ask your user for feedback. Let's create a variable called feedback and prompt the user to rate your game out of 10. If the score is greater than 8, print out: "This is just the beginning of my game empire. Stay tuned for more!" Otherwise, print out: "I slaved away at this game and you gave me that score?! The nerve! Just you wait!"
    HINT - var feedback  = prompt("Message");

var feedback = prompt("Could you rate my game out of 10?");
    if (feedback > 8) {
        console.log("This is just the beginning of my game empire. Stay tuned for more!");
    } else {
        console.log("I slaved away at this game and you gave me that score?! The nerve! Just you wait!");
    }


07 / 07 : Congrats! You've just programmed your first game! Now it's your turn to program your own 'choose your own aventure game'.






No comments:

Post a Comment