GHOST STORY In this program we will use an algorithm to make a story using Looks command blocks simple animation sound effects and loops The story will follow a cat in a scary building who gets frightened away by a ghost STEP 1 SET THE SCENE a Click the Stage icon in the Sprites pane a Click the Choose backdrop from library icon a Click the hallway outdoors image or choose your own backdrop a Click OK Sprites Sprite1 New backdrop STEP 2 ADD SOUND a Click the cat sprite in the Sprites pane a Click the Sounds tab a Click the Choose sound from library icon a Click bell toll a Click OK bell toll a You will need to repeat this to add three more sounds door creak scream-female and laugh-male2 AVOID THE BUGS Bugs are errors or mistakes in your code that stop your program from working properly Looking for those errors is called debugging Make sure you enter your code very carefully If things aren’t working properly use the tips on page 31 to help you 4 CUSTOMIZE Change what the cat and the ghost are saying Try adding another character or sprite to the story Try starting another story with different sprites What will your new story be about What will happen
STEP 3 SAY HI a In the Scripts Area build the following code 100 180 when clicked show go to x y say Hi I’m Scratch the cat for 2 secs play sound bell toll say I can hear someone coming for 2 secs play sound door creak wait 3 secs play sound scream-female until done say HELP repeat 80 When the green flag is clicked run this code Make sure the cat sprite is visible Go to the bottom-left of the screen Show a message for 2 seconds Play the sound effect of a bell Show a message for 2 seconds Play the sound effect of a door creaking Wait for 3 seconds don’t do anything Make the cat scream Show another message Start a loop that repeats the next two lines 80 times You’ll learn more about coordinates on page 8 Loops are a way to make several lines of code repeat You’ll find out more about them on pages 6 and 7 move 5 steps next costume hide Move the cat 5 steps forward Show its legs in different positions so it seems to walk Keep repeating Hide the cat it has run away STEP 4 ADD A GHOST a We need another sprite for our story Click Choose sprite from library a Click Ghost1 a Click OK key concepts ALGORITHM l To make a program work properly we need to figure out all the steps we need to take and put them in the right order l An algorithm is similar to a plan that helps us get all these steps correct STEP 5 MAKE IT SAY BOO 225 when clicked go to x y 32 hide set size to 100 wait 6 secs set ghost effect to 50 show say Boo for 3 secs repeat 100 move 2 steps change size by 1 play sound laugh-male2 When the green flag is clicked run this code Move the ghost to the top-left Hide the ghost for now Make sure the ghost is normal size 100 Wait for 6 seconds while the cat shows messages Make the ghost partly transparent Show the ghost Show a message for 3 seconds Start a loop that will repeat the next lines 100 times Move ghost 2 steps forward Make it 1 bigger Keep repeating Play a sound effect laugh-male2 Now press the green flag to check your code 5
LOOPS If you want your code to do something over and over again it’s much quicker to use a loop The repeat loop in Scratch allows you to repeat some code a specific number of times To practice let’s draw a simple square STEP 1 DRAW A SQUARE when clicked pen down repeat 4 When the green flag is clicked Put the pen down to draw when the cat sprite moves Start a loop that will repeat 4 times one for each side move 100 steps turn 90 degrees Move the cat sprite forward 100 steps Turn the cat sprite right 90 degrees Keep repeating a When you run your code by clicking the green flag you should see the cat sprite draw a square on the screen Try to change your code to make the square bigger Now make it smaller Can you add more code to draw another square below it STEP 2 LOTS OF SQUARES a If you want to draw lots of squares you could make another loop that fits around the first loop After each square is drawn it will turn the cat sprite 15 degrees before drawing the next one Change your code so it looks like this when clicked set size to 50 clear pen down repeat 24 repeat 4 When the green flag is clicked Make the cat sprite 50 of its normal size so it doesn’t get in the way Clear the screen to remove any previous patterns Put the pen down to draw Start a loop that will repeat 24 times one for each square Start a loop that will repeat 4 times one for each side move 50 steps turn 90 degrees Move the cat sprite forward 50 steps Turn the cat sprite right 90 degrees turn 15 degrees End of the loop go back to repeat Turn right 15 degrees so the next square is in a different place Keep repeating 6
When you run your code it will draw a pattern like this The pattern has 24 squares each at a slightly different angle CHANGING COLOR a Look back through the code and think about what each line does Try changing your code so it draws more squares To prevent the squares from overlapping you will need to change the angle turned after each one There are 360 degrees in a complete turn So with 24 squares the sprite has turned 15 degrees 360 24 15 For 36 squares the sprite must turn 10 degrees 360 36 10 In the Pen blocks find the set pen color block Drag it into your code to change the color of your pattern set pen color to To choose a new color click this square then click on a new color somewhere on the screen STEP 3 LOTS OF LOOPS a Change your code by adding another loop below the first one Use the set pen color block to choose a different color for each part of the pattern After your code runs you should see a double pattern like this when clicked set size to 50 clear pen down set pen color to repeat 36 repeat 4 When the green flag is clicked Choose orange move 100 steps turn 90 degrees Draw 36 large squares turn 10 degrees set pen color to repeat 24 repeat 4 Choose red CUSTOMIZE Think about how you could add more loops to create a third smaller pattern in the middle What color will you choose move 65 steps turn 90 degrees turn 15 degrees Draw 24 medium squares key concept THE LOOP In coding when we want to repeat some code we use a loop Now press the green flag to check your code 7