1 of 34

Rapid Fire Numbers!

Vineet Srivastava

www.scratchcourse.wordpress.com

2 of 34

In this lesson, we will …

  • Build a game inspired from the popular ‘Tux of Math’.
  • This is a fairly complex game requiring us to use lists, cloning, and keeping track of individual clones.
  • We will also have to code this game carefully, in a step-by-step manner so that we do not get overwhelmed.

2

www.scratchcourse.wordpress.com

3 of 34

Rapid Fire Numbers Game!

  • Numbers pop up on the screen in pairs.
  • We have to rapidly answer a math question
    • For example, the sum or product of these numbers.
  • If we answer the question correctly, the numbers disappear.
  • If more than 1 pair has the same answer, all the pairs disappear.
  • If the numbers drop without us answering correctly, we lose a life.
  • The goal is to stay in the game for as long as possible.

3

www.scratchcourse.wordpress.com

4 of 34

Building this game

  • This game will require us to use concepts of lists very carefully. We will maintain several lists for different purposes.
  • Also, we will maintain separate IDs for all the clones created.
  • Thus, this game will bring together several concepts that we have learnt so far.

4

5 of 34

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

6 of 34

Add a ‘NUMBER’ sprite and costumes

  • Use Glow-0.
  • Add costumes, Glow-0 to Glow-9.

6

7 of 34

Add one more costume

  • We add one more costume, the PLUS sign.
  • Notice, the game can be extended by using more operations, in which case we can use multiple other costumes.
  • Use two crossing lines, of the same colour as the numbers. (See next slide for a trick).

7

Make sure the costume is centred correctly

8 of 34

Costume for the plus sign

  • Duplicate one of the numbers, say number 9.

8

Create two lines with the same colour (use dropper tool.) Create one sleeping line and one standing line.)

9 of 34

Lists that we will use

  • For this game, we will use 5 lists:
    • Left Side Numbers (List Name: LeftClone)
      • Store Clone IDs of the numbers appearing on the LEFT side.
    • Right Side Numbers (List Name: RightClone)
      • Store Clone IDs of the numbers appearing on the RIGHT side.
    • Operator (ListName OperatorClone)
      • Store CloneIDs of the operator.
    • Answers (List Name: Answers)
      • Store the correct answer
    • Clones to be deleted (List Name: DeleteList).
      • IDs of the clones that need to be deleted.

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.

10 of 34

Creating Clones

  • In this game, we will create 3 clones at a regular interval, for example, every second or every two seconds.
  • These clones are
    • A number on the left side of the pair.
    • A mathematical operation (plus)
    • A number on the right side of the pair.
  • We will use CLONING and COSTUMES to get these effects.

10

11 of 34

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.

12 of 34

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

13 of 34

Making the game run forever

  • In order to make the game run forever, we will put the clone creation logic inside a forever.
  • You can even consider making the wait random.
  • Also, set x to be random – ensure it is limited to a range where the clone on the right is nicely visible.
  • Use colour effect to ensure that different sets appear somewhat different.

13

This changes the colour of each set of clones somewhat – making the game visually appealing.

14 of 34

Picking up a random costume

  • We will use two variables for picking up random costumes for our number sprite – for the left and right clones.

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.

15 of 34

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

16 of 34

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.

17 of 34

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.

18 of 34

Note

An alternative development of this game could be as follows:

  1. Write down the code for clone creation. (Create 3 clones at a time).
  2. Make sure clones fall.
  3. Then Add clone ID and create the lists.

18

19 of 34

Lists LeftClone, RightClone, Answers

  • Recall, the lists LeftClone and RightClone store the IDs of the clone.
  • The list Answers stores the correct answer that we get from the two clones that are shown.
  • Important point is that the ‘location’ (or index) of these items is the same. We will use this property later.

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.

20 of 34

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

21 of 34

Forever ask a question!

  • In a separate forever loop, Add the ask and wait statement and make the etx empty.
  • This will cause the answer box to appear, but with no text.

21

22 of 34

Processing the Answer

Ask yourself, what do we need to do once the user provides an answer:

  1. We must determine if the answer is a valid answer for any of the questions on the screen.
    1. We can find this by looking at the list Answers.
  2. If Yes,
    • We must find the IDs for the clones which resulted in this answer.
    • We must delete those specific clones.
  3. If No,
    • Nothing to be done

22

23 of 34

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

24 of 34

Let’s Take an example!

  • If an answer of 6 is provided, index will be 1.
  • Then, clone IDs 1, 2 and 3 will be moved to the DeleteList, and these lists will become empty.

24

25 of 34

One more example!

  • If an answer of 11 is provided, index will be 2.
  • Then, clone IDs 4, 5 and 6 will be moved to the DeleteList.
  • These clone IDs will be removed from the LeftClone, Right Clone and Operator Clone list.
  • 11 will be removed from the Answers list.

25

26 of 34

What if more than 1 question have the same answer?

  • Wrap the statements above into a ‘REPEAT UNTIL’ statement.
  • That way, all the clones that have this answer will end up in the delete list.

26

Keep repeating, until Answers no longer contains <answer>.

This code goes into the same loop as the ASK block

27 of 34

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.

28 of 34

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.

29 of 34

Getting the clones to actually get deleted.

  • Notice that we had previously given the code for the clones to fall.
  • We will now add a bit more code to actually delete the clones. For this purpose, the deleteList is used.
  • If the DeleteList contains the Clone ID, delete this clone.
  • Also, remove that clone ID from the DeleteList – this keeps the list small – only the active clones are part of the delete list .

29

30 of 34

Ending conditions!

  • We want the answer to be give BEFORE the numbers fall so much that they cross the RED line.
  • Create a backdrop with a RED line.

30

31 of 34

Keeping track of fallen clones

  • We keep track of how many clones have fallen.
  • Recall, for every answer that we miss giving, 3 clones fall down. This is captured in the variable ‘fallenClones’.

31

32 of 34

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.

33 of 34

And you are all set!

  • This activity requires using lists very carefully.
  • Also, we looked at how clones can be given their own IDs and how these IDs can be used.
  • Revise the concepts and apply them in developing a Rapid Fire Game of own.

33

www.scratchcourse.wordpress.com

34 of 34

Ideas to spice up the game!

  • The first and most obvious extension of this game is to incorporate more operators. In the class we used only the PLUS sign. You could ALSO use minus, multiply etc too. Basically create a random operator. And set the Answers list accordingly.
  • You can consider asking more elaborate questions, like 2 + ? = 8 (In this case the answer should be 6). This will require more lists and more careful coding.
  • Finally, a number of things can be done on how the clones travel – for example, they can go on an inclined path. Etc.
  • Color effects can also be used creatively for this game.

34

www.scratchcourse.wordpress.com