Code Club Lesson 1
Code Club Lesson 1
Before we start learning to program, let us review some simple concepts about computers. Most
people now use computers whether in the form of a home computer or a mobile smart phone. The
software you run on your computer such as Microsoft office, Google Chrome, Facebook app, or the
WhatsApp app all are “programs”, written by programmers using what is called a “programming
language”. A program is simply a set of instructions which tell the computer what to do, e.g. the
WhatsApp program tells the computer in the phone to display any messages that it might receive, and
send any message that the user writes to the appropriate contact or contacts, a music player program
reads the music that is sitting in the phone and sends it to the speaker or earphones. Back in the 1950’s
when computers cost millions and were the size of buildings, programmers would need to flip
hundreds of switches to create a program. Computer scientists since then have created programming
languages to help programmers easily create programs. Once a programmer writes a program using a
high-level language, it needs to be “compiled” or “interpreted/translated” into a “binary” which can
run on a computer. All the software you run was once written by a team of software programmers
and compiled. Surprisingly, the most complex software uses a few programming constructs that can
be mastered easily.
Today we will start learning how to program using the Scratch programming language. Programs may
be written using Scratch either through an online web-based editor or an offline editor that can be
installed on a computer. To keep things simple, we will use the online web-based editor.
Once the Scratch website loads, click on the create link at the top of the page.
Figure 1
A Scratch program comprises objects called sprites, e.g. the cat in figure 1, reacting to certain events.
These events can include the program starting up, the sprite being clicked, the sprite bumping against
the side the of the program window. When you start a new Scratch project, you get the cat sprite by
default. You can remove it and add a new sprite. You can also have multiple sprites in your program.
Let’s make a program with two sprites, a cat and a dog. To add the dog sprite, click on the little button
at the bottom of the window marked list of sprites which has a small ‘+’ on it. Now we have two
sprites in the program as shown in figure 2.
Figure 2
When the program starts ( when the user clicks the little green flag above the program run space), we
want the cat to move to a certain position in the program run space. Position in the program run space
can be specified using a cartesian coordinate system as shown in figure 3.
Figure 3
Let’s say that when the program starts we want the cat to move to the position x=-110, and y=-110,
and the dog to move to position x = 110, and y=110. Now let’s put things into perspective. The event
is the green flag being clicked and we have to program the behaviour of both the cat and the dog.
To program the event behaviour for the cat, select the cat in the list the list of sprites window, and
then click on the events category of blocks on the far left side of the screen as shown in figure 4.
Figure 4
When you click on the events category, the list of blocks will show all the events available for the
selected sprite. Drag the when green flag clicked event into the main editor window as shown in
figure 5.
Figure 5
This is the start of the behaviour of the cat sprite for when the green flag is clicked. In programming
terms it is referred to as the event handler. Recall that we wanted the cat to move to position x=-
110, and y=-110 when the green flag is clicked. To do so choose the motion block category and drag
the glide to position block and attach it to the event block in the editor as shown in figure 6.
Figure 6
The glide block lets you choose how long the sprite will take to move from its current position to the
position specified in the block. Set the value of x and y to -110 in the glide block. Now in the list of
sprites, select the dog and sprite and repeat the same process, however specify the dog spriteto
move to x=110 and y=110.