An intro to programming concepts with Scratch Session 3 of 10 sessions

advertisement
An intro to programming
concepts with Scratch
Session 3
of 10 sessions
Repetition and
variations
1
Session 3 goals
• Learn how to program with repetition
• Changing costumes
• Controlling execution (behavior) using
special keys
2
The sneezing cat script
• We repeat 20 times
Say “AAAHHHH”
for a short time
Then grow the
cat 5% bigger
3
Exercise: bring the cat size down to
normal
Repeat 20? times
Briefly say
“CCHHEEEW”
Then shrink the
cat’s size by 5%
Repeat N times
and repeat
forever are in the
Control menu
4
Program to compute the average
An optional piece: to omit this,
proceed to slide #18
5
A better average algorithm
( do blackboard work first)
•
•
•
•
Let’s average N numbers
Say 5 numbers { 12, 14, 15, 10, 10}
Variable sum will be used to add them
After we add all the numbers, we then
divide by N
• In the example above, sum =0, 12, 26, 41,
51, and finally 61
• Then average = 61/5 = 12.2
6
Exercise at your seat with paper
• Write SUM = 0
• Write N = ____
• Repeat with the class
get the NEXT number __ __ __ __ __
add it to the SUM
__ __ __ __ __
• Compute the average by dividing
the sum by N _____
7
Doing it in a Scratch program
• Ask the user how many numbers (N)
• Set our SUM to 0
• Repeat the following N times
ask the user for the NEXT number
reset SUM to SUM + NEXT
• Set AVERAGE to SUM / N
• Tell the user the AVERAGE
8
Making the script
• We need variables N, SUM, and NEXT
• We need a repeat N loop
• We need to ask the user for a number NEXT
inside the repeat loop
• We need to add NEXT to SUM each time
• We need to say the answer
9
AVG : making the variables
Make variables for:
N: how many to average
NEXT: the next number
SUM: the sum of the
numbers
AVERAGE: the average
of the numbers
10
AVG : asking for the numbers
Try this loop first and watch
Try this loop; watch the value of
NEXT change with your input.
11
Let’s write the program in 3
steps, and slowly execute it
Step 1: get N
Step 2: ask the N numbers and add them
up
Step 3: compute and say the average
12
AVG 1: get N from user
3 parts of our program: (1) ask user for N; (2) add up the
N numbers; (3) compute and say the average. Click on
each part to see it run!
13
AVG 2: add N numbers to SUM
We
added
and14
14to
Wejust
added
1212
and
and
ready
add 15.
sumare
and
areto
asking
for 15.
14
AVG 3: execute the 3rd part
Check all the
variables to see
if they are
correct for this
problem
15
Exercise: make a single script and
run it on new numbers
16
Save your average program
You can always use it again in the
future. That is what programs are for.
So, file it.
17
Exercise: have the cat do a flip by
rotating 20 degrees 18 times.
• Repeat from the Control menu: set the
number of repetitions to 18
• Rotate from the Motion menu: set the
size of each small rotation to 20 degrees
18
Changing costumes
Another way to change the
appearance of a sprite.
19
Making a new costume
1) Get bat2-a sprite from
the “sprite box”.
2) Click on sprite
3) Click on “Costumes”
4) Click on “Import”
5) Now get bat-2b sprite
from the sprite box
20
Use a loop to make the bat fly!
1) When and repeat from Control menu and 2) next costume from
Looks menu.
21
Changing coordinates
• We can randomly set the location of the bat
so it will “flutter”.
• Set X to a random number
• Set Y to a random number
• Move the bat to location (X,Y)
• Of course, the bat should remain on stage!
22
Using pick random
Click the stop sign at the stage upper right to stop the forever loop.
Do we need a wait in the loop? Try it to see the change in flying.
23
Controlling the bat’s direction
•
•
•
•
•
Let’s create multiple scripts.
Click space bar for random moves
Click right arrow to move right
Click left arrow to move left
The bat will behave differently depending
upon which key is typed!
• (So, could a gamer catch the bat?)
24
Multiple interactive bat flight
25
Exercise:
• Add a behavior (script) to move the bat up
with the up arrow.
• Add a behavior to move the bat down with
the down arrow.
26
Adding a background
• Click on the
Stage icon at
lower right
• Click on
Backgrounds
• Click on bat icon
(Sprite1)
• Click on Scripts
• FLY AGAIN!
27
Changing background
• Click on the stage icon
• New menu of operations appears
• Click each menu operation to see what it
does.
• How can we program changes of background?
• More later.
28
Exercise: can we make Cassie do
jumping jacks?
• Search the sprite locker to find different
costumes for Cassie.
• Make a script that makes her move by
contolling the changes in costumes.
29
End of Session 3: outcomes
Student should be able to write a program
controlled by a loop, and execute a script by
clicking special keys. The student should also
know how to use “costumes” to change the
appearance of a sprite.
30
Download