video

Lesson video

In progress...

Loading...

Hello, and welcome to lesson three of our variables in games unit.

I'm Andy and in this lesson, we're going to look at using variables to improve a game.

As with the previous lessons, you'll need a web browser and access to the Scratch website.

If you'd like to save your work, you'll need a Scratch account.

If you don't have one and you're under 13, you'll need to get your parent, carer or teacher to set one up for you.

You'll also need something to write on, and something to write with.

So, if you'd like to clear away any distractions in front of you, then we can begin.

In this lesson, you will choose how to improve a game by using variables.

You'll decide where in a programme to change a variable.

You'll make sure of an event in a programme to set a variable.

And you'll recognise that the value of a variable can be used by a programme.

I'm going to start by looking at this example.

So, this is a simple pong game, and we can see the ball bouncing back and forth.

And when it hits the bat, it bounces back.

And when it hits the bottom red zone, it stops.

If this was going to be improved, what things could we have variables or values for to improve this game? I'll give you a moment to think.

We could have a number of different things.

We could have a score, we could have a timer, or we could have how many lives you have.

So far, you've changed variables in a programme.

What would happen if the score in a game was never reset? The score would just continue forever, and each person having a go would get a higher score than the previous person.

So, when would be a good place to start the score at zero? If you look at the blocks on the right, those are the blocks you would use to set the variable's score to zero.

For your first task, I'd like you to make a score variable in a programme.

The programme already exists and you can access it by going to the link that's shown there.

This is in your worksheet too.

And you need to go to variables, and make a variable name score.

Once you've done that, come back and we'll move on to the next thing.

So, pause your video now.

Okay, so, hopefully you've managed that.

If you've had any difficulties, then I'll show you now what you need to do, and you can make yours the same if you need to.

So, I need to go down to the Variables section.

And at the top here, it says Make a Variable.

I choose that and into the box, I'm going to type score.

Once I've typed that in, I click OK, and I now have a score variable shown there, and it's appeared on my window there.

Okay, so that's the first step.

Now that you've got your score variable, you're going to do some experimenting.

You're going to use the code that is already there, and try placing your variable in different positions.

So, you need to drag a change score block into position A.

So, that's between the point in direction and repeat until.

Snap the block in there.

Read through the code, and work out what you think it will do.

Once you've had a think at what it will do, actually try the code out and see what happens.

So, write down both your prediction, and write down what actually happens.

You'll then need to do the same by trying the change score block in position B and in position C.

Okay, so if you'd like to pause the video now, go and try that task placing the block in different places.

But remember to read the code through first, and predict what you think will happen before you actually try it out.

Pause the video now.

Okay, so, these are the three different positions you should have tried out.

Now, we'll work through each, and we'll think about predictions, and then we'll try them out and see what actually happens.

Okay, so, we'll work through each of those different positions of the score.

So, I need to take a change score by one, snap it into position A which was here, and I'll read it through and then try it out.

So, when the green flag is clicked, go to position 20, 150.

And what's going there is this sprite 'cause it's this sprite's code.

Point in direction 45, change the score by one.

Okay, so, the score there will go to one.

And then we've got a loop here, and it says carry on doing this loop until the ball touches the red border at the bottom.

So when it's not touching that red, it moves 15 steps.

If it touches the edge, it bounces.

It then checks if the ball touches the paddle, then also that bounces back up the screen, and off it goes again and it comes back around the loop.

So, the score changes once and it just changes there, so we'll try that one out.

If I click on the green flag, let's see if I can bounce the ball.

It's changed by one.

And I think I can stop now.

So, the score changed by one, it didn't change again.

So, carefully, I'm going to move it to position B.

I need to take that little piece out.

And position B is down here.

So, I've currently got a score of one.

I'm going to reset it by just clicking over here.

If I click set score to zero, you can see it's now gone back to zero, I can try it again.

So, this time, we've got the same start bit, the same start position.

It starts at the set place.

We've got a repeat, and if it's not touching red, then it moves and if it touches the edge of a wall, it bounces and then it changes the score.

If it touched the paddle, it bounces.

So, as it's moving, score increases.

So, every 15 steps, the score will go up by one.

So, let's check that one out and see what happens.

If I press the green flag, and I can bounce the ball.

Ooh, that score's increasing really quite quickly.

If you can see up in the top corner.

Okay, and I'll let it stop there, 278.

So, whilst the ball is moving, the score is increasing.

And then we need to try it in the third position.

I need to carefully move it around without breaking the rest of the code.

That goes back in there.

And position C was down here.

So, this time, we've looked through the code several times, and it's in amongst the touching the paddle bit.

So, if the ball touches the paddle, then this is the bounce and move, and change the score by one.

So, every time it gets hit with the paddle, then it will increase by one.

So, I've got a big score here.

I'm going to reset it again, and we'll just try this out.

And so, each time it hits the paddle, it will go up by one.

So, green flag, hopefully, I just got one.

Two, three, four, five, there we go.

Stop, so, there we are, three different positions.

It's the same thing, each one was changing the score by one.

But depending on where we put it, it had a very different impact on the score in the game.

Okay, so, you're going to do a little bit more experimenting now.

You can place the change score by one block in position B or C.

And try putting different values in.

So rather than one, you can try using the value of two, five or 10 and see what happens as the game goes on.

Also, you could try using a negative number, what happens then? And lastly, we've got a set score to zero in some of the screenshots.

That set score to zero is already shown.

So, if you'd like to add that at the start of your programme, that would be good.

Do we always have to set it to zero? Have a think about that as you're trying out the different values for changing the score.

So, you're going to go back to the same code, and you're going to try different values for the variable.

If you could pause the video now.

Okay, so hopefully you've seen that you can change the values to two, five or 10.

And that would mean the score would change by factors of two, five or 10.

So, you might've ended up with some really big scores very quickly.

You should have also seen that the score could have gone down.

If you used negative numbers, and that could be one, two, five, then the score would go down by that value each time.

And lastly, I asked you to sets the score at the start, and we probably went for zero.

But it doesn't have to be zero.

The game could have been that you started on 10, and it used a negative number to count down and take one off each time until you got to zero.

So, if we look at this example here, what would happen if this code snippet was wrong? Okay, I'll give you a moment to look at it.

So, change score by one, change by score by two.

So, if we started on zero, it would have gone to one and then it would have gone two more, so one plus two is three.

That would have made the score three.

So, we could have just used one block.

We could have just used change score by three.

What about this example? So, in this example, we've got change score by one, change score by minus one.

I'll give you a moment to think about that one.

So, in that one, the score actually would go to one, and then back to zero.

So, actually, we don't need these blocks.

It doesn't actually change the score at all.

So, you have another task here.

What I'd like you to do is try adding two, two change score variables to different places in your code and see what happens.

And those changing values can be one or two or five.

You can choose what the value is, but have two of them.

And they are both changing score.

So, please pause the video now.

Okay, so hopefully you've tried that out now, and you had two change scores in two different positions on here.

So, I've made some score blocks for now.

I'm going to try them out, so I've got change score by minus one.

And I'm going to put it here.

I could put it somewhere else, but this is the one while the ball was moving.

So, while it moves, it's going to take one off, and then I'm putting the other one down here.

Every time it touches the paddle, put 50 on.

So, if I click on the green flag and try that out, I can see my score going up and down very quickly, but it's gradually going up.

Okay.

Okay, so, what we've done so far, the score has always been shown on the screen, but it doesn't have to be displayed all the time.

So, we're going to make a change.

We're going to hide the score so it's not shown.

And then right at the very end, we'll show what the score is.

So, rather than showing it all the time, we'll just report the score at the end.

I'm also going to show you how to add comments to some of your code because commenting shows that you know what different parts of your programme does.

And if somebody else reads it, it would explain to them what the different things do.

So, at the minute, it shows the score here.

If we come over to this side of the screen, I can click on that and not show the score anymore.

But I still want the person to know what points they got at the end.

And I can do that by using a say block.

So, at the end, I can add a block with say, and I can then go back to the variable, and I can say the score.

So, if I now run the game, and let it bump, ooh.

I'll try to let it land.

It then says my score is -112.

We can make it a little bit nicer than that by adding another block which is a join block.

So, if I go over here, I can get it to put two bits together.

So, I'm going to get it to join.

Your score is and I'm going to put a space in, so there's a space between is and the number.

And then it'll display the score.

So, we'll try that one out and we have a run it.

Let it land, your score is -27.

Okay, so that's a say and a join block connecting the two together.

And the last thing I mentioned we'd do is we're going to add some comments.

So, to add a comment, if you click the other mouse button, there is an out an Add Comment box.

So, I can put some text in here.

This sets the score to zero at the start of the game.

And I could have another comment here.

This takes one off the score as the ball moves and so on.

So, we can add a whole load of comments describing what different parts of our programme does.

Please pause the video now then and try switch off the score being displayed, and only show it at the end.

Also, add some comments to your programme.

In this lesson, we've looked at how to set variables, and experimented with how they can be changed.

We've also displayed a variable in a different way.

Rather than showing it all the time, we just displayed it at the end of the game.

So, that's the end of this lesson.

If you'd like to share your work, you can share screenshots, those kinds of things.

Please ask your parent or carer to share your work on Instagram, Facebook, or Twitter, tagging it with @OakNational and LearnwithOak.

Okay, see you next time.