In this project we will do some math to calculate how long it would take to walk, drive or fly all the way around the world! This project comes in 2 parts, this part 2.
In a separate lesson you should have completed part 1 of this project. Open your project in Scratch and continue to the next step in this lesson.
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 My Blocks toolbox, create a new block called 'round decimals' and give it a number input called 'number'.
Once you've created the block give it the following code. This does the following:
define round decimals (number)
set [amount v] to ((number) * (100))
set [amount v] to (round (amount))
set [amount v] to ((amount) / (100))
You can create your own custom blocks and give them their own instructions (code). To create a custom block follow these steps:
A 'Define My Block Name' will appear in the code area. You can add your code underneath this and then use the My Block Name block in the 'My Blocks' toolbox anytime you wish to run that code.
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 My Blocks toolbox, create a new block called 'calculate' with a text input called 'type' and a number input called 'speed'.
Once you've created the block give it the following code.
define calculate (type) (speed)
say (join (join (type) [ travel at ]) (join (speed) [ kilometers per hour ...])) for (3) seconds
round decimals ((earth) / (speed))
say (join [So it would take ] (join (amount) [ hours to go all the way around!])) for (3) seconds
round decimals (((earth) / (speed)) / (24))
say (join [That's ] (join (amount) [ days!])) for (3) seconds
define round decimals (number)
You can create your own custom blocks and give them their own instructions (code). To create a custom block follow these steps:
A 'Define My Block Name' will appear in the code area. You can add your code underneath this and then use the My Block Name block in the 'My Blocks' toolbox anytime you wish to run that code.
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.
when I receive [person v]
calculate [People] (5) // we walk at 5 km per hour
when I receive [car v]
calculate [Cars] (100) // 100 km per hour for cars
when I receive [plane v]
calculate [Planes] (5) // 1,086 km per hour for planes
when I receive [space station v]
calculate [The international space station] (277724) // The international space station travels at 277,724 km per hour!
define calculate (type) (speed)
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!