Remember that you can find blocks easily by using the search bar
Activity: Info Category Variable Properties
We have previously worked with variables we created. A lot of the time, software developers need to interact with variables and values that were created elsewhere.
The info category in blocks contains a few variables (data properties) which we are allowed to update. These properties have to do with score, life, and time. We will take a quick look at how to use these as variables in our code.
In this activity, students are introduced to:
- Using the score and life properties
- Combining numeric values with math operators (*)
- The benefits of using score and life over other options
- The countdown block
- The pause block
Concept: Using score to keep track of button presses
The first example will be a simple one - simply counting the number of buttons pressed and keeping track of them as a score. We will discuss on any button pressed block in more detail later, but for now we just need to know that whatever is inside of the block will happen each time a button (A, up, and so on) is pressed.
Example #1: Counting button presses
- Review the code below
- Create a new project and name it “button count”
- Create the sample code and run the code
Notice that the score pops up in the top right corner as soon as it is used for the first time - that is one benefit of using the score variable to keep track of the points the player has earned. Next, we will add in code to in order to create a timer, to see some of the other benefits of the info blocks.
Student Task #1: 10 second button smash
- Start with the code saved as “button count” in the prior example
Create an on start block
Add in a start countdown 10 (s) block into the on start block
Run the code you created in task #1 a few times, and try to get different scores. Notice the benefits of using both the countdown and the change score by blocks - the countdown creates a timer that counts down to 0, and then ends the game at that point. The score keeps track of the value for you which is shown in the top right corner. When the game is over, the score maintains a high score automatically through multiple runs of the game.
Concept: Using life
Beyond score, another important value to keep track of is the players life total. This allows us to make games where players can be penalized for mistakes, without simply ending the game immediately when they make one.
Example #2: changing life totals
- Review the code below
- Create a new project and name it “do not touch the button”
- Create the sample code and run the code
This simple game gives the user a simple task - to not touch a button. If they do touch a button, the life will go down to 0, and they will lose. The game is a bit boring, but it does demonstrate a few of the benefits of using life: life total shows up in the corner as a number of hearts, and when you run out of the lives, the game will end.
Student Task #2: Touch the button 15 times
- Start with the code saved as “do not touch the button” in the prior example
- Modify the initial value of the life to be 15, instead of just 1
- Add in the change score by block used in the first task, and modify it to add 2 to the score each time a button is pressed
- Add in a countdown, and set it to run out after 2 seconds
Student Task #3: Estimate rate of presses
Overview
When a nurse needs to take a patient’s heart rate with their other vital signs, they do not want to (or have time to) sit around for a full minute to count how many beats there are. Instead, they can use another, quicker approach to estimate the patient’s heart rate - count how many heart beats there are over 15 seconds, and then multiply that value by 4 to get an estimate for the full minute. In this task, we will use the score to do the same thing, only with button presses. The pause block is new in this example, and pauses the code at that point for however many milliseconds it is asked to wait - in this case, we pass 6000 ms so that it pauses for 6 seconds, and then multiply by 10 to get an estimate for the full 60 seconds (one minute).
Coding
- Review the code below
- Create a new project and name it “button rate”
- Create and run the code
Change the say block so that after 6 seconds have passed it says the score. You may also want it to show a message explaining what the value represents
Use the x block to multiply the score by 10 and store it in a variable called
minuteScore
, so it will correspond to one minute’s worth of button presses- Make the sprite say the result stored in
minuteScore
. Edit the sprite so it looks better - Challenge: instead of outputting an exact estimate, give a range that the button presses will likely fall into - estimate this by making the low end of the range correspond to
(score - 1) * 10
, and the high end of the range correspond to(score + 1) * 10
. For example, if the score were 5, the output should be something along the lines of “between 40 and 60”
What did we learn?
- List one extra behavior you get for each of the three values we used in the info category (score, lives, and countdown).
- List one potential downside of using score over just using your own variables to keep track of the state of your game.