1 an intro to programming concepts with scratch session 3 of 10 sessions repetition and variations

30
1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

Upload: moses-simpson

Post on 17-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

1

An intro to programming concepts with Scratch

Session 3of 10 sessionsRepetition and

variations

Page 2: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

2

Session 3 goals

• Learn how to program with repetition• Changing costumes • Controlling execution (behavior) using

special keys

Page 3: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

3

The sneezing cat script

• We repeat 20 times

Say “AAAHHHH” for a short time Then grow the cat 5% bigger

Page 4: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

4

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

Page 5: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

5

Program to compute the average

An optional piece: to omit this, proceed to slide #18

Page 6: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

6

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

Page 7: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

7

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 _____

Page 8: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

8

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

Page 9: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

9

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

Page 10: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

10

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

Page 11: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

11

AVG : asking for the numbers

Try this loop first and watchTry this loop; watch the value of NEXT change with your input.

Page 12: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

12

Let’s write the program in 3 steps, and slowly execute it

Step 1: get NStep 2: ask the N numbers and add them

upStep 3: compute and say the average

Page 13: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

13

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!

Page 14: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

14

AVG 2: add N numbers to SUM

We just added 12 and 14 and are ready to add 15.

We added 12 and 14 to sum and are asking for 15.

Page 15: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

15

AVG 3: execute the 3rd part

Check all the variables to see if they are correct for this problem

Page 16: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

16

Exercise: make a single script and run it on new numbers

Page 17: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

17

Save your average program

You can always use it again in the future. That is what programs are for.

So, file it.

Page 18: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

18

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

Page 19: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

19

Changing costumes

Another way to change the appearance of a sprite.

Page 20: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

20

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

Page 21: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

21

Use a loop to make the bat fly!

1) When and repeat from Control menu and 2) next costume from Looks menu.

Page 22: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

22

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!

Page 23: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

23

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.

Page 24: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

24

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?)

Page 25: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

25

Multiple interactive bat flight

Page 26: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

26

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.

Page 27: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

27

Adding a background

• Click on the Stage icon at lower right

• Click on Backgrounds

• Click on bat icon (Sprite1)

• Click on Scripts• FLY AGAIN!

Page 28: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

28

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.

Page 29: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

29

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.

Page 30: 1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations

30

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.