Guessing Game (Scripting variables)

Let's build a number guessing game where the player tries to guess a secret number!

  1. Click here to open a new blank project.

  2. Create a new variable called secret number.

    If you have not yet done the last project, Keeping Score, then you can follow the instructions below to create new variables.
  3. The block under the "Sensing" palette will ask the player a question (which we can put in the blank space in the block), and then wait for the player to input an answer and press enter. Then, that answer will be stored in the variable.

    For example, the following block will cause the sprite to say "Guess what my number is!" and wait for a response.

    And you can access the response by dragging the oval-shaped block from the left-side bar under the "Sensing" palette:

    Using the block, set secret number to a random number (perhaps between 1 and 10) and ask the player to guess what it is. After the player answers, check whether their matches the secret number or not. Finally, give the player feedback: if the two numbers match, then the sprite should say something like "congratulations!", otherwise it should say something else

  4. Hmmm, this guessing game is a bit too easy since our secret number is visible in the top left corner of the stage. If only there was a way to make the secret number actually secret!

    So far you have used global variables that all parts of the code can access. There are different types of variables called local variables (which Snap! calls scripting variables) which can only be accessed in specific parts of the code. If you've done the Row of Dollhouses project, then you've already seen script variables: The inputs to custom blocks only exist within the block and can't be used outside of them.

    Predict: what do you think this code will do?
  5. Build the code above and run it several times. What happened? You can follow the steps below to create new script variables.

    You can create multiple variables from the same script variable block by clicking the black arrow to the right of your variable name.

  6. Delete your variable secret number.

    Now add a script variable block to your code that creates a local variable called secret number that will be truly secret!

  7. Using the block that you used in the Row of Houses project, repeat asking the player to guess the secret number until they successfully guess it. When the player finally succeeds, then you can congratulate them.

  8. The game is a little too hard, since the only feedback the player gets is whether or not they player guessed the right number. Edit your code so that after the player makes a wrong guess, tell them if their guess is too big or too small.

  9. You can personalize this project just like with the Sprite Chase project, by changing the sprite costumes and the backgrounds.
  10. (Optional) Let's give the player a sprite and have the two sprites talk to each other! Create a new sprite by clicking and personalize the sprite.

    In the Sprite Chase project, you used the tell block to control the second sprite so they could have a conversation. Snap! gives us another powerful tool for having sprites interact with eachother: the broadcast and when_I_receive blocks. Consider the code below:
    If you are confused about what this code is doing, try making the left block in one of your sprites and make the right block in your other sprite, then run it. What happened?

    Using these blocks, have your two sprites take turns making secret numbers and asking each other to guess them.

Credits

Content adapted chiefly from pages 1 and 2 of BJC (June 2017) U2L1 "Starting a Number Guessing Game"