What are we going to learn this week:
Modularistion – How to make your code simpler and easier to look after.
Sequential Processing – review
Databases – storing static data to use later in the day.
Remember the Piano last week, where we ended up with the same code repeated again and again, all that copying, yeaagh!!!
Well this week we are going to have a look at doing the same thing but a little smarter.
This is the code that we ended up with last week, where everything was the same apart from the Mouses X position that we were checking.
Well we are going to look at making this a lot smarter.
Let’s imagine if we could get the notes on the Keyboard to got from 1 to 15 instead of 48 to 72, that should then make things a lot simpler. In order to do this we need to do a little number crunching, don’t worry if you don’t understand all of what follows.
My Piano Keyboard goes from X = -222 to x = +222 it would be easier if it went from 0 – to 444?
We can sort this out easily enough, we just make our own variable to store the Mouse X value, and add 222 to it, like this:
So our Keyboard is now 444 units wide and there are 15 keys on it, that means that each key takes up (444 / 15 ) 29.6 units.
If we divide the MouseX value by 29.6, we get the number of the Key that has been pressed (ranging from 0 to 14). For example if the User clicks on the third note, this would have an X value of around -150, we add 222 to that, which gives us 72, we then divide 72 by 29.6 which gives us 2.43, if we just take the number before the decimal point, this gives us 2, which is the third key 0,1,2. And to make it three we just add one.
Now to get just the number to the left of the decimal point is not as simple as rounding, as we always want to round DOWN. So the simplest way to do it in Scratch is to just treat as some text and find out where the “.” is. If it is in the third character then we take tw characters, otherwise we just take one. Fortunately we know that there will only be a maximum of two numbers on the left. And finally we add the one at the end so we end up with this…
Now this has got us a number from 1 to 15, how do we translate that into the correct Note to play?
We need a Database of all the possible Notes that can be played. We will store this information in a List, and fill it full of data when you click on the Green Flag.
The Notes go from 48 through to 72, with a few missing inbetween. What we want to do is have the Notes numbered from 1 to 15. This will make it easier to figure out what note to play when the user clicks on the keyboard.
We fill the list as like this…
So how does this all help us get rid of the big long repeated code block that we had before?
Well, instead of looking to see where the mouse is ourselves we just ask the code that does all the Maths to figure it out for use. We can use a Broadcast and Wait for this, so we have one “if mouse down” which asks the other code what Note should be played.
If you take out the Record and Playback code, this is everything you need…