Around the World

Mathematics ScratchNormal

In this lesson we will do some math to calculate how long it would take to walk, drive or fly all the way around the world!

Note: the instructions for this project are for Scratch 2.0 but they should also work for Scratch 3.0.

1

Create a new Scratch project

Create a new Scratch project and delete the cat sprite by right clicking on the cat and then clicking on the 'delete' option in the menu that appears.

https://scratch.mit.edu


2

Add a backdrop

Open the backdrop library (by clicking the backdrop library button highlighted in yellow in the figure on the right) and scroll to find the stars backdrop. Add it to you project by double clicking on it (or selecting it and clicking the 'OK' button). This will add the backdrop to your project.



3

Create 'earth' and 'answer' variables

In this lesson we will be calculating how long it takes to go all the way around the world depending on what speed you are travelling. The circumference of the earth is 40,075 kilometers around so the math formulas we will be using are:

how long in hours = 40,075 / speed (kilometers per hour)

how long in days = (40,075 / speed) / 24 (we divide by 24 as there are 24 hours in a day)

To help us with these formulas we will need two variables. In the Data palette, create two new variables by clicking the 'Make a Variable' button.

  1. 'earth' this will store the circumference of the earth (40,075 km)
  2. 'answer' this will store the answer when we perform a calculation

When you create your first variable you will notice that new blocks for variables become available to use in the Data palette.


4

Add the earth sprite

Open the sprite library and scroll down and find the earth sprite. Select the sprite by clicking on it and then click 'OK' to add it to your project (you can also double click it to add it).


5

Setup the start

At the start we will put the earth in the center of the screen and setup the variables. We'll then use the lookssay Hello! for 2 secs blocks to give some information and instructions.

Add the following code to the earth sprite:

  1. eventswhen green flag clicked 
  2. datahide variable [answer] 
  3. datahide variable [earth] 
  4. dataset [earth] to 40075 
  5. motiongo to x: 0 y: 0 
  6. lookssay Hello! for 5 secs 
    (replace [Hello!] with operatorsjoin hello world)
    (replace [hello] with [The circumference of the earth is ])
    (replace [world] with operatorsjoin hello world)
    (replace [hello] with dataearth)
    (replace [world] with [ kilometers])
  7. lookssay Choose how you want to go around the world by clicking on an option below. for 5 secs 

6

Create a 'round [number]' custom block

As our formulas are using division, we may end up with answers that have many decimal places. For example:

40,075 / 1,086 = 36.90147329650092

To make things easier to read we will round these numbers to 2 decimal places, so the answer to the above sum would appear as 36.90.

We will be rounding our answers several times so to makes things easier for us we will create a custom block that has all the code for rounding a number to 2 decimal places.

In the More Blocks palette, create a new block by clicking the 'Make a Block' button.

  1. Type in 'round' as the name of the block
  2. Click on 'options'
  3. Click the button beside 'Add number input:'
  4. In the box that appears, change 'number1' to 'number'
  5. Click the 'OK' button. 

You will notice a large define round number block appears in the code area and a round 1 block appears in the More Blocks palette. 

Add the following code to the define round number block:

  1. dataset [answer] to 0 
    (replace [0] with operators  )
    (replace the first [] with number
    (replace the second [] with [100])
  2. dataset [answer] to 0 
    (replace [0] with operatorsround  )
    (replace [] with dataanswer)
  3. dataset [answer] to 0 
    (replace [0] with operators  )
    (replace the first [] with dataanswer)
    (replace the second [] with [100])

7

Create a 'calculate [type] [speed]' custom block

We will also create a custom block that performs our calculations to work out the time in hours and in days and says the answers on the screen.

In the More Blocks palette, create a new block by clicking the 'Make a Block' button.

  1. Type in 'calculate' as the name of the block
  2. Click on 'options'
  3. Click the button beside 'Add string input:'
  4. In the box that appears, change 'string1' to 'type'
  5. Click the button beside 'Add number input:'
  6. In the box that appears, change 'number1' to 'speed'
  7. Click the 'OK' button. 

You will notice a large define calculate type speed block appears in the code area and a calculate   1 block appears in the More Blocks palette. 

Add the following code to the define calculate type speed block:

  1. lookssay Hello! for 3 secs 
    (replace [Hello!] with operatorsjoin hello world)
    (replace [hello] with operatorsjoin hello world)
    (replace [hello] with type)
    (replace [world] with [ travel at])
    (replace [world] with operatorsjoin hello world)
    (replace [hello] with speed)
    (replace [world] with [ kilometers per hour...])
  2. moreblocksround 1 
    (replace [1] with operators  )
    (replace the first [] with dataearth)
    (replace the second [] with speed)
  3. lookssay Hello! for 3 secs 
    (replace [Hello!] with operatorsjoin hello world)
    (replace [hello] with [So it would take ])
    (replace [world] with operatorsjoin hello world)
    (replace [hello] with dataanswer)
    (replace [world] with [ hours to go all the way around!])
  4. moreblocksround 1 
    (replace [1] with operators  )
    (replace the second [] with [24])
    (replace the first [] with operators  )
    (replace the first [] with dataearth)
    (replace the second [] with speed)
  5. lookssay Hello! for 3 secs 
    (replace [Hello!] with operatorsjoin hello world)
    (replace [hello] with [That's ])
    (replace [world] with operatorsjoin hello world)
    (replace [hello] with dataanswer)
    (replace [world] with [ days!])

8

Add the person, car, plane and spaceship sprites

Add the following sprites by opening the sprite library and adding each sprite:

  • Jaime Walking
  • Car-Bug
  • Airplane
  • Spaceship

We want the spaceship to be pointing towards the right instead of straight up. Select the spaceship in the sprite list and then do the following:

  1. Click the 'Costumes' tab.
  2. Choose the 'Select' tool (the button with the arrow image).
  3. Click on the spaceship in the editor, an orange box will appear around it.
  4. Drag the small circle (that's above the box) so that the spaceship is pointing towards the right.


9

Code the person

At the start when the green flag is clicked, we'll shrink the person sprite and position the sprite at the bottom. Add the following code to the Jaime Walking sprite:

  1. eventswhen green flag clicked 
  2. looksset size to 30 % 
  3. motiongo to x: -186 y: -124 

Next we'll program that when we click this sprite, it will send a message to the world sprite to calculate the hours and the days. We'll also make the sprite appear above the earth and walk around it. Add the following code to the Jaime Walking sprite:

  1. eventswhen this sprite clicked 
  2. eventsbroadcast [human] 
    (to create a new message click on the black arrow and then 'new message...')
  3. motiongo to x: 0 y: 62 
  4. motionpoint in direction 90 
  5. controlrepeat 360 
    1. motionmove 1 steps 
    2. motionturn 1 degrees 
  6. motiongo to x: -186 y: -124 

10

Copy the code into the car, airplane and spaceship sprites

The code for the car, airplane and spaceship sprites is nearly the exact same as the person sprite. So to save time we're going to copy the code into the car, airplane and spaceship sprites by dragging it from the code area of the Jaime Walking sprite, into each sprite in the sprite list.


11

Update the car, airplane and spaceship code

Now that we've copied the code from the Jaime Walking sprite we just need to slightly change it for the car, airplane and spaceship so that they appear in the correct position at the bottom and when they're clicked they send the correct message.

Change the code for each sprite as follows:

Car

  1. Change the [-186] in both go to x: -186 y: -124 blocks to go to x: -80 y: -124 
  2. Change broadcast [human] to broadcast [car] (use the black arrow to create a new message)

Airplane

  1. Change the [-186] in both go to x: -186 y: -124 blocks to go to x: 50 y: -124 
  2. Change broadcast [human] to broadcast [plane] (use the black arrow to create a new message)

Spaceship

  1. Change the [-186] in both go to x: -186 y: -124 blocks to go to x: -80 y: -124 
  2. Change broadcast [human] to broadcast [space station] (use the black arrow to create a new message)
12

Receive the messages and calculate

Our last step is to add some code to receive the messages and run the calculations. Depending on which message we receive we will use the calculate block and pass in the name and the speed that it travels.

Add the following code to the earth sprite:

  1. eventswhen I receive [human] 
  2. moreblockscalculate People 5 

  1. eventswhen I receive [car] 
  2. moreblockscalculate Cars 100 

  1. eventswhen I receive [plane] 
  2. moreblockscalculate Planes 1086 

  1. eventswhen I receive [space station] 
  2. moreblockscalculate The international space station 27724 

13

Test it out!

That's all the code for the project! Test it out by clicking the green flag and then clicking one of the sprites, it should calculate the time for the hours and the days while the object circles around the earth!


End of lesson
Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu