advanced scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 in this lesson you will learn:...

19
24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists. Advanced Scratch 3 Lesson m A Tejas and Jyoti are planning to write a scratch game called “Logic Bingo”. ey start discussing the same with Moz. Moz: Why do you call it “Logic Bingo”? Jyoti: In the game there are some conditions. e players have to find out these conditions using logical reasoning. ere are only four chances to win the game. So we named the game “Logic Bingo”. Moz: So what is this game? Tejas: For sports day in our school, each student can participate in two games from a list of 6 games. Jyoti: Rules for combinations of games are as follows: Participation in two team games is invalid. Participation in two individual participation games is valid. A team game participation and an individual participation game is valid. Tejas: We want to convert this into a scratch project. In this project students have to find the correct combination of games, that can be played on sports day, in 4 chances. Jyoti: We will not reveal the valid and invalid combinations to students. We display the names of the games. Moz: Good project. So how do you start? Team games Individual participation games Foot ball Basket ball Kho kho Athelitics Swimming Gymnastics

Upload: others

Post on 07-Jul-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

24

In this lesson you will learn:How to capture the input from the user.To write programs using variables and lists.

Advanced Scratch

3Lesson

mA

Tejas and Jyoti are planning to write a scratch game called “Logic Bingo”. They start discussing the same with Moz.Moz: Why do you call it “Logic Bingo”?Jyoti: In the game there are some conditions. The players have to find out these conditions using logical reasoning. There are only four chances to win the game. So we named the game “Logic Bingo”.Moz: So what is this game?

Tejas: For sports day in our school, each student can participate in two games from a list of 6 games.

Jyoti: Rules for combinations of games are as follows: Participation in two team games is invalid. •Participation in two individual participation games is valid. •A team game participation and an individual participation game is valid. •

Tejas: We want to convert this into a scratch project. In this project students have to find the correct combination of games, that can be played on sports day, in 4 chances. Jyoti: We will not reveal the valid and invalid combinations to students. We display the names of the games.Moz: Good project. So how do you start?

Team games Individual participation games

Foot ballBasket ballKho kho

AtheliticsSwimmingGymnastics

Page 2: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

25

Tejas: In the second step we do not know the instructions for i, ii, iii, and v. We want to explore these first.Moz: That is a good idea. Ok start with the first one (i). This is called receiving input from the keyboard.

Moz: To find out how to use this instruction, right click on the instruction to get help.

Tejas right clicks on the instruction and gets help for the instruction ask.

Recieving input from the keyboardMoz: Which block do you find instructions to sense a mouse click?Tejas (thinks): Sensing. Oh! So words entered using the keyboard is “sense the keyboard input”. Isn’t it?Moz: Correct. Jyoti: Let us check Sensing.

Jyoti: The main steps in our project are:Step 1: Display the games.Step 2: Repeat the following sequence four times.

Ask the student to enter the first game. Recieve the answer.i. Ask the student to enter the second game. Recieve the answer.ii. Compare the two games.iii. Display whether the combination is valid or invalid.iv. Keep score of valid combinations.v.

Step 3: Display the final score.

Look at this instruction

Info

To find out about the functionality of an instruction right click on the instruction. This gives you a help option. Help option displays how to use the instruction with an example.

Page 3: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

26

Moz: Build a small block using the instructions and execute.

Tejas: This is good. We can ask a question and also capture the answer entered.Jyoti: Let us enter another question.

Example ask block and its execution steps

Page 4: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

27

Creation of VariablesTejas: The input entered for the first question is lost.Jyoti: I wish the computer can save the answer somewhere.Moz: Sure it can. So what do you need? How do you solve such a problem in Maths?Tejas: In Maths, we solve such problems using a variable.Moz: Correct. The same logic is used in computers too. Check out the Variables block.

Assigning values to a variableJyoti: Can we store a word in this variable?Moz: Yes. When a word is saved in a variable, the variable is called a string variable. Letters, words or sentences are called strings in Computer language.Jyoti: I want to save answer in the string variable name. Moz: Check out the help for answer under the Sensing block.

Tejas and Jyoti create variables name and age.

Page 5: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

28

Moz: The instruction set allows you to save answer in a variable that you already created.

The variables are set to the answer entered by the user.

Variable operatorsTejas: Suppose the name typed is “Jyoti” then I want to say “Hello Jyoti”. How do I get this?Moz: That means you want to join two words and display it. Join is a string operator. Check out these instructions in Operators block.

Tejas: How do we compare strings? We will need it for our project.Moz: When you want to compare if two numbers are equal how do you check? For example:The lengths of three sides of a triangle are given. How will you determine if it is an isosceles triangle?

Jyoti checks help for string operator join and adds the instruction to the scratch block and executes.

Page 6: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

29

Jyoti: We have to compare the lengths as follows:If AB = BC or AB = CA then the triangle is called an isosceles triangle as two sides are equal.Moz: So what is the operator that you used to compare?Jyoti: ‘=’.Moz: You can compare strings for equality just like numbers. Where do you find the comparison instruction?Tejas: In Control block.Tejas and Jyoti write the following blocks to see how operators and control statements can be used.

Moz: Good. Explore the other string operators too. You may get some other ideas of comparison.Tejas and Jyoti explore the following string operators:

Script for the cat Sprite

Script for the football Sprite

Stage after execution of program

Concatenates (combines) strings.

Reports the number of letters in a string.

Reports the letter at the specified position in a string.

Page 7: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

30

Logic bingo by Tejas:Six sprites for the 6 games. Cat sprite to ask a question and capture the response.

Tejas: The game has a referee of the game who keeps asking questions and displays messages at each stage of the game. Player is the one who plays the game of Logic Bingo.Moz: Good. Have you decided on the variables that you will require for the game? Jyoti: Since the player enters two games of his or her choice to save the names of games, we need two variables, game1 and game2.Moz: Good. What else do you need?Tejas: We can decide if a game is valid or invalid by comparing the types of games. So each time the player enters a choice we should save the type of the game. Later on we can do the comparison and decide if the choices are valid or invalid. So we need two variables, team and individual.

Step 1: Click on Make a variable to create a new variable.

Step 2: Type in the name of the variable in the pop-up box and click OK .

Moz: Good. So next what do you do?Tejas: Import the sprites and position them on the stage.Jyoti: We have to also create the variables.Moz: Good. Summarize the steps.

Page 8: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

31

Step 3: The new variable game1 is created.

Import sprites and position on stage:Select sprites from the available sprites or paint the sprites required for the game.Six sprites for six games and cat sprite as referee of the game. One sprite which has instructions of the game.

Create the required variables:Four variables are required. Two to save the names of games and two for the choice of games.

How does the game start?1. Start the program.2. Hide the sprites and provide the instructions of playing the game.3. Hide the instructions and show the sprites.

Moz: What is the next step? How does the program start?Jyoti: Following is the start of the program.

Moz: How do you take responses from the player?Tejas: Referee asks a question. Player responds. The response is saved in game1.Jyoti: We have to also save the type of game. We can use broadcast. Moz Sprite can broadcast the name of the game. Then the corresponding game sprite will receive and save the type of game in choice1.Moz: Very good logic, Jyoti. Tejas: If the choice of game is team game, then choice1 = team and if the choice is individual type of game then choice1 = individual.Moz: Good. Jyoti: We can use the same sequence and logic for the second choice of game too.Moz: Correct. Go ahead and build the blocks for referee and game sprites.

Page 9: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

32

Cat Sprite block:Referee gives an instruction “Enter the name of the game you want to play on sports day”Player responds by entering the name of the game.Save the name of the game in the variable game1.Broadcast game1.

Game Sprite blocks:The game that has been chosen receives the broadcast.Type of game is also saved by the Sprite.Type of game is broadcast to referee Sprite.

Cat Sprite:Receives the broadcast and says the type of game so that the player knows type of game1.

Scripts for referee Sprite for choice1

Individual game Sprite Script to if choice1 is swimming

Team game Sprite Scripts if choice1 is football

Stage for choice1 of the player for the game Logic Bingo

Page 10: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

33

When 1. choice2 = individual type of gameIf choice1 = team game then valid. If choice1 is another individual game then valid.

So when choice2 is individual game: Individual game Sprite simply broadcasts correct to referee Sprite. No need of any comparison instructions.

When 2. choice2 = teamIf choice1 = individual then

Team game Sprite broadcasts correct to referee Sprite.If choice1 = team then

Team game Sprite broadcasts wrong to referee Sprite.

Scripts for referee Sprite for choice2 Stage of the player for choice2 response

Moz: Now that you have one choice, what should be your logic for the next one?Tejas: The block for referee sprite to ask and receive the response for choice2 will be the same as for choice2, excepting that response of player will be saved in variable game2. The broadcast message too will be different for each choice of game to distinguish from choice1.

Jyoti: Now we have to build the logic of comparing choice1 and choice2.

Page 11: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

34

Team game Sprite Scripts if choice2 is football

Referee Sprite scripts to display valid, invalid or duplicate choices

Final stage of the game Logic Bingo

Individual game sprite scripts if choice2 is swimming

Moz: What would the referee Sprite do now?Tejas: Referee Sprite simply says valid or invalid as per the signal that it recieves.Moz: Good. Suppose someone enters their choice1 again in choice2?Jyoti: We can handle that easily. Each of the games Sprites can compare with game1 and if it is same a broadcast “duplicate” can be sent to referee Sprite. Then the referee Sprite can give an appropriate message.

Moz: Have you noticed there is one more option under the Variables block: ?

Tejas: Is it something like the lists we make for purchasing things from the grocery store?Moz: Yes, it is similar to that.Jyoti: We will click on it and make a new list.

Page 12: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

35

Moz: Good. Go ahead and complete the game. I would like to play this game. Chin Chinaki...

Lesson

Outcome

After you have studied this lesson, you will

be able to:

Write programs using commands under

the Operators (like, Join) and Variables

(variable, list) blocks.

Get input from the user and use it in the

program.

Tejas and Jyoti explore the various commands for list and write the following program.

There are three main types of blocks in the Blocks Palette:Stack Blocks: These blocks have bumps on the bottom and/or notches on the top, such as . You can snap these blocks together into stacks. Some stack blocks have an input area inside them, where you can type a number (such as ) or choose an item from a pull-down menu (such as pop in the block). Some stack blocks, such as , have a C-shaped mouth where you can insert other stack blocks.

Hats: These blocks have rounded tops, such as . These blocks are placed at the tops of stacks. They wait for an event to happen, such as a key being pressed, then run the blocks underneath them.

Reporters: These blocks, such as and are designed to fit in the input area of other blocks. Reporters with rounded ends (such as ) report numbers or strings, and fit in blocks with rounded or rectangular holes (such as ). Reporters with pointed ends (such as ) report boolean values (true or false) and fit inside blocks with pointed or rectangular holes (such as or ).

Page 13: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

36

WORKSHEETSBook V Lesson 3

Gargi has written a program to capture the input from the user. She has created 1. a variable named Input. What will be the final value of this variable after the execution of the program?

The following is a small program. The last line of the program is missing. When 2. executed the user gave the name as Annie. If you want the sprite to say “Hello Annie”, what is the last line of the program?

Given is a small program which compares two strings. The user has entered 3. the name of two items he likes to eat. The following is the screen shot of the program. Can you tell what the Sprite will say at the end of this program?

Hello!a. User nameb. User’s friend’s namec. answerd.

Oh! You entered the same thinga. Cakes Cakesb. Enter Item 2c. Enter item 1d.

Page 14: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

37

In the following program there are three variables: Choice1, Choice2 and 4. Answer. When the program runs fully, what will be the final value of the variable: Answer?

Following is a program to compare numbers. What will the Sprite say when 5. the numbers entered by the user are 250 and 500. Circle the correct option

Here is a piece of program which calculates 6. the length of the name the user enters. But the commands are jumbled up. Can you rearrange the commands by numbering them.

Basketball, a. 0b. Footballc. 2d.

Choice1 is less than Choice2 a. 250 is less than 500b. Choice1 is greater than Choice2c. 500 is greater than 250d.

WORKSHEETSBook V Lesson 3

Page 15: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

38

WORKSHEETSBook V Lesson 3

Scratch blocks for background and two sprites are given below. Find out what 7. the project is about. The costumes of the background are also given below.Instruction blocks of background

Script of Sprite nextScript of Sprite restart

Costumes of background

__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Page 16: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

39

WORKSHEETSBook V Lesson 3

The stage of the game Collide which has Cat and Gobos as sprites, and the 8. scripts for the cat, yellow Gobos and the pink Gobos are given below.

Cat Script Script for yellow Gobo Script for pink Gobo

initializes the __________ of the sprite and the variable ______.i.

What is the Goal of the game?: ii. Help the Cat ____________ the mine field of Gobos with the _________.

What are the Rules of the game? iii. Collect _____ Gobos to earn points, Avoid _______ Gobos to lose points.

What happens when you earn points? iv. Maximize _________ while playing the game.

Page 17: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

40

Book V Lesson 3

Write scratch blocks for the aeroplane 1. Take offi. Landingii.

Open the following Scratch projects and do the activities.2. Bouncing Music Ballsi. : Use the arrow keys to move the small balls. When the small balls hit the big ball music is produced. Follow these steps to start the project:Scratch ---> Projects ---> Simulation ---> Bouncing Music BallsActivities to do:

Press H to read the help on how to play. •Use arrow keys and + and - keys on the keyboard and observed what happens.•

Hint: Use , , .

Page 18: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

41

Book V Lesson 3

Mouse Recorderii. : This projests lets you record and play back movements of the mouse. It uses lists to store x and y positions of the mouse. Follow these steps to start the project:Scratch ---> Projects ---> Interactive Art ---> Mouse RecorderActivities to do:

Press record button( ) move •the mouse around to record the movements of the cursor. Press stop ( ). Press play ( ) to see the recording.Explore the Scripts to see how list is •used in the program.

Piano Machineiii. : Balls bounce on the piano keys to make music. To start the project follow these steps:Scratch ---> Projects ---> Music and Dance ---> Piano MachineActivities to do:

Click and drag any piano keys •to a new position for different music. Click on the number icon to •change the number of balls in motion.Change the program and create •your own piano.

Explore!Explore what are boolean operators? And find 1. out the fumctions of commands under Operators block like , .

Page 19: Advanced Scratchvijaya/ssrvm/dokuwiki/media/... · 2010-05-15 · 24 In this lesson you will learn: How to capture the input from the user. To write programs using variables and lists

42

Teacher’sCornerThe purpose of this lesson is to teach students how to use the instruction blocks related to conditional statements, variables, lists, operators and keyboard input, available in Scratch. Students are already familiar with this programming language and understand that they can make the computer do something using this activity.

Begin by revising the functions of different instruction blocks already taught. You can ask them to write a small project to refresh their memory of what they already know. Tell the students that they will now learn to use additional instruction blocks to write a variety of interesting projects.

Start Scratch and open an existing project (e.g. Examples --->Speak up ---> Global warming) that includes sensing and variable instructions. At this time, do not show the Script. Ask them to observe the animation. Now show the Script area and ask them to observe if they find any new instruction blocks, and of what colour. Ask them to which instruction block set do they belong. Let them guess its functions, if they can. To do this, you can use the strategy of think pair share, where students can partner with their classmate and do this activity.

Now explain the functions of different sensing options using the example mentioned in the book. Right click on the different block and open the help screen and ask them to read their functions. Do it for a couple of instructions and ask the students to explore the remaining instruction blocks themselves. Similarly teach the functions of different variable options.Give a demonstration of the Script included in the lesson to show how to use the different instruction blocks.

Emphasize that it is important to plan and organise the different activities you want to do using Scratch. Ask the students to write the Script for the Scratch project in their notebook. Now ask them to exchange it with their partner and program using Scratch for each others activity.

Summarize the lesson and give the students activities to practice.

Further Reading:http://scratch.mit.edu/

Book VLesson 3