Workbook Version 3 Created by G. Mullin and D. Carty
Introduction... 3 Task 1. Load Scratch... 3 Task 2. Get familiar with the Scratch Interface... 3 Task 3. Changing the name of a Sprite... 5 Task 4. Costumes... 6 Task 5. Getting Instructions in the Correct Order... 7 Task 6. Moving using the Arrow Keys... 9 Task 7 Changing Costumes... 14 Task 8 Adding a New Sprite... 15 Task 9 Repeating Instructions using a loop... 17 Task 10 Adding a Background... 23 Task 11 Duplicating a Sprite... 27 Task 12 The Forever Loop... 28 Task 13 Monkey Game... 32 Task 14 Controlling the Monkey with the Mouse... 33 Task 15 Programming the Bee... 34 Task 15 IF Statement... 35 Task 16 Touching the Bananas... 36 Task 17 Getting Points... 37 Task 18 Adding a Timer... 38 Created by G. Mullin and D. Carty 2
Introduction Scratch is a new programming language that lets you create your own interactive stories and computer games. Task 1. Load Scratch Your teacher will show you how to load the Scratch software. Your screen should look like this: Task 2. Get familiar with the Scratch Interface Look at the diagram on the following page which shows the Scratch Interface. Each part of the interface is labelled, and it is important that you get to know their proper names. Complete Exercise 1 in your exercise booklet. Created by G. Mullin and D. Carty 3
Created by G. Mullin and D. Carty 4
Sprites Every character or object in Scratch is called a Sprite. When you open Scratch, there is already one Sprite in the project the cat. Currently, the Sprite is called Sprite1. This is too vague. It is VERY important to give meaningful names to Sprites we will change this Sprite s name to Cat. When we begin working with several Sprites and they are called Sprite1, Sprite2, Sprite3 etc, you will quickly forget which one is which. If you give your Sprite s suitable names immediately, it is much easier to work with them. Task 3. Changing the name of a Sprite On the Sprite Information area, change Sprite1 to Cat. Created by G. Mullin and D. Carty 5
Task 4. Costumes We are now going to change how our Sprite looks. One way we can do this is by changing it s Costume. Each Sprite may have many Costumes. To find out how many Costumes our Cat has, click the Costumes tab as shown: You should now see that this Cat has 2 Costumes: You should notice that the Costumes are slightly different. We use Costumes to make it look like our Sprite is moving. We will use this later in Task For now, switch back to the Scripts tab: It is important to save your work regularly. 1. From the file menu, select Save As; 2. Click Scratch Project > then choose H:\ 3. Save your file as Task 4 in the new filename box 4. Save to your own Scratch folder. Created by G. Mullin and D. Carty 6
Task 5. Getting Instructions in the Correct Order It is VERY important to get instructions in the correct order. The computer has no common sense and will follow your instructions precisely. When getting ready for school today, which of these two instruction sets did you follow? A B 1. Put socks on 1. Put shoes on 2. Put shoes on 2. Put socks on If you followed the instructions in box B, you might look a little silly at the moment!!! This is why it is important to make sure that the instruction blocks are in the correct order otherwise, we might not get the results we want. Complete Exercise 2 in your exercise booklet. Created by G. Mullin and D. Carty 7
Task 5a Read and Understand Coordinates Imagine that your Sprite (the Cat) is in the middle of a graph, like below. The graph has an X axis (left to right), and a Y axis (Up and down). 100 Y X -100 100-100 To get our Cat to move to the right, we must increase it s X value. Similarly, to move to the left, we must decrease it s X value. For our Cat to move up, we must increase it s Y value. To move the Cat down, we must decrease it s Y value. Complete Exercise 3 of your exercise booklet. Created by G. Mullin and D. Carty 8
Task 6. Moving using the Arrow Keys If you have played computer games, you will know that we often use the arrow keys to control our character. We are now going to control our Cat s movement using the arrow keys. To do this, we must give our cat Instructions. These come in the form of blocks. Under the Motion tab, drag the change x by 10 block into the Scripts area: Double click the change x by 10 block in the Scripts area. You will see that the Cat moves 10 steps to the right. Try changing the number to 100, then double click the block. You will see that the Cat moves further to the right when the number is increased. For now, lets change it back to 10. We want the Cat to move to the right when we press the right arrow key. To do this, follow the steps below: Click the Control tab. Created by G. Mullin and D. Carty 9
Drag the When space- key pressed block into the scripts area, and snap it on top of the change X by 10 block When the white line appears (above the change X by 10), this means you can snap the blocks together, just like lego bricks. At the moment, when the space key is pressed, the Cat will move 10 steps to the right. We want to change this to the right arrow key: Click on the down arrow next to the word space and select right arrow. You should now have this: This means we now have our first Script. A script is a group of instructions which perform a specific task, in this case, moving the Cat to the right when the right arrow key is pressed. A Sprite can have many Scripts, which mean that it can do lots of different things. We now have to create a similar script for moving left when the left arrow key is pressed. Created by G. Mullin and D. Carty 10
Drag another When space- key pressed block into the Scripts area. Click on the down arrow next to the word space and select left arrow. We know that the change x by 10 block moves our Cat 10 steps to the right. When we increase the number of steps, the Cat moves further to the right. To get our Cat to move to the left, we use a negative number of steps. Click the Motion tab. Drag another change X by 10 block underneath the When left arrow key pressed block. Change the number 10 to -10. The Script should now look like this: Save your work as Task 6. Created by G. Mullin and D. Carty 11
You should now be able to control your Cat using the left and right arrow keys Go ahead! We will now use the same technique to move the cat up and down. From the motion tab, drag the change Y by 10 block into the Scripts area. Now from the Control tab, drag in the when space- key pressed block and snap it on top: Now change space- to up arrow- Created by G. Mullin and D. Carty 12
You should now be able to create a Script for moving down using the down arrow key: Create a new script as shown Remember to change the value to -10. Save your work as Task 6a Now complete Exercise 4 of your booklet. Created by G. Mullin and D. Carty 13
Task 7 Changing Costumes In Task 4, we learned that Sprites can have different costumes that change the way they look. We will use Costumes to make our Cat Walk. Open your Task 6a project. From the Looks tab, drag the next costume block and snap it under change X by 10. Repeat this for the other 3 Scripts: Created by G. Mullin and D. Carty 14
Now when you press and hold the arrow keys, you should see the cat walk! When an arrow key is pressed, he moves and changes costume. As we know, one Sprite can have many Costumes. Our Cat has two Costumes, but we could add more if we wanted. Task 8 Adding a New Sprite To add a new sprite to your project, click the New Sprite button: You will see the following window appear. There are several folders which contain lots of Sprites. Double click the people folder. Scroll down until you find boy4-walking-a. Click OK Created by G. Mullin and D. Carty 15
You should now see that the boy has been added to your project, but he has been called Sprite1 Rename him Boy. (If you are unsure, look back to Task 3) We are going to create Costumes for our Boy Sprite, this will make his actions seem more realistic. Click on the Boy Sprite from the Sprite List Click on the Costumes Tab Click on Import We are now going find the costume called boy4-walking-b and click OK Save your work as Task 8 Created by G. Mullin and D. Carty 16
Task 9 Repeating Instructions using a loop Computers are very good at doing the same thing over and over again. We are now going to see how we can use a loop to get the computer to repeat our scripts over and over again. There are several types of loop The first one we will look at is called a fixed loop. We are going to make our Cat do a dance! Open Task 6a again which you saved earlier. You should have control over the Cat using the arrow keys. From the Motion tab, drag in the Move 10 steps block Created by G. Mullin and D. Carty 17
Now from the Looks tab, drag in the Next Costume block and snap it underneath Move 10 Steps. We need some music for our Cat to dance to Click the Sound tab and drag in the Play drum block as shown. Change the drum to number 36 Bass Drum 1. Created by G. Mullin and D. Carty 18
Now repeat these actions until you have a Script which looks like the one below: Notice the second Move block is showing Negative 10 steps, and the second Play Drum block is playing drum 57. Now double click this Script. You will see that the Cat does a very short dance! For the Cat to do a proper dance, he needs to repeat these instruction blocks over and over again. By using a Fixed Loop, we can decide how many times we want him to repeat these actions. From the Control tab, drag the Repeat block to the scripts area on its own. Created by G. Mullin and D. Carty 19
Now drag the Script you just made inside the mouth of the repeat block you should see the white line appear. Created by G. Mullin and D. Carty 20
You should now have the following: Change the repeat number from 10 to 15 This tells the computer to repeat these actions 15 times and then stop. Now double click this Script and your Cat should do a dance! Now lets see if we can get the Cat to do this dance each time the spacebar is pressed. Created by G. Mullin and D. Carty 21
From the control tab, drag the When space- key pressed block on top, like so: Now you have complete control over the cat. You can move him in any direction using the arrow keys, and you can get him to dance when you press the spacebar! Save your work as Task9 Created by G. Mullin and D. Carty 22
Task 10 Adding a Background So far, our Cat has been moving around a boring white background. We can fix that now by adding a suitable background to the stage area. Just like a Sprite has Costumes, the Stage has Backgrounds. Open Task9 if you haven t already done so. Click the Stage icon in the Sprite List area: Now click the Backgrounds tab and click Paint : The Paint Editor window will appear. Created by G. Mullin and D. Carty 23
From the Backgrounds window, select Import. The Import Image window appears Double click the Indoors folder. A selection of backgrounds will appear. Created by G. Mullin and D. Carty 24
From the selection of Backgrounds, choose Spotlight Stage, then OK. The Paint Editor window has now updated showing the image you selected. Click OK. Created by G. Mullin and D. Carty 25
You will now see the Background has been added to your project, and our Cat now has a stage on which to perform! Select the Cat from the Sprite List area and press the spacebar the Cat should now be dancing on stage! Created by G. Mullin and D. Carty 26
Task 11 Duplicating a Sprite Once we have created a Sprite, we can make copies of it very easily. Right click the cat and select Duplicate. You will see a second Cat appear, but he has been named Sprite1 automatically. Rename him Cat2. Now position Cat2 to the left of the first Cat. Created by G. Mullin and D. Carty 27
Cat2 is a exact copy of Cat. This means that he has all the same Scripts, so when you press spacebar- you will see both Cats dance together! You can duplicate Sprites as many times as you want. Save this as Task11 Task 12 The Forever Loop Another very important loop in Scratch is the Forever loop. This does exactly what you might think any instructions placed inside this loop will be repeated over and over again forever. (Well, that is until the program is stopped!) Open Task11 You should see the 2 cats and the stage you created previously. Make sure that Cat is selected in the Sprite List area: Created by G. Mullin and D. Carty 28
Now in the Scripts area, we are going to change the repeat loop to a forever loop. First, you must click and hold the Move 10 steps block at the top, and drag this outside of the loop: Now we must separate the repeat 15 block so that we can delete it. Drag it away so it is on its own, then right click delete. Created by G. Mullin and D. Carty 29
Now, drag in the Forever block and snap it to When space key pressed. You can now drag the script inside the mouth of the forever block: Created by G. Mullin and D. Carty 30
Now press spacebar you should notice that Cat will now dance forever, but Cat2 stops after 15 loops. To stop Cat dancing, click the stop button at the top right. Save this as Task12 Extension task 1 Now try creating a small animation for yourself. You should: Start a new project and delete the cat. Find the Cassy sprites Change the costumes and try to make Cassy look as if she walks halfway across the screen and then goes into a dance. Use some music too in your animation. Add a background if you have time. Created by G. Mullin and D. Carty 31
Task 13 Monkey Game Start a new Project. Delete the Cat (right click delete) Add a new Sprite monkey1 (from the Animals folder) (if you forgot how to add a new Sprite, look at Task 8) Rename this Sprite call him Monkey. Just now, he is way too big we have to make him smaller. Click the Shrink Sprite button: Now click the monkey a few times you will see him get smaller. We need to add two more Sprites to this project a bee and a bunch of bananas. Find these Sprites and add them to the project: Shrink them down to a reasonable size and rename them Bee and Bananas. Created by G. Mullin and D. Carty 32
Task 14 Controlling the Monkey with the Mouse Double click the Monkey Sprite. We want the Monkey to follow our Mouse movements. We must give him instructions to do so. Construct the following module: This tells the Monkey When the game starts, point towards the mouse pointer and move 3 steps continuously. TRY IT! Click the green flag and watch as the Monkey follows your mouse movements. Now lets try changing the number of steps you should see that this changes the speed of the monkey. Reset it back to 3 steps. You may have noticed that sometimes the Monkey turns upside down. To stop this happening, click the Only Face Left / Right button: Created by G. Mullin and D. Carty 33
Task 15 Programming the Bee Set the screen up so it looks like this: Now double click the bee and add the following module: This tells the bee to point upwards and move. the screen. This tells the Bee to Bounce if he touches the edge of You will also have to click the Only Face Left / Right button for the Bee. TRY IT! Click the green flag and you should see the Bee moving up and down the screen. Created by G. Mullin and D. Carty 34
Task 15 IF Statement The object of our game is to get the Monkey to the Bananas without getting stung by the Bee! If the Monkey touches the Bee, he should go back to his starting position. To instruct him to do this, we must use an IF statement. Double click the Monkey. From the Control Tab, drag in a second to the scripts area. Now drag in the Forever IF block and attach it: Note the Condition space we will use this to tell the computer what the condition is. From the Sensing tab, drag in the touching block and put it in the Condition Space: Now select the down arrow and choose Bee : Now drag in the block from the Motion Tab. (you may have to type in the correct X and Y co-ordinates X=-189, Y= 3) Your module should now look like this: Created by G. Mullin and D. Carty 35
We have just told the Monkey : If you ever touch the Bee, go to your Starting Co-ordinates TRY IT!! Task 16 Click the green flag to start your game. Allow the Bee to touch the Monkey you should see that the Monkey goes back to his starting position! Touching the Bananas We want the monkey to say Yum when he touches the Bananas, but think I m Hungry when he isn t touching them. His instructions go something like this: IF I touch the Bananas Say Yum!! Else Think I m Hungry Double click the monkey and create the following module: (you will need the IF ELSE block from the Control tab) Now add the following instruction blocks so your module looks like this: These instructions are carried out whenever the monkey touches bananas This instruction is carried out whenever the monkey is NOT touching bananas. Created by G. Mullin and D. Carty 36
TRY IT!! Click the green flag to start your game. You should see that the monkey will think Hungry, but when he touches the bananas, he says Yum! and returns to his starting position. Task 17 Getting Points Every time the monkey touches the bananas, he should get a point. To do this, we need to create a Variable called Points. Note: A Variable is something that holds data in a program, for example Points will hold the number of points the monkey has but this can change during the game. From the Variables tab, click The following box appears name the Variable Points : Click OK. Now add the block to the existing module as shown: This tells the computer to set Points to 0 every time the game starts. Created by G. Mullin and D. Carty 37
We want the points to increase by 1 each time the monkey touches the bananas. Drag the block into the module as shown: This adds 1 to the Variable Points if the Monkey touches the Bananas. TRY IT!! Click the green flag to start your game. You should see that the points increase each time the Monkey touches the Bananas! Well done you have created the basis for your first computer game! Save this as Monkey Game. Task 18 Adding a Timer It would make the game more interesting if there was a time limit. Create a Variable called Time. (look at Task 17 if you forgot how to do this) Construct the following module: This sets the time to 30 at the start of the game This takes one off time each second Created by G. Mullin and D. Carty 38
TRY IT!! Click the green flag to start the game. You should see that time is running out! NOTE: At the moment, if time reaches 0, nothing happens the game continues, and time decreases through negative numbers. This is because we have not told the game to stop. Construct the following module: This is under the Operators tab This is under the Variables tab. This tells the computer that if time is ever equal to 0, stop the game. TRY IT!! Click the green flag to start the game. You should see that everything stops when time runs out. Save your work. Now ask your teacher for the Puff Collector Booklet. Created by G. Mullin and D. Carty 39