This week we looked at both the Progam project and the Recursion (Branching) project. with a plan to get the output of the Recursion project be the input of the Program project.
The 2 Projects
The Program project that we did a couple of weeks ago can read a list of commands and get Scratch to perform those commands. We programmed it to be able to:
- Put the Pen Up or Down (P:0 or P:1)
- Move some steps (M:100, where 100 is the number of steps)
- Turn in a different direction (T:90, where 90 is how far to turn)
Each Command was identified by the First Letter.
The Recursion project that we did last week can draw a branching Tree pattern. The commands it uses to draw the Tree are:
- Pen Up and Down
- Move some steps
- Turn in a different direction.
Joining them together
The Recursion Project changes
You can see that the Recursion project using the same commands as the Program project, but in order to get the commands from the Recursion project to the Program project we have to add some code to the Recursion project.
First job is to create a List, we called it Commands as it will store all the commands that we have run.
We then need to read through the code and wherever there is a Pen Up, Pen Down, Move or Turn we need to add an item to the Commands list.
This is the code and I have highlighted where
You can see that I have marked 7 places where we need to add code to Insert into commands.
When the Inserts are added the code will look like this, just remember that the Insert has to put in the Command letter that Progam needs Joined to the same action that the Recursion code is doing. All the entries go in the last place in Commands.
You can see that we have added a delete all to start and then after the Pen down we have added a P:1 to the last place in Commands.
On the Pen up we have added a P:0 to the last place in Commands.
Then we did the same to the Code that did the drawing of the Branches.
Notice that all the Inserts use a JOIN block, the first section is the Command letter that the Program project is expecting, the second section is the exact same code from the code block above.
For example when the code turns Degrees * -2 the insert also has Degrees * -2 in the second section of the JOIN.
If we run the Recursion Program now, the Commands list will end up with lots of commands, if you have 10 Branches it creates 5117 Commands!
You can see that Commands starts with a Pen Down (P:1) and then starts all the Moves (M:…) and Turns (T:…) and when the Tree is finished there are 5117 items in Commands.
Next step is to Export the Commands to a file. This is simply a case of Right Clicking on the Commands List and selecting Export, just make sure you add a .txt to the file name and also remember where you saved it!
The Program Project changes
Now we switch projects and open the Program Project.
First thing we changed was to remove all the test code, add some instructions and move the broadcast Play to when the space key pressed.
Next step, click the green Flag and follow the instructions.
So right click on the Program List and Import the file that you Exported from the Recursion Project. And then press the Space key…
Hmm, not quite what we were expecting, but close, the Tree is on it’s side and the drawing has started in the centre of the screen rather than at the bottom.
This test has highlighted a number of problems with our Program Project. We don’t have any code to set the Direction of the Sprite or the X and Y Positions.
Well that is easy to fix, we just add some more Command Letters to our Programming code:
- D:nnn – This will set the Direction to point in, where nnn is the Direction to point in.
- X:nnn – This will set the X Position, where nnn is the X position.
- Y:nnn – This will set the Y Position, where nnn is the Y Position.
This gave us 3 more IF sections in our code like this:
You can see that the new commands are almost the same as the old ones, we just change the letter we are looking for and the code block that gets run if we find one of the New letters.
So D:0 for example will run the Point in Direction code with the value 0 (Up).
The Recursion Project – more changes
Now this where we have to jump back to the recursion project and add some more Inserts so we can set the correct direction and the correct X and Y positions. Again this is not too difficult, we just look for code that sets the direction or X and Y Positions and add some Inserts into the Command list.
You can see 3 extra insert code blocks, 1 for the point in direction code and 2 for the go to x: y: code block. We have to add 2 here because our Program project is very basic and doesn’t know how to do a Go To…
The Commands List will now get 3 new entries at the beginning and have a total of 5120 commands now. Don’t forget to Export this new list to test the Program project again.
Program Project – test 2
Now that we have the Recursion project writing out the extra Direction and X and Y commands we can test again in the Program project.
And, test 2 is a little better than test 1.
It’s still not perfect, the line is all the same thickness and the colour is always the same.
But you could easily add some more Program letters to add this extra functionality.
As always the code is on the Scratch web site, I have added 2 projects that should produce the results you see above:
User Id : cdadvancers1819
Password : advancers