Let's Move!

Introduction
Part 1: Let's Scratch!
Part 3: Let's Broadcast Variable Messages!
Part 4: Let's Make A Game!
Part 5: Let's Get A Job! A Fizz Buzz programming test.

Select New from the File menu at the top of the Scratch window. Rename the Sprite1 to something meaningful.

From the Control blocks choose the When Space key pressed block and add a Move 10 Steps from the Motion blocks.

Change the Space key to the Right Arrow key from the Control block's drop down menu.

Press the right arrow key on the keyboard and watch that cat move to the right.

Keep moving the cat to the right until it reaches the edge of the stage. As Scratch is a safe and friendly environment the cat will not vanish completely and so you can grab its tail and drag it back to the centre.

If necessary drag the cat back to the centre. Change the Right arrow key control to Up Arrow. Press the Up Arrow key. Yikes! The cat moves right. Change the control to Left key. Press the Left Arrow key and the cat moves right!

Scratch sprites move in the direction they are pointing. From the motion controls find the Point In Direction block and insert it into your script between the Key Pressed and Move blocks. Set the Right Arrow Key pressed and set the Point In Direction to 90 Right.

Right click on the Right Arrow script and select Duplicate from the menu that pops up. In the duplicated script change Right Arrow to Left Arrow and change the direction to -90 Left. Our cat can now move left and right but when it moves left it flips upside down. To alter this click the middle box next to the image of the cat in the top centre section which will limit the cats rotation to flipping left or right.

For Up and Down simply Duplicate (with a right click on a script) and change the settings for the key pressed and direction to point.

Let's get logical

Grab an  If  block from the Control blocks and add it to the end of your Right Arrow script. If instructions are a powerful way for a programmer (thats you!) to make it look like the computer is acting intelligently. The If instruction checks for some condition such as..

Is the cat at the edge of the stage?

and if the condition is true some other instructions are performed but if the condition is not true we carry on and ignore the other instructions. Find the numbers labelled X, Y and Direction under the box where you named the cat.

Move the cat left and right and watch the X number. It is recording the horizontal location of the cat. As you move right the number increases and as you move left the number decreases. Zero is in the centre. When X is around 200 the cat is on the right hand edge and when X is about -200 the cat is on the left hand edge.

With that information we can check when the cat has reached an edge and then make it do something. Grab a Greater Than block from the Operators and pop it into the hole next to the word If on the  If   block.

Now we need the X Position of theCat block, if you called your cat some other name it should say that name, and pop it into the first blank hole on the green Greater Than   >   block. Now type 200 into the second blank hole on the   >   Greater Than block.

Our If instruction says:

If the X position of the cat is greater than 200 do the stuff inside the If block otherwise just carry on.

Inside the If block add a Play Sound block and set it to "Meow" from the drop down menu. Under that add a Set X to  block and type 200 into the empty space. Add a Say for 2 seconds block, type "Ouch!" (or some other suitable exclamation) into the space and change the 2 seconds to 1 secs.

Now most of the time the computer will look at the If statement and decide that it is not true and so will ignore it. But when the cat reaches the right edge and the value of X is greater than 200 the condition will be true and so the Meow and Ouch! commands will be carried out and the value of X will always be reset to 200 so the cat can go no further.

If you would like to see this happening select Start Single Stepping from the Edit menu which will flash each block as it is executed when you press the right arrow key.

For moving left you will need to add a Less Than   <   block to the If block as the value of X is decreasing.

For Up and Down you will need   >   for Up and a   <  for Down and change the X Position to Y Position from the drop down menu as Y is used to record the Up and Down movement. The cat will find the edges around 130/-130.


Introduction
Part 1: Let's Scratch!
Part 3: Let's Broadcast Variable Messages!
Part 4: Let's Make A Game!
Part 5: Let's Get A Job! A Fizz Buzz programming test.


duncanmoran.net