Using Variables and Lists in Scratch (Part 2) | Tutorial

Alphabets Sounds Video

share us on:

In this lesson, we expanded our understanding of variables and lists in Scratch by creating a custom variable for controlling sprite brightness and implementing interactive sliders. We also learned how to create lists to store multiple pieces of information, such as user responses, and explored various applications for lists, including game inventories and calculations. The lesson encourages experimentation with these tools to enhance our Scratch projects.

Using Variables and Lists in Scratch (Part 2)

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!

Creating a Variable for Brightness

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!

Testing the Brightness Control

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!

Creating Opposite Brightness for Background

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!

Using Sliders for Other Controls

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.

Showing and Hiding Variables

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.

Passing Information with Variables and Lists

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.

Creating and Using Lists

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.

Fun with Lists

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.

Experiment and Create!

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?

  1. How did the concept of creating a custom variable for brightness enhance your understanding of controlling sprite properties in Scratch?
  2. What challenges did you face when implementing the brightness control for the sprite and how did you overcome them?
  3. Reflect on the process of creating opposite brightness for the background. How does this technique contribute to the visual dynamics of a Scratch project?
  4. In what other creative ways can you envision using sliders in your Scratch projects beyond controlling brightness?
  5. Discuss the importance of showing and hiding variables on the stage. How does this feature impact user interaction and interface design?
  6. How do you plan to utilize lists in your future Scratch projects, and what benefits do they offer over single variables?
  7. Reflect on a project idea where using both variables and lists could significantly enhance the functionality or user experience. What would that project look like?
  8. What new insights or skills have you gained from experimenting with variables and lists, and how do you plan to apply them in your programming journey?
  1. Create a Brightness Slider

    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!

  2. Opposite Brightness Challenge

    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!

  3. Design a Custom Control Panel

    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.

  4. Build a Superhero Profile List

    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!

  5. Invent a List-Based Game

    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.

VariablesVariables 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.

ListsLists 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.

BrightnessBrightness 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.

SpriteA 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.

SliderA 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.

ControlControl 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.

InformationInformation is data that is processed or stored by a computer. – The program collects information about how many times a button is clicked.

BackgroundThe 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.

SpeedSpeed 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.

VolumeVolume 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.

All Video Lessons

Login your account

Please login your account to get started.

Don't have an account?

Register your account

Please sign up your account to get started.

Already have an account?