Вы находитесь на странице: 1из 39

Randomization and Variables

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.

Monkey prompts user what to do


Set monkeys initial position
Let user move monkey with arrow key
Set score to 0
Loop 5 times
Put banana in a random position
Show and let banana fall down
If banana meets monkey
score = score +1
Hide banana
6. Game Over

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

Write your own game


You will be asked to write your own
catch game. Remember to add a
prompt before the game starts and
game over when it ends.
At this point you are initializing the
score in the bananaScript. It will be
modified later after you learn more
about the scope of variables

Haunted Castle
Can you write a program of a ghost is appearing
and disappearing in different spots in a castle?

Move Sprite to Random Position


Put the randomized
number in the appropriate
box
We use 140 and 200 and
not the whole stage to
accommodate the size of
the sprite
Write the program and
save it as haunted we
need to modify it later

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

Variable Name and Initial Value


Variable Names:
Must be unique within the method can be case
sensitive depending on the language
Should be meaningful

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

Testing and Fixing Bugs


The program seems
to work well
however, if you hit the
same ghost twice or
even thrice, the score
continues to add up.

The way to fix it is to


check whether the
ghost has already been
clicked each time it
appears. If it is the first
time, add score,
otherwise, no score

Also, the game never ends. You


should add a global variable to set a
time limit for the game to end

Algorithm for First Person Shooting


Game
Haunted is a first person shooting(ffs) game. Lets do
it properly again starting from the algorithm:
1.Set all variables to 0 and timeLeft to 10
2.Set up ghost and gives instruction
For interactive games, you
should always provide
3.Loop Forever
instructions or tell user
timeLeft = timeLeft -1
when or how to input
make ghost disappear
information this is called
a prompt
set isClicked to 0
make ghost appear in a random place
4.When ghost is clicked
If isClicked is 0
5. If timeLeft = 0
Add score and effect
Stop everything
Set isClicked to 1

backdropScript: Global Variables


Remember we use
global variables for
score and timer because
they are for all sprites in case you add other
sprites (e.g a witch) the
score still works
Convention: We set and
if possible deal with
global variables in the
backdropScript

Script for the ghostSprite

Modify and save


haunted again.

Add comment:
Calculate scores and
add effect

Codes for the Methods

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:

Total = Total + Amount Each Time

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.

Add the Sprites


You need a cashier and some fruits. Add
prices to fruits and make a check out sprite

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

The Code: Initialization and


Calculate one item (1)

The Code: Other Items (2)


As all the other items calculate the same
way, click and drag the appleScript to the
other fruits and change the current price to
the corresponding price

The Code: checkOut (3)


When the user clicks the checkOut sprite, it
needs to send a message to the cashier so she
will do the calculation. You have already
learned how to use it in Unit 4 use broadcast.

The Code (4) : cashierScript

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."

Create and Iterating through a


List
We are going to create a list of the first 5
even numbers and ask the cat to say them
You need to create TWO variables a list
variable for the even numbers and a
variable for the index

Create the Variables


Fill the list with the first five even
numbers after you have created it.

Algorithm and Code


1. Set indexNum to 0
2. Repeat as many times as there are
items in the even list
add 1 to indexNum
cat say even(indexNum)
Save the
program as
even

Notice we can
add items and
the program
still works

Question and Answer Program


When do we use a list?
If you have make a question and answer
program, you should use a list variable.
Lets look at an example:
Number Question

Answer

1.

Which is the greatest city?

Toronto

2.

Which is the best school?

Mary Ward

3.

Who is the coolest teacher?

Ms Chan

4.

Should you take ICS4U1?

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)

Coding the Method

Coding the Main Program

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

Вам также может понравиться