Check out a free preview of the full Build a Game Project: Feed-A-Star-Mole course:
The "King Moles" Lesson is part of the full, Build a Game Project: Feed-A-Star-Mole course featured in this preview video. Here's what you'd learn in this lesson's course:

Brian implements king moles into the game, and sets them to be worth more points than the other moles.

Get Unlimited Access Now

Transcript from the "King Moles" Lesson

[00:00:00]
>> Brian Holt: Okay, there's two more things we got to do after this. We're going to do the king moles, right because those are worth two points. And we're gonna do the score meter at the top with the worm, okay.
>> Brian Holt: So let's go do the king moles first.
>> Brian Holt: So the first thing we need to do is we need to generate moles that are king moles.

[00:00:41]
>> Brian Holt: So coming back here. And the king moles have little crowns on them and they are worth double points if you click on them.
>> Brian Holt: So under mole.gone, the other thing we are gonna say here is we're gonna say mole.king
>> Brian Holt: getKingStatus
>> Brian Holt: Okay, I'm gonna write a function up there that's going to randomly assign it to be either a king or not a king.

[00:01:20]
>> Brian Holt: I guess we could've called this royal or something like that. Probably something less gender specific, sorry.
>> Brian Holt: But we'll stick with it for now, cuz I'd have to change a lot. So function getKingStatus. So this should return either true or false. True if it is a king, false if it's not a king.

[00:01:47]
And we want to make about 10% of them be kings. So what we're gonna do is we're gonna say math.random. If you remember, return rather, return math.random. Math.random returns a random number between zero and one, right? Some decimal between that, it's roughly uniform distributed. It's not, it's pseudo random, it's not random.

[00:02:13]
But it's close enough for our purposes. So if we asked if math.random is greater than 0.9, that would mean roughly 90% of the time, it would come back as false. And 0.1, or 10% of the time, it would come back as true.
>> Brian Holt: Makes sense?
>> Brian Holt: So we're gonna come back down here to our game, and gone.

[00:02:43]
So this is where we're getting whether or not it's a new king.
>> Brian Holt: And we're determining that when it comes in as hungry, right?
>> Brian Holt: Now, we have all these places where we're setting the background. Whenever we're setting a background, we have to check if it's the king or not.

[00:03:08]
If it is the king, then we have to show a different mole, right? We have to use a different background. So every time that you see that we're just gonna have to say if (mole.king)
>> Brian Holt: And it's gonna look really similar, but it's just gonna say mole-king-hungry. I think this is mole king, no, king mole.

[00:03:38]
So king mole hungry else that, right? Otherwise the mole is just normal. So if I refresh this, some of these will come up as kings. The rest of the animations are going to work yet, but you should see at least some of them coming up as kings. See, so that one is, but then when it goes, yeah, it'll disappear.

[00:04:04]
So we have to go to the rest of them. That makes sense though how we're getting king moles? So let's just go do the rest of these. So if mole leaving then we have to say if mole.king
>> Brian Holt: Else
>> Brian Holt: And
>> Brian Holt: king-mole-leaving.png.
>> Brian Holt: And where else? So that one, we have hungry and this is when we have sad, so we have to have sad kings as well.

[00:04:47]
If mole.king,
>> Brian Holt: Else,
>> Brian Holt: We have king-mole.sad
>> Brian Holt: And then last one down here in fed, we have to do that as well. So if (mole.king)
>> Brian Holt: Else
>> Brian Holt: king-mole-fed. So now if we refresh this, hopefully it should go through the whole cycle for all the kings. So there's one, and when it leaves, you can see it's sad and you can see also the little hat in the background.

[00:05:40]
And then if I feed one of them, you can see it has its happy face and everything like that. Right now they just look different. But we're not giving many different score. So down in fed, we have to check if mole.king here as well. In fact we can just reuse this one up here.

[00:06:01]
>> Brian Holt: So if the mole is not a king,
>> Brian Holt: Then we just add one to the score. And if the mole is a king, then we're gonna say score += 2,
>> Brian Holt: Or score = score + 2, if you prefer.
>> Brian Holt: Right, because king moles are worth double in our little game here.