exploring IT - Grade 10 Java and Delphi ProgrammingError! No text ...

bahrainiancrimsonSoftware and s/w Development

Nov 13, 2013 (3 years and 11 months ago)

152 views

exploring IT

-

Grade 10 Java and Delphi Programming
Error! No text of
specified style in document.

exploring
IT

Grade 10
Java

and
Delphi

Programming

exploring IT

-

Grade 10 Java and Delphi Programming

Learning Unit 1 Introduction to Scratch

Page
3


Developed by
Delia Kench


Further copies of this book may be ordered from the above email address

colleenp1
@absamail.co.za


Available in Exploring IT Series in 2012 based on the new CAPS document:



Exploring IT Grade 10 Theory



Exploring IT Grade 10 Java Programming (including Scratch)



Exploring IT Grade 10 Delphi Programming (including Scratch)




exploring IT

-

Grade 10 Java and Delphi Programming

Page
4

Learning

Unit 1 Introduction to Scratch

Table of Cont
ents

Learning Unit 1 Introduction to Scratch

................................
................................
.....................

1

1.1

What i
s Scratch?
................................
................................
................................
................................
...........

1

1.2

Installing Scratch

................................
................................
................................
................................
..........

1

1.3

Layout of the Scratch Screen

................................
................................
................................
.......................

1

1.4

Coding your first program

................................
................................
................................
.............................

2

ACTIVITY 1

................................
................................
................................
................................
..............................

2

1.5

Moving the Sprite Across The Screen

................................
................................
................................
..........

4

ACTIVITY 2

................................
................................
................................
................................
..............................

4

EXERCISE 1

................................
................................
................................
................................
............................

6

1.6

Moving the Sprite Around the Screen

................................
................................
................................
..........

7

1.6.1

Using Move Motion Blocks

................................
................................
................................
................................
.........

7

ACTIVITY 3

................................
................................
................................
................................
..............................

7

1.6.2

Using Glide Motion Block

................................
................................
................................
................................
............

8

ACTIVITY 4

................................
................................
................................
................................
..............................

8

1.6.3

Using Rotation Motion Blocks
................................
................................
................................
................................
.....

8

ACTIVITY 5

................................
................................
................................
................................
..............................

8

EXERCISE 2

................................
................................
................................
................................
............................

9

1.7

Sequencing

................................
................................
................................
................................
...................

9

ACTIVITY 6

................................
................................
................................
................................
..............................

9

1.8

Adding Sound

................................
................................
................................
................................
.............

11

ACTIVITY 7

................................
................................
................................
................................
............................

11

1.9

Adding Speech

................................
................................
................................
................................
...........

11

ACTIVITY 8

................................
................................
................................
................................
............................

11

EXERCISE 3

................................
................................
................................
................................
..........................

11

1.10

Changing a Sprites Effects

................................
................................
................................
.........................

12

ACTIVITY 9

................................
................................
................................
................................
............................

12

ACTIVITY 10

................................
................................
................................
................................
..........................

12

1.11

Using a When Sprite Clicked Block

................................
................................
................................
............

13

ACTIVITY 11

................................
................................
................................
................................
..........................

13

EXERCISE 3

................................
................................
................................
................................
..........................

13

CHECK POINT 1

................................
................................
................................
................................
....................

13

Learning Unit 2 Counting Loops

................................
....................

Error! Bookmark not defined.

2.1

Repeating code a set amount of times

................................
.........................

Error! Bookmark not defined.

2.1.1

Algorithms and Pseudocode

................................
................................
.....................

Error! Bookmark not defined.

2.1.2

Representing the solution as an algorithm

................................
..............................

Error! Bookmark not defined.

ACTIVITY 1

................................
................................
................................
..............

Error! Bookmark not defined.

2.2

Using Threads

................................
................................
..............................

Error! Bookmark not defined.

ACTIVITY 2

................................
................................
................................
..............

Error! Bookmark not defined.

EXERCISE 1

................................
................................
................................
............

Error! Bookmark not defined.

2.3

Creating a Sto
ry using Multiple Sprites

................................
........................

Error! Bookmark not defined.

2.3.1

Painting our own Background
................................
................................
...................

Error! Bookmark not defined.

ACTIVITY 3

................................
................................
................................
..............

Error! Bookmark not defined.

2.3.2

Displaying Lines of Text using Costumes

................................
................................

Error! Bookmark not defined.

ACTIVITY 4

................................
................................
................................
..............

Error! Bookmark not defined.

2.3.3

Using the wait block to run scripts at dif
ferent times

................................
...............

Error! Bookmark not defined.

ACTIVITY 5

................................
................................
................................
..............

Error! Bookmark not defined.

EXERCISE 2

................................
................................
................................
............

Error! Bookmark not defined.

2.4

Variables

................................
................................
................................
.......

Erro
r! Bookmark not defined.

2.4.1

Creating a variable and giving it a value

................................
................................
..

Error! Bookmark not defined.

ACTIVITY 6

................................
................................
................................
..............

Error! Bookmark not defined.

E
XERCISE 3

................................
................................
................................
............

Error! Bookmark not defined.

2.4.2

Complicated expressions

................................
................................
..........................

Error! Bookmark not defined.

ACTIVITY 7A

................................
................................
................................
............

Error! Bookmark not defined.

2.4
.3

Order of Operations

................................
................................
................................
..

Error! Bookmark not defined.

ACTIVITY 7B

................................
................................
................................
............

Error! Bookmark not defined.

EXERCISE 4

................................
................................
................................
............

Error! Bookmark not defined.

2.4.4

Inp
ut and Output Statements

................................
................................
................................
......

Error! B
ookmark not defined.

exploring IT

-

The Solutions

Learning Unit 1 Introduction to Scratch

Page
5

2.4.4.1

Input

................................
................................
................................
................................
.......

Error! Bookmark not defined.

ACTIVITY 8A

................................
................................
................................
............

Error! Bookmark not defined.

2.4.
4.2

Input Prompt

................................
................................
................................
..........................

Error! Bookmark not defined.

ACTIVITY 8B

................................
................................
................................
............

Error! Bookmark not defined.

2.4.4.3

Output

................................
................................
................................
................................
....

Error! Bookmark not defined.

2.4.4.4

Dat
a Types

................................
................................
................................
.............................

Error! Bookmark not defined.

EXERCISE 5

................................
................................
................................
............

Error! Bookmark not defined.

CHECK POINT 1

................................
................................
................................
......

Error! Bookmark not defined.

2.5

Number Sequences

................................
................................
......................

Error! Bookm
ark not defined.

ACTIVITY 9

................................
................................
................................
..............

Error! Bookmark not defined.

EXERCISE 6

................................
................................
................................
............

Error! Bookmark not defined.

2.6

Number Series (Adding a Sequen
ce of Numbers)

................................
.......

Error! Bookmark not defined.

ACTIVITY 10

................................
................................
................................
............

Error! Bookmark not defined.

2.7

Calculating the average

................................
................................
................

Error! Bookmark not defined.

ACTIVITY 11

................................
................................
................................
............

Error! Bookmark not defined.

2.7.1

Choosing good test data

................................
................................
...........................

Error! Bookmark not defined.

ACTIVITY 12

................................
................................
................................
............

Error! Bookmark not defined.

2.7.2

Generating Random Numbers

................................
................................
.................

Error! Bookmark
not defined.

ACTIVITY 13

................................
................................
................................
............

Error! Bookmark not defined.

2.7.3

Storing the output as a variable.
................................
................................
...............

Error! Bookmark not defined.

ACTIVITY 14

................................
................................
................................
............

Error! Bookmark not defined.

EXERCISE 7

................................
................................
................................
............

Error! Bookmark not defined.

2.8

Applying Counting Loops and Variables to Animations

...............................

Error! Bookmark not defined.

ACTIVITY 15

................................
................................
................................
............

Error! Bookmark not defined.

2.9

Troll Game
-

A More Complex Game

................................
...........................

Error! Bookmark not defined.

2.9.1

Multiple Back
grounds

................................
................................
................................

Error! Bookmark not defined.

ACTIVITY 16

................................
................................
................................
............

Error! Bookmark not
defined.

2.9.2

Using a Broadcast to Send a Message
................................
................................
....

Error! Bookmark not defined.

ACTIVITY 17

................................
................................
................................
............

Error! Bookmark not defined.

2.9.3

Receiving a Broadcast Message

................................
................................
..............

Error! Bookmark not defined.

ACTIVITY 18

................................
................................
................................
............

Error! Bookmark not defined.

2.9.4

Variables for All Sprites and Information Hiding

................................
......................

Error! Bookmark not defined.

2.5.1

Tracetable

................................
................................
................................
..................

Error! Bookmark not defined.

exploring IT

-

Grade 10 Java and Delphi Programming

Learning Unit 1 Introduction to Scratch

Page
1

Learning

Unit 1 Introduction to
Scratch

1.1

What is Scratch?

Scratch
is a visual programming language that can produce animations, sound and games quickly. It
consists of a
sprite

(the default is a cat) that can be manipulated through
sets of instructions

called
scripts
. A program can have more than one sprite that can in
teract with each other

using scripts
.
A
program

generally is a set of instructions that tells the computer what to do.

Since Scratch lends itself to teaching program concepts
quickly
, it simplifies the coding process by
providing code blocks that can be

l
ocked together to form
a script

for your sprite.
This script will be the
instructions to give your sprite
actions
.

1.2

Installing Scratch

1.3

Layout of the Scratch Screen


Scratch provides you with an IDE (Integrated Development Environment) to code

programs.
An IDE is a
program that provides functions and features to help create code.
It provides you Code Blocks on the
Code Blocks
Pallete

Scripts Area

Sprite

Stop and
Start
Buttons

Mouse co
-
ordinates

New Sprite
Buttons

Sprite
Information
Area

Stage to
change
background

Sprite Execution
Area

Sprite Area

exploring IT

-

Grade 10 Java and Delphi Programming

Page
2

Learning Unit 1 Introduction to Scratch

right, and area to drag your code blocks onto called the Script Area, a screen with a default sprite to
show the result of your pro
grams

called the Sprite Execution Area
, information
about
the sprite you are
working
in the Sprite Information Area

at the top, a stage that can be changed to provide a background,
buttons to add new sprites and the co
-
ordinates of your mouse position.

1.
4

Coding your first program

We are going to code a program to make the cat

walk

on the spot on the screen.

ACTIVITY 1

|

Open the
Scratch

program

In programming it is important your code element with a
good descriptive name and to do this in a similar
manne
r. All the sprites that we use will be given a name
that best describes the sprite. All sprite names will begin
with lowercase letters. We will use this convention later
on when we program in Java.

|

Rename the sprite to
cat

by highlighting Sprite1
and t
yping cat in top middle of the screen.

Click on the Costumes tab under your new cat’s name.
Scratch has given the sprite two costumes; one with the cat’s
legs open and with them more closed. We are going to use
these two images to
animate

the cat walkin
g.

Scratch links “costumes” to a sprite. A sprite can have many
costumes and the sprite can change from one “costume” to
another. We will create many programs that use

costumes
.

|

Rename costume1 to
run

and costume2 to
walk
.

To start the program we need to

set the sprites costume to
run then to walk. To make this happen slow enough for us to
see the change we need to add a wait between each
costume change.

|

Click on the purple
Looks

button above the blocks
palette.

The first block is already set to
switch
the costume to walk.

|

Click
o
n the
Scripts

tab

below
the
S
prite
I
nformation
A
rea to
change back to the
S
cripts
A
rea.

|

Drag the
first
block to switch
costum
es from the B
locks
P
alette onto the scripts area.


We need to get a wait block which
is available in th
e orange Control section of the Blocks Palette.

|

Click on the Control button and drag the
w
ait 1 secs

block onto the Script Area.

exploring IT

-

The Solutions

Learning Unit 1 Introduction to Scratch

Page
3

Later on we will show that
infinite
loops

are not to be used in some
programming examples, but in this
case it i
s perfect.

These two blocks
are called
stack blocks

and
can lock together to show
the code will be executed one after the other.

|

Drag t
he
w
ait 1 secs

block to the switch costume block so it goes
together.


|

Now drag a second
s
witch costume block

from the Look section and lock
it
to your code in the
script area.

The block is set to change to the walk costume and needs to be changed.

|

Click
the down arrow next to the word walk in the switch costume
block and select
run
.

|

Add another block to wait one second after the costume switch.


|

Run the code by double clicking on the first block in the code. The
cat should show a change from its legs ope
n to closed

in the
Sprite
Execution Area
.

This code is not very interesting as the cat only shows each position only once. We would like to
continuously change from one position (costume) to the next to make the cat look like it is walking.

Scratch provi
des
Control Blocks

that can be used to control how the
code runs. We will use a
forever loop

that will continuously run our
code to create an animation. A forever loop is also called an
infinite
loop
.

|

Click on the Control button to access the control bl
ocks in the
palette.

|

Drag a
forever

block and lock in ont
o

the code so that it fits
around

the code. This ensures that all four statements are repeated in
order each time.


|

Run the code again by clicking on the code to watch the cat walk
repeatedly on the

screen.

|

Click on the red Stop button in the top left corner to
stop the code running.

The cat walks really slowly as the wait is
set to
1 second
between each costume.

|

Change
both
the wait
blocks
from 1 second to 0.1
second make the animation run faster.


|

Run your script to check that it works.

|

Click on the red Stop button in the top left corner to
stop the code running.

Since we have added an infinite
forever loop
, the code will
never stop running unless we actually tell the code to stop.

To make the pr
ogram look more interesting we can change
the background or stage.

|

Click on the
Stage

button
below the Sprite Execution Area.

|

At the top middle of the screen click on the
Background

tab and click
Import
.

|

Select the
Nature

folder, s
croll down the background
s and choose
woods
. The stage behind the
cat should change to a picture of woods.

exploring IT

-

Grade 10 Java and Delphi Programming

Page
4

Learning Unit 1 Introduction to Scratch

|

Click on the cat
in the Sprite Area

to view the script you have written for the cat.

It is not ideal to double click on the code to run the code. Scratch provides a new typ
e of control block
which is called a
hat block
.

|

Click on the C
ontrol button to find a
w
hen flag clicked

block. Drag the block to the top of your
code.

|

Click on the f
lag button next to the Stop button to run the code and
then click on Stop when you ar
e finished..

Hat blocks

do not have an indent above it so nothing can be placed
above the block or execute before it. It is used to
start the program
.


The
when flag clicked

block is an example of a
event
. An event is
code that responds to something happ
ening. In this case the event is
the user clicking flags. Today’s code is full of events, each time you
click a button or move a mouse
,

it is an example of an event. Code has
been written to respond to the event just like we have written code to
re
s
pond

to when the flag is clicked.

Finally this animation can be run in a full screen.

|

Click on the button on the top right screen to switch to
P
resentation
M
ode.

|

Click the

fl
ag to run the program and the Stop button to stop.


|

Return to the programming screen

by clicking on the return button on the top
left above the animation.

|

Save the program as
CatWalking
.

To name a program, w
e will use the naming convention of joining words together
and capitalising the first letter of each word. This convention will be u
sed in Java to name all programs.

Congratulations! You have completed your first Scratch program!

1.5

Moving the Sprite Across The Screen

A
CTIVITY

2

Using the
CatWalking

program, we will extend it so that the cat moves
left and right across the screen.

|

Lo
ad the
CatWalking

program

if it is not already loeaded
.

We need to add movement to the cat which is provided by the Motion
section of the Code Blocks.

|

Click on the Motion code section.

|

Drag the
m
ove 10 steps
block onto the cat’s script and place it
after

e
ach costume switch.

When you drag the code block a white
line will appear that will show you where the code will be inserted.
The code block will snap into place.

Make sure you have two block to move 10 steps so that the cat appears to move after each
step.

|

Run the code and you will see the cat move right off the screen!

|

Stop the program.

exploring IT

-

The Solutions

Learning Unit 1 Introduction to Scratch

Page
5

To get the cat back onto the screen you can drag it back using your mouse. Notice that the co
-
ordinates
of the cat in the Sprite Information Area
changes as you drag
it.

It would be a better idea to set the cat in the middle of the screen before the cat starts to move using
code so that the user does not have to drag the cat back each time.

The
Sprite
Execution Area is divided into blocks similar to a
Cartesian P
lane.

At the centre of the screen
the co
-
ordinates of the sprite (0,0).


y

is
positive

x

is
nega
tive

y

is positive

x

is positive

x

is negative

y is negative

x

is positive

y

is negative

|

Click on the Motion button and drag the
go to x: y:

block
to be
t
he first line of the script. Change the x and y values to 0.

An essential part of using a loop is to know what goes
before

the loop,
what goes
after

and what goes
in

the loop.


The code to position the
cat in the middle of the screen is place
d

before

the
loop. The code to
move t
h
e cat
across the screen
is
enclosed
in

the loop and
is
encased by

the forever code block that wraps around the repeated
code blocks.

|

Save and run the program to check it works. End the program
when you are finished.

The cat still

charges off to the right of the screen. So we need to add
and instruction to get the cat to bounce off the edge of the screen.
We need to detect if we have reached the screen’s edge, and we need to do the
each time

the cat
moves. Since this checking is

to be repeated, it makes sense that it must go in the loop. But where?
The check can go anywhere and the code will still work, however it makes sense to check after a
complete step is taken. So we will add as the last line of the loop.

|

In the Motion con
trol block section, drag the
i
f on edge, bounce

block to the last line of the loop.

|

Run the program to check that the cat moves horizontally across the screen.

The program flips the cat over each time it moves left so we need to tell Scratch to flip the c
at to face left
and not upside down.

|

Click the second button with bidirectional arrows to make the cat flip about the
vertical axis.


|

Run the program to check it works.

|

The cat moves slowly, so experiment by reducing the speed in the wait block to make th
e cat move
faster.

To make the move all over the screen we can change the
direction it faces. The direction of the cat is shown in the
Sprite Information Block. It is measured by 0 being to the top
of the screen, 90 right, 180 the bottom and 270 left. I
t works
in the same way as a compass.

(0,0)

y axis

x axis

exploring IT

-

Grade 10 Java and Delphi Programming

Page
6

Learning Unit 1 Introduction to Scratch

We can change the direction the sprite faces by dragging the blue line around. Notice that the numbers
change in the direction block.


In the diagram alongside, the blue line is facing to the top right and the
directi
on value is 45 degrees.

It is better to use code to change the direction of the cat. We need to add
a code block to do this. Again we have to decide where to put the block.
We want the direction to be set at the beginning of the program so the
code bloc
k must go before the loop starts. It can go either before or after
the code to set the cat’s position as both must happen before the cat
starts to move.

|

Using the Motion
b
locks, drag the
point in direction

block after the
go to x: block. Set the directio
n to be 45 degrees.

|

Save and run the program to check that it works.

In

Object Oriented Programming

(OOP) the sprite will be the object.
Each sprite has certain
properties

that stores information about the
object. The values of x, y and direction are som
e of these
properties
. As
the sprite changes position and direction, so do these values change. An
object also has
behaviours
that describe what the object can do. In this
case the script that we have just written that moves the cat around the screen is

the cat’s only behaviour.

Since this programming course
teaches O
bject
O
riented
P
rogramming it is important to become familiar
with these concepts. In OOP, properties can be called
fields

or
attributes
. Behaviours can also be
called
methods

or
actions
.

It is important to understand that an objects consists of two parts. One to
store its data

(properties)
and
the other to
describe how
it

behaves

(behaviours)
.

EXERCISE

1

1.

Create a
n animation
program called
BouncingBall
to bounce a ball around the screen
in a similar
way to the cat. Select a ball by clicking on the Choose New Sprite button in the Sprite Area. Change
the background to some kind of sports field.

2.

You will need this code later on to code a game.


exploring IT

-

The Solutions

Learning Unit 1 Introduction to Scratch

Page
7

3.

Create a
n animation

program called
BalletExerc
ises

using a series of ballet figures available in
Scratch.

4.

Using the four ballet dancers as costumes, make the dancer bend their legs, straighten their legs,
take off on one foot, do a split leap and then land in reverse order.


1.6

Moving the Sprite A
round the Screen

1.6.1

Using Move Motion Blocks

A
CTIVITY

3

Moving a helicopter around the screen using different motion blocks.

|

Create a new Scratch program and
save it as

Helicopter
Move
.

If you
still have the previous program,
save it and then s
elect
F
ile | New

from
the menu bar to clear the scripts and reset the sprite.

|

Delete the cat sprite by right clicking on it
in the Execution Area
and
selecting
delete
.

|

Import a
helicopter
sprite
using the new sprite buttons and selecting
the middle button Choosin
g a new Sprite from File. Select the
Transportation

folder

to find the helicopter.

|

Rename the sprite as
helicopter
.

|

Go to the Scripts Area for the helicopter and
s
elect the Move block to position the helicopter in the
four corners of the screen and add a
wait after each move.

The co
-
ordinates of the helicopter do
not have to be exact as long as the helicopter is positioned in each corner.

|

Save and run the program to ensure the helicopter starts at the top left corner and moves
clockwise with a 1 second wa
it between each move

|

Add a
forever

loop around the code and finish it off with When flag clicked hat block.


exploring IT

-

Grade 10 Java
and Delphi Programming

Page
8

Learning Unit 1 Introduction to Scratch

|

Save and run.


1.6.2

Using Glide Motion Block

Using the helicopter program above, we will use a glide block to smoothly
move the helicopter as op
posed to making discrete jumps every second.

The code to reposition the helicopter takes two blocks of code, one to move
the helicopter and one to wait. Scratch has a glide block that has a built in
timer that will move a sprite to a position over a set
amount of seconds
making the movement flow.

Often in programming,

instructions can be replaced by other instructions that
can achieve
the same or a similar task

more successfully and
possibly

in less
steps. As a programmer you need to know that there ar
e many ways that
a
problem can be solved
, and
you need
to select the best
solution

so that your
program runs efficiently.

A
CTIVITY

4

|

Load the
HelicopterMove

program if it is not already loaded.

|

Replace the four sets of
move

and
wait

blocks with four
glide

blocks to the same positions.

TIP: You can separate blocks by dragging the block below down
and away from the other code blocks.

TIP: You can delete a block of code by dragging it back onto the
Block Palette.

|

Snap the four glide blocks into the forever lo
op.

|

Save the program as
HelicopterGlide

and run the program.

Note that the helicopter moves
smoothly around the screen to the same positions.

1.6.3

Using Rotation

Motion Blocks

In all of the cases of moving the helicopter, it has remained upright. The co
de we have written changes
only the x and y properties of helicopter object. Each sprite object has a third property being direction.
The rotate block on the Motion section can alter the direction of the helicopter. If we place this Motion
block in a lo
op together with a Move block the helicopter can move in an arc motion. (If we do not use a
move block the helicopter will spin on its own axis.)

A
CTIVITY

5

|

Load the
Helicopter
Glide

program if it is not already loaded.

|

Remove all the glide blocks from the
forever loop.

|

Add in two blocks: one to rot
ate 15 degrees and one to move
1
0 steps.

|

Save the program as
HelicopterRotate

and run the program.

The helicopter pretty much rotates on the spot.

|

To make the arc bigger, increase the move amount to 30.

|

Drag
the helicopter to the side of the screen and run the program
again.

The helicopter should spin over the edge of the screen.

exploring IT

-

The Solutions

Learning Unit 1 Introduction to Scratch

Page
9

We can make the helicopter bounce off the
edges by using the
i
f on edge, bounce

block in the Motion
section.

|

Add the
i
f on edge, bo
unce

block to the last line of the loop.

The logic in the loop will be that the helicopter rotates, moves and then check that it is on the edge. If it
is not on the edge then nothing will happen. If it is it will bounce off the wall. This will be repeate
d EACH
time the loop is run.


EXERCISE

2

1.

Create a program called
Bat
Flying

to animate a bat flying and bounce off the edges. Use the bat
sprites supplied by Scratch and use different costumes to animate the bat flying.

Add in waits and
alter the rotation

and move values to create a realistic motion.


1.7

Sequencing

The sequence or order in which the code is executed is extremely important in programming. As a
programmer you need to make sure that you can know which order to code your solution. In some
cases the order does not matter, like set a sprites position and set its direction before we add the forever
loop to animate. These two instructions can be any order and the animation will still work. Similarly if we
move and rotate or rotate and then mo
ve the animation will still work. What wont work is if we take the
block to move and animate out of the loop and plave them after the loop, the animation wont work.

This means you need to think
carefully
about the order of the program, and what must go be
fore, in and
after the loops.

A
CTIVITY

6

We are going to
create an
animation of a
boy walking using
five costumes.
Each time he
puts his foot
down, he will
move forward.

|

Start a new
Scratch
program
and save it
as
BoyWalking
.

|

Import a boy sprite from the p
eople folder using the sprites supplied by Scratch.

|

Rename

the sprite as
boy

and add all the boys from a to e as costumes for your sprite.

exploring IT

-

Grade 10 Java and Delphi Progr
amming

Page
10

Learning Unit 1 Introduction to Scratch

|

Add a code
b
lock to set the boy’s position to (
-
200,0)

|

Add a
forever

block and in it add code to switch
through

each

of the five costumes label a to e.

|

After each
costume
switch add a
block to
wait

for 0.1
seconds.

|

Add a
when
fla
g

cli
cked
block at the
beginning of
the code.

|

Save an run
the program.


The boy should
walk on the spot
looping though all
five costumes.


TIP
: You can copy a b
loc
k of code by right clicking on the
code block. If the block is linked to other blocks they will all
be copied.

To make the boy move across the screen, we need to add
move blocks. To simulate walking we will only let the boy
move once

his foot touches the ground.

|

Add a
move 10 steps

block after boy4
-
walking
-

a costume
and boy4
-
walking
-

c.

|

Change the
wait

time to 0.05 seconds to speed up the
animation.

|

Add an
if on edge, bounce

block to make him move back
and forth across the screen.

|

Save and run the program.

|

In the code alongside we have added the move after the
wait block. Experiment by switching wait and move block
to see if it has an effect on
the
animation.



exploring IT

-

The Solutions

Learning Unit 1 Introduction to Scratch

Page
11

1.8

Adding Sound

We want to add a sound each time the boy’s feet tou
ch the ground to sound like footsteps.

Whilst there
is an extensive sound section, we are going to use a quick beat.

A
CTIVITY

7

|

Load the program
BoyWalking
.

|

In the Sound section drag the
play drum 48 for 0.1 secs

block to the code after the boy puts dow
n
his first foot.


|

Change the 48 to a 47 to make the sound a low
-
mid
-
tom.

|

Drag a second
play drum

block to after the boy puts the next foot down. Keep the drum at 48 so
that each footstep is slightly different.

|

Save the program as
BoyWalkingWithSound

and
run the program.

Can you explain why the sound is not even?

1.9

Adding Speech

We can make the boy march by saying left and right each time he puts a foot on the ground. Scratch
provides speech bubbles in the Looks section of the code
blocks.

A
CTIVITY

8

|

L
oad the program
BoyWalkingWithSound.

|

Drag the
say hello! f
or 2
secs

block and place it after the
code to play the first drum for the first foot.

|

Change the text to “Left”.


|

Do the same after to add speech after the second sound.
Change the text to “Right
”.

|

Save the program as
BoyWalkingWithSoundandSpeach
.

|

Run the code. Why does it take so long?

|

Change the time in the say “Left” block to be 0.1 secs and
do the same for the say “Right” block.

|

Save and run the program again.


NOTE: In programming we denot
e text in quotes as in “Left”
and “Right” in this example. The importance of this will become
apparent when we code in Java.


EXERCISE

3

1.

Add sound to the
BouncingBall

program so that each time the ball hits an edge a sound is heard.

2.

Add speech to the
Ball
etExercises

program so that each time she bends her legs she says “Down”
and each time she jumps up she says “Up!”.

exploring IT

-

Grade 10 Java and Delphi Programming

Page
12

Learning Unit 1 Introduction to Scratch

3.

Create a new program called
BreakDancing

that will use one of the sprites with multiple costumes to
dance around the screen with sound effec
ts. Be as creative as you can
to make this animation
seems real. Include and appropriate background.


1.10

Changing a Sprites Effects

In the Look control section a sprite can have effects applied to the sprite to alter its appearance. This is
not the s
ame as a costume as a costume give the sprite a whole new image. The effects alter the
existing image of the sprite.

ACTIVITY 9

|

Create a new program called
SpriteEffects
.

|

Rename the cat sprite to
cat
.

|

Under the Look button, drag
a
change color effect by 2
5
block

onto the Scripts Area. Add a
wait 0.1 sec

block and
place a
forever

loop around these two statements.

|

Add a
when flag clicked

block to give an appropriate start.

|

Save and run the program.

The colour of the sprite should constantly change. Note t
hat color is spelt the American way as
Scratch

was written
by MIT, an
American University.

|

Change value in the
change color effect by 25

block to 10 instead of 25 to alter
the colour

of the sprite

in smaller increments

producing
a more gra
d
ual change

in
co
lour
.

Not only the colour effect can be changed, a sprite can be altered by many effects.

|

Change effect in the

change color effect by 10

to
fisheye

and run the program.

Note that cat distorts until it appears as a large fish eye. Since we have used a f
orever
loop this process continues to run.

We can add an effect that adjust the sprite’s appearance and set
it back to its original form.

ACTIVITY 10

Making a sprite flash.

|

Load program
SpriteEffects

if it not already loaded.

|

To remove all effects, add a
c
lear graphic effects

block
before the loop to return the sprite to its original form.

|

Change the effect to
brightness

and set the value to
30
.

|

Add a code block to
set the brightness effect to 0

and another
wait 0.1 sec

block after the bright
has been retu
rned to 0.

|

Save and run the program.

We could have used a second
clear graphics effects

block in place of the
set brightness effect block
to 0

to achieve the same outcome. This would have worked in this case as we are working with only the
brightness effe
ct. If we were using multiple effects and wanted to remove a single effect we could not
use the clear effects block at it will remove all the effects.

exploring IT

-

The Solutions

Learning Unit 1 Introduction to Scratch

Page
13

In terms of Object Oriented Programming, the code blocks to change the affects of a sprite are routines
that can be applied to a sprite to alter its image. If we had many sprites and we wanted to change each
sprite effects, we would have to write similar code for each sprite. The methods or routines of an object
can only be applied to that particular objec
t.

1.11

Using a When Sprite Clicked Block

The
when flag clicked

block was our first
event

block. It is a hat block that signals the beginning of the
script. A simple but effective code block is the

when sprite clicked

event or hat block. This changes
t
he code only to be run when the user clicks the sprite. The code will run, but simply wait until that
particular event takes place. Using
a
when sprite clicked block

is suitable for
games
,

as this requires the user to interact with the
sprite.

ACTIVITY 1
1

|

Load program
SpriteEffects

if it not already loaded.

|

Remove the
when flag clicked
block

and add a
when cat
clicked

event block.

|

Save and run the program. Notice that the code only runs
when the user clicks on the cat.

EXERCISE 3

Code a program called
Fa
ncyEffects

and
with a sprite of your choice.



Give your sprite a meaningful name.



Add an interesting background.



Add a script for your sprite so that when the sprite is clicked it goes through a series of effects.



Each time the effect changes, the s
prite must say what the effect is that is performing.


CHECK POINT

1

4.

What is a program?

5.

What are the advantages of using Scratch as an introductory programming language?

6.

What does Scratch call small programs for each sprite?

7.

What does IDE stand for and exp
lain it is used for.

8.

What is the naming convention for the names of sprites?

9.

What is the naming convention for naming programs?

10.

What is the difference between a program and a script?

11.

What is the difference between a stack block and a hat block?

12.

What does a

forever block do?

13.

What is an infinite loop and why are the ideal for animations?

14.

What is an event and give an example of an event in Scratch?

15.

Give an example of an event when using the computer?

16.

Explain how the direction works for the sprite objects.

17.

What

is an object and give an example.

18.

Give others words for properties and behaviours.

exploring IT

-

Grade 10 Java and Delphi Programming

Page
14

Learning U
nit 1 Introduction to Scratch

19.

What can be used to replace go to and move blocks to create a smoother motion?

20.

What is sequencing in programming and why is it important?

21.

What does an if on edge bounce blo
ck do?

22.

In the code below, why
will
the sprite n
ever

bounce off the edges

even though it has a if on edge,
bounce block
?

23.


24.

How can the code be fixed so that it will bounce off the edges?