 Welcome to the ultimate Python turtle graphics game tutorial part 16 enemy types So in this tutorial what we're gonna be doing is taking our enemies oops, and you'll see let me just every single time we start this up and So right now we've got one enemy. It's it's a red Square and it loops and it doesn't really It kind of just kind of wanders around so we want to do is we want to like learn how to update our enemies And so we're gonna go ahead and do that. So basically what we're gonna do is in our enemy class so I'm gonna go over here and click on my enemy class and This is basically the code for the enemy. So what I want to do is I'm gonna add something called self dot type and I'm gonna have three different types of enemies. So I'm gonna use random dot choice and I'll make a list so I'm gonna put extra set of square brackets there and I'm gonna have a hunter type I'm going to have a mine type and I'm gonna have a Surveillance type Now I could make these shorter. I can make it HM or S But I like to make my code a bit more readable and don't forget the closing Right parenthesis there So watch what I do here. So when I create the enemy sprite I'm gonna have some code to deal with that. So I've created a random choice. I'm gonna go if self dot type just type equals hunter And what I'm gonna do is I'm gonna set the self dot color To red and this is the normal. Okay. Let's say self dot Shape they're already really give them all square for now But I could change I can make that random as well And I'll leave it that for now and then I would say L if And as I like to say I'm a little bit lazy. So I'm just gonna copy this and paste that there So we got hunter and we got mine. So I'm gonna make the mines orange And I'm gonna go ahead and do the same thing If I could use elves here, but we'll go ahead and use L if And just a little side note. I could have actually created separate classes I could have made a hunter class a mine class and a surveillance class and that would work as well Just kind of try to balance how you want your code to look I guess and I'm gonna make this one pink so we have different types So let's go ahead and see if that's working. Okay, so I'm gonna go ahead and run my game and Now I came up red. So let's kill that and see if it see what happens when we get to level two Okay, okay, so there we got an orange ones. That's a good sign Okay, and kill that we got a pink one. You'll notice on the radar. It's also pink. Which is kind of cool And because it's using the same color Okay, so now we got two reds and we got we got one red. We got some oranges So we know that that part is working. So I mean that's kind of cool, but what we want to happen is We need a little bit more code so that they behave a little bit differently So what we'll do is I'm gonna go down to the update section of my code and basically all this stuff here is going to be the same if it's active and What I got to do is in here. I'm gonna add some code for the different types So for example if self dot type equals hunter Now here's some simple code I could have done this a little bit nicer, but I'm gonna try and change a little bit more I'm gonna say if self dot x is less than player dot x So that means if the hunter is to the left of the player I'm gonna say self dot DX plus equals zero point five Okay Does that make sense? If the self is to the left of the player DX actually DX should be plus because we want to go towards the player else Self dot DX plus or minus equals zero point five And I'm gonna do the same thing with the Y. So if self dot Y Is less than player dot Y self dot dy Plus equals zero point five actually should be zero point zero five zero point five We'll just we'll fly up the screen. You'll see in a second. We have to deal with that then And then else Self dot wa DIY Minus equals zero point zero five and you might need to change these numbers. So they're a bit more, you know Copicetic with your system speed now. We also have the other types. So L if self dot type equals A mine now mines are gonna sit there and not move Okay, and then we can add some code later to play with those I guess What we'll say self dot DX equals zero and self dot dy equals zero. So we want them just to sit there and then L if self dot type equals Surveillance We'll say what we want to do with these guys is we want them to actually run away from the player So I'm just gonna go ahead and Do that and then just change this so we want them to go away from the player Instead of towards the player. So they're gonna actively avoid the player. Okay, so let's go ahead and test this and see what happens Okay, so you see this is a Surveillance type and it is running from the player is the player is over here. You know as it keeps bouncing into the wall Now imagine six, you know, it's running away. So Now, of course, you see how the speed is getting a little bit too high and we'll fix that in a few minutes, but I just want to see that we do have Okay, so the red one and you see how the red one went past the player But now let's see if it's coming towards it. So you see now it's working its way over again The code isn't perfect And I it's kind of intentional because I want to I don't want to go too close to the player because you'd never win Maybe you see how it's kind of circling around the player and then finally it works in to where the player is And you see I think there's I think there was a problem with the collisions I thought the collisions were working better for the shooting the gun or shooting the thing But we might have to address that one later, but you see how it's getting a little bit fast So when we go ahead and just kind of try to fix that one Real real quickly. So Did I set that up in the sprite, let me check the sprite class here It's been a while I'm very very busy this past week. So if you're watching this as I'm doing it Yeah, so you see here, there's no maximum. Okay, so what I want to do is I'm gonna go ahead and set self dot max D it's DX equals Let's see here I'm not sure let's try five again I'm not really sure where the good number is for this self dot max d y equals Five so basically you want to say that's the maximum speed that this this object can go. So what I'll do is in my Where's that here in my enemy class for now? We could do it in the other we could do it in some of the other classes as well Well, let's try this and then let's go ahead and deal with the speed up Make sure Okay, so let's set your max speed So I'm gonna say if self dot DX is greater than self dot max DX and self dot DX Equal self dot max DX that'll limit the speed to whatever that number was that we put there L if self Dot DX now. This is where it gets a little tricky is less than self dot negative max X Negative self dot max DX Let me say self dot DX equals negative self dot max DX Okay, so what that does is if it's moving to the left DX is negative It'll deal with that case and I'm just gonna go ahead and copy this and I'm hoping this works I should probably test it first, but let's go ahead and put y's in there And again the the five, you know, you might want to change that We also make that as a variable like give up give certain types of maximum speed Yeah, there's all kinds of options we can do here. I don't know if five was a good number We'll have to kind of play around with it here. I'm gonna go ahead and run that And that's a mine, so it's not moving anywhere. Let's go ahead and kill that. Okay, so Okay, so you can see the red ones they're moving. I think that's not too too fast. I think that's a pretty decent speed oops See here, so you see how they are coming around a little bit and so the so all the reds will converge on the player like that one just did and The pinks will run away So you can see how they're kind of going away from the player and we can we can make this a little bit more advanced We could do some stuff with radars and things like that But let's go ahead and leave that just the way it is for now And the other thing that I think I did in my code and that's one of those things I forgot I should tell you what we'll leave it there I think that's a good good place to stop this one So you get the idea and this is this is also the same way that we can actually make a boss So what we could do is we can later create a boss when we say, okay, it's a boss and it has these attributes And so so the enemy so what we do is we came up with three different types And when we create it, we just give it a random type either a hunter a mine or surveillance hunters are red Mines are orange and surveillance is pink and If it is a hunter, it will go Towards the player it will try to find the player if it is a mine It will sit there with no speed and just kind of wait now We could add it like so it explodes at some point There's a lot of different things we could do and then if it's a surveillance type It runs away from the player and then also we had to set a maximum speed So that it doesn't end up going way too fast. We should probably put this code in The player as well, but we could we can add that later. Yeah, so that is that that was number 16 so now we have multiple enemy types Subscribe for updates keep on coding