0% found this document useful (0 votes)
216 views24 pages

Ci Grade 7 Part 1

Uploaded by

anuveena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views24 pages

Ci Grade 7 Part 1

Uploaded by

anuveena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

1 Programming for a purpose

You will learn how to:

use variables
broadcast

change the costume of a Sprite


change the background of a game
detect cllisions
use timers and wait
use random numbers.
Once you have learnt what these new skills are, and how they are used, you will be able
to think about them when you design your game.

Before you start


You should:

h a v e used Scraicn to create simple programs, including sequerces, repetition and


procedures
h a v e fixed problems in your own programs to make sure
tey work
have had experience of using and creating flowcharts to plan a program.

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,

Input: putting data


into a computer,
for example typing,
clicking buttons. Revie
Output: data being
given from the
computer to the
User, for example An interactive program means that the user is involved in the program and they are able to

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.

Copyright Material - Review Only Not for Redistribution


1 Programming for a purpose

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

Detecting user interaction 1: deBecting click buttons


A computer game might need the user to press buttons, for example a keypress. In a ca
driving game, the car is going to mo/e forward when the up arrow is clicked. Th: geme needs
to know when the up arrow has been clicked by the user so it knows when to move the car.

To do this in Scratch, yc, ill need to click on Events and then drag

e n space wsnd

Copyright Material Review Only Not for Redistribution


Next, you need to click on the down arrow next to the
Word 'space.

You would then choose the key trom the drop-down


spac box.

You would then add instructions for when this key is


pressed below this block.
S o iirow
University
P

right amow

Activity 1.11
Open a new Scratch window.

Right-click on the cat Sprite.


Select Deleie
Click on ihe 'New Sprite" button.

Add a car Sprite to a new Scratch


file

oo

10
Copyright Material Review Only- Not for Redistribution
1 Programming for a purpose

Make sure it is facing to the right of the screen.


Add blocks so when the right arrow key is pressed, the car meves ten spaces.
Test your code works.

Activity 1.2 Tip


Aid new blocks so when the left arrow key is pressed the car moves back ten spaces.
Moving back will
Test your code works. have a negative
number, for

Activity 1.3 example-10.


Add new blocks so when the up arrow key is pressed the car rotates left 90 degrees.

Test your code works.

Activity 1.4
Add new blocks so wnen the down arrow key is pressed the car rotates right 90 degrees.

Test your code works.

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.

When the Sprite is clicked make the Sprite move forward.

Test your code works.

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

Broadcasting: make a car move when you click on a Sprite


Key term When you add cone to a Sprite, you can affect only that Sprite.

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

Copyright Material Review Only Not for Redistribution


IProgramming for a purpose

For the ball Sprite


Next, you need to click un Events and then drag hi srte and also drag

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.

For the car Sprite

YOu would start by clicking on Events.

You would then drag


al recC

Copyright Material Review Only -


Not for Redistribution
Wext, you need to choose the correct broadcasl message from the drop-down menu.
You can then add your action blocks below it.

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 a dinosaur Sprite.


Add four other Sprites, one to move the dinosaur forward, one for moving back, one
for turning left, one for turning ight.

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

there is the Poinis


This variable is called Points. At the moment
0
number 0 in the Points box.

You can change the value in Points. For example, yOu

could change Points to the number 2.


Points
The variable now has2 stored init. 2

3sked
You could ask the variable what is in it. If you
Points what is in it, it would tell you 2.

You can add to the value in Points. For example, you


could add1 to the current value. Points
2 +1=3. 3
Points currently has 2 in it, se

The variable now has 3 stored in it.

You could add 10 io it.


Points
Points currentiy has 3 in it, so 3 + 10= 13.
13
The variable now has 13 stored in it.

Now u could subtract 3 from it.

13-3 10.
Points currently has 13 in it, so
Points
The variable now has 10 stored in it.
10

If you asked Points what it has in it now, what would


it say? 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.

* aliles Cur les yrile ury

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

You can set the value oí a variable by dragging myvariatble


You then enter tie number you want the variable to start at in the box.

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.

Wnen this Sprite is clicked, increase the variabie by 5.

Activity 4.3
Add a third Sprite to your program.

When this Sprite is clicked, decrease the variable by 2.


Test your code works each time.
1 Programming for a purpose

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

Yes and No.


Yes or No.
An would be "Should I finish all my homework?". The choices are:
Example
f you choose Yes then you do your homework,. If you choose No, then you don't. Did you know?

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.

If you set the alarm,


you will wake up on

time (unless you hit


the snooze button!).
If you dont set the
alarm, you will be
late for school!

There are two options


then

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

say You wirl

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

say YOu vin

say Keep goingl

When the value in Points is equal to 10 then the outputis


You win'

When the value in Points is not equal to 10, the output is


Keep going
Creating the Selection statement
Key term The first block needs to be an operator. it can be either

Operator:A
<,=or*>
command that goes checks if the value on the let aind the right are the same,
between two values for example:

or variabies, e.g. I s 10-2? False, 10 is not the same as 2.


I s 5 2? True, 5 is greater than2.
I s 10 10? Faise, 10 is not less than 10.
The operator lias a space on both sides of it. You can enter
hon
a number ina box, or put a variable in it.

Numbeis are typed in the boxe


1 Programming fora purpose
To add variable, click
a
o: Variables, then drag the variable name into the box.

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

Selection with repetition


You night need to continually check a value, for example as soon as the variable 'Points'
is 10 then you want to output "You win'. If this inside
is not a loop then the blocks will run
once when the
program first starts and never again.

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.

A collision can be added to a selection statement. .


Ifa collision is detected, then an action happens.

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

Putting this inside a forever looP will keep it checking continually.

Here it is being used.

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.1 Tip


Create a new Scratch program. Use the 'touching
Color block.
Add one dinosaur Sprite.
Add blocks so the dinosaur can move up, down, left and right when the user clicks
the arrow keys. Tip
Add a red rectangle. The 'say' block is in
the Looks menu.
If the dinosaur touches the red shape, make it say a message.

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

Sprite disappear. 'Show' will make the Sprite appear.

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.

This code will output the


message "What's your name?"
and wait for the user to type something

ask Whats your name? atid wait

The data the user types will just


disappear. lfyou want, or need,
to keep it tihen you can store
it in a variable. In the Sensing
menu is the block 'answer.
Store this in a variable directly
beneath the ask command.
You will need to change the
name of the variable you
store it in to make sure it is
appropriate. Click on the
down arrows and ente: the
name you want

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

say eto yetame

Copyright Material -Review Only- Not for Redistribution


1 Programming for a purpose

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

within set limits or


bounds.

In this example the selection has been used to check w'hether the age is greater than 4

and less than 100.


!f the age is not valid (it is not within these bounds then it outputs "That's not a

valid age
You can make this validation even more LISeiul by putting it within an iteration (or loop).

hitsora vad a t T; ana nac

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

greater than 4 and less than 100.

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.

Adding a new costume

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

You might also like