Programming and Spirals This week we combined 2 Scratch Projects: Introduction We did a lot of jumping around, from one project to the other, as we had to make changes to both projects to get everything working. Once everything was … Continue reading →
The Snake project has changed a little bit from Saturday. I had to add another Sprite to get the “touching colour” code to work. So now there are 3 Sprites that move around.
The one that you move around with the Arrow keys.
One that follows 0.2 seconds behind, this one now draws the tail and is not touching the first one.
This one follows 2 seconds behind and erases the tail.
I also changed the first Sprite to Move “2” steps not one, this speeds it up a little. I have uploaded the completed project to the AthenryAdvancers account on http://www.scratch.mit.edu so you can take a look and have a go.
My challenge to you is to beat my high score of 1200
Magnify Updates
I have uploaded a Gravity project with one single Sprite to the AthenryAdvancers account on http://www.scratch.mit.edu.
You can use this Template project to create your own Magnify project, think of an idea and draw out the Sprites you will need on a bit of paper, give the Sprites Names on this bit of paper, it doesn’t have to be a masterpiece, any rough old picture will do.
Then you need to get one Image for each of your Sprites, save them all to the same Folder on your Computer.
Once you have your diagram and all your images you can download a copy of the Magnify Project and save it to the same folder as all the images.
The steps (process) to follow to use this Project are:
Rename the StartSprite to the Level 1 Sprite in your diagram
Update the Variables, for this one you only need to update the MyName variable
Change the Costume by uploading your image for this Sprite, delete the original costume
And then to add More Levels and Sprites use these steps:
Copy the Level 1 Sprite
Update the new Sprites name
Update the new Sprites variables, the MyParent needs to match the MyName of the Sprite from the level above
Update the MyX and MyY variables to where you want this Sprite to appear on the screen, you may have to test a few different places to get it right
Change the Costume by uploading your image for this Sprite, delete the original costume
For the final week we are going to convert the Christmas Card into a Christmas Game.
We will be starting with the Christmas Card that we made last week, if you weren’t there or want to start with the same one that I will be using, have a look at the bottom of this document, I have added instructions on how to get it.
The Plan for this week.
We will be adding some additional Sprites. We will be using Scratch Sprites rather than drawing our own to save time.
A sprite for Santa’s sleigh
This will move backwards and forward across the top of the screen
A sprite for the Presents
This will fall from Santa’s sleigh to the bottom of the screen, not directly down though.
This sprite will have many costumes.
As sprite for you
You will be able to move Left and Right across the bottom of the screen to try and catch the falling presents.
The Sleigh Sprite
Scratch does not have a sleigh sprite, so I used the reindeer.
He needs to start on the top left of the screen and move all the way to the top right of the screen and then turn around and go back to the top left, he will just keep doing this.
The code might look something like this:
The Present Sprite
We might have to draw this one.
The present Sprite will start from wherever the Sleigh Sprite is glide down to a random place at the bottom of the Screen, you can make the Present a random size as well to make it more interesting.
The is a Scratch block to put one Sprite in the same place as another Sprite So the code to move down the screen will look something like this:
There will also be code to count the number of presents caught. So, if the Present Sprite touches you, it will count as being caught, so you can increase your score.
When the present is caught you can change to a random costume to display the unwrapped present.
I’ll let you figure out this code as a little challenge (I haven’t done it yet 😊 )
You Sprite
This is the one that catches the presents. I used the Scratch cat, finally.
Not much code for this one, he just needs to move left or right using the left or right keys on the keyboard. So, something like this:
A nice background with Snow, Christmas Trees, a house.
We might use different Sprites for the Trees.
Flashing lights on the House and the Tree.
Falling snow.
Snow building up on the ground.
An animated Snowman.
Snowflakes
To make it even more interesting, we are going to make the snowflakes different sizes and fall at different rates depending on how big they are.
We will use the clone functionality to make the snowflakes.
Each clone will pick a random size and will glide to the bottom of the screen, how fast if glides will be related to how big or small it is.
The snowflake is going to be quite hard to draw as it will be a small white dot on a white background, so we will be careful.
The building up on the ground part, is quite easy, we just pick a random Y value to stop at, as long as it is close to the bottom of the screen we should be ok.
Flashing Lights or Tree Sprites
Again, these might be quite difficult to draw as we have to draw them over the background and get them to be over the Trees?
If we draw the complete Tree in the Sprite I think it is going to be a lot easier.
I know Windows has a Calculator, but we are going to build our own. This will give you an idea of how real Calculators actually work.
The Calculator should be able to:
1. Add, Subtract, Multiply and Divide 2 numbers.
2. Be able to work with decimal points (2.5 for example).
3. Display the sum and the result on the screen.
4. Have a “clear” button to reset everything ready for the next calculation.
Let’s look at the steps that you need for a calculation
1. Clear the variables.
2. Click on the numbers for the first number.
3. Click on the operator you want to use.
4. Click on the numbers for the second number.
5. Click on the equals button.
6. Display the result.
Hint: When you click on the Operator, this indicates we have got the first number and need to start storing the second number. See variable 6.
It sounds like there is a lot to do, but if we are smart with the way we work, there is a lot of code that is very similar and we can copy and paste it and then modify it slightly.
You will need 6 variables (for all Sprites) to share:
1. The first number in the calculation (NumberOne), not visible.
2. The second number in the calcualtion (NumberTwo), not visible.
3. The complete sum (Question), visible.
4. The operator (+,-,x,/) for the calculation (Operator), not visible.
5. The result of the calculation (Result), visible.
6. A variable to indicate what number we are working with (SecondNumber). not visible.
Hint: When you press clear, all the variables should be set to blank, except the SecondNumber which should be 0
You will need 17 Sprites, don’t panic! Remember copy and paste is your friend:
11 Number Sprites 0,1,2,3,4,5,6,7,8,9 and the decimal point.
4 Operator Sprite +,-,x and /.
1 Equals (=) Sprite.
1 Clear Sprite.
Scratch code blocks that will be used:
Operators, we will use the join operator a lot as well as the +,-,x and /.
If Else block with variable 6 to decide what number to update.
set variable blocks – lots of these!
Hint:
Start by creating the variables.
Start with just 4 Sprites, 1, +, clear and Equals
Once you can get 1 + 1 working, the rest is quite easy.
The answer is 2 (not 11) by the way, just in case.
Coding Tips:
Here is some of the code that we will be using:
This is the code when you click on a number Sprite, this example is for the number 1, can you see what needs to change if this was the number 2 sprite?
This is not just an ordinary clock though, we are going to build a clock that works in the same way as the Railway clocks works in Switzerland.
These clocks work in the following way:
The Clock waits for a signal from the master clock
When it gets the signal, it moves the minute hand on by one minute (and the hour hand if necessary)
then the second hand moves round once in about 58 seconds
It then waits for the signal again from the master clock.
This means that all the clocks on all the Stations are synchronised exactly.
In Computer speak this is called Event Driven Programming, where the code simply waits for an Event before doing anything.
So what’s the plan?
We need 4 Sprites
For the Clock Face
For the Hour Hand
For the Minute Hand
For the Second Hand
You can create them however you want but this is what the Swiss railway clocks look like and I will try and create something similar.
Remember one thing, when creating the Sprites make sure they are centred correctly.
So, the clock face should be perfectly centred and then the hands should be centred near one end something like this, I have highlighted where the centre is, with the green circle.
The code
Remember this will be event driven, so we will be using Broadcasts a lot. We will also have to calculate how far to move each hand, this means we will be using the number 360 a lot as that is one complete circle.
So just to let you know how far each hand should move:
The Second hand will move 360 degrees for each event.
The Minute hand will move 6 degrees for each event (360/60 minutes)
The Hour hand will move 0.5 degree for each event (360/60 minutes/ 12 hours)
The first event will be broadcast from the Stage:
The Minute hand will receive this broadcast and move 6 degrees it will then broadcast another 2 events, one to the Hour hand and one to the second hand so they can move.
This is the code for the Minute hand which should be enough to work out the rest of the code as well.
The extra code above is to make sure the Minute hand starts in the correct position.
And one final thing which might be a little tricky, but I’ll leave it as a challenge for you, the Second hand should only take 58 seconds to go all the way around.
Luckily Scratch comes with a Piano Sprite, which we used and expanded it to fill the width of the screen.
Next step was to create the list of all the notes, there are 14 keys on the Piano so we need at least 14 notes in our list.
We found out what notes are possible by using one of the Sound blocks and looking at what was possible
This gave us our list of possible notes:
Now on to the code…
We needed to work out what key on the Piano had been clicked, and convert it to a number between 1 and 14 so we could play the correct note from the list.
This required some tricky calculations, to convert the Mouses “X” position to a positive integer between 1 and 14.
First we added a number to make X always positive
Second we divided that by the size of a note.
And finally we rounded it up, using the ceiling function.
This ended up with the following code and a couple of Variables to store the “Extra” number to make X positive and the size of a note:
Once we had the positive integer we could use it to select the correct note to play from the list:
We did start some of the Buttons, and we will complete them next week. Notes for the buttons will be included then.
Buttons
In order to make the Piano a bit more usable we added 3 Buttons:
Record
Stop
Play
All three Buttons had two costumes, we used the second costume to change the colour of the Button, this made it easy to see if you had clicked the button or not.
The Record button, simply set a Data Flag to indicate to the Piano code that it should “record” the notes being played in a List variable.
It also flashed while recording was “on”, this is the code for the Record Button:
We also had to add some additional code to the Piano to make sure the notes were recorded:
The Stop button was quite simple, we just set the Data Flag back to 0, and changed the costume for a short while to make it clear that the button had been pressed.
The Play button was a little more complex as it need to read all the items in the List and play the correct notes. It also flashed while playing. This is the code from the Play button:
The Final project looked something like this, you can get a copy from the Scratch Web Site, see the Notes below.
Notes:
Note: My version of the project has been uploaded to https://scratch.mit.edu you can Sign in using the following details:
This week we built a Mad House with different rooms that you could go into and if you found the right place to click something would happen.
The coding for this project shows how you can re-use code, this has 2 benefits, it makes coding quicker and it also makes all the code consistent, so it all looks the same.
The Plan
As always, it’s best to have a little bit of a plan.
We decided on the following:
4 Rooms – each room would have 2 Sprites
Clicking a room would make it large
Clicking the Space key would make the room small again
We would build one room first so we could copy the code.
The Code
The first room we created was the Kitchen, the plan was when you clicked on the saucepan, it would boil over.
The Sprites need to fill the whole screen and also need a coloured background to ensure that the Mouse Click is recognised by the code.
These are the 2 Sprites that I built:
It’s not easy to see, but the “On” Sprite has some flames coming out of the pot.
Because we are having 4 Sprites, when the program starts we need to make the Sprite a quarter of it’s full size and move it to one of the corners of the Screen. We also discovered that we would need a variable (a flag) to indicate if the room was Large (1) or Small (0). We did this in the GreenFlag code like this:
We moved to top left corner.
we set the size to 50% (this makes it fill a quarter of the screen
We set the variable to 0 (0 = small, 1 = large)
We set the start costume to the Off costume.
Next step was to write the code for when the Sprite was clicked, this would make the Sprite fill the screen, slowly! It would also set the flag to say that the Sprite was large.
The biggest piece of the code was making the Sprite grow slowly, we did it in 50 steps, changing the size was easy as it just changed by 1 each step, but the X and Y positions were a little trickier as we needed to make sure they were going in the right direction and also changing by the right amount to reach the full size in 50 steps.
Only run this code if we are small (0)
Make we can’t run again set the flag to 1.
Make sure we are in front of the other Sprites
X needed to go from -120 to 0, 2.4 each time
Y needed to go from 90 to 0, so -1.8 each time
Size was simple, 1 each time
And to make it nice and smooth, we wait for .1 of a second in each step.
We then moved on to the code that should run when the Space key is pressed, this would reverse what happened when the Sprite was clicked.
Again, we only run if we are large (1)
The X and Y changes are the reverse of the ones above.
And finally we set the size back to small again (0)
Now we can move on to the code that changes the costume if the user clicked on a certain spot on the screen.
We decided that the user should click on the pan for the costume to change, so first we needed to work out where the pan is on the screen (It’s X and Y positions). This is easy in Scratch 2, when you move the mouse across the Stage the X and Y position is shown at the bottom of the screen, so you just need to place the Mouse on the 4 sides of the Pan and make a note of the X and Y values. X values for the min and max left/right positions, Y values for the min and max up/down positions. Hopefully this picture will help:
$
We needed to place the Mouse on each of the positions indicated, which then gave use the correct values to place in the code.
The code then looked like this:
We loop forever
Only check X and Y if we are large (1) and the Mouse is clicked. Then check where the Mouse is. This where we use the values we got when checking where the Pan was on the screen.
And that is it for the first Room.
The Second Room.
Now that all the code is written for the first Room, the second Room becomes a lot easier, we simply duplicate the first Room Sprite and then make the following changes to the code:
Change the 2 Costumes to something else for this new Room.
Add a new Variable for this Room
Update all the places where the Variable is set to this new one.
Update the starting X and Y positions to one of the other corners of the Stage.
Update the X and Y changes when the Room grows and shrinks to make sure it is going to the centre of the screen correctly and also back to it’s own corner.
Update the X and Y values to a new location on the Screen, depending on what you have drawn on your new Costumes.
The Finished Program
I only got 3 Rooms finished and this what it looked like at the end:
Next Steps
If you want to improve the program you can look at adding the following:
The final Room to make it 4 rooms.
Extra code change the Costume back to normal if the item is clicked again, this would check what Costume is currently, this can be done using the “costume #” from the Looks blocks.
Notes
The one I did in the class is available on the Scratch Web Site:
Todays project is a Calculator that will be able to do simple addition, subtraction, multiplication and division.
There will be a Sprite for each number button and for each type of sum, plus an equal and a clear button, so lots of Sprites. The finished Calculator might look something like this:
The Plan
As always, it’s a good idea to have a think about what Sprites, Variables and Code you might need, having a think about the Calculator, we came up with the following list:
Sprites
1 Sprite for each number button and one for the decimal point, so 11 in total.
1 Sprite for each of the sum buttons, +, -, X and / so 4 in total.
1 Sprite for the equals button.
1 Sprite for the clear button.
Variables
2 Variables, one for each number in the sum.
1 Variable for the sum type.
1 Variable for the full sum being calculated.
1 Variable for the answer.
1 Variable to indicate which number variable we should use.
Code
Code for a number buttons.
Code for the sum type buttons.
Code for the equal button.
Code for clear button.
We also thought that it would be a good idea to complete only 1 number button and 1 sum type button and make sure that was all working before adding the rest of the buttons.
The Variables
We created all the variables we would need first:
Variables
NumberOne – this is for the first number in the sum
NumberTwo – this is for the second number in the sum
Flag – this is to indicate which number we should be using when a button is pushed.
Operator – this is to indicate what sum, + – X or / we should be doing.
Question – this is to hold the complete sum, e.g. 10+23
Answer – this is to hold the answer, e.g 33
The First 3 Sprites
To get things started we created a number Sprite, a plus (+) Sprite and an equal Sprite
All the Sprites are very simple, just a square with text in the middle, just remember to make sure that there is a background colour as well, so the When Clicked code will work correctly.
The code for the Number Sprite looks like this:
The code will check the Flag variable and then add 1 to either the NumberOne or NumberTwo variable, it will also add 1 to the Question variable.
Note that the number 1 is added to whatever is already in the number or question variables.
The code for the + Sprite is very simple:
We just set the Flag to indicate that we should move on to NumberTwo, we set the Operator to + and finally we put the + in the Question as well.
The code for the equal Sprite again is quite simple as we only have one sum type.
We check the value of the Operator and if it is + we add the NumberOne and NumberTwo variables together and put the result in the Answer Variable.
The other Numbers and Sum types
Once we had the number one, the + and the equal Sprites working it was quite simple to copy and create the other number Sprites. The process was:
Right click on the Number One Sprite and Copy it.
Change the Costume of the Sprite to the next Number.
Change the code of the new Sprite to the next Number.
The bits of the code that we changed are circled in red here:
The other sum types we did the same way, copy the + Sprite and adjust the code circled in red:
As we added the different sum types we also had to add more code to the Equal Sprite, which ended up looking like this, where there is one IF for each sum type:
The Clear Button
In order to do more sums we also added a clear button, again it is a simple Sprite with some text on it, the code looked like this:
This code was placed on the Stage, as we needed to run it when we clicked on the Green Flag as well as when we clicked on the Clear Sprite.
Notice that most of the variables start with nothing in them, so they are blank, it is only the Flag that needs to have 0 in it.
Notes
The project that we built is available on the Scratch Web Site:
User Name : cdadvancers1819 Password : advancers
Project : Class-Calculator
Remember, you can put your own projects up there as well if you want.
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.
The Plan
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:
M:10
To Turn the Sprite 90 degrees we would use
T:90
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:
Notes
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.