Loading...
Hi, it's Rebecca here, your computing teacher.
I'm going to be taking you through lesson two of our programming essentials in Scratch part two unit.
Today, you're going to need your Scratch account and you're also going to need a pen and paper for you to answer any of the questions that you're given today.
You're also going to need to remove any distractions out of the way that you can so that you can focus and concentrate on today's lesson.
So when you're ready, let's begin.
In this lesson, you will identify condition-controlled iteration, and you will use condition-controlled iteration.
So to get you started, we're going to make a prediction.
This code here is for a sprite called building.
Make a prediction about what might happen to the building when this code is executed.
So take a good long look at it and make a prediction.
Write your prediction down with your pen and paper so that we can have a look afterwards and see if your prediction was correct.
So pause the video now so you can make your prediction.
Great.
Now let's have a look in Scratch and see if you were right.
So here is that programme now in Scratch and you can see we're on the building sprite and here's the code for the building sprite.
So hopefully, you've got your prediction written down and we'll just see if you are right.
It's quite a tricky one, this one.
So let's see if you were correct.
So I'm going to press the green flag and see what happens.
Ah, so the buildings are moving to the left, and then they seem to be changing as well.
Each time a new building goes off the screen, a different building comes on the screen.
So there's quite a lot going on there.
So let's take a look at the actual code.
So we've got here when green flag clicked, we've got a forever loop, and that forever loop will run until the game is actually stopped cause that's how they work.
And then we've got set X to 280.
So the location of the building is being set to 280.
Then we've got next costume.
So that means that there's going to be a change of costume.
And we've also got repeat until.
So this is a new block that you might not have seen before.
So repeat until X position is less than minus 220.
I wonder what that means.
It must be something to do with it going off the screen there.
And then we've got change X by minus 10.
So that's just moving it left by minus 10.
So there's quite a lot of going on there.
It would be interesting to know if your prediction was correct.
Was it a thumbs up or a thumbs down? Which one was it today? Maybe you got it half right? You may be out a bit of a guess and you're a bit unsure about what that repeat until block actually did.
Now, the exercises that we're going to be doing next will hopefully, allow you to really look at that block of code properly and investigate it to figure out exactly what is happening.
So now we're going to start investigating this code and we're going to do some exercises together first of all, and then you're going to go off and investigate it yourself with a focus task.
So we're going to work through this together, first of all, just to try and get used to investigate in our code.
So, first of all, the first question if we look at practise one, it says, what is the following block of code being used for? And it says, set X to 280.
So that is a question for us to go and investigate in our Scratch programme.
So in Scratch, the block that we're focusing on is this set X to 280.
And I've just changed my background screen on here so that you can see the coordinates and the lines so that you can get an idea of how this works.
So X goes across and Y goes up and down.
And this line here, if you go this way, X gets to a higher number.
And then if you go this way, then X becomes a minus number that keeps getting smaller and smaller.
So our first position is set X to 280.
So when we run our programme, it's very, very fast how it works.
But when we run our programme, you can see the X.
The highest one that you can actually get to on the screen is 240.
So 280 is setting it off screen.
So that's making sure that it comes from off the screen, it doesn't suddenly just appear there.
So if I just press stop on that for a second.
If I change this to 240, you'll see that the building just suddenly appears.
It's still quite quick, but the building just suddenly appears.
So just having it back 280 makes it look a little bit more realistic, like it is flying in from the side.
So that's what the set X to 280 block is doing is making sure that the building is off the screen and it is setting the location of the building each time this loop is run.
So our next practise question is what happens when you set X to 50 and it says, remember to change it back as well.
So we've already tried changing it to 240.
So I wonder what happens when you change set X to 50.
Let's have a go.
So I'm going to change it to 50 now and just see what happens.
Like so.
And then I'm going to run again and see what happens.
Ah, so now it's only coming up around halfway through the screen, isn't it? Just slightly to the right of the centre bit.
So again, it's not looking very realistic at all now, is it, the way it's working.
So I'm just going to stop that and I'm going to set it back.
So our answer to that question is it's coming roughly halfway through the screen rather than coming off screen now.
Our third part to this question says, what is the following block of code being used for? It's the one that says change X by minus 10.
So let's go take a look in Scratch and see if we can figure out the answer to that question.
So change X by minus 10 is this one here, and it's inside this repeat until loop.
So I think that's doing something with it and that's making something happen here.
So let's just run it again and just see what's happening.
So we know that change X by minus 10 moves something just by minus 10.
And we know that that's not happening once because the building isn't just moving minus 10 and then that's the end of it.
It keeps going.
So there's obviously something going on here with this repeat until loop that's making that happen.
But change X by minus 10 is basically just moving it to the left by minus 10, and then because it's in a loop it keeps going on that minus 10.
So let's take a look at the other practise question.
So we've got practise four now.
What happens when you change X by minus 30? So that should be interesting.
And remember to change it back.
So let's go and take a look.
So before we change it to minus 30, so this change X to minus 30, what do you think might happen? Just have a little pause, have a little think.
It'd be interesting to see what actually does happen.
So let's just change it to minus 30.
Minus 30.
I'm going to press the green flag.
What's going to happen? Ooh! That is going very, very, very fast now.
Scratch the Cat is going to have to fly very fast to dodge all of those buildings.
That's interesting.
That one.
So we'll see.
I wonder what happens if I change it to minus 100.
Are you even going to be able to see the buildings? Let's see.
Oh wow.
That is super fast.
So let's change it to minus one.
I think it's going to be mega slow now.
Let's see.
That is a very, very easy game now.
It's super slow.
So let's just change it back to minus 10.
So that change X by minus 10, that block there, is saying how many steps to move.
And then when it's stuck in a loop as well it's being repeated over and over again.
It actually makes it, if we use a larger number, a larger minus number, it's going to make it go super fast.
But if we use a small number, like minus one, it's going to make it go super slow.
So that's interesting to see what happens there.
Our final practise question is number five and it says, how do the two blocks relate to each other? So you've got set X to 280 and you've got change X by minus 10.
So now we know that set X to 280 is saying that the start location for that building that's within that forever loop.
So every time the building starts again, it's going to be set to 280, the location 280.
And then change X by minus 10 just moves it along by minus 10.
And because it's stuck in a loop these actions keep repeating.
So it keeps going to move the building to that X location at the beginning of that forever loop.
And then during the loop, you've also got the building moving by 10 steps and stuck within a different loop.
So they're relating to each other by thinking about the location of the sprite.
So that is our investigate that we're going to do together.
And now it's up to you to do the rest independently.
So I want you to find the worksheet, and I want you to look at the investigate questions.
Write down your answers and really think about them.
Now, if you do struggle with some of them don't worry because I am going to go through them with you.
So if you find some a little bit tricky then that's absolutely fine.
The investigate tasks are really for you to start looking at that code and really starting to understand what's happening.
But don't worry if you get a little bit stuck.
It's absolutely fine.
So write your answers down as you go along and then we'll come back and we'll recap on what has happened.
So pause the video and then come back and press play when you're done.
So let's have a look at the answers to these questions.
So we'll look at question one and two together.
We've got, how many costumes does the building sprite have, and does the programme ever run out of costumes? So hopefully you've got your answers and let's check if you are right.
So first it says, how many costumes does it have? So if we go to, we're on the building sprite and if we go to costumes, we can see that the building has got 10 costumes.
So hopefully, you put 10 costumes.
And then the second question was, does the programme ever run out of costumes? Well, yes or no.
But I mean, the answer is no because it's stuck in a forever loop.
So when it gets to costume 10, it just goes straight back to costume one again, and that's what that next costume block is actually doing.
It just goes through the loop a little bit like when you're, if you're listening to music on a playlist and you've got it on repeat, it's just going to go all the way through those songs and then just go back to the top, and it's just going to play those same songs again.
And exactly the same thing there is happening with these costumes.
So no, it's never going to run out of costumes.
And the answer was 10 to how many costumes it actually has.
So the question three then, it says, what needs to happen for the programme to stop? And why do you think this? Well, at the moment, our block is in a forever block, and a forever block can only end when you press that red button to stop the actual programme.
So what needs to happen for it to, for the programme to stop? You need to press the red button to stop the programme.
And why do you think this? Because it's stuck in a forever loop.
So hopefully, you've got something a little bit like that.
Then we've got question four.
This block is a condition like the ones you've seen in an if statement.
When will this condition be true? So let's take a look at it.
We've got X position.
So X position is referring to where the building is located at that time.
And then we've got less than minus 220.
So this is going to be true when the X position of the building is less than minus 220.
So when it's got all the way to the end and a little bit off, then that condition is going to be true.
So the next question, when will the condition be false? Well, pretty much all the other times the condition is going to be false because it's going to be the opposite of that, it's going to be greater than minus 220.
So it's going to be further along, further up the X axis.
So that's when the condition is going to be false.
And then the third one, what specifically will happen when this block of code is executed? So this is when we start to put all the pieces of the puzzle together and really try to understand what that loop is actually doing.
So let's take another look in our Scratch programme.
So now we're back in our Scratch programme.
You can see there we've got our repeat until loop.
So this is our new kind of loop.
It's our condition-controlled loop cause we've got a condition here.
And it's saying repeat until the X position of that building is less than minus 20.
So when that building has got to the end, we stop repeating.
But all of the times I want you to keep repeating this instruction over and over and over again.
So that's what that means.
Repeat until.
So we've got an instruction inside it.
So repeat these instructions that are inside it until X position is less than minus 220.
So when it's got all the way to the end.
So keep repeating these instructions.
So that's what, it doesn't just do minus 10 once.
It just doesn't step once and then stop.
It keeps repeating until that building has got to the end of the screen, to that X position of minus 220.
So that's what our repeat until loop is doing.
It's conditioned-controlled.
So it'll do it until that condition becomes true.
So for question seven, we have all of the code underneath when green flag clicked is surrounded by a forever block.
What do you think the forever block is being used for? So our forever block makes sure that everything inside it is repeated over and over and over again until we stop the programme.
So it's being used to make the building keep moving and keep appearing as well all the time.
So that's what it's being used for.
Then you've got, when will the forever block stop running? And hopefully you knew this from the practise exercises.
It's going to stop running when we press that red button to actually stop the programme.
And then question nine, the repeat until block repeats until the condition becomes true.
Why does the loop start again once the building has left the screen? So let's just go back to Scratch and just check that we can get the right answer there.
What we want to know is why does it start again? And that's because we've got a loop here, a forever loop, and then inside that we've got another loop.
So this will run forever, but this will only run until the position gets to minus 220.
But because it's in a loop, it's going to do it again and again and again.
So let's just look at this whole loop as a whole.
We've got forever.
So all of these things inside the forever loop are going to keep repeating forever.
It's going to set it to 280, so our building.
It's going to switch costumes so that it looks like we've got different buildings that he's flying past every time.
And then the building is going to keep moving until it gets to position minus 220.
And the reason it keeps moving is because we've got their change X by minus 10 which moves the building to the left by minus 10 within this loop.
And then this loop stops.
But then because it's in a forever loop, it goes back again.
So it goes set X to 280.
So it's putting that building all the way back over to the right again.
And then we've got a new costume, so a new building comes, and then again it starts moving to the left.
So this repeat until loop will repeat until the building has got to minus 220.
But because it's in a forever loop, it's going to then do a new building and it's going to move the next building, and it's just going to keep repeating that until we stop the programme.
So this new loop that we've been looking at today, this repeat until loop, is condition-controlled iteration.
Condition-controlled iteration is used when you want an instruction to repeat based on a condition.
And we set our condition there.
So where is the condition in this block of code? Take a look at it and decide where that condition actually is.
Have a little pause.
Here's the answer.
So were you right? So this is our example of our condition and you have seen these in your if statements as well when you've been using them in Scratch before.
So it's just a condition.
A condition can either be true or false.
And this is going to repeat until that condition becomes true.
So now you've had a play around with investigating that code and hopefully, you really understand what is happening there with that code.
You're going to start modifying the code.
And I'm going to do a demonstration for you just to show you how to do a few modifications yourself.
So we've got to add movement to the cat.
So at the moment the cat does nothing, does it? It just stays in one place the whole time.
We're going to make the cat fall with gravity.
You know, it'd be a bit boring if you were just pressing up and down all the time.
If it falls with gravity, it makes it a little bit more exciting.
We're going to make the cat fly.
And we're going to stop the cat from moving off the screen as well cause that can happen when you start getting a bit more into it.
And you'll see what I mean when we start doing that.
So I'm going to show you a demonstration of how to do those things.
So the first thing that we're going to do with our modify is we're going to make it so that Scratch the Cat can move up and down.
So to do that, we need to add some controls and some blocks of code to Scratch the Cat.
So if you look at cat flying sprite there's no blocks of code there at all.
If we just have a little wander around there's definitely no code there at all.
So to make Scratch the Cat go up and down, we're going to have to, and we want to do it on the key presses when we press up and when we press down.
We need to add some movement.
So we go to control.
No, we don't.
We go to events, and we want to do it on a key press.
So we want, if it's up arrow is pressed, we're going to do something, and we're going to have another one for down arrow is pressed.
So at the moment, if I press those things nothing is going to happen because I haven't added any instructions to them.
But what we want to do is for Scratch the Cat to go up when I press the up arrow, and down when I press the down arrow.
So is that going to be changing the X axis or is it going to be changing the Y axis? Which one do you think? Thumb up for X and thumb down for Y.
Which one do you think it is? It's Y.
So we're going to be doing, changing the Y axis.
The Y axis is the up and down, and the X axis is across.
So to go up, we're going to change Y by, let's just do 10 for now cause we can always change it later.
So change Y by 10.
And then to go down, it's just going to be the opposite.
So it's going to be minus 10.
So we've got green flag now.
Okay.
So if I hold it down, it does it.
And if I press it in little steps, it does it too.
So we're starting to get a little bit of a game there now.
So our next modification is to make Scratch the Cat actually fall from gravity.
Cause at the moment, Scratch the Cat is just going up and down whenever I press it, when I press the green flag.
This game isn't very realistic because really gravity should be pulling the cat down and then I should be pressing the up arrow to make it fly, to make it go up.
So what I'm going to do is I'm going to get rid of the key press for going down, but I'm going to make it fall constantly all the time throughout gameplay.
So if I want to make something happen forever, what kind of loop do you think that I need there? Hopefully, you got it right.
It's a forever loop.
So what I'm going to do is I'm going to take this piece of code out and I'm going to move this, and I'm going to have, I'm going to find the forever loop.
Where are you, forever loop? There you are.
So I'm going to put the forever loop in, and I'm going to run my code and see what happens.
Ah, nothing happens.
So I press up and it keeps going up.
The down arrow is not working anymore.
My forever loop just sat there doing nothing.
So what have I missed from my forever loop? Yeah, it's the green flag clicked.
So I need to go over to events and I need to have my green flag clicked, and then now it should start falling from gravity.
So let's just have a look and see what happens.
Okay, so it's falling from gravity.
And now Scratch the Cat is stuck down the bottom and it's not quite working.
So now we need to move on to modification number three to actually make it fly cause it's just stuck there at the moment.
So I'm just going to stop it.
Cause at the moment, when I press up it's changing Y by 10.
So it's going to go up by 10.
But when I press the green flag it's going to change it by minus 10.
So it's always fighting against it.
So it's going to be pulling it down and you're trying desperately to get it up but it's not going to work.
So if I mess around with these settings, if I put minus five perhaps, let's try minus five, you can just move Scratch the Cat up just by dragging it along if you want to.
Now, let's see what happens.
So it goes quite quick, but it's still ooh, still very hard.
It kind of works if I hold it down but it's still quite hard to do.
So maybe I'll try minus two and just see what happens.
And I'm just going to move Scratch the Cat back up.
Okay.
That's looking a little bit better now.
That's a little bit easier to use.
So now with our third modification, Scratch the Cat is actually flying because these settings are right, and you can always play around with these settings.
Just try and get them right when you go and you might want it to jump faster, if you want to, rather than changing this but it's up to you.
You can play around with those settings.
It's your game.
Okay? So that was our modification three.
So my next modification is to stop the cat from falling off the bottom of the screen cause at the moment, when I run it, it gets stuck down the bottom and that's it.
But we don't want that to happen.
We want it to stop a little bit higher.
So what I want to do is I don't want it to keep going forever all the way down to the bottom of the screen.
What I want it to do is I want it to keep going until it gets to a certain Y position.
So now I don't need my forever loop.
I need another kind of loop.
So what kind of loop am I going to need? Have a think.
Shout it at the screen now.
Yeah, it's the repeat until loop, okay? So I still want this block of code cause that's going to be really important but I'm going to get rid of my forever loop and I'm going to get my repeat until loop now.
So where is my repeat until loop? So now at this point, if you got stuck, you ca think, well, how on earth did they do it for the buildings one.
So we've got X position less than minus 220.
So it's going to be something like that.
So now I've got to think, well, where are those blocks? So we need a green one.
And it's going to be, might be less than, it might not be, let's just, like we can just try it.
And it's not going to be the X position anymore because this is to do with up and down.
So we want the Y position, which was a blue one.
So where's Y position? There's the Y position.
And then we've got to think, well, what is this location? So it could be, we just move it around and we look for the Y location.
So we've got here minus 160.
We've got here, I think probably, I'll probably want to stop it when it gets to about here.
Maybe minus 165, ooh, maybe 170.
I'm going to go for minus 170.
So less than minus 170.
And then we're going to put this back in.
So now I'm going to put this back in and just see what happens.
I didn't put Scratch the Cat at the top.
So it's a bit boring at the minute.
So let's just put Scratch the Cat back at the top and then run it.
Okay, I'm going to try and play it.
And then if it gets a bit too fast or I get stuck, is it going to stop? Yeah.
So now they'd stop falling when it gets to that Y position being less than minus 170.
So it's stuck there now.
So now it's a bit boring again cause it stopped falling.
Cause I can go up, but it's not falling anymore.
So my game's become boring again.
So what I can do now is I can think, well, what happened last time to make it keep happening? So again, let's look at the buildings one.
So we have this repeat until loop, and then that repeat until loop was in a forever loop.
So let's bring that forever loop back.
So here's our forever loop.
Let's just push that one back, put that one in there.
Let's see what happens now.
Yeah.
So now when I press up, it's going to keep going up.
And then when it gets to that bottom of the screen it's going to stop, which is fantastic.
So there's one thing missing cause at the moment when I run my game, I'm always having to put Scratch the Cat up manually, in order to start my game.
But that's not a very fun game having to get your player to move the cat to the right place.
So what I want to do is at the beginning of the game, I want Scratch the Cat to be about here.
So if I want Scratch the Cat to be about there, what I need to do is I need to look there at the X and Y position of Scratch the Cat.
And I need to, when the green flag is clicked, I need to make sure that Scratch the Cat is in the right place.
Just like we've got here, we've got set X to 280.
We can do this with Scratch the Cat as well.
We can just do the set X position or we can do both as well.
So let's just have a look and see what happens.
So let's go for set X first.
So set X to minus 176.
Actually, no, this block's going to be even better for us.
Let's grab this block instead.
So go to X of that and Y of that.
And because Scratch the Cat was already there when I pulled the block across, it's actually filled it in for me.
So if you look, the X position there and the Y position there has been copied into there.
So let's just see what happens now.
Brilliant.
And then let's just stop it there.
And then hopefully, when I press the green flag now, Scratch the Cat should be at the top.
Yeah, and it is.
So you can see now that we're starting to build a game, quite a cool game as well.
So what I want you to do now is have a go at those modify tasks.
And if you want to add extra things to it it's absolutely fine with me.
It's your game.
So maybe you want to have it say, if it hits a building, it loses a life or you want to have a score that tracks the time to see how far they can get and how many buildings they can take over.
Whatever you like.
But, you know, what I want you to do is those modify tasks first, and then you can just do whatever you want, extend it as much as you like, and make it your own.
Okay? So have a go.
So here are your modify steps, just to remind you.
I want you to add movement to the cat, so these up and down arrows, and then change it to make the cat fall with gravity, and then make the cat actually fly so that it feels a bit more realistic.
And then stop the cat from moving off the bottom of the screen.
So all of those things are really great things that you can do, to improve and modify this programme that you've been given.
So you're just going to ncc.
io/flycatfly, it's exactly the same link as before, and you are modifying the code.
Pause the video and come back when you're done.
So hopefully, you've done those four modifications and these are just, this is just a reminder of those ideas for extending the game if you'd like to extend the game.
So if you do want to have a go at doing that, then you can pause this screen and have a go at that.
Now that's completely up to you.
It's an optional activity.
If you want to have a go, there're some ideas for you to have a go.
So pause now, if that's what you'd like to do.
Great.
So here's a little test for you.
Which of these is an example of condition-controlled iteration? So what I want you to do is point at the screen at the one that you think it is.
I give you a little bit thinking time and it's that one.
Well done if you got that right.
So you know it's conditioned-controlled iteration because it's got that repeat until, but also it's got that box there where you can put a condition in.
So that's another clue too.
So hopefully, you've enjoyed today's lesson and you've made another really cool Scratch project.
And if you'd like to share that with us today, please ask your parent or carer to share your work on Instagram, Facebook or Twitter tagging @OakNational and hashtag LearnwithOak.
Hopefully, see you soon.