video

Lesson video

In progress...

Loading...

Hello and welcome to lesson six of our Variables in games unit.

I'm Andy and in this lesson we're going to look at refining and improving your code.

For this lesson, you'll need to have a web browser and access to the Scratch website.

If you'd like to save your work, you'll need to have an account in Scratch.

If you don't have an account, you'll need to get a parent, carer or teacher to set an account up for you.

You'll also need something to write on and something to write with and you also could do with having your designs and the code you've created in previous lessons.

So, if we can clear away any distractions then we can start.

In this lesson, you'll evaluate your project, you'll identify ways that your game could be improved, you'll extend your game further by using more variables and you'll refine your game.

Are all games the same? In a moment, you're going to go to the Scratch website and have a look at some of the games.

Try the other versions of the kind of game that you've made, look at what's similar and look at what's different, and do you get any ideas from these games that you could put into your game? Write your ideas down.

So, go to the Scratch website, search for fruit catcher and look for ideas that you could put into your own game.

So pause the video now.

So, I hope you found some good ideas.

We're going to look at a couple of ideas together.

The first one we're going to look at is adding a timer to a game so we can set how long a game lasts for.

So, here's an algorithm and the algorithm for the timer is when the game starts, set a timer for 60 seconds.

The timer will repeatedly count down in one second intervals until the time equals zero.

So, we're going to look at adding the variable and then adding the code.

So here's my project from the previous lesson.

I had these three sprites which repeatedly moved down the screen and you try and catch them with the ball sprite and if you didn't catch them, they would touch the bar which was at the bottom of the screen, that was a sprite as well, and if they touched that, they'd go back to the top and fall again.

We added a score variable and decided that catching different ones would give you different points.

So the score variable would change by different amounts.

We are now going to add a timer.

So, the timer needs to be a variable and if you recall, I used the screen bottom sprite to set the score to zero, the score variable was set to zero.

I think we'll carry on working there and I've put the algorithm in so we can see what we're doing to guide us along.

So when the game starts, set the timer for 60 seconds.

So I need a timer variable.

I need to go to variables and I need to make a variable and my variable is going to be called timer and I add that and a bit like score, I need the timer to start at a set amount.

So I've got set, change that one to timer and it should be set for 60 seconds.

So I need to click in here and put in 60 seconds.

And I'll put that with the score one so they're together, I can remember those go there, they're near each other, my variables being initialised.

So next thing we need to do is make the timer repeatedly count down in one second intervals.

So, the timer needs to change, it needs to go down, so that needs to be minus one and we also need a one second interval and we need a repeat.

So I've got this, wait one second.

So we've got wait a second and we've got change it by one, make it one less and we need to repeat it.

So, it actually needs to repeat until the timer is zero.

So rather than repeat it a set number of times, I'm going to use this repeat until.

So there's a condition in that one.

I need to put that up there and I need to repeatedly wait a second and change the timer by one until the time equals zero.

So I need an operator to check, so we've got this equals, repeat until and we need the variable timer and until it equals zero.

So, repeat until time equals zero.

So, if I click go now, we can watch it counting down.

And there it is counting down.

I'm going to have to wait a whole minute to see if it counts down and actually stops when it gets to zero.

To speed things up a little bit, I could temporarily change the timer to a smaller number, like five or ten.

So if we now try it, three, two, one, see and it should stop at zero.

There it is, it's stopped, it's not counting any further.

So, as a little trick, we can change our set timer variable to a different amount just for testing.

So there we are, we've got repeat until the timer equals zero, wait a second, change the timer variable by one every second.

So your task now is to add a timer and follow the algorithm which is on the worksheet.

Remember you need to add the variable and then add the conditional loop which counts down until the timer is zero.

So pause the video now and add that code to your project.

Okay so hopefully you've added that now, we have a timer which counts down.

The timer doesn't actually stop the game, it doesn't do anything at the minute, it just counts down, when it gets to zero, the timer stops but the game carries on.

So, this page is the algorithms mixed up now.

We've got the timer and the game play one which is what you were using last week.

I've moved it around a little bit to make it easier to read on this slide.

So the timer algorithm, when the game starts, the player is given a 60 second timer, the timer will repeatedly count down in one second intervals until the time equals zero.

So you've just worked with that one and created the count down timer.

The main game play, a sprite moves down the screen from a random position at the top.

If it falls onto the bowl, you change the score by different amounts.

It then falls again from a random position at the top of the screen.

If the sprite touches the screen bottom, doesn't touch the bowl, just the screen bottom, it falls again from the top of the screen and the score is not changed.

This continues until the timer equals zero, so this is the main game playing and when the timer reaches zero, stop the game.

I've just put down here the different sprites, what they actually do because rather than doing one algorithm for each sprite, this is the same algorithm for all sprites but each one has a little bit of a difference.

The beach ball moves down in four steps and if it's caught it adds one point.

The star moves down eight steps, so that's slightly quicker and that added five points and the ghost moved down quickly, like the star and if that was caught, it took five points off the score.

So, we need now to go and add this until the timer equals zero.

So we're going to go back to Scratch now and add a little bit more code which stops the game.

Okay, so now we need to add the additional code and the additional code, this was the bit for the timer.

So this means the timer stops at zero, but we wanted to stop these falling down the screen when the timer equals zero.

So that bit of code needs to go on sprite.

So on the sprite, at the minute it says, forever fall down the screen and we want to change that so it actually stops when the timer is zero.

So we don't want to forever loop anymore, okay, 'cause it goes forever.

We need to go into control and we can use another repeat until, so I'm going to take this away, repeat until and again, we want to check, does the timer equal zero, so repeat until the timer equals zero.

So, I'm going to put timer in there, I'm going to put zero in there and I don't want forever anymore so I need to get rid of that, I want al the rest of the code, which I can put in there and get rid of the forever.

So, as before, I'd have to wait a minute to test this, so I'm going to go and change how much time there is and I'll leave it turned down for now.

I think five seconds will do.

So, time's just five seconds and if I press go, I've been watching the star three, two, one, stop.

Timer zero, star stops moving.

That's fantastic.

So, we need the other sprites to do the same thing.

So your task will be to put that little bit of code on to all of the sprites.

So the code was, I'll just stop that moving, repeat until.

So rather than repeat forever, repeat until the timer equals zero.

So we get rid of the forever loop and we put this repeat until conditional loop in, okay? So your task now is to go back to your project and follow the algorithm to add the code to stop the game when the time runs out.

So, that was a conditional loop which stops when the timer equals zero and it's the same for each of the falling sprites.

So, pause the video now.

Okay, I hope you've got that all done.

So the timer counts down and when it gets to zero, all the sprites stop moving down the screen.

We're going to look at a different improvement to your game now.

The idea of having lives in a game.

So you get so many lives and when you lose them, the game stops.

So we're thinking about a variable for lives, each time you miss an object, you lose a life.

It could be all objects, or a particular one, and you're going to write the algorithm.

So think about the name that you'll give your variable, how you'll set it, how and when will it change, is it changed by all sprites or just some sprites, and you might choose to use the timer algorithm as a starting point.

So if you look at the timer algorithm, think about how a lives algorithm is similar and how it's different and that might help you to write your algorithm for lives.

So your task is to write or draw an algorithm for lives in your game.

So please pause the video now.

Okay, so I hope you've got something for that.

I'm going to have a go at the same thing.

So this is the timer algorithm that could have been a starting point.

So no longer a timer, it's going to be about lives.

When the game starts, the player is given, we don't want time bits, so that can all go, is given five lives.

Okay, so that's where we start from.

The sprite that moves down the screen from a random position, that's okay.

If it's caught it changes the score and it goes to the top again, and then if it's falling down, it touches the screen bottom, that's where I think we take the lives away.

So, if the sprite touches the screen bottom, I'm going to have mine just check if it's a star or a ball, I'm not going to check for the ghost, 'cause the ghost takes points anyway.

So if the fallING sprite touches the screen bottom, check if it's a star or a ball, if it is, take one life away and it falls again from a random position at top of the screen.

So there we are, if it touches the screen bottom, check if it's a star or a ball, if it is, take a life away and that's all okay and this continues until, not the timer, until lives equal zero.

Okay, I don't think I need to change any of the rest.

So there we are.

When the game starts, the person's given five lives, the top bit is all the same.

If it touches the screen bottom, check if it's a star or a ball.

If it is, take one life away and then it falls again and that continues until lives equal zero.

Okay, I think that's pretty good.

So, you can tweak your algorithm for lives if you need to and then you can go and implement your algorithm as code in your project.

Remember to test it as you go.

So, add lives to your game, follow your algorithm, add the variable and then add the code.

So, please pause the video now.

Okay, so I hope you've managed that.

I'm going to have a go at doing the code myself, so if yours didn't quite work, you can look at this and hopefully fix any bits that weren't right.

So, I needed to give the player five lives.

So, I need to make a variable first, if I click on that, I'm going to give the variable name lives, 'cause that seems to be a sensible name to give it.

So, I've got a variable, lives and where I set the others, I'm going to set lives to five.

So that's the player having five lives.

I don't need the timer anymore, but I'm going to leave the code in because we can probably reuse it later.

I'm just going to switch it off so we don't see it.

So I've got my lives and I now need to set it so that if it's missed by, if the player misses catching the ball or the star, it takes a life off them.

So that needs to be code in the star and I need to change lives by minus one, so I need that one and it needs to be minus one and it's if they miss it and it touches the bottom screen.

So, if touching the bottom of the screen, go back to the top and take a life off them.

And up here, I want the game to repeat until they've got no lives left.

So I'm going to take the time off and put lives in there, so repeat the game until they've got no lives.

So that one does that, I need to do the same for the beach ball.

Change lives by minus one, and I need to check that the lives aren't at zero.

So that's that one done, and then the ghost, I'm not taking a life off, but I still want to check whether the game has got to life zero.

So I think that's the code I need.

If I press go, there we are, we've got them all falling and we've got lives two, one and it's stops.

That's pretty good, let's just check it again.

So, there we are, if I catch things it doesn't take lives off, but if I miss them, it takes them off and the game stops quite quickly.

Great, so that's that bit working.

Okay, I hope you've got your code working, if not, you could pause the video and just go and make some changes to your code.

I'm just going to look at the, just before we finish this, I did leave the timer code in and we can actually check whether the lives have gone to zero or if the timers got to zero.

So in the operators, there is an or, so I can say, if the lives is zero, or and I can say, is the time zero.

So I need the variable there, timer zero.

So that's the little piece of code I can use, repeat until lives is zero or the timer is zero and just for speed, I want that in my other sprites, so I'm just going to borrow it for a minute and drop it onto the beach ball, drop it onto the star, I need to pop that one back in there, go to the beach ball and I need to just switch those over, so I need to get rid of that one and put this one in.

Go to the star and I need to get rid of that one and put this one in.

So, I should now have a timer and lives and they should all work.

So if I press go, you can see the timer counting down, the lives counting down and in that case, you can see the game has stopped, I've still got one life left, but the timer's run out.

If I just try it again, let's see if we can make the lives run out before the time does.

It's still the timer, but it's because we've got less time than we need 'cause I was testing.

So I'm just going to go back to the how long the timer last for and I'll make that 30 seconds instead.

So we've now got 30 seconds and we've got five lives.

So let's run the game, I'm running out of lives quickly, but the timer's still going and that's it, it's now stopped on lives.

So, the game is now set such that when I run out of lives or I run out of time, it stops and you can see the timer's carried on, but the game has stopped, I can't get any more points.

So there we are, the game checks two things, there's lives and a timer and I can see my score changing and I've been working with three different variables there.

Okay, so you now have a little bit of time when you pause the video, you're going to refine your game, you need to rest it, check it's all working, you can add anything else that you want to add.

You may have had some ideas from things that you watched earlier, or you might want to have the check of time and lives and as you add things, remember to test and debug as you go along.

So, if you'd like to pause the video now.

Okay, so that's it for this lesson and for this unit.

So in this lesson, you reviewed other catching games to identify ways that you could improve your game, and you extended your game further by using more variables.

So if you'd like to share your work, you could share it with Oak National.

If you'd like to, please ask your parent or carer to share you work on Instagram, Facebook or Twitter, tagging it @OakNational and with the hashtag, LearnwithOak.