Missile Command - Adding Some Scores and Lives
This is part 6 of a multipart series on creating a version of Missile Command using the educational tool, Scratch. For this entry, I am going to be adding a means of scoring for the user to get feedback on how they are doing. Specifically, I need to keep a count of:
- How many alien missiles they have destroyed and,
- How many lives they have left.
I will define lives left as being the number of times a missile can hit the area at the bottom of the screen. I plan on setting this as three since that seems as good a number as any and it also gives the player some semblance of a chance.
First Things First
Before we can begin, we need to modify our existing script so that it can handle the idea of scoring because at present, as soon as one of the conditions for which a score change occurs (i.e. a missile being destroyed and a missile hitting the bottom of the screen), it jumps out of the repeat…until loop without telling us what caused it. As a quick recap, this is how our script in the Enemy-1 sprite currently looks.
Specifically, look at the orange repeat…until portion where we test for the three conditions which will cause the loop to end. They are: alien missile hits the bottom, or, one of our circles (I mean, defensive strikes!) hits a missile.
Changing the loop to use a variable
So, step number one is to change it to use a variable which will indicate that the loop needs to exit. Create one and call it stop-missile. It is probably worth mentioning the difference between variables which are “For all sprites“ or “For this sprite only“ (the options given when you create a variable).
If you choose for all sprites, it means that any of the other sprites can use that variable. They can see what is inside, they can change it and they can use it. Alternatively, for this sprite only means that it is only visible inside the script we are using. Once we have created it, add in a set command (orange control section) which sets the value of stop-missile to be 0. Then, add a test in for the repeat…until loop which tests for stop-missile being equal to 1. The idea is that we will keep looping around and around until the value of the variable becomes 1; in effect, true, meaning, stop looping. Yours should look like this:
The code we need to make this work is below. Notice the use of the if command to test whether the missile has hit the bottom and the else (with an if inside) which checks whether we have destroyed an alien missile.
In both cases, we set the stop-missile to 1, so that when it next tests for this condition at the top of the repeat block, it exits.
Keeping Score
Next, we need to start keeping score. If an alien missile hits the bottom of the screen, our number of lives will decrement (reduce by 1) and if we hit an alien missile, our score will increment (go up by 1). To prepare for this, create two new variables called player-lives and player-score. What do you think? Should these be sprite or whole program visible? I’d make them whole program visible because we will very likely want to use these elsewhere. You can probably guess what comes next. In the first if, we are going to change the player-lives by -1 (in effect, reducing it by 1). In the second block, we need to add 1 to the player-score. This is shown below.
Time to run the code and observe what happens. Play this a few times to check everything is working and then look closely at the values of the variables at the top (they should have been added to the display screen when you created them). Anything amiss? You should have realised that each time you play the game, the scores do not change. That’s great if you are doing well because your score will become huge but not so good if you are down on your luck with lives. Add this next block to reset everything each time you play:
One of the really cool things about Scratch is that you can have lots of these When Clicked blocks, and they will all execute (run) when you click on the green flag.
That’s it for now. Next time, I will add in some sound because whilst no-one can hear you scream in space, it would be nice to hear something at least.
The complete script.
Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com