Scratch Advanced – Week 15 – Radio Waves

What are we going to learn this week:

  • A tiny little bit about Waves and how clever they are.
  • A pratical demonstration.
  • Thinking – Yes, it’s tough, you will have to do that part 🙂
  • Coding – Sine Waves

I will hopefully demonstrate how differnt types of waves behave.

I will admit, that I am not an expert here and will probably be unable to answer any of your questions, but you see that’s where you come in and start thinking about these things yourself.

A Pratical Demonstration.

What’s in the Box?

I will bring the Box and we can take a look inside, I might even have two boxes, you never know.

Thinking.

Hmmmmm….

Coding.

Remember the Gravity simulation we did, well we can do the same with Waves.

We will build a few Transmitters and a Receiver which can be “tuned” in to the different transmissions. In fact this would be an ideal Application to do using the Networking in Scratch. Does anyone remember that from last year?

Lets see how we get on, we might set this up with one person as the Transmitter and one person as the Receiver.

I have some of the coding done to Transmit a Wave, but will need help in building a transmitter and obviously something to broadcast as well 🙂

Ok, I know Iam supposed to put up the code before the day, but better late than never.

Here is the code for the Transmitter of the radio Waves. Careful with the SIN code has there are a few nested calculations there.

Transmitter

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

And here is the code for the Reciever.

Reciever

Scratch Advanced – Week 13 – Gravity and Speed

What are we going to do this week?

  • Gravity
  • Speed
  • Momentum

We are going to look at Gravity and how it works here on Earth. We will also be looking at how, when you travel fast enough, you can appear to defy Gravity, this is how Satellites stay in orbit around the Earth.

We will also take a look at momentum and how things behave once they are moving.

And in order to demonstrate the effects of Gravity,  we will build a Spaceship Game, were you have to launch yourself into Orbit.

Depending upon how we get on this may run over two weeks.

Now back to Gravity. If you  want to get all smart, there is a very clever equation that Sir Issac Newton came up with back in 1687. But we won’t go there for now.

Gravity is basically a force of attraction and the closer you get to something the stronger that force is, also the bigger somehting is the more gravitational force it will have.

It’s a very small force, it takes the whole of the planet Earth to hold us down.

So how are we going to show this in Scrartch, well we will start with a Rocket on the Earth, that we have to Launch upwards, Rockets produce Thrust, which pushes them up into the air. The Earth has Gravity which is trying to pull the rocket back down, but the further you travel away from the Earth the weaker the Gravity and so the Less Thrust you need.

I think I just spotted three Variables there…

1. Thrust

2. Gravity

3. Height

They might come in useful in our program.

We are going to try and build a Scratch Game that will launch a rocket into orbit. This mioght take us a couple of weeks, but should be good fun.

So lets try and write down the rules that we need the Game to obey:

1. Pushing the up arrow should fire the rocket and get us moving.

2. If we stop firing the rocket then gravity will start to pull us back down

3. The higher we go the less Gravity can pull us back down.

4. If we travel fast enough, it can make us appear to defy Gravity, that’s how satellites stay in orbit, and the Moon for that matter. But we will leave this out to start off with as it can start to get really complicated 🙂

To start us off I found a Lunar Lander Game that has some of the elements that we need, this will be a good starting point to  get us going.

To start off we only need a simple sprite, a ball with two legs, that should do for our rocket 🙂

 

Scratch Advanced – Week 12 – Welcome back

What are we going to do this week:

1. Re Cap on the exam.

2. A Magic Eight Ball, Question and Answer Game.

3. Flow Charts – how to explain code with pictures 🙂

1. Re Cap on the exam

I though we might have a quick review of a couple of the questions from the Exam. There was one question that nearly everyone missed and I think it might have been the way I wrote the question, so we’ll just go over it quickly.

Question 8:

If you want two pieces of code to run together (parrallel processing) circle all the ones that you could use:

a. Broadcast

b Broadcast and wait

c. Green Flag

The Broadcast and wait will NOT run things in parallel, it will Wait for the other code to finsh before continueing.

2. A Magic Eight Ball, Question and Answer Game.

I thought we would use some of the List skills that we used in the Christmas Game to build a Magic Eight Ball type Question and Answer game.

So here are the requirements:

1. A Genie should appear from a Lamp.

2. There should be a friendly Sprite Character who will ask if you if you have any questions.

3. There will be a place to type in your Question

4. Questions can only be ones that have a Yes or No Answer.

5. A nice background to match your friendly Sprite

6. A random answer should be selected from the following list:

It is certain
It is decidedly so
Without a doubt
Yes – definitely
You may rely on it
As I see it, yes
Most likely
Outlook good
Yes
Signs point to yes
Reply hazy, try again
Ask again later
Better not tell you now
Cannot predict now
Concentrate and ask again
Don’t count on it
My reply is no
My sources say no
Outlook not so good
Very doubtful

I decided to use a Genie in the Desert, emerging from a Lamp to answer my Questions.

Here are my Sprites:

Genie

 

Lamp

 

 

 

 

And here is my stage:

Giraffe

 

 

 

The Code is Quite simple, again I have used the Stage as the Starting point for all the Code as I think this makes quite good sense as there is only one Stage, but generally many Sprites.

First we have to populate our Answer List with the different possible answers.

Then we can let the Genie know it is time to do the talking…

Now, I added the Lamp as, we need code under the Lamp to get things started. I decided that you should have to rub the Lamp to make the Genie appear. Once the Genie was out of the Lamp, we will let you ask the Genie a Question.

The code, well you know how to populate lists, so I’m not going to go through that.

For the Lamp, we need to check to see if the Mouse is touching the lamp and the Mouse Button is down, if it is we will keep counting up till we reach 10 and then let the Genie take over. We can do that with a Broadcast. But don’t forget that we also want the Genie to appear out of the Lamp, so as we are counting to 10, we need to make the Genie grow.

This is what I came up with…

LampCode

 

 

 

 

 

For the Genie, there are a few pieces of code, one to set him up, one to make him appear and grow, and finally the one where he gets you to ask a Question.

GenieCode

 

 

 

 

 

 

 

And finally, I came up with a third, invisible Sprite, but it allows the Giraffe on the background to say a few things as well. I came up with another list of things that he could say and populated those at the begining as well.

GiraffeCode

 

 

And I have been really kind and uploaded the finished Scratch Project onto the Scratch WebSite.

Scratch Advanced – Week 10 – Christmas Game – Final Episode

What are we going to learn this week.

1. Global Variables – A  Variable that all Sprites can see.

2. More requirements

3. Ninja Belts

And that is about it for new things. We are primarily going to be concerned with getting the Present working in the Christmas Game and the final display of what you have caught.

Now, we will start in reverse and look at the “more requirements” part first. Remember we had a requirement for the game that a present should fall and if we catch it, it should be put under the tree. Well we can write that out a little better as another set of Requirements, so here goes:

More Requirements:

The present should:

1. There should be Ten presents.

2. The presents should fall in the two minutes at random times.

3. Each present should fall from the Sleigh to the ground.

4. If the present touches the fire, then it is caught, and should be moved under the tree, anywhere under the tree, but not all on top of each other.

5. Each present should be a different size and colour.

6. If a present is caught we need to keep track of how many we have caught.

So, what we can do is create one present that meets all of the above requirements and then copy it.

For number 6, it looks like we need a Global Variable so all the Present Sprites can update it. So lets make that..

And while we are on the subject of Global Variables, if the Present Sprite needs to drop from the Sleigh, it needs to know where it is, so the Sleigh needs to update a Global Variable with it’s X value all the time.

If you select the Stage, when you create a variable it is automatically a Global Variable. You don’t get the option to create it “for this Sprite only”

We need one for the X Position of the Sleigh and one to record the number of Presents.  Don’t forget to update the Sleigh Sprite so that it updates the X Position variable. Something like this:

SleighCode

Notice that I don’t have the Green Flag to Start the Game, I have used a GameStart broadcast, this is Broadcast from the Stage where I have all my initialisation code as well.

While we are creating the Global Variables, we can also create the two lists that we need to store the names and descriptions, so two lists as well.

This is what I have to set the lists at the begining of the Game:

StageCode

Now back to the Present and the requirements.

This is the code that I have that meets the requirements… Random Colour, Random time delay, after the delay, go the X Position of the Sleigh…

Present01

Now we have to sort out how to recognise if the present has been caught by the fireplace. I picked one of the colours in the fire, and used the if touching code to find out if it has been caught. If it is caught, then we move it to the Tree.

We don’t just move the Present to the same point though otherwise they would all land on top of each other. I pick a random point under the Tree. Like this:

 

 

 

 

 

 

 

Scratch Advanced – Week 9 – Christmas Game Part 2

What are we going to learn about this week.

1. Code reuse
2. Testing and Debugging
3. Exams!

1. Code Reuse – how to plan ahead and only write something once, remember the button we did that we exported and used again in other projects? Well, you can do the same kind of thing
within Scratch if you need several Sprites to do exactly the same thing.

First you create one Sprite and add all the code that you need for that one Sprite.

When you are happy with the way that Sprite works then just right click on the Sprite and select duplicate. The key to getting the best out this of this functionality is to ensure that the first Sprite that you have done has everythin working correctly.

2. Testing and Debugging – This is normally done using the original requirements, you basically go through the list of things that the Game should do and tick them off if it works. It helps if you have the list written down and then you know what you are testing.

And, it also helps if you get someone else to test your Game, as they will test it differently to you.

So here is the list of requirements from last week:

1.It needs to look like my living room with a nice window and lovely fire place.
2.At Christmas, when you look out the window it is normally snowing.
3.We normally have our tree in the corner, with a nice set of lights on it.
4.Santa normally flies across the top of the house and drops the presents down the chimney, lately he has not had a good aim, so we will need to be able to move the fireplace left and right.
5.If we catch the presents, we can move them to under the tree.
6.I don’t want the game to last more than two minutes.
7.And when it ends I want to know what all the Presents are.

It’s best to use Presentation Mode to do your testing.

And go through each item on it’s own. At the moment we haven’t completed all the code for everything, so you can only test the parts that are complete.

If it passes, perfect, if not, it’s time to start debugging.

Debugging, the first thing to do is run your test again, to confirm that it’s not working. There are two very important things to know here:

1. What SHOULD happen – this is what is described in the list of requirements.
2. What is happening – this is the result of your test.

When you have those two bits worked out, you can start looking at the code to see what is wrong.

In Scratch, it is possible to see which piece of code is actually running, this is done using the Edit – Start Single Stepping.

Try it out, but DON’T use the Green Flag to start everything, just click on one of the Control Blocks to start that one piece of code.

3 Exams – ok, don’t panic, but it is that time of year again.

We are all going for the Blue Belt this time, and your knowledge will be tested on what we have learnt in the last 8 weeks. Here is a refresher of the things that we have covered:

Week 1. Movies.
Week 2. Sliders and Buttons
Week 3. Random Numbers, Stamping, Saving Images.
Week 4. Exporting Sprites
Week 5. Code Reuse – Parallel and Sequential Programming.
Week 6. Lists, for saving data and reading it out.
Week 7. Code Modules, using LIsts as a Database.
Week 8. Coding to Requirements.

Advanced Scratch – Week 8 – Planning for Christmas

Dear Santa,

I would like a nice new game for Christmas.

I would like it to be a Christmas game where you have to catch the presents in the fireplace

Just to make it easier, here are some more things that I would like it to do.

  1. It needs to look like my living room with a nice window and lovely fire place.
  2. At Christmas, when you look out the window it is normally snowing.
  3. We normally have our tree in the corner, with a nice set of lights on it.
  4. Santa normally flies across the top of the house and drops the presents down the chimney, lately he has not had a good aim, so we will need to be able to move the fireplace left and right.
  5. If we catch the presents, we can move them to under the tree.
  6. I don’t want the game to last more than two minutes.
  7. And when it ends I want to know what all the Presents are.

So you might need to think about the following:

  1. What to put on the Stage and what to use Sprites for.
  2. Whether one Sprite should be in front of another Sprite. This is something that we have not done before.
  3. How many costumes each Sprite needs
  4. Do you want to use one Sprite for all the Presents and just change the size and colour.
  5. Stamping could be used to get the Presents under the Tree.
  6. How to do the Snow and the Flashing Lights on the Tree.

Advanced Scratch – Week 7 – A fancy Piano

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…

 

Advanced Scratch – Week 6 – Lets Build a Piano

 

What are we going to learn this week, well it might take two weeks to get this completed.

Lists – How to put stuff in.

Lists – How to get stuff out.

Record – How to record things that happen, we might use a list for this 🙂

Playback – How to replay things that have happened, we might use a list for this 🙂

Mouse – where is it on the Screen, we use this to determine which Piano key has been pressed.

Lists

So what is a List, well they are like variables, you get to them from the same screen, but there are just a lot of them joined together. You would normally use them for similar types of things, like a Shopping list for example, where you put different items of food:

Chocolate, Crisps, Biscuits, Tea, Coffee.

We are going to use a list to store all the different Piano Keys that have been clicked. We can then use the same list to Play the Notes back again.

We also get to use the Button Building Block to add some buttons, one for Record and one for Play etc.

So what do we need for out Piano, well, we need a keyboard for a Start, we only need 16 notes (2 octaves) and we can either draw it ourselves or borrow one from somewhere.

Something like this would be good

Then we need to play the right note depending upon where the user has clicked.

There is one piece of easy code that just needs to be repeated over and over again with slightly different numbers. Here is a sample of what is needed and then you just need to repeat it with the different numbers for the location (mouse X / Y position) of the different keys.

 

 

 

Playback Time – so how do we play back what we recorded, well I just imported my Go Button Sprite to start the Playback and added the following code to run when I recieve the Broadcast from the Go Button… Remember how we did this before.

I needed to create another variable as well to keep track of which Note in the List we should be playing.

And finally, there is some initialisation code that keeps things nice and tidy when you click the Green Flag. But you could import another Button and use that to reset the Notes List, which would be much nicer. And don’t forget we already have the Button as a Sprite, so this should be easy to add in.

The go to x,y code I found to be quite useful as when you Click on the Piano Keys it can move the Keyboard around a little. But again, remember this will only happen in “Design” mode, when you switch to Presentation Mode, you won’t see this problem.

 

Advanced Scratch – Week 5 – Code Recycling and parallel processing

What are we going to learn this week.

  • Buzzer Game – Using old code and a little new code we can build a game.
  • Code recycling – How to re-use code that you have already written.
  • Parrallel vs Sequential Processing – sounds worse than it is 🙂

Buzzer Game

Navigate through the Maze to earn More Money, the more you get the Harder the game becomes.

Most of the code for this game you have already written, cool… But there is some  new code that you will need.

So, lets do the New code first…

We need a coin, or something that you can move with the mouse and that will BUZZ and Shake when it touches a particular colour.

This is my Coin here…

I have one costume for normal and one costume fo buzzing!

We now need to set up some code so that you can move it with the mouse, this is similar code to the button code we did, and I found a better way of doing it!

What you want to happen is if the mouse is on the Coin you can Click to grab it and it will move wherever the mouse moves until you let go of the mouse.

We also want it to Buzz! when it touches a particluar colour, to test this I just drew some lines on the Stage and used the Dropper tool to select the right colour.

SEQUENTIAL PROCESSING – This is my Buzz code, I just move it around a little and make a noise. Notice on the Broadcast that it is a Broadcast and Wait, this allows the Buzz code to work correctly.

 

And the code that allows you to click and drag the coin around the Screen goes something like this…

Advanced Tip 1. To test this piece of code, you will need to run your program in full screen mode, otherwise Scratch is in “design” mode and moves the Sprite anyway.

The Maze.

CODE RECYCLING PART 1. Remember last week whne you did the Truchet Patterns, they were pretty maze like, weren’t they. Well all we need to do is import the Truchet Sprite, maybe play around with it a little bit and you will have your maze.

What I changed in my Truchet Pattern, was the size of the initial square, I made it bigger, and also I didn’t stamp along the top line or the bottom line so the coin has somewhere to aim for.

The other thing that you might have to change is if you have used curved lines, you might want to change them for straight lines, otherwise the gaps between them can be very close. Alternatively you can make your coin very small.

So once I had adjusted my pattern, I ended up getting something like this.

CODE RECYCLING PART 2. Remember the Button we did, well let’s bring that into the Game as well so we can use it to Start everything off

Now you need to connect everything together. This is where the fun Starts…. 🙂