video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca, your Computing teacher.

And this is Lesson Three of Programming Essentials in Scratch Part Two.

And if you're wondering why I am holding a star today that's because the game that you'll be investigating today involves the main character collecting lots of stars.

And it's probably one of my favourite games out of the unit.

So you're going to really enjoy it, I think.

To get ready, make sure that you've got your Scratch account and that you've got a pen and paper handy to make notes and answer some of the questions.

And also really important, try and move as many distractions out of the way as you possibly can.

And then we can begin.

In this lesson, you will evaluate when to use different loops.

And you'll be doing that by investigating that programme that I just mentioned.

To start off, I want you to describe the function of each type of Scratch loop.

On your screen, you'll see those three types of loops.

You've got the forever loop, the repeat loop and the repeat until loop.

And I want you to write down with your pen and paper what you think each loop actually does.

Just pause the video while you do that.

Fantastic.

That first loop is the forever loop.

And the forever loop will, whatever instructions are inside that forever loop, they will keep running over and over and over again until the programme is stopped.

That is a function of the forever loop.

The repeat loop carries out those instructions that are inside it for a set amount of times.

For example, if you wanted to draw a square, you could get it to draw an edge and turn and then repeat that four times to draw the whole square.

Repeat loops, if you know the number of times that you want those instructions to be repeated, then you would use a repeat loop.

And then the final loop is the repeat until loop.

The repeat until loop will keep running until that condition becomes true.

And you know there's a condition there because you've got that symbol there where you would put your condition in there.

Repeat until, all of those instructions that are inside that loop, will be repeating until that condition that you've put in that box is true.

Those are your three types of loops that you can use in Scratch.

Now we're going to investigate some code and you're going to investigate a Scratch programme that uses different loops to achieve different things in the programme.

And this programme is a game called Tera's Adventure and you have to jump to dodge the snowballs.

And you dodge the snowballs and you also catch the stars as well.

It's quite a fun game to play and you'll be able to have a look at that in a moment.

Like we did in Lesson Two, we're going to investigate the code to see what it actually does and try and read it and really sort of figure out what all those little bits of code do.

We'll do the first few as a practise together and then you'll have a go by yourselves working on it independently, and then we'll come back and we'll check.

The first practise question is, count all the forever loops and the repeat until loops, how many are there altogether? Let's go to our Scratch programme and have a look.

Wow, that is a lot of code.

Sometimes when you look at other people's programmes you're like, "Oh, my word, that is so overwhelming.

There's loads of code there, how am I ever going to understand what this programme actually does?" And that's the whole point of doing this investigation.

Because it allows you to really look at the fine details of each part of this programme so that you can start to piece the puzzles together and see how it actually works.

That first question was how many repeat loops and forever loops are there in total? We can have a look at this programme.

We can see that there are three sprites for us to use.

Let's have a look.

We've got, no, that's not a repeat until loop.

We can't count that one.

That's not one either.

We've got forever loop.

We've got one.

We've got another one, two, three.

That's a repeat until loop.

I don't think there are any more on that screen.

See if you can see them on this one.

Let's just move it around.

There we go.

We've got three so far.

How many can you see? We've got a forever loop there and we've got a repeat until loops.

And now we're on five.

Don't think there's any more.

Let's look at the snowflake.

I feel like I should move around again.

Have a look.

How many are there? We've got the forever loop and we've got the repeat until loop.

The total amount of repeat until loops and forever loops that are used there is seven.

That would be our answer to that question.

Let's go back to the worksheet and see what our next thing is.

Practise two says how many count-controlled loops are in Tera's Adventure, across all sprites? If you remember, those count control ones are the ones that do it based on knowing the number of times that something needs to happen.

This is going to be our repeat loop where we put an amount or something like that or some kind of count inside that little box.

Let's go back to the Scratch programme and see if we can find those and count them.

Our count-controlled loops are our repeat loops.

If we have a look here, we've got a repeat and then we've got a value, a set number of times that we want that instruction to repeat.

And we sometimes might put a variable in there or other things in there, but we are definitely repeating it for a set amount of times.

And that's why it's called a count-controlled loop.

We've got here our repeat 10.

We've got one.

We've got repeat 30 so we've got two.

Can't see any more counts on there, can you? Nope, I can't.

Let's have a look.

Can you see one on here? Nope.

Can't see one on there.

And I can't see one on there either.

There's just two that I used here.

Only two times the repeat loop has been used.

That's interesting.

The answer there is two.

For practise three then, it says, how similar are the blocks of code on the star sprite and the snowflakes sprite? We need to go and have a look at those two pieces of code and see how similar they are.

Let's go and have a look.

We're looking at how similar the code is for the star sprite and the snowflakes sprite.

Let's have a look there.

I've got when green flag clicked hide forever go to this location.

Wait, show repeat until.

Then we've got another one when I receive game over, stop.

Okay, let's just have a look at the other one then.

Do they look exactly the same? Go to 202-104.

Are they exactly the same? 202-104.

Pointing direction minus 90.

Yep, it looks to me like the code for the star and the snowflake are exactly the same.

I can't see any difference at the moment.

No, I can't see any difference.

How they relate to each other, they look exactly the same, don't they? That's interesting.

I wonder how that will start to work when we run this programme, we actually see what happens with it.

Now we've had a little practise at investigating those with those three practise questions, what I want you to do is use the worksheet to carry out a further investigation.

And please don't worry if you can't find the answer or if you're not sure of the answer to some of the questions cause we will go through it after you've had a go yourself.

It's really important that you have a look yourself first, independently, to really start to get to understand what this programme is doing.

And please feel free to actually play the game.

I know I haven't said do that yet and I haven't shown you how it works or anything like that.

But all you have to do is you press the space bar to jump and you try and avoid the snowflakes and you try and catch the star.

You can have a little go at playing that game as well.

The short link is on the top of the worksheet for you to look up to, so you can find it through there.

And I want you to use your pen and paper to write your answers down to each of the questions that are on the worksheet.

Have a go.

Pause the video now and then come back to me when you're done.

Hopefully you've had a really good time investigating and having a go at that programme there.

Now we're just going to go through some of the answers just to make sure that you got those right.

And if you were struggling, just to give you a little bit help as well.

Question one, was look at the repeat until loop that is used with the star sprite.

What condition needs to be true for the loop to terminate? Let's go and have a look and see what the answer was.

On the star sprite, what condition needs to be true in order for the loop to terminate? We're looking at this one here.

We've got repeat until.

And then we've got our condition here and it says touching edge or touching Tera.

We want and also this is quite similar from Lesson Two with our building one where it's got move 15 steps.

It's keeping moving in one direction and he's repeating it until and it's basing it on a condition.

It's quite similar to that one in that building's loop that we did in Lesson Two.

You might have seen a little similarity there.

Let's have a look.

Touching edge or touching Tera.

Let's just have a look, let's just run the code and just see what that actually means.

We're on the star as well, we're going to have to look out for the star.

It stops moving when it gets to the edge, but it also stops moving when it reaches Tera.

You can see there.

We've got, what needs to be true for this condition to terminate? Well, it needs to either touch the edge, that's the edge of the screen there or touch Tera, our sprite, Tera.

That's what needs to be true.

Question two, in the same repeat until loop, what does the move 15 steps block actually do? And I kind of answered that in the last one, didn't I? It's very similar to the building one from Lesson Two.

And what it's doing is it's keeping that star moving over gently until the loop terminates.

It makes it appear that the star is flying across the screen.

That's what that's doing.

Question three, the star sprite also contains a forever loop.

Describe the role of the first two motion blocks inside the forever loop.

Let's go and have a look and see what they actually do.

Here are those two move blocks that are inside the forever loop.

Those first two.

We've got go to X location and we've got point in direction minus 90.

And again, it's very similar to that building programme that we did last, the fly cats fly from Lesson Two.

What it's doing is it's setting the position of the star sprite so that it's off the screen over here.

And then it's pointing it in the direction of minus 90.

And what that means, basically, is pointing it so that it's looking left.

It's making sure that the star is off the screen and it's facing left.

And that's so that when the move 15 steps block starts running, it's actually moving in the correct direction.

And it hasn't decided to go up or down or wherever it thinks it should be.

We've actually told it where to face and where to move.

The next question, number four, says, the same forever loop contains this block of code.

It says, wait pick random one to three seconds.

What do you think this block of code does? Let's go and have a look.

We know what the weight block does.

It just pauses for a number of seconds.

Whatever we've told it to do, and this pick random, what it's actually doing is it's picking a random number between one and three.

That means that the star sprite doesn't always come every one second or every two seconds or every three seconds.

It appears random.

We're never quite sure when the star sprite is actually going to come out.

If you have a look while we play the game, you can see, there isn't a set amount of time for when the star actually comes out.

Snowflakes come out that time.

It's not coming out every second or every two seconds.

It's totally random when it actually comes out.

That's what that pick random one to three is actually doing.

It's picking a random number between one and three.

Now question five, looking at the same forever loop, describe the purpose of its design.

What that forever loop is doing is it's making sure that the stars keep moving towards Tera or to the edge of the screen until the game is over.

That's the whole point of that forever, that block of code, is it's making sure that the star just keeps moving towards Tera so that she can start earning points or losing points if she doesn't collect the stars either.

Then we've got question six, which says, what do you think this block of code on the star sprite is used for? Why is it needed? It's possible you haven't seen anything like this before.

You might have but let's go and have an investigate and see what it actually does.

This is one that you may or may not have found tricky if you've not seen this before.

We've got this event block and it's saying when I receive game over, and that means that a word is being broadcast to it.

And when it receives it, it's going to carry out those instructions.

The instructions are to stop other scripts in the sprite.

Now this is another way that we can stop our forever loop.

Again, you might not have seen this before.

'Cause at the moment you're probably thinking, well forever loops, you can only stop those by pressing the stop button.

But actually you can use this block called stop other scripts in this sprite.

And this will stop that forever loop from running.

Then it'll stop that whole block from running.

It's quite a useful block of code that you might use it when you're making your own games.

It's even got this other bit that says hide, and that's to do with the looks.

What that's going to do is it's going to hide wherever the star sprite is on the screen.

It's going to hide it so it looks like the game is finished.

It says when I receive game over, and you may or may not have done this, but actually if you go to Tera's blocks of code, you can actually find the block of code that says broadcast game over.

Can you find it? It's actually here.

It says broadcast game over.

And now you might not have looked at this code yet.

You might not understand what this code actually does but this is a timer it's counting down 30 seconds.

When those 30 seconds are up, it's going to say game over.

When it receives game over, it's going to hide everything.

When it gets to the end of those 30 seconds, it's going to stop everything from moving on the screen so it appears that the game has ended for the user.

Question seven was looking at that repeat block.

Why has this code in the Tera jumps subroutine been used instead of turn 360 degrees? There's several ways that you could have done that.

You could have just known the answer and just put the answer.

Or actually you could have gone in and modified the code and just changed it to a different block and just see what happens.

Let's have a go at doing that.

Let's go to Tera's code and see what's going on.

We've got here, repeat 10, turn 36 degrees.

The question was, why not just put turn 360 degrees? Why do you have to have that repeat 10 there? What's the point of it? Let's just run the code and see what happens at the moment.

If I do press the space bar like that, then Tera jumps in the air and twists and spins around like that.

That's quite interesting, isn't it? You can see her, see her actually spinning around.

Let's just have a look at what happens if we don't do that.

I'm going to take that out.

I'm going to take that out.

I'm going to put this in and I'm going to change it to 360 and I'm going to put that back in.

Let's run the code and see what happens now.

It's very, very boring now.

She's not spinning at all.

It's quite boring to watch.

It's not very exciting.

If I press stop and put it back in, like so, remember to change that back to 36.

Then I'm going to press the green flag again and now she's jumping away and she's spinning around.

That repeat loop needs to be there so that it'll step it through and you can actually see it happening.

'Cause otherwise the 360 in one go will just do it so fast that you won't be able to see it.

Having that repeat loop there really helps with the visuals, with making it look like she is actually spinning around.

Moving on to question eight, it says, why does the play sound block need to be inside the forever loop? What is its purpose? Again, with our investigation, we could just take it out and just see, well what does it do when it's not there compared to when it is there? So that we can see what actually happens.

Let's go and have a go.

Here's our forever loop then with the play sound until done, in there.

At the moment, you've probably realised already that when we've got this played the sound in the background is playing.

I don't think you can hear it very well at the moment but the sound in the background is playing.

If I just stop that, if I take it out of the forever loop, I might have to be very quiet so that you can hear this.

Let me just turn my sound up a little bit, see if you can hear this.

If I press play now, let's see what happens to that sound.

Ah, so it stopped, which isn't very exciting 'cause there's still lots of gameplay to go.

We've still got 15 seconds left of this game and the music stops us.

Not very good, is it? Our forever loop is making sure that that music repeats and repeats until our game is actually over.

Let's just stop it and put it back in.

And now it should work fine.

For question nine, it says, this block of code is used as a countdown time before the game.

Which other type of loop could have been used for the same purpose? Describe how it could have been used.

Let's just look at what this one is actually doing.

It's setting a time into 30 so that's going to be a variable and it's holding the value 30 inside it.

And then it's saying repeat until that variable timer holds the value zero.

And as soon as it does, it's going to move on.

It's going to break that loop and it's going to move on to the next lot of instructions.

And then inside that repeat until loop, we've got wait one second.

And we've got a change timer by minus one.

It's essentially doing a countdown timer and that repeat until loop is going to keep going until the timer gets to zero and then the loop will break.

Another kind of loop that you could have used for this situation could have been a repeat loop.

A repeat with a value in so a count-controlled loop.

You could have done repeat 30 and then you could have had the wait one second inside it.

And that would have done pretty much the same thing.

But if we did that, then we wouldn't know the actual value that was being held in the timer at the time.

It's possible that's why this block was used instead of the other one.

Let's have a look at the next question.

It says question 10, in the Tera angry subroutine, why has a repeat 30 loop been used for the costume instead of a repeat until loop? Let's go and have a look and see why.

If we take a look, we've got repeat 30, change colour effect by 25.

And what that is doing, Tera angry is when she's been hit by a snowflake, because those things actually don't help her at all.

There we go.

When she's angry, the change colour effect by 25 makes it look like she's been hit and she's been a bit shocked by something.

We need that effect to be visible.

And we've got here repeat 30, it's doing that 30 times, very, very quickly as well.

It's just changing.

It's going through that effect to change the effect values 30 times like that.

Now we know that we want it.

We know that we want it to happen 30 times and change the effect 30 times.

That's why using a count-controlled loop in this situation is better than using a repeat until loop.

You could have done a repeat until loop, you could have made a variable.

And you could have said, like we did with the timer, you could have had the 30.

Let's just go back to the timer one.

If you have a look at that, we could have done repeat until effect equals zero.

We could have set effect to 30 and we could have counted that down but that would have really overcomplicated this loop.

Whereas we can just really do this quite simply just by using the repeat 30 loop instead.

It's just quicker to programme and it does exactly what we needed to do.

That's why we've chosen the repeat 30 loop instead of a repeat until loop.

And last question, question 11 says, this block of code uses a forever loop.

Could a repeat until loop have been used instead? Explain your answer.

I personally would have gone in and put a repeat until loop and just to see, just to actually find out if that's the answer.

Let's go and do that now and see if it does work.

But this block of code over here is actually looking, it's got a forever loop and it says, if touching star then we want Tera to cheer.

We're going to go to this subroutine and she's really happy.

And she gains a star.

The variable increases by one up here.

And if she's touching a snowflake, then she's going to be very angry because she's going to lose all of her stars.

It says set stars to zero.

That's quite an important part of the game.

But we've used a forever loop here.

And the question was, could you have used your repeat until loop instead? Let's have a look.

Let's take that forever loop out.

I'm not going to move it over there.

I'm just going to leave it there for now.

And I'm going to go to control and I'm going to find the repeat until loop.

And I'm going to put that there.

Now at this point, then you have to start thinking, well what condition needs to be true in order for this loop to break? This is going to keep playing until the game is over.

Then you might start thinking, well, maybe I've got this other thing that says, received game over mode and then it stops.

Maybe I could do something like that.

But actually if you go to the event, there isn't a box that would fit in there to do with receiving game over.

You would need one of these.

that's not going to work, but perhaps there's something else, perhaps there's something else that we've been counted or been looking out that could be true in order for this loop to break.

And actually when the game is over, timer is going to be zero, isn't it? If we look here, we've got this condition, we've got repeat until timer equals zero.

I wonder if it's still going to work if I make this block go into the repeat until block.

Let's just have a look.

I'm going to duplicate it and I'm going to put it in there.

And while we're testing, cause I don't want you to have to wait 30 seconds, I'm just going to change the timer to five, just so that we can see what happens.

Let's have a look.

I'm going to press the green flag.

And the game has stopped because she has stopped receiving those stars and it seems to have worked.

So it does work.

It's just probably, I don't know, maybe it's a little bit harder to programme.

It might take you a little bit longer to think those few steps out.

In this situation you probably could have used either loop in this situation, but perhaps the forever loop was just a little bit easier to programme at that point in time.

That's why the forever loop was chosen.

Hopefully now by investigating Tera's Adventure and looking at all those blocks of code, you've got a really good understanding of how that programme actually works.

But also you've really started to see when to use loops in certain situations and that some are perfect for solving a problem.

And sometimes there might be one or two options when you're making these decisions.

But typically there is a better loop to use when you're making your decisions.

And we can evaluate these loops.

Now we're not going to use Tera's Adventure anymore for this lesson, but please feel free to remix that programme and do whatever you want to it.

You could change it so there's a different scene, a different character.

You can have different things flying at Tera.

You could have more stars or more snowflakes.

You can have different timings.

And at the moment it's got pick random one to three, it could be pick random 1 to 10 or something like that.

There's lots of things that you can do with that programme to remix it and make it more your own.

Please have a go at doing that as an optional activity if you want to.

But we're moving on now and looking at evaluating the loops.

I'm going to give you some loops to have a look at.

And I want you to decide which one would be the best loop to use.

This first one then is, a programmer wants to draw one single square in Scratch.

Select the loop that you would use and explain your reasons.

I want you to write that down now.

I want you to pause the video and write it down and come up with your answer.

Off you go.

Awesome.

This is the one I picked and then I'll explain why.

I went for this one.

And the reason I went for this one is because with a square, you know it's got four sides.

You know that the instructions need to repeat four times.

When you know the value, you're most likely going to pick that repeat loop where you say the amount of times.

Move and then move, turn right, that's going to draw a square.

The one on the left, the forever loop.

Well, that's just going to keep drawing a square forever.

That one's completely useless.

The one on the right though, the repeat until sides left, that would work, but have a look at it.

It's much, much more complicated for you to understand it but also for you to programme it as well.

It would take you much longer to programme that and get the solution.

Although it works, it takes a lot longer to programme and it's much better to choose the other loop, the repeat four loop.

Because the hard work's been done for you, it's count-controlled and it's just going to do it four times.

You don't have to add any extra bits in.

That's why the middle one is better.

Let's have a look at this one then.

A programmer wants to display a 10 second countdown in Scratch, select the loop that you would use and explain your reasons.

Again, I want you to think about that and write it down, pause the video, and then come back when you're ready.

Off you go.

Brilliant.

Here's the one I choose.

And then I explain why.

I actually went for the middle one.

Now you might've gone for the left one.

You might have gone for the right one.

But the middle one, I've chosen because it doesn't involve too much extra thinking.

I know it's going to be a countdown timer for 10 seconds.

And it needs to be displayed as well, that value.

There's a variable there.

And then it can be displayed on the screen.

I know it's going to happen 10 times.

That hard work has been done for me with that, just writing the number 10 in there.

And it's going to do the countdown.

It's going to do exactly what I want to do and the hard work's been done for me.

Whereas the one on the left, you've got that extra bit in there with the condition and thinking about the condition and what needs to be true in order for that loop to break.

Although that one would work, it just involves a bit of extra thinking in order to programme it.

Why use that when you can use a loop that's done the hard work for you? The one on the right.

Well, I mean, yeah, it would work cause it's got stop that script when it gets to zero.

But it's not the best option.

That's quite a complicated way of doing it.

That one's not sensible at all.

The next one then, a programmer wants to make a sprite dance until the game is stopped by the user in Scratch.

Select the loop that you would use and explain your reasons.

Again, pause the video and write your answers down.

Great.

Here's what I chose and I'll explain why.

I chose the forever loop.

Hopefully that's the one you all chose as well.

I went for the forever loop because, have a look at the problem, what does the programmer want to happen? To make the sprite dance until the game is stopped.

We want it to keep going forever and ever and ever until the user stops it, until some something external stops it.

If you look at the middle one, repeat 1,000, well that might work because it might last for the amount of time that until they press stop.

But it's not going to work in every single situation.

That's not going to work.

The one on the right with the repeat until loop and time left equals zero and having time left, I don't think even that will make sense.

That one's definitely not right.

In this situation, we've got our problem and we've got a loop that perfectly matches that solution.

That's the one we choose.

In this situation, the forever loop is the only one you could pick.

With the last one, there were the two options, but with this one, there is only one, it's the forever loop because that's the problem that we've been given.

Finally, what I want you to do is write down three key facts about evaluating loops that you have learned today and write those down so that you can remember them for when you're looking at this in the future.

Pause the video now.

Brilliant.

That is the end of today's lesson.

I hope you've enjoyed it.

I hope you've enjoyed Tera's Adventure and that game and that you're raring to go at modifying that and making that your own.

If you'd like to, please ask your parent or carer to share your work on Instagram, Facebook or Twitter, tagging @OakNational and #LearnwithOak because we would really love to see if you've remixed that programme.

I would really, really love to see what you've done because I bet you can come up with some fantastic ideas.

Well done and I'll see you soon.