For background information and how to get started with Snap! (formerly known as BYOB), click here for lesson 1. I’m using BYOB version 3.1.1 for my examples, but you can follow along with either version linked below.
- Snap! (BYOB): The Drag-and-Drop Programming Language for Beginners: Lesson 1
- Snap! (BYOB): Lesson 2 – Making a Custom Block and Introduction to Costumes
My experience with Snap! made learning C++ much easier. If you can wrap your mind around the logic for this game, you’re already thinking like a programmer. I always begin by asking myself what the program requires. Is there something that needs to change or something that needs counted? Those are my variables. Is there a decision to be made? That might be an if/else statement. You always want to think through the logic before jumping in.
For this exercise, I’m using blocks from the following categories:
- Looks (Purple)
- Control (Brown)
- Sensing (Blue)
- Operators (Green)
- Variables (Orange)
After this tutorial, you should be able to create a number guessing game. This information easily transfers to coin flips or similar simple games.
This program requires 4 variables:
Note: Your variables can have different names but they should be clear in their purpose and follow thisCaseFormat.
Click Make a variable, one-by-one enter variable names in the pop-up box.
The checkmarks display our variable names on the stage. You won’t display them during gameplay, but they should be up for “debugging” before the game is ready.
Under Control, grab the repeat until __ block.
Under Operators, grab the equals block.
Place the equals block after repeat until.
Under Variables, grab the correctGuesses variable you created and place it inside the equals block. Manually enter a number in the right-hand side of the equals block.
This is the main loop of the program. It’s pretty straightforward – the program loops whatever is inside the block until the player has made 3 correct guesses. This is an idea you’ll run into many times in programming.
Jumping out of the loop for a moment, under Variables, grab the set __ to __ block. By default, this will show one of the variables. Click the dropdown menu to bring up options. Set totalGuesses and correctGuesses to 0. Repeat the same steps for randomNumber but place it inside the loop.
Under Operators, grab the pick random __ to __ block. Manually enter a number range.
Under Sensing, grab the ask ____ and wait block. This block makes the Sprite ask the user a question then waits for a response. Manually enter the question or comment.
Under Variables, grab another set __ to __ block. Set the first option to ourGuess and grab the answer block under Sensing for the right-hand side.
Under Control, grab the if/else block. If/else statements are common in programming so get familiar with them. Looking at the program so far, a random number is picked, the user makes a guess, then something needs to happen if it is correct, or else something needs to happen if it’s incorrect. First, let’s build a “check if it’s correct” block.
Under Operators, grab an equals block and place it to the right of if. Under Variables, grab ourGuess and randomNumber, and place them inside the equals block.
Under Looks, grab the say __ for __ secs block. Manually enter the response for a correct guess and amount of time you want it displayed.
Under Variables, grab the change __ by __ block. Set the first part to correctGuesses and right-hand side to 1. Notice what is happening in the if part of this statement. The guess is compared to the random number, if that is correct, a statement is output, then correct guesses increases by one.
If the guess is incorrect, we only need to display a message saying it’s wrong. Nothing changes on the counts because we’re not counting incorrect guesses. But we are counting total guesses, so that variable is placed after the if/else statement (because we want it to happen whether the guess was right or wrong) but inside the repeat loop.
Under Looks, grab the say __ for __ secs block. Manually enter a message. Under Variables, grab the change __ by __ block. Select totalGuesses and increase it by 1.
Jumping out of the repeat loop, I’m going to add blocks that relay the total number of guesses to the user. Under Looks, grab the say __ for __ secs block. Don’t worry about changing the text yet.
The simple say block has the one text box but this program requires multiple boxes since it needs to output both text and the total guess count. Under Operators, grab the join __ __ block, place it inside the say block, grab a second join block, and place it inside the first join block. This creates 3 boxes of changeable output.
Now the boxes can be customized. The first and last boxes inside the join blocks should have text. The middle box should pass through totalGuesses. Under Variables, grab another totalGuesses block.
Below is the finished program. From top to bottom: totalGuesses and correctGuesses are set to 0. The repeat block starts and continues looping through every step inside until the criteria is met, in this case, that’s correctGuesses equaling 3.
Still inside the loop, a randomNumber is chosen between 1 to 5 inclusive. The user is asked for input and the program waits for an answer. That answer is passed on to ourGuess. Then we jump into the if/else statement. If ourGuess matches randomNumber, a message is displayed and the correctGuesses count goes up by 1. Else, a message displays and no count is changed.
We come out of the if/else statement, remaining in the repeat loop, and totalGuesses increases by 1. The program runs again, starting from the repeat until line. Once 3 correctGuesses are counted, we come out of the repeat loop where a message is displayed and the totalGuesses is relayed back to the user.
Run the program with the variables displayed on the stage. Test the program with correct and incorrect answers. Watch the count to see if everything works.
Unclick next to the variables you created under Variables to remove the boxes from the stage.
The game is more challenging when you can’t see the answers. 🙂