Make a story-based game

Our final challenge is to create a story-based game, where the user will be asked questions, and the outcome of the program will depend on their answer. Let’s begin!

 

Create a new project in Scratch and choose a sprite of your choice. You can also choose a background if you wish.

Click the picture to enlarge it. This project has a lot of code, so it’s best to take it in small steps. Add the code above, and read below to see what each block does:

  • When green flag clicked: The code below this block will run when the green flag is clicked.
  • Ask (Hello! How are you feeling today?) and wait: This block will ask the question to the user, and store their answer in a system-generated “answer” variable.

We will need a custom variable for this project - following the steps from the previous projects, create a new variable called “question answered?” Note that, when a variable is created, its original value is 0 - this will be important to help you understand the next step.

Repeat until <(question answered?) = (1)>: For this program, we’re going to want our sprite to continue asking the question until the user provides an answer we can work with. For this project, we will assume that, when our “question answered?” variable is equal to 1, we have received an answer that works, and if the variable is equal to 0, we have not yet received an answer that will work. Therefore, this block will run the code contained within (which we will add in the next step) until the user has provided an answer we can work with. Since our “question answered” variable is currently equal to 0, as explained before, the code is guaranteed to run at least once, and may run more times than that if the user doesn’t provide a workable response.

Add this new code to your existing code, and take a look below to understand each block:

  • If <(answer) contains (good)?> then: If the user’s answer contains the word “good,” we can assume they had a positive response. Therefore, we will run the code contained within this block. You can use “or” blocks to expand the words to check for; consider adding “great” and “happy.”
  • Say (“Great! I’m so happy to hear that”) for (5) seconds: This block will make our sprite say this message for 5 seconds.
  • Set (question answered?) to (1): As explained before, we set “question answered?” to 1 to show that a valid response has been received. Since we received a valid response, we can set our variable to 1 to show that the code doesn’t need to loop any more.

The code for a negative response will be very similar:

  • If <(answer) contains (bad)?> then: If the user’s answer contains the word “bad,” we can assume they had a negative response. Therefore, we will run the code contained within this block. You can use “or” blocks to expand the words to check for; consider adding “sad” and “terrible.”
  • Say (“I’m sorry to hear that! I hope things get better for you”) for (5) seconds: This block will make our sprite say this message for 5 seconds.
  • Set (question answered?) to (1): As explained before, we set “question answered?” to 1 to show that a valid response has been received. Since we received a valid response, we can set our variable to 1 to show that the code doesn’t need to loop any more.

Finally, we need to add some code in case the user doesn’t answer “good” or “bad”:

  • If <(question answered?) = 0>: We only want to run the code contained within this block if a usable answer hasn’t yet been provided, so we will check for that condition. We will enter the loop if “question answered?” is equal to 0.
  • Ask (“I’m sorry, I don’t understand. Are you feeling good or bad?”) and wait: These instructions let the user know the type of answer we’re looking for. After the user answers, our code will loop to the top, and our code will run through again using the user’s new answer. This will happen again and again until a user enters an answer that contains either “good” or “bad.”
  • Set (question answered?) to (0): At the very bottom of our program, outside of our repeat loop, will set “question answered?” to 0 so that, if the user runs the program again by clicking the green flag, our variable is reset and ready to be used again.

 

Source: https://www.create-learn.us