Rapid Fire Numbers!
Vineet Srivastava
www.scratchcourse.wordpress.com
In this lesson, we will …
2
www.scratchcourse.wordpress.com
Rapid Fire Numbers Game!
3
www.scratchcourse.wordpress.com
Building this game
4
The ‘scheme’
5
Create Clones of the number and Assign them Unique IDs using local variables
Keep adding clone IDs to the lists: LeftClone, RightClone, OperatorClone
For every pair of numbers, store the correct answer in the list Answers
When user provides answer, see if it is present in the list Answers
If yes, move ALL the clone IDs that resulted in the answer into a delete list
Delete the clones that are on the DeleteList
and prune the DeleteList
Key Point: The lists Left Clone, Right Clone, Operator Clone and Answers
are perfectly coordinated in the sense that the item k of the list Answers corresponds to the Answer obtained by items k of the Left/Right/Operator Clone lists
Add a ‘NUMBER’ sprite and costumes
6
Add one more costume
7
Make sure the costume is centred correctly
Costume for the plus sign
8
Create two lines with the same colour (use dropper tool.) Create one sleeping line and one standing line.)
Lists that we will use
9
We can choose to create these lists right in the beginning.
Alternatively, we can first do the cloning part without the lists. Then we can add the clone IDs to the lists (create the first 4 of these lists).
Finally we can later create the DELETE list.
Creating Clones
10
Create a variable for cloneID
11
Make sure this variable is ‘For this sprite only’.
Recall this will ensure that each Clone can be given a unique ID.
Initial Settings
12
Notice, we will use 5 lists in this project. We will initialize them (delete all elements) in the beginning.
Extremely important, as the first clone needs to be numbered 1.
Send the clone to the top of the stage. And set size to 30%.
Making the game run forever
13
This changes the colour of each set of clones somewhat – making the game visually appealing.
Picking up a random costume
14
Notice:
Costume 1 is 0.
Costume 2 is 1, Costume 3 is 2 and so on.
Hence the sum of the two numbers being displayed is ‘temp1 + temp2 – 2’.
We add this to the list Answers.
Create Clones and Add IDs to Lists
15
Clones that appear on the LEFT side
Clones that appear on the RIGHT side
Separation between the LEFT and RIGHT clone
Notice this will place the PLUS sign in between the two numbers
See the Clone IDs Carefully
16
Notice that we are changing the local variable cloneID before creating the clone. As a result, each clone ends up getting created with a unique ID of its own.
The clones on the LEFT Side will have IDs like 1, 4, 7, 10 , …
These are added to a list LeftClone.
The clones on the RIGHT Side will have IDs like 2, 5, 8, 11, …
These are added to the list RightClone.
The clones for the operator will have IDs like 3, 6, 9, 12, …
These are added to the list Operator Clone.
What do the clones do?
17
After the clones are created, we just get them to ‘fall’ from the top of the stage. Hence, we just do change y by -2.
Note
An alternative development of this game could be as follows:
18
Lists LeftClone, RightClone, Answers
19
1
4
7
2
5
8
LeftClone
RightClone
12
14
10
Answers
5
8
2
Number (Face Value)
7
6
8
Number (Face Value)
We are not storing the actual numbers. We are storing just the clone IDs.
Let’s take some examples!
20
Clone ID 1
Clone ID 2
Clone ID 3
Notice, the first answer is corresponding to Clone IDs 1, 2 and 3.
Clone ID 4
Clone ID 5
Clone ID 6
2 + 2
7 + 7
8 + 9
4 + 9
Notice the lists LeftClone, RightClone, Operator Clone store the clone IDs.
But the list Answers stores the actual answer (since that is what a user provides).
Forever ask a question!
21
Processing the Answer
Ask yourself, what do we need to do once the user provides an answer:
22
Processing the answer
23
Find where the answer exists.
Add those clone IDs to the DeleteList.
Remove those items from the Answers and the Clone ID lists.
(This step ensures that the lists do not become very large. Also, at all times, only the ‘active’ clones are in the list.
This code goes into the same loop as the ASK block
Let’s Take an example!
24
One more example!
25
What if more than 1 question have the same answer?
26
Keep repeating, until Answers no longer contains <answer>.
This code goes into the same loop as the ASK block
Repeated Answer Example
27
Notice, both these have the same answer -- 10
We will keep repeating the deletion till both 10s have been removed.
Also, all the 6 clones have been put on the Delete List.
Repeated Answer Example (Cont.)
28
If we answer 10.
First, the index will become 1.
Clones 19, 20, 21 will be moved to delete list.
Next, clones 28, 29 and 30 will be moved to Delete List.
Getting the clones to actually get deleted.
29
Ending conditions!
30
Keeping track of fallen clones
31
Ending and Score
32
For every 3 fallen clones, Reduce lives by 1.
Start timer
When Lives become 0, store the timer as a score and stop the game.
And you are all set!
33
www.scratchcourse.wordpress.com
Ideas to spice up the game!
34
www.scratchcourse.wordpress.com