We used Scratch to build our own Programming Language
Scratch used different blocks for different commands, so we decided to write our own Programming Language that would then call the different blocks. This may sound strange, but what it means is that we can have a simple and generic Scratch Program that can run code written in a simple file.
- Describe how our language would work.
- Create a List variable to hold all our commands.
- Create a variable to hold the parameter value.
- Write some test commands to the List variable.
- Write some code to get the parameter from our commands.
- Write some code to translate our commands to Scratch commands.
First job was to describe how our language would work with Scratch, we decided that our Programming Language would be able to Draw pictures using the Pen Down, Move, Turn and Pen Up controls.
So we needed 4 commands in our Language, we decided that all the commands would use the same format:
- First Letter would indicate the command
- Second Letter would be a colon “:”
- The rest of the command would be the parameter to use with the corresponding Scratch command.
For example to Move the Sprite 10 steps we would use:
To Turn the Sprite 90 degrees we would use
To put the Pen Up or Down we would use:
P:1 for Pen Down
P:0 for Pen Up
And then we wrote some test code to put some of these commands in to our list variable.
If you look closely you can see that the commands should draw a square, Pen down, Move 100, Turn 90 and so on until we have the square drawn.
This code was put under a When Green Flag temporarily for testing.
We then moved on to the code that would get the parameter value from the command, for example the command M:100 the Parameter is the 100 part. We used a Custom Block for this called P1. Which looked like this:
We send in the full command (M:100 for example) and then we read each letter and if we are on the 3rd letter or more, we add the letter to the P1 Variable, this is similar to what we did for the Calculator numbers.
The last piece of the puzzle was to add some code to read every item in the List and process each command, again this was very similar to the Calculator where we have an IF block for each possible command. Something like this:
We read all the items in the Program List and we check the first Letter, this will tell us what Scratch block that we need to use to run the code.
You can see for each item in the Program List we call the P1 block to get the Parameter value, which we then use in the Scratch Block.
This is what the project looks like when you run the Test Program:
The project that we built is available on the Scratch Web Site, note that I added a couple more commands Pen Colour and Pen Width as we may need these at some point.
User Name : cdadvancers1819
Password : advancers
Project : ClassVersionProgramming
Remember, you can put your own projects up there as well if you want.