Make a Maths game for your child part 2: Subtraction section

The subtraction section to your game


If you are new to this blog, it would be better if you started here and work your way through the steps in part 1 then come back this page.  Enjoy!

  • Make a Subtraction sprite
  • Amend the ‘Adding sprite’ code
  • Make the Level 1 sprite
  • Main code for subtraction
Make a subtraction sprite

First, select the 'Adding sprite'.  Click on the costume tab and select copy.  Select edit and then select the text button and change the wording from ‘Adding’  to ‘Subtract’ then click ok.  Select the costume again, double click on it, now select it and select turn into new sprite.  Your new sprite will appear in the sprite window on the bottom right and on the stage.  Position the sprite just above the 'Adding sprite'.  Rename the sprite Subtraction and select the Scripts tab.  


Figure 1











Second, add the following code.  Drag in three hat blocks from the control section: when Substitution is clicked and two when i receive blocks.  Set them to:
  • when i receive Hide all others
  • when i receive Show all L1 (you will have to create this one by clicking on the black triangle and selecting new)
Add the following code to ‘when substation is clicked':
  • Broadcast hide all others 
  • Show
  • Glide 1 secs to x:_ y:_
  • Broadcast Substitution
Set the x and y position to the same as the x and y position of the ‘Adding sprite’.  This is not that complicated just go to the ‘Adding sprite' and look in the Motion section of its code blocks then make a note of its coordinates taken from the goto x:_ y:_ in the side panel. 
Figure 2










Set the first when i receive hat block to when is receive hide all others.  Then add the following code:
  • Hide (form the looks menu)

Set the second when i receive hat block to when i receive Show all L1.  Then add the following code:
  • Go to x:_ y:_  where x and y is set to the coordinates of the origin position of the subtraction sprite.
  • Show (from the Looks section).
  • Stop all (form the control section).
Your code for this sprite should look like figure 3
Figure 3











Amend the Adding sprite code
Select the adding sprite and drag in two when i receive hat blocks

Set the first to when i receive Hide all others and add the following code:
Hide

Set the second to when i receive Show all L1 and add the following code.
  • Show
  • Stop all
Your 'Adding sprite' code should look like figure 4
Figure 4







The Level 1 sprite
Make a new sprite and call it Level 1 then add the following code.
  • when Level 1 is clicked (Hat block)
  • Broadcast Show all L1 (You need to make this by clicking on the black arrow and selecting new from the menu)

Your code should look like figure 5.  Position the sprite on the right hand side of the stage.  See figure 8
Figure 5
Figure 6



The Main code for subtraction


Select the cat sprite and drag in when i receive block and set it to When i receive Show all L1 and place a show block directly under it. Now place this block near the victor tune code so it is out of the way.

Amendment to the last part of the code you put together in the last blog.  
Look at figure 8
Figure 8


You will need to move the orange blocks at the bottom of your code to the Victor tune section of code.  Place these four orange blocks between the change Total correct answers (5) by 1' block and the 'repeat 2 loop'.  

As the program runs, each block is executed one at a time.  Making the alteration will not speed up the program, but it will shorten the code.  







Your ‘Adding code’ in your cat sprite should now look like figure 9
Figure 9



















Now make a copy of the long piece of code on the left by right clicking on the when i receive Adding hat block and selecting duplicate.  Place the copy in a clear section underneath the code you have just copied while ensuring to leave a gap between them.

You are going to change three parts to this code.
Set the hat block to when is receive Subtraction'.
Change the operator from a + b to a - c by right clicking on the green part with the + sign and selecting - sign from the menu.  Change the operator in both ask blocks to –  (see figure 10)


Figure 10















If you want to ensure there is always a positive answer, change ‘Set a pick random 1 to 10’ to ‘Set a pick random 10 to 20’.


Your code should now look and behave like this


Next blog - Add multiplication to your game
Happy programming :-)
Please feel free to leave a comment or contact me if you have any questions or ideas.



Comments

Popular Posts