Friday, 5 January 2018

6) Using Variables


Using Variables
What are Variables?
Variables are the place holders to hold values. These values keep on changing during program execution. In Scratch, variables are represented with blocks shaped like elongated circles, uniquely labeled by the programmer. Variables can contain text (strings), numbers, or booleans (true/false values). eg to store the speed, variable is speed. In Scratch it will appear as
How to create Variables?
Following steps are followed to create a variable:
1. Variables block is clicked from Block Palette to create variables which is orange in colour, as shown below:

2. Click on Make a Variable from Variables block Palette. The Message box appears to enter the variable name as shown in screen. Type a variable name in this box.
 
Choose any one of the two options For all sprites or For this sprite only and clicked on OK. When the variable is created following blocks appeared in the Variables Block Palette:
  • To initialize the value of a variable 
  • To change the value of the variable
  • To show the variable
  • To hide the variable

Activity 1: Show a ball dropping on the ground using a variable speed.
Follow the steps to drop a ball:
1. Create a ball as a sprite on the stage as per your previous knowledge.
2. Define a variable speed and initialize it to 0.
3. Set the ball position initially.
4. Change the speed of ball step by step to make the ball fall (for that take negative step value).
5. Write the following code in Script Area
 
6. After clicking on green flag the ball moves down to the ground and the variable speed shown on the screen display a value which is incremented from 0 till it touches the ground.
 

Types of Variables:
The variables in scratch behaves as Global Variable or Private Variables. Global Variables are used by all sprites whereas Private Variables can be use by one sprite only.
 


To make a global variable select For all sprites in Variable name dialogue box and for Private Variable select For this sprite only.

A variable is also used for display as each variable gets its own Stage monitor. Stage monitors are used for
  • Displaying health
  • Displaying score
  • Showing completion of levels
  • Showing time
  • Displaying speed of movement
  • Adjustable Variables for interactive games


Activity 2: When a player is healthy the variable health is at maximum value. The health decreases when a player gets hurt and increases when he gets healed
Follow the steps for Activity 2:
1. Create three sprites one a player, a stone to hurt the player and a fruit to heal him.
 

2. Add following script to Sprite 2 to make it move on the screen :
 

3. For moving Sprite 3 , add the following script :
 

4. Now program the player to move right and left to escape from stone and eat bananas. The health gets increased by 1 if he eats banana and reduced by 1 when he is hit by the stone. When the health is 0 stop the activity and if health is 30 then say healthy enough. So the script is:


Use of random numbers as variables
Random numbers are generated in Scratch to simulate random events in the real world. They are also useful when creating challenges in games.

The Pick Random () to () block is used to generate any number between the two values given in the brackets. For example, if 1 and 10 is given, the output will be 1,2,3,4,5,6,7,8,9,10 including both end points,

Activity 3: Moving a sprite and it randomly jumps on the stage.
Follow the steps:
1. Choose the sprite and assign its position on screen.
2.Take a variables speed to generate a random speed for moving sprite and keep a record of speed. The speed is generated between 50 to 100.
3. Type the following script to jump it randomly.




 

No comments:

Post a Comment

1) Introduction to Email

Introduction to Email Introduction Communication, the transfer of information, has been the key element needed ...