Lesson video

In progress...

Loading...

Hi, everyone.

My name is Mrs. Jenkins.

I can't wait to learn with you today! Welcome to today's lesson, which is called Improving a game, taken from the unit, Using variables in programming to develop a game.

By the end of today's lesson, you will be able to choose how to improve a game by using variables.

In today's lesson, we have three keywords.

Those keywords will appear throughout the lesson, so it's really important that we know what they mean before we get started.

We have score, selection, and operator.

Score is a named value which will track a player's points.

Selection is part of a programme where if a condition is met, then a set of commands is run.

Operator is a symbol used to compare values.

In today's lesson, Improving a game, we have two learning cycles.

We have Use variables in a programme and Use selection with a variable.

We're going to begin with Use variables in a programme.

What are variables? Well, Laura says, "Variables are stored in a computer's memory in a named location." And Izzy says, "They can be accessed and changed by a computer programme." Great answers, Laura and Izzy.

Thank you so much.

Look at this Scratch game.

What variables could be set and changed in this kind of game? What variables could have been set and changed in this kind of game? We could set and change things like score, timers, or lives.

So you could have a score going up every time it touched the paddle.

You could have a timer that counts down and the game stops when the timer gets to zero.

You could start with lives.

So if you had three lives and the ball hit the red line, you might lose a life each time.

To create a variable in Scratch, you need to open the variables menu and click the Make a Variable button.

Add the name of your variable and make sure it is appropriate for the values that the variable will be storing.

Once you've added the variable name, click OK.

Your new variable will be seen in the menu here.

There'll be options to change to the variable name if it doesn't appear by choosing the triangle, which will give you a dropdown menu.

This code sequence will work as a setup code.

This means the score will be reset at the beginning of each game when the flag is clicked.

So it says when the green flag clicked, set score to 0.

So when the green flag clicked is an event block, and that goes at the top.

Then we have set score to 0, so that is our variable.

That means when the game begins, the score will go back to zero.

How would you use this block to set a score in the game? "Set score could be used to reset the score back to zero.

This is useful at the start of a game, or if there's an action which will make the score a certain amount." That's right, Sofia.

Thank you.

How would you use this block to change a score in the game? "I would use change score by to increase the variable and make the score go up.

If I used a negative number, like -1, the score would decrease instead." Thank you, Andeep.

And that would make a game very exciting if we had scores going up or down.

Great ideas.

Okay, let's have a little check-in now.

What would happen to the score variable when this code is run? A, the value will increase by one; B, the value will reset; or C, the value will decrease by one.

What do you think? Have a go.

How did you get on? I asked you what would happen to the score variable when this code is run? A, the value will increase by one; B, the value will reset; or C, the value will decrease by one.

The answer is the value will decrease by one, because the code says change score by -1.

Great job, everyone! We have a code here that has been created by Laura, and she says, "Be careful where you place your blocks!" It is easy to accidentally move your coding sequences around while trying code in different positions.

So this is where she's put something in in the wrong place.

If that happens, you can fix a mistake by right-clicking on the coding workspace and selecting Undo, and that will remove the mistake you have just made.

Laura said, "How did you find out where to place your change score variable block?" And Andeep says, "I thought about what I wanted my game to do, tested out a few positions, and used undo if it didn't work as I expected it to." Well done, Andeep.

You can also have planned your score as part of a design.

True or false? You don't need to test your Scratch project.

What do you think? Have a go.

How did you get on? I asked you, true or false? You don't need to test your Scratch project.

It is false.

It is important to test your programme as you create your project.

If you put a block in the wrong place, you can use undo to change it.

Great job, everyone! We are going to move onto our Task A now.

I would like you to open our project, oak.

link/paddle-ball-1, and create a score variable and add it to your programme.

The score should reset when the flag is clicked.

Explain why you added the score variable in that position.

Have a go.

How did you get on? I asked you to open the project oak.

link/paddle-ball-1 and create a score variable and add it to your programme.

The score should reset when the flag is clicked.

Explain why you added the score variable in that position.

So your code might look a little bit like this.

When the green flag is clicked, set score to 0.

Go to x 20, y 150 and point in the direction of 45.

Repeat until touching the colour red.

Move 15 steps.

If on edge, bounce.

If touching the paddle, then turn and pick a random 160 to 200 degrees and move 15 steps.

Laura says, "I added the set score to 0 block right under the green flag event.

This way, the first action that is triggered when the game starts is to reset the score.

Great job, Laura! Well done.

How did you all get on? Here's our set score to 0.

Right, moving on.

Continuing with Task A, I would like you to test your programme and find out where the change score by 1 block should go, and can you explain why? Have a go.

How did you get on? I asked you to test your programme and find out where the change score by 1 block should go, and to explain why.

Let's have a look.

We have put our change score by 1 underneath the if touching paddle.

So that means every time the ball bounces on the paddle, the score will change by one.

Izzy has put, "I put my change score by 1 block inside my if.

then.

block.

That way the score increases when the ball touches the paddle sprite." Great job, Izzy! Well done.

Okay, you can see the example here, oak.

link/paddle-ball-2, with all that added code into it.

Well done, everyone! We're going to move on to our learning cycle two now, Use selection with a variable.

What is selection? Selection is a part of a programme where if a condition is met, then a set of commands is run.

This is an example of selection.

If the score is equal to five, then change the costume.

So the score is equal to five is a condition.

Change the costume is an outcome.

Sofia asks, "What is a comparison operator?" A comparison operator compares two values.

For example, we have the less than symbol, the greater than, or the equal to.

You can use them to make decisions in a programme.

You might have come across them in your maths lessons.

In Scratch, you can use operator blocks.

There is equal to, less than, and greater than.

Okay, let's have a little check.

I would like you to think about what does this operator block show? Does it show A, equal to; B, greater than; or C, less than? What do you think? Have a go.

How did you get on? I asked you, what does this operator block show, A, equal to; B, greater than; or C, less than? the operator block shows equal to, and we can see that equals symbol in the middle.

Great job, everyone! There are two condition blocks in Scratch.

They're both used to check whether a condition is true.

You have if.

then.

and if.

then.

else.

Operator blocks can be inserted into conditional blocks.

The if.

then.

else.

block is used to check whether a condition is true or false.

The condition goes in this hexagonal shape.

If the condition is true, the outcome goes in this space.

If the condition is false, the outcome goes in this space.

Think about your paddle ball game.

You can trigger actions to happen in your game by using selection.

What actions do you want to trigger in your game? I asked some of the Oak children, what actions do you want to trigger in your game? And Izzy explained, when the score reaches five, she'd like to change the paddle costume to be smaller.

Great idea, Izzy.

That will make the game a little trickier.

And Sofia says, "When the score reaches 10, change the backdrop." Another great idea.

You could have a celebration backdrop, Sofia.

Okay, we've got another little check-in now.

What do you think will happen when the score is more than five? So we have when the green flag clicked, switch costume to paddle, forever, if score is greater than five, then switch costume to paddle2.

What do you think? Have a go.

How did you get on? I asked, what do you think will happen when the score is more than five? And when the score is more than five, the sprite's costume will change to the next costume.

Great job, everyone! The programme is using the value of the variable to determine whether to change the sprite's costume.

Okay, we're going to go onto Task B now.

I would like you to return to your Scratch project, and you're going to add the operator block to your paddle sprite's code.

Choose an action to take place when the score reaches a certain value.

Have a go.

How did you get on? I asked you to return to your Scratch project and add the operator block to the paddle sprite's code, and to choose an action to take place when the score reaches a certain value.

You can view our project by going to oak.

link/paddle-ball-3.

I have changed the code to say, when the green flag is clicked, switch costume to paddle.

And if the score is greater than five, switch the costume to paddle2.

And I'm gonna make sure that that paddle shrinks a little bit when it moves to paddle2 to make it a little bit harder to hit the ball, because I think it makes it a little bit more exciting.

Okay, you have worked really hard in today's lesson.

Well done.

Before we finish, let's summarise what we have learnt.

Variables can be used to improve a Scratch game by keeping track of a score.

You can update the score variable by using the change score by block.

The score can be reset by using a set score to block.

Operators are used within selection to trigger events when the score variable condition is equal to a specific value.

Great job, everyone.

You have worked really hard today.

I can't wait to learn with you again soon.