Saturday, May 31, 2014

Let's write a small Scratch program from scratch hehe :-)

If you've never used "Scratch" before, head over to "http://scratch.mit.edu/" and click on "Join Scratch" to open an account.  You don't have to, but all your hard work will be lost if you're not signed in, and who wants that eh?
Now you're signed in, click on the "Create" button to start a new project, and guess what: you get the Scratch Cat in the middle of your "backdrop" (the place your project will run in).
You can even click on the green flag to run your program, but guess what: it won't do anything :-).

So let's make it do something.
This would be a good time to read the "Getting Started" tips you get by clicking on the "?" question mark near the top right of your Scratch page (you are in the Scratch Editor page if you just clicked on "Create").

If you're done reading tips or want to just jump in and write your first program (or script) let's go on :-)
We're going to make the cat follow the mouse.
Cats have been chasing mice since forever so this is nothing new and is also very useful, both to feed the cat and in games, eh?You can see that the cat "Sprite" is already selected, so everything we do will apply to it.
Drag the "point towards" motion block onto the "Scripts area".
Click on the little down-arrow at the right of the block and select "mouse pointer".
As you can expect, this will make the cat turn towards the mouse pointer.
Click on the green flag and you'll see that still nothing happens :-(
That's OK and that's because the program needs to be told you want it to start.
You do that by clicking on the "Events" button near the top-middle of the screen.
This is where the "do something when something happens" blocks are.
Drag the  button on top of the "point-towards" block.  It should "lock in place".
Let's test it now: click on the green flag and voila: the cat is pointing towards the mouse.
Let's make it move towards the mouse.
Click on the "motion" button near the the top-middle of the screen and drag the "move 10 steps" block to the bottom of the script.
Test it now. Click repeatedly on the green flag and you'll see that each time the cat get's closer. YEY!
But this means the mouse has to be on the green flag so you can click on it again and again... not much fun.
So let's tell the script to keep doing it by itself once we start it.

Click on the "Control" button near the top-middle of the screen.
Drag the "point towards" block down and away from the top block.
Now drag the "forever" block on top of the movement script until the white outline shows that the "forever" block will surround both blue blocks.
Now drag the forever block to the bottom of the "When green flag clicked" block and you should get something like this:
If you now click on the green flag to start the script you'll see that the cat follows your mouse as much as it can and would eat it for sure if only you had written the program to let it do so.
You can experiment by changing the 10 in the "move 10 steps" block to smaller or larger numbers and see what happens.
Next time we can give it a mouse to eat :-)


No comments:

Post a Comment