In our last lesson, we learned about variables and lists in Scratch. These are tools that help us store information in our programs. Now, let’s explore how to use a custom variable and create a list with some fun examples!
First, let’s make a variable to control the brightness of a sprite. You might notice that there’s no built-in block for brightness in Scratch. So, we’ll create our own variable called “brightness.” Once we create it, we can see it on the stage by checking the box next to it in the block palette.
To make it interactive, we can turn it into a slider. Right-click on the variable’s display on the stage, and choose “slider.” Now, you can slide it to change the sprite’s brightness!
Let’s write a simple script to test this. When you click the green flag, set the “brightness” variable to zero. Then, make the program continuously set the sprite’s brightness effect to the value of the “brightness” variable. Try it out and see how it works!
What if we want the background to have the opposite brightness of the sprite? We can do that too! Create a black box as the background and write a script that sets the background’s brightness to 100 minus the sprite’s “brightness” value. Now, the sprite and background will have opposite brightness levels!
Sliders are useful for more than just brightness. You can use them to control speed, volume, and more. You can even set limits on the slider’s range by right-clicking on the variable’s display. For example, if you have a “spin” variable controlling how fast something spins, you might want to set a maximum limit so it doesn’t spin too fast.
You can also show or hide variables on the stage using the “show variable” and “hide variable” blocks. For instance, you might only want to show a volume slider when the mouse is in a certain position, keeping the screen neat until the user needs it.
You can pass information to a variable or list in many ways, like clicking a sprite, moving a slider, or using code blocks. Variables can only hold one piece of information at a time, so if you need to store more, you can create multiple variables or use a list.
To create a list, go to the Variables category and click “Make a List.” Lists are great for storing multiple pieces of information. For example, if you’re asking a user questions about their superhero name, power, and weakness, you can store each answer in a list using the “add thing to list” block.
Lists keep adding new information without deleting the old ones, making them longer until you decide to remove some items. You can access specific items in a list using the “item of list” block.
Lists can be used for many things, like creating custom messages, animations, or even storing game inventories. You can also use them for calculations, like finding the sum of numbers, or for recording sprite movements.
When you save your project, the list’s state is saved too. So, you don’t need to add items again unless you’ve deleted them.
Explore all the blocks available for lists and think about how you can use them in your projects. Experiment with adding, editing, or resetting data in a list. How will you use variables and lists to make your own awesome Scratch program?
Let’s start by creating a variable called “brightness” in Scratch. Make it visible on the stage and turn it into a slider by right-clicking on it. Experiment with sliding it back and forth to see how it affects your sprite’s brightness. Try setting different initial values and observe the changes!
Now, let’s make the background have the opposite brightness of your sprite. Create a script that sets the background’s brightness to 100 minus the sprite’s “brightness” value. Test it out and see how the sprite and background interact with each other!
Use sliders to control different aspects of your project, like speed or volume. Create a new variable for each control, and set limits on the sliders to prevent extreme values. Design a neat layout for your control panel and test how each slider affects your project.
Create a list to store information about your superhero character. Add items like name, power, and weakness to the list. Use the “add thing to list” block to input data, and display the list on the stage. Think about how you can use this information in a game or story!
Design a simple game that uses lists to keep track of scores or inventory. Use the “item of list” block to access specific items and update the list as the game progresses. Challenge yourself to create a fun and interactive experience using lists!
Here’s a sanitized version of the provided YouTube transcript:
—
In our previous video on variables and lists, we learned that variables and lists hold information you can use in your program. Now, let’s see some examples of using a custom variable, as well as creating and using a list.
Let’s create a variable that stores the brightness of the sprite. You’ll notice there’s no reporter block for brightness available in the block palette. I’m going to check the box next to my custom “brightness” variable in the block palette, so it will be shown on the stage. Now, I can right-click on its stage monitor and I’ll see a few options: “normal readout,” “large readout,” and “slider.” Let’s make this a slider so the user can control the sprite’s brightness.
To test this, I’m going to create a quick script that says when the green flag is clicked, the “brightness” variable should be set to zero. Then, forever, the program should set the brightness effect to the number in the variable “brightness.” Let’s test this.
What if we want to have the background’s brightness be the opposite of my sprite’s? I’m going to make a black box as my background and then create a script that says the background should forever set the brightness effect to 100 minus what is in the “brightness” variable. Let’s test this again. The brightness of the background and the sprite should be opposite.
Sliders can be handy for other things too, like speed or volume. If you want to set limits on the range of numbers available in the slider, you can right-click on the variable’s stage monitor to set a minimum and maximum. For instance, here’s a project where the slider controls the speed of the spin by setting the “spin” variable as the number of degrees to turn. When it spins too fast, I get dizzy, so I may want to set a lower maximum number for the slider.
You could play with code to show or hide the variables on the stage, via a stage monitor, based on something like mouse placement or reaching a certain point in a program. In this project, for example, by using the “show variable” and “hide variable” blocks, the volume slider is only shown when the mouse is in a certain position on the stage, so it doesn’t clutter the screen until the user wants to change it.
You can pass information to a variable or list by clicking or moving a sprite, adjusting a slider, via code blocks, and more. You can also pass information from one reporter block, variable, or list to another. This could be helpful because variables and reporter blocks can only hold one piece of information at a time. To store multiple pieces of information, you’ll need to create multiple variables to store different pieces of data. Then, use the “set variable to” block to pass information from one to another. Alternatively, you may want to consider storing the data in a list.
To create a list, select the Variables category from the block palette and click the “Make a List” box. This project, for example, is collecting the user’s answers to questions about their superhero name, power, and weakness. Each answer could be passed and stored in individual variables by setting those variables to “answer” after each related question is asked. Or, each answer could be added to a list by using the “add thing to list” block where “thing” is “answer.”
With a list, new information is simply added to the list, rather than previous data being overwritten. The list gets longer and longer until you tell it to delete some or all of the information. A list, also known as an “array” in other programming languages, consists of numbers paired with items. Information from lists can be requested based on their placement within the list using the “item of list” reporter block.
If you look at the “say” block in this example program, you can see how the information is being added to a sentence to make a customized message for each user based on their answers. There are a variety of simple and complex uses for lists, such as using a predefined list to determine animation, performing calculations like determining the sum of numbers in a list, storing inventories in games, encoding and decoding messages, recording multiple values like the movement of a sprite around the stage, and creating a system for users to type letters or display words on the stage.
These are just a few of the possibilities. The state of your project is preserved when you save it, meaning if there’s information in your list, you do not need to tell the program to add those same items to your list each time the program is run, unless you have deleted them from the list and they need to be re-added.
Look through all the blocks available for lists. What do you think each does? How might you use some of the blocks in a program to add, edit, or reset the data in a list? Experiment! How will you use variables and lists to create your own customized program?
—
This version maintains the instructional content while removing any informal language or unnecessary details.
Variables – Variables are like boxes in a computer program where you can store information that can change. – In our game, we used variables to keep track of the player’s score.
Lists – Lists are collections of items or values stored in a specific order in a program. – We created a list to store the names of all the players in the coding club.
Brightness – Brightness refers to how light or dark something appears on the screen. – We adjusted the brightness of the screen so that the colors in our animation looked just right.
Sprite – A sprite is a character or object in a computer program that can move and interact with other elements. – In the coding project, we programmed the sprite to jump over obstacles.
Slider – A slider is a tool in a program that lets you adjust a value by moving a handle along a track. – We used a slider to change the size of the text in our presentation.
Control – Control refers to the commands that manage how a program runs or how a sprite behaves. – We used control blocks to make the sprite stop when it reached the edge of the screen.
Information – Information is data that is processed or stored by a computer. – The program collects information about how many times a button is clicked.
Background – The background is the part of the screen that is behind the main objects or sprites. – We chose a beach scene for the background of our game.
Speed – Speed is how fast something moves or happens in a program. – We increased the speed of the car in the game to make it more challenging.
Volume – Volume is how loud or soft a sound is in a program. – We adjusted the volume of the music so it wouldn’t be too distracting during the game.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |