Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 14 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
14
Dung lượng
534,5 KB
Nội dung
Summer Computing Workshop Introduction to Variables Variables are used in every aspect of programming They are used to store data the programmer needs later in the program Let’s create a variable for the new sprite Call this variable “score” On the left you should see and blocks These are the two most used blocks when it comes to manipulating variables in Scratch Programmers use similar methods of setting the value to a specified number, or incrementing/decrementing the value Now we will look at a brief overview of this session Session Basic Overview Incrementing/Decrementing Variables – This function is useful when decreasing or increasing variables by a specified number (e.g the a player losing lives in a game or increasing a player’s score) Setting Variables – This function is used to set the variable’s value to a specified number (e.g setting the score to zero after the game is restarted or setting the players lives to max after the game is restarted) Using Variables – Programmers use variables to store data for later use Problems/Type differences – Similar to the issues related with input, different types of data may have unexpected outcomes Incrementing/Decrementing Variables For this examples we need a forever loop, an if conditional block, a change score by block, a change x by block, another sprite (total of 2), and another block we haven’t used before called This block is found in the sensing category and will be used in the predicate part of an if/else block to detect the collision of some other object (another sprite, an edge, etc) We also need to give the user control over the cat similar to the cat in the input session Use whatever type of single-character input you want, allow the user to move the cat towards the other sprite eventually allowing them to collide Now we will create a structure that increments the score only if the two sprites are touching Let’s construct this example (see example 5.1 on the Constructed Examples page at the end of this section) Notice what happens when we stop and restart the program, the score doesn’t change as though the game started over This is where the ability to set the variable comes in handy Setting Variables To demonstrate setting a variable, we will use the same script we just pieced together Drag a to the scripts area and put it above the forever loop but still a part of the Scratch script When you think the variable score will be set; before the next game starts, or as the next game starts? Run the script and see for yourself (see example 5.2 on the Constructed Examples page at the end of this section) If you notice, the setting of the variable doesn’t occur when the stop button is pressed, it occurs after the green flag is clicked again Now we will enter single stepping mode and watch the script as it runs in order to further establish this concept Using Number Variables There are many different ways programmers use variables throughout their programs Variables that contain numbers are often used in repeat blocks, move blocks, and change by blocks Numbers can also be stored for later use in the program For this example, we need to construct a script that asks the user how far they want the sprite to move, then sets that value to a variable called “move.” The sprite then moves right or left depending on the entered value Construct example (see example 5.3 on the Constructed Examples page at the end of this section) Using Word Variables Words can also be used and stored into variables Examples could be asking someone's name, or what their favorite color is, or if they would like to continue with the program The example we will construct will modify the previous script to also ask the user if they would like to continue, and if not, to enter the word “no.” This will require us to create a new variable and set it to the answer Everything needs to be in a repeat until loop and we only repeat if the answer variable is not equal to “no.” Ok, sounds difficult but it really isn’t too bad Let’s now construct this example and see how it works (see example 5.4 on the Constructed Examples page at the end of this section) If you stop the script, notice the variable still contains the word “no.” This means we must set the value of this variable to anything other than “no” immediately after the script is started Problems that could Arise In the program we have created, when the sprite asks “How far would you like me to move?” try entering a word or letter instead of a number Again the sprite doesn’t move Just as discussed in the input session, Scratch doesn’t allow the programmer to specify different types of data If you notice, the value of the variable “move” is whatever you entered In most other programming languages, programmers specify what type of content the variable will contain If it were possible in Scratch, it would be good practice to specify what type of data the variable was going to contain later in the program This is so we don’t have incompatible variable types Amazing Example Now we will construct a fairly difficult script using only one sprite The script demonstrates how variables can be used throughout a script The goal is to have the sprite draw a grid using nested loops and the pen feature that was discussed in session First, we want to clear what has been drawn and point the sprite in the 90 degree direction Ask for the size of each box inside the grid, assign that value to a variable called box size Ask for the width of the grid or how many blocks wide and assign it to a variable called width Ask for the height of the grid which is how many boxes high the grid will be and assign the value to a variable called height We will need repeat loops Now let’s construct our example (see example 5.5 on the Constructed Examples page at the end of this section) Constructed Examples Example 5.1 Example 5.2 Example 5.3 Example 5.4 Constructed Examples Example 5.5 Projects Create a Scratch script that averages multiple numbers You can either ask the user to enter how many numbers they would like to average and use a repeat loop, or use a repeat until loop and wait for the user to enter “done.” Create a Scratch script that asks the user to enter numbers, stores them as variables A and B, then switches theses numbers Session Questions What is useful about loops? Once variables contain a value, can that value be changed? Can a variable contain a number, then later in the program, contain words? If so, could this be a problem? There are two loops, the outer executes times, the inner executes times Inside the inner loop, there is a block of code that makes a sprite say “hello world.” How many times will the sprite say “hello world?” Can a variable be used any place a number can be used? ... example 5. 5 on the Constructed Examples page at the end of this section) Constructed Examples Example 5. 1 Example 5. 2 Example 5. 3 Example 5. 4 Constructed Examples Example 5. 5 Projects Create a Scratch. .. in the input session, Scratch doesn’t allow the programmer to specify different types of data If you notice, the value of the variable “move” is whatever you entered In most other programming. .. variables in Scratch Programmers use similar methods of setting the value to a specified number, or incrementing/decrementing the value Now we will look at a brief overview of this session Session