|A Programmer's Guide to Scratch 3|
|Written by Lucy Black|
|Monday, 27 April 2020|
Page 3 of 5
The Foundations of Programming
When you first encounter Scratch a very standard response is to think that this isn't programming.
Partly the problem is that programmers aren't often taught the very fundamentals of what they are doing. Instead they learn how to express the fundamentals using a particular programming language rather than what the fundamentals actually are.
Programming is about writing instructions to get a particular job done. Instructions are by default obeyed one after another and this is what putting the blocks together in a chain is all about.
As well as the default order programming needs two other ways to work with lists of instructions.
You need to be able to conditionally select which set of instructions are obeyed - an if statement in most languages - and you need to be able to repeat a set of instructions until some condition is satisfied - a loop.
With these three things - default sequential, conditional and looping- you can write any program that can be written.
Scratch has the three forms of control. You can write a sequential flow of control just by putting blocks together. If you look in the control pallet you will also find blocks that allow conditionals and looping.
The next example will show you how loops and conditional work in Scratch.
Bounce a ball
The best way to demonstrate is, of course, via a demonstration. So let's create that other simple common basic demo program and bounce a ball around the screen.
This isn't difficult but the simplest solution isn't the traditional x,y coordinate way of doing the job. Each sprite has a heading, i.e. a direction it will move in, so it is simpler to write code that turns the sprite through an angle and moves it in the new direction.
First we need a ball.
Right click on the cat in the Sprite List and select delete. Next select Paint New Sprite,
it's the paint brush icon, and when the editor opens draw a colored circle to represent the ball. You can use the paint can icon to fill the outline with black if you want to:
Now what we have to do is set the rotation of the ball to give it an initial heading and then use an infinite loop to repeat the move forward 10.
You need to select Events and place a When green flag clicked block on the script area. Then select Motion and the turn 15 degrees block and place it next. Finally select the Control palette and place a forever block on the script area. You can see that the forever block has a slot inside it - any blocks you place in there are repeated, well, forever.
Yes this is the Scratch equivalent of the infinite loop.
All you need to do is now select Motion and add a move 10 steps block within the forever block.
If we leave the program at this, the ball would simply move off the stage. There is a supplied conditional that will reverse the ball's direction if it encounters a boundary. You should be able to find the if on edge bounce block in the Motion palette - and yes this is a Scratch conditional. Place it in the forever block after the move block.
This makes our final script:
If you click on the green flag then you will see the ball bouncing around the screen.
Notice that this simple program has introduced all of the essentials of programming - sequential, conditional and looping flow of control.
|Last Updated ( Monday, 27 April 2020 )|