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

## Presentation on theme: "1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations."— Presentation transcript:

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

3 The sneezing cat script We repeat 20 times Say “AAAHHHH” for a short time Then grow the cat 5% bigger

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

5 Program to compute the average An optional piece: to omit this, proceed to slide #18

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

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 _____

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

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

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

11 AVG : asking for the numbers Try this loop first and watch Try this loop; watch the value of NEXT change with your input.

12 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

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!

15 AVG 3: execute the 3 rd part Check all the variables to see if they are correct for this problem

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

17 Save your average program You can always use it again in the future. That is what programs are for. So, file it.

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

19 Changing costumes Another way to change the appea rance of a sprite.

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

21 Use a loop to make the bat fly! 1) When and repeat from Control menu and 2) next costume from Looks menu.

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!

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.

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

25 Multiple interactive bat flight

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.

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!

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.

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.

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.

Download ppt "1 An intro to programming concepts with Scratch Session 3 of 10 sessions Repetition and variations."

Similar presentations