Ci Grade 7 Part 1
Ci Grade 7 Part 1
use variables
broadcast
Introduction
                                                                                                Key terms
Computer programs need to be planned and designed before they are created. This
is usually done following something called tihe Software Development Cycle. This is             Software
a structured sequence of actions that aliow you to plan, design, create, test and then          Development
improve a computer program.                                                                     Cycle: a formal
                                                                                                set of processes
It is important that programs are planned first for many reasons, such as:
                                                                                                followed to plan,
t o make it clear what the program has to do                                                    design, create and
   to make sure everyoe on the team working on it fully understanas ihe requirements            test a system.
t o make sure the person (or group of people) who want the gane are happy with what             Test: to make sure
   the game Wii do.                                                                             a section of code
From this pi2n, you can design how the program will work, ior example using a flowchart.        runs correctly.
This willet you find any problems betore you start, and t means you re not making it up
as you go along!
Once you have created your program you need to test it to make sure it works fully and
that there aren't any problems. If you have been asked to make a particular program for a
client, they will have a clear idea of what ihey want. If you cannot deliver the program they
want, you may not get paid tor the work you have done - it it doesn't work, for examp,
                                                                                                                   7
                             Copyright Material Review Only Not for Redistribution
                         Programs usually require user interaction using an interface. The interface is the part
Key terms
                         of the program that allows the user to interact with it. It includes the on-screen buttons,
Interactlve: a           text, images and so on, that the user can then click, type into and look at.
program that has
                                        x47
both input and
output for the user.                            Pres
                                                                               0032253
Interface: the
pat oi a program
that lets the user
input data and that
produces output for
the user,
on scre@n, from          p u t data into the coniputer. This is input, for example clicking buttons, typing text and
Speakers.
                             numbers. Ina computer game, this might be by using a hand-held      controller
Analogue: data           .get data from the ccmputer. This is output, for example you can see images and text,
in the real world            and hear souna.
(that is not in a
computer).
                                                               Review
 Stay safe!
You mignt de
using the'Scraich
Internet' version.
Make sure you stay
safe while online,
and only use the
website you have
 been given. Do not
talk to other people
online, or visit other
websites without
                          Most data that is in the real world (that is not in a computer) is analogue data. This can
 the permission of
                          be any value, represented in any form, for exaniple sound or images. This data must be
yourteacher.              converted for a computer to understand it.
A computer only understands digital data. This is 1s and Os. If you want to input data into
                                                                                               Key terms
a computer, it has to be turned into l5 and 0s. This can be done with ary type of data, for
example letters, nurnbers, sound and images. This is data capture                              Digital: data in
                                                                                               a computer, it is
 nis image shovvs an analogue sound wave at the top, that can have a large range or
values, ana a digital wave at the bottom where the data has been encoded as 1s and 0s.         stored in 1s and Os.
The digital sound wave is not identical to the analogue sound wave.                            Data capture:
                                                                                               gathering data from
                                                                                               the real world and
                                                                                               turning it into a
                                                                                               form the computer
                                                                                               understands.
                                                                                               Feedback: the
                                                                                               input changes the
                                                                                               program, which
                                                                                               then produces
                                                                                               output. The output
                                                                                               becomes the
                                                                                               new input in the
A special form of input and output is known as feedback. This is when the output from a        programn.
process becomes the input into a process. So it is all ruri 2utomatically, and the data that
IS Creaed changes what happens next.
Skill1
To do this in Scratch, yc, ill need to click on Events and then drag
e n space wsnd
right amow
     Activity 1.11
     Open a new Scratch window.
oo
10
     Copyright Material           Review    Only- Not for     Redistribution
                                                                               1 Programming for a purpose
 Activity 1.4
 Add new blocks so wnen the down arrow key is pressed the car rotates right 90 degrees.
 sk                                                   o
Detecting user interaction 2: user clicking objects
You can get your user to interact with your game by clicking on objects in the actual
game, for example when the user clicks on the car, the car moves forward.
Start   by clicking on the Sprite
                                                                                      o
Thenyou can click on Events and then drag             se
Finally, you can add your instructions for when this Sprite is clicked below this block.
                                                                                                              11
                                Copyright Material Review Only Not for Redistribution
                          Activity 2.1
                          Add a Sprite to a new Scratch prozra.
                          Activity 2.2
                          Change the progiam. from Activity 2.1. When the Sprite is clicked, make the Sprite:
  lo slow your sprite
                          1   Move for ward 20 steps
 down so you can
 see it move, add a       2   Turn leit 90 degrees
 Wait' block after        3   Move forward 20 steps
 each instruction.        4   Turn right 90 degrees
                          S   Move forward 20 steps
                          Test your code works.
Skill 3
 Broadcast: sending     Broadcasting iets you send out a          message to tell another   3prite to do something, for
 a message in           examplc nmove.
 Scratch that other     This is how to make a car move ten steps when the bal is clicked.
 Sprites can see, and
 then react to.         Fi:t, you need   to   add   a car   Sprite and a ball Sprite to your Scratch stage
o o
You would then click on the drop-down menu and choCse New Message
                                          New sdge
                             New message name
Move
cancer OK
You will need to change the message to make it meaningiul. It should describe its
 purpose, tor example Car Move.
                            Activity 3.1
                            Open a new Scratch wir dow.
                            Add two   car   Sprites:.
                            When the first car is clicked, make the second car move forward.
                            When the second car is clicked, make the first car move forward.
                            Test your code works.
                            Activity 3.2
                            Open a new Scratch window.
                            Add blocks so that when rnoving forward is clicked it broadcasts a message. When
                            the dinosaur receives ihis message it moves forward. Repeat for moving back, and
                            turning left and right.
                            Test your coce works.
Key term
Variable: a space          4
 in memory where          variables
you can store data
                          A variable allows you to store data in a program, for example a number. This data is
 temporarily.
                          stored in the computer's memory, a Lit like putting something in a box. You give the
                          variable a name so you can remember what it is called, and use it later. You might have
Did you kpow?             lots of variables, so they all need io have names that describe what they are storing.
Variabies are a           In a game where you are cotching stars, you might want to count how many stars have
fundaniental part         been caught. This woula be stored in a variable, it could be named Stars.
of programming,           In a game where your spaceship is flying through space, you might want to store the
without them you          number of planets it has visited. This would be stored ina variable; it could be named
cant store any            Planets.
data! They are
only temporary
though. To store
data permanently, it
needs saving to a file.
14
                                                                                   1   Programming for   a   purpose
                                                 3sked
You could ask the variable what is in it. If you
Points what is in it, it would tell you 2.
                                         13-3    10.
Points currently has 13 in    it,   so
                                                                          Points
The variable now has 10 stored in it.
                                                                            10
Creating a variable
1   To create a variabie, first you need to click       on   Variables.
2   Then you wouid click on "Make a Variable.
                                                                                                                 15
                                    Copyright Material Review Only- Not for Redistribution
                      3 You should give the variable a meaningful narne. You will need to remember what it is
                         called later.
N nxle nare.
                                                                            came
 You are probably
 only going to need
 to use 'For all      'For all Sorites' means any Sprite can change and read thhe variable.
 Sprites.             'For this Sprite only' means that this Sprite only can change and read the variable.
                      4 Next, you would click 'OK'.
                      The new variable called Points will appear on the screen.
                      If you don't want to see what the value is, you can untick the box on the left of the word'Points.
Setting a variable
16
                                                                              1 Programming for   a   purpose
To set the variable to a starting value (for example Owhen ihe program starts), put a
"When green flag clicked' block followed by the 'set Poinis to 0' block.
Changing a variable
You can change the value of a variable by dragging
YOu will then enter the number you want to change the value by; 1 means increase it by 1.
2 would increase it by 2.
-5 would decrease it by 5.
In this example, whenever the ball Sprite is clicked, the variable is increased by 1.
  Activity 4.1
  Open a new Scratch window.
  Create a variable with the   name   Count. Set it 'For all   Sprites
  Each time the Sprite is clicked, increase the variable by 1.
  Activity 4.2
  Add a second Sprite to your program.
  Activity 4.3
  Add a third Sprite to your program.
                                                                                                 Key term
Skill 5
                                                                                                 Selection: When a
Selection                                                                                        section of code is
Selection s vhen you    use a   question   to decide what to do.                                 run depending on a
                    selection is the 'if' statement. This   oriy   has two   possible answers:   Condition.
The most   common
You can use this with a variable. For example if the number of points is equal to 10,            You have to make
make the Sprite say "You win".                                                                   lots of choices every
                                                                                                 day.   If you decide
Selection blocks in Scratch 2re in the Control menu.
                                                                                                 to eat a cookie then
                                                                                                 there will be one
                                                                                                 less cookie to eat
                                                                                                 laterron.
then
19
                          Copyright M A                                         edistribution
                     ihen
                     When the answer is True, then Scratch runs tihe commands inside the block. If the answer
                      False, the code is not run, for exampie.
                     is
Poents
                     f     value in Points is equal to 10, then it will output "You win'. If it is not, then nothing
                       the
                     will happen.
                     If 'Ther Else
                     When the answer is True, the statement after then' is run.
                     Vhen the answer is False, the statement afier 'else' is run.
                     e.g.
                                            Foints                hen
Operator:A
                     <,=or*>
command that goes    checks if the value on the let aind the right are the same,
between two values   for example:
Tatorinle
      Cote
                        ehumes         dSuds
Vriables
Pon
ane
vtes
Drag the actions you want into the space urder the 'if' block.
Put the 'if under a block to tell it when to run, for example:
ckted
                                                        Proints
                                                                     10        hen
YOuwin
                                                                                                                                  21
                                        Copyright M                                                     edistribution
                       Puttingthe'if   block inside   a   forever   block   means   it will   keep checking the value of the
                       variable.
en Pchcod
Poine hen
Y win
                          Activity 5.1
                          Edit the program you made for Activity 4.3.
                         Add a selection statement thet constantly checks the value of your variable.
                          If the value is equal to 20 then    output a winning message
                         Activity 5.2
IS greater than
                          Edit the program. Change the selectiorn statement. Instead of checking if the value is
< is less than
                          equal to 20, check if the value is greater than 20.
10 2 is true
                         Activity 5.3
                          Open a new Scratch window.
Tip
                          Create a program that:
You could use two
if blocks. Or one if      When the user presses the right arrow, the Sprite moves to the right, and 1 is
then else' block.          added to a variable.
                             When the user presses the let arrow, the Sprite moves to the left, and lis
                             subtracted from a variable,
                          I f the variable is greater than    10, a message is output.
                             If the variable is less than 1, a different message is output.
                                                                                  1 Programming for   a   purpose
skill 6
Collisions
In Scratch you can detect when one Sprite touches
                                                                    Castunes      onta
another Sprite, or another colour. You can then
         an action, for example if a car hits a star           Sensing
 perfor
ther it has a point added. When a car goes off the
road then it stops.
Touching a colour
                                                        CEra
To detect ifa Sprite has toucl ed a specific colour
you will need to select        o           from the
Sensing menu.
This block goes into an if statement. If the Sprite is touching the colour red,
then it will broedcast the message "Touching
ouching.colc
                                                                                             Tip
                                                                                             Another Sprite
                                                                                             might use this
                                                                                             message to
                                                                                             perform another
                                                                                             task.
     The   car will move   when you press the right arow key.
     When it touches the red circle the message Touching' will be broadcast.
     To create yourown Sprite (like the red circle) click on the paintbrush icon
     Use the drawing tools to draw your Sprite
                                                                                        oe
     Touching another Sprite
     You can also detect whether it is touching another Sprite.
     To do this, from the Sensing menu you would choose the block            ro m   o
     Then you woulá choose the Sprite from the drop-down menu.
In this example, when the car touches the bell it will broadcast a message.
24                           A
                                                                            1   Programming fora purposee
Activity 6.2
Add a green circle to the sanne program.
If the dinosaur touches the green shape, make it say a different message.                Tip
                                                                                         Use the 'touching
Activity 6.3                                                                             color' block.
Add more red and green shapes. You should not need to add any more touching
commands. Ine dinosaur should say the same messages each time it touches a
colour.
Activity 6.4
Add an insect Sprite.
Create a variable called Insects. Revisit Skill 4 to see
how to create a variable.
Each time the dinosaur touches the insect Sprite, add
1 to the variable 'Insects'.
Activity 6.5
When the dinosaur touches the insect Sprite, make the
insect Sprite disappear.
In the menu Looks the block h i          will make the
Activity 6.6
Add   more insect   Sprites
Each time the dinosaur touches any of the insect
                                         and make the
Sprites, add 1 to the variable 'Insects'
insect disappear
Activity 6.7
Each time the dinosaur touches any of the insect Sprites
make the dinosaur say, "Yum, I like eatina incacte"
                                                                                                           25
                               Copyright M                             edistribution
Skill
Entering data
Your user can enter data into
Scratch. You could then use
this to perform specific tasks,
for example you could zie a
character a name or you could
say how many times ycu want to
play a game.
Getting aata
The block Ask and wait is found in
the Sensing menu.
You can then use the answer elsewhere, for example here the name entered is input:
                                  wIen cicAed
                                   ask Whars your name?Nd wan
                                   set   myelaneeanswer
Skill8
Validating da ia
You can validate data to check whether it is reasonable. For example it you are entering            Key term
                                                 unreasCn2ble.
the age of the user, then an age of 239 would be                                                    Validate: To check
                        selection   statement, exactly the   same as in Skill 5.                    that data entered
You io tiis   using a
                                                                                                    is reasonable, or
In this example the selection has been used to check w'hether the age is greater than 4
valid age
You can make this validation even more LISeiul by putting it within an iteration (or loop).
This time the validation takes place inside the repeat until. The loop will keep on saying
"That's not a valid age, please try again" and raking the age as input until that age is
                                    CRPYright M                                    edistribution
      You can also do validation on text, for example to make sure they have entered Yes or No
      to answer a     question.
      In this example, the program will repeatedly ask "Are you ready to start?" until the user
      enters either "Yes" or "Yor "y' or "yes. All four options are given because any of these
      are allowed to be entered.
                                                              -
                  A      d    tn etv   ma
If anything else is entered it will just ask the same question again.
        Activity 8.1
        Create   a new    Scratch program.
        Make the sprite ask the user a number of questions with multiple choice options.
        Repeatedly ask the question until the user enters one of the possible answers.
        Activity 8.2
        Create a Scratch program that can make three set novements.
        Ask the user which movement they would !ike. Validate the input and keep on
        asking them until they give a valid choice. ihen perform the movement they have
        selected.
        Activity 8.3
        Create a Scratch program where the user has to enter information about
        themselves e.g. their age, height, favourite colour.
        Validate the inputs and output appropriate messages if the inputs are valid, and
        invalid.
28)
                                                                                     1   Programming for   a   purpose
Skill 9
Costumes
A Sprite can have several different costumes (images) that they can change between. For
example you inight want a character to change colour or point in a different direction.
To add a new costume, you would click on a Sprite and then choose Costumes.
You can either load a pre-existing Sprite, draw a new sprite, upload from a file, or take a photo.
You can edit your Sprite using the tools on the Costumes page.
                                                                                          o
                   Changing costumes
                   The Looks menu lets you     change costume
                   You can change to a specific costume, or you can move to the next costume. The next
                   costume will move to the next in your sequence. For example if the first costurme is
                   Currently being used, then the second one will load.
Las
Tip
 TOU Will need a
                     Activity 9.1
                     Create a new Scratch program. Add a Sprite.
  oreverloop and
 'next costume       Add a second costume for the Sprite.
 block.              Add blocks so when the code runs, the Sprite keeps changing from one costume to
                     the nexi.
                     Activity 9.2
                     Create a Scratch program with one Sprite ihat has two costumes.
                     Add blocks so when the right arrowk=y is pressed, the Sprite moves forward and
                     changes costume.
                     Activity 9.3
                     Create a Scratch program with one Sprite that has four costumes: one for each
                     direction the Sprite is tacing (up, down, left and right).
                     Add blocks so when the right arrow is pressed, the Sprite loads the costume that is
                     facing to the sight.
                     When the tp arrow is pressed, the Sprite faces up, and so on.
                     Activity 9.4
                     Create a Scratch program with a wardrobe of outfits. The user has to enter the
                     number of the outfit they want the Sprite to wear