Session 2

advertisement
Summer Computing Workshop
PROGRAMMING WITH SCRATCH
Session 2
Input in Scratch
 Multi-Character input - This is used when the user is
prompted to enter a number or word.
 Problems with multi-character input - Possible problem
with type differences
 Single-character input – This is mainly used to key
specific events (an example may be moving a sprite when
an arrow key is pressed)
Asking Questions
 Now we will discuss how to ask the user for a word or number
which can then be used in the script. We will do this by having
the sprite ask the user his/her name and then replying using
the name.
 Click sensing and drag an
block to the center
scripts menu. We want the program to start when the green
flag is clicked so do what is necessary. If it isn’t already
written, click in the small text field within the block and type
the question “What’s your name”.
 Now drag a
block and join it to the bottom of the
block
Asking Questions
 Since we want the sprite to answer with the name and a
short sentence, we need the special block
is located in the operators section.
which
 The goal is to place something such as “Great to meet
you” into the left text field. Drag the answer block (found
in sensing) to the right text field. This joining is called
concatenation.
 We want the join block inside of the say block so now
let’s construct the example. (see example 2.1 on the
Constructed Examples page at the end of this section)
Using number input
 Lets do a small project that uses a sprite to ask the user how
many steps he/she wants the sprite to move (refer to example 2.1
if you need help)
 Now that we have a script that does this, what do you think will
happen if we entered a word instead of a number? Try it and see
if you were correct.
 The sprite doesn’t move!! This brings up the topic of different
data types. In Java, C++, and other programming languages, the
programmer must specify what type of data it is before the data
can be used. In Scratch the two data types are words and
numbers. Other programming languages may not properly run if
data types are incompatible. Scratch runs the program but
doesn’t perform the action if the type is incompatible. It is
difficult for the sprite to move “word” steps.
Movement blocks
 To enable the sprite to move by using single character




input, we must first learn some of the most used motion
blocks
Drag a
and a
to the center section
By using these blocks we can move the sprite any
direction on the x and y axis.
Change x moves the sprite along the x axis, a negative
number left , a positive number moving it right
Change y moves the sprite along the y axis, a negative
number moving it down, a positive number moving it up
Single Character Input
 Now we have movement blocks on the screen, we only have
two directions so we must duplicate both blocks so our sprite
can move all four directions (up, down, left, right)
 After we have four blocks on the screen, change the number in
one of the change x blocks to negative and the same for a
change y block.
 Now drag a
block to the center and duplicate it
three times so we have a total of four
 Now we will construct the example (see example 2.2 on the
Constructed Examples page at the end of this section)
Single Character Input

Now we will do an example using another simple new concept. The special block
we will be using allows the user to change the direction the sprite is facing.

First choose a sprite (preferably one that has a top down view)

Drag four

Since we have 4 directions to move, we must have 4 key pressed blocks and 4
move blocks

All of these will work together to move the sprite and face the sprite in the
direction the user entered

Construct example (see example 2.3 on the Constructed Examples page at the
end of this section)
blocks to the center window
Constructed Examples
Example 2.1
Example 2.3
Example 2.2
Session 2 Project

Create a small animation using single and multi- character inputs. Use
single-character input to key events or move a sprite about the screen. Use
multi-character input to ask for a question , the Sprite must respond to the
question using the answer to the question.
Session 2 Questions
1. What are two different types of input?
2. Scripts are made up of one or more _____?
3. Explain the flow of execution in a script/method.
4. For what might we use multi-character input?
5. For what might we use single-character input?
Download