Академический Документы
Профессиональный Документы
Культура Документы
Random Numbers
When programming
games, we often want
to use random
numbers
Scratch provides
built-in functions for
generating random
numbers.
Catch Games
You need to use random numbers in catch games
Scenario: 5 bunches of bananas are following
from the sky and the user is to use the arrow key
to move a mouse to catch them
In order to record scores, we need to use a
variable we will use it in this program and
explain later.
Algorithm
1.
2.
3.
4.
5.
bananaScript (1)
You should be
able to write the
monkeyScript
yourself. Heres
how you write the
main program of
the bananaScript
bananaScript (2)
Notice you only need
to randomize x. It
always falls from the
sky
It needs to fall straight
down use the same
number for x
Haunted Castle
Can you write a program of a ghost is appearing
and disappearing in different spots in a castle?
Score
The program can easily be turned to a
game by letting the user click on the
ghost.
To record score, we need to use variables
we have already done so before but lets
look at it closely before continuing with the
game.
Variables
A variable is a named storage location in
the computers memory
A variable can only hold one value at a
time. A new value will replace the previous
one
Scope
The scope of variable refers to the part of a
computer where a variable is binding
Local variable: belongs to a specific sprite
or method
Global variable: belongs to and can be
used in the whole program
Initial Value:
The value that is first stored in the variable when
it is created. Initializing means setting a value for
a variable when it is first created.
In Scratch you must initialize as it keeps the
value of the variables of the previous run of the
program
Counting Numbers
Use data to create a local
variable to let the cat use the
loop to count from 1 to 5.
Haunted Game
Now modify the
haunted program by
adding a global
variable for score.
You should be able to
count the score now
Add comment:
Calculate scores and
add effect
Accumulator
The score increases by 1 each time, but
you can also decide how much to increase
An accumulator is a variable used to
calculate the total of some numbers
Imagine you have a new piggy bank. The
first time you put in $2. The second time
$1. The third time $4. How much is there
in the piggy bank?
Algorithm
1st time: Total = Total + 2 ( 0 + 2 = 2)
2nd time: Total = Total + 1 (2 + 1 = 3)
3rd time: Total = Total + 4 (3 + 4 = 7)
As a result, you have $7 in your bank.
The algorithm is:
Cashier
We will write a program to demonstrate
this.
There are a few items with prices. The
user is going to click on the item and the
total amount will be calculated.
Algorithm
1. Make three global variables current price, tax
and total price and initialize them to zero
2. Each time an item is clicked:
current price = item price
total = total + current price
4. When check out is clicked:
tax = total * 0.13
total = total + tax
Cashier says the total
Save the
program
as
cashier
List
You should notice that you can create a
list as a variable.
A list is a data structure for collecting and
organizing data into a group
Each item in a list is called an element
It uses an index variable to keep track of
where each element is located in the list
Iteration
When a list is created, it has an index.
We usually access the information of a list
through its index.
One of the most useful operations with a list is to
repeatedly perform some action with each item
in the list. We call this iteration or "iterating
through a list."
Notice we can
add items and
the program
still works
Answer
1.
Toronto
2.
Mary Ward
3.
Ms Chan
4.
Of course
Algorithm
1.
2.
3.
4.
Create one list for questions and one list for answers.
Fill the two lists with elements
Set indexNum as 0
Loop the number of element times
Add 1 to indexNum
Display questions(indexNum)
Get users answer
if users answer = answers(indexNum)
display Correct
else
display answers(indexNum)
Testing
Notice the answer has to be EXACTLY the
same to get a Correct response. Case
matters
Next, you will be creating your own
programs. Refer to the unit guide for the
requirements