 Welcome to space arena the ultimate python Turtles graphics game tutorial part 9 in this part We're gonna learn how to detect collisions between objects. So when we talk about collision detection usually a lot of times what we do well, I shouldn't say usually but What I've done in a lot of my previous tutorials as I've used the distance method So I've measured the distance from this object to this object and that distance is less than a certain threshold Which is half of this Size or the radius? Plus half of this radius now in the case of these objects. They're all 20 by 20 So half is about 10 so the the if they're within 20 pixels of each other The center is within 20 pixels. You can say you have a collision now this works great for objects that are basically either square or round and In this game that would work just perfectly fine But what I'd like to try is something called access a line bounding box and what that'll let us do later Is if you want to you know, customize your game is to have rectangular Objects objects that are longer than they are taller or taller than they are longer So let's go ahead and take a look at what that looks like now I don't know this formula off the top of my head I actually went and googled it like a good person and I came across this Page on the Mozilla web on the Mozilla website and I'll put a link down below in the description. So The How can I put it so? Basically what it does is you can see here is you've got a rectangle around your shape so this could be a bomb this could be an enemy whatever it is and If they touch you can see how that is considered a collision So basically what you're doing is you're looking at the x-coordinates the width and the height and there's like so There's just like nice little formula there So I'm just gonna go ahead and steal this because good artist borrow bad artists There's our good artist steal bad artists borrow and what I'm gonna do is I'm gonna go ahead and put this into the sprite class And so what I'm gonna do here and watch watch what what happens here And I could put this the bottom doesn't matter, but I was put here for now deaf is collision the reason I put is because There's only gonna be one of two results. It's either gonna be true. There's a collision or it's gonna be false That's all this is gonna do So what I want to do is I'm gonna do self and other So self is say the player the other would be an enemy or self could be the enemy and Other could be the missile. Okay, so that's what we want to look look for here now So let's go ahead. I'm just gonna take that formula and I'm actually looking at it I took a picture of it. So I have a I'm looking at it now. So this is what it would be in Python. So if self dot x is less than other dot x Plus other dot width now keep in mind we we did define a width for our sprites in Down oh do we or do it didn't wait we did for somewhere. I thought that we had width in there Okay, well, then we need to add that so that's good. So I'm gonna hit self dot width equals 20 and self dot height equals 20 and 20 comes from the fact that in the turtle module that is the default height and width of objects now if you Replace your objects with some images later You'll need to change these values. But for now, let's use 20 and 20 Okay, and then there's and now watch what I do I put a little forward slash that lets me come down here and continue this line so self dot x plus self dot width is Greater than other dot x And don't put a space after this Okay, you should get an error And I of course I spelled self wrong self then the next line is self self dot y Plus other dot y Sorry, it is less than sorry. Yeah, I made a mistake there No, that's fine. That's good. Okay, and self dot y is less than other dot y plus height and and self dot y plus self dot height is Greater than other dot y So that's our big long if statements and Then we return Otherwise we're going to return false Now I think this is going to work. I hope I didn't mess it up. Let's go ahead and try it Watch let's just run this and see if it compiles Yeah, okay, so no obvious errors at this point Now what we got to do? So we have the method, but we haven't used it So I'm gonna do down here is I'm gonna add Check for collisions So what I gotta do is for Sprite in sprites So think about that. So for each sprite in every sprite I want to check and see if it collides with certain other sprites So for example so Watch this if is instance Sprite an enemy. Okay, so if it's an enemy And I'm gonna go if player Dot is Collision Notice here. There's no underscore. This is this is a built-in method of Python If it's a collision with the enemy or with the sprite, sorry So that means the player has collided with an enemy sprite. So I'm just gonna say player dot x equals zero Player dot y equals zero. So we'll send the player back to 00. So let's test that see if it works Okay, we got an error actually enemy is not defined if is instance sprites enemy Did I make an enemy class yet? I Did not make an enemy class yet. I just made it into a sprite. So let me go ahead and fix that real quick so I'm just gonna go ahead and Copy the player thing real quick And go ahead and make up an enemy class So class paste that enemy and it's gonna be x and y Otherwise the enemy acts as a regular sprite. So then I'm gonna say enemy equals enemy So I've made an enemy class. Okay, so let's go ahead and test that Okay, we got an error Height is not defined So line 74. So let's go ahead Up to our sprite class today. Ah, should be Other dot height Okay And hopefully some of you saw that before I did Okay, so see what happens when I touch the enemy Okay, well nothing happened Okay, oh my gosh, that was freaky Okay, so you can see how some the collision is not working So this just tells me that my formula is wrong. So let me go ahead and compare that again. I have a picture of it That's why I'm not switching back to it on the screen So it's self x is greater than ah That's the first mistake self x is less than Rectangle plus width and self x plus self width greater than a wreck to and self y Yeah, it's less than Okay, so let's go ahead and try that. So again, this is the process. Nothing. Nothing ever works the first time Let's go ahead and Okay, so you can see I hit the enemy I'm gonna test that I want what I want to do is I want to test it from different directions Make sure it's working, but it should work if it works from one I'm coming from the top now Okay, so that worked Okay, you know the collision not gonna be a hundred percent accurate because the player does rotate and it gets a little bit thinner at some Points, but you can see how that's basically working So now I'm gonna I'll put this down in the comments below so you just copy that and paste that because it's kind of a mess to type in but So we can see how basically that little method lets us Check for collisions. Okay, so Let's go ahead and do if is instance Sprite let's do power up Okay, and if player dot is collision With the sprite That's what we want to do with the power up Let's do sprite Dot X. Let's just move it just so we know that we've done something with it. Let's just move to 100 100 You know, we'll do close something worth it later Okay, so let's go ahead now. We don't have we don't have a power up class So we need to go ahead and create that This is another reason why we created classes and we could have done it a little differently, but classes just make things easier so power up and So for now, it's basically just a copy of the sprite class But we will have some different things to do with that later and I'm gonna make my sprite a power up Let's go ahead and run that And okay, so you see how the power up moves. So just again, we're just testing the collisions Okay, so that and now let's do something really cool Let's do Now let's take care of what happens if it hits the missile Now this is going to be probably a little bit more probably maybe a little bit more interesting. So in the case of an enemy If Missile dot Is collision with the sprite so let's go ahead and just move the sprite to negative 100 Dot y equals negative. Let's just move it to negative 100 100 and then if the missile collides with a Okay, let's move that to Plus 100 negative 100. Let's go ahead and test that and see if we can start shooting things All right, and Okay, so it kind of works Now what do you notice there? About the collision. I'll watch this now. You see this also. There's like some kind of bug Okay, and this is this is the process so what what are some of the things that we see happening and some of the things that we see happening are that The collision is registering really early. So before the weapon even gets to the object and The second thing we see is that this thing keeps resetting because actually the weapon is still sitting there It's just not being rendered. So we have two things to fix now The first one is pretty easy the missile if you recall when we rendered it We set its size to 20% of the Regular size so 20% of 20 is 4 so the missile height equals 4 and It's with equals 4 also So let's go ahead and test that so So you see how the collision is gonna be a little bit tighter now Now the other thing you see also is the missile keeps going So we want to get rid of that we want to fix that So we're just fixing each thing as we go and in this case if there is a collision we just say missile Dot reset Because we made that nice little reset method for it missile that reset Yes, let's go ahead and fix that and it seems like it's working Pretty happy with that and then they said the other thing that we got to fix is where The missile hit here, but it's still actually sitting there hidden. So that's why we have this weird effect going on So we need to check first If there's a collision And we also need to check if it's active if missile that state equals active And there's a collision And if missile that state Equals active And there's a collision Then we do this so I think that fixes everything Just missed that so you see how you can get pretty close, but if you don't actually hit it Oops So now the game is actually kind of getting to be a bit more interesting. I think Alright, and we probably last did we do collision power up on the player? Okay, we did that one and Okay, we did do that. Okay. So now we have all of the basic collisions working Okay, so just do a quick review. We are using the It's called access-aligned bounding box method a a bb collision Where we check the boundaries of the height and width of the object and again, there's the formula again I'll copy it and put it down below. That's it's easy to mess up and then in our main game loop because we got to check every time through we update the Sprites so that updates their location and everything and then we check each sprite against different objects so in the case of Enemies we check. Okay. Is this sprite at an enemy if so Check against the player check it against the missile. Is this sprite a power-up check it against the player check it against the missile So what's nice now is What I could do is I could go ahead and create a It's a Second enemy And I'll put that at negative 100 and I'll put it one You go the other way and I'm gonna go ahead and make a second power-up I got to call it and everything has to have its own name and power-up to power-up to And we'll put that at Negative 100 and I'll put that negative and negative So then if I add that to my sprites list we got there enemy to And sprites append Enemy as I power-up to Now we can have multiple objects running Okay, and then because we programmed it because the way we programmed it no matter how many objects we have We don't need to do anything separate. You know, we can have a hundred enemies in there Now the game's starting to kind of come together a little bit. Oops. Oh, I didn't I thought I fixed that Let me go ahead and fix that real quick. I thought I fixed that in the last video But notice how when I fire it goes again Let's see here fire Okay, so in the fire method for the missile I got to do this so if self that state I Equals ready Okay, so I can only fire the missile if it is ready. So that was my mistake there. Sorry about that So let's go ahead and run that one more time. So I only want they only want to fire one missile at a time Just missed. Okay. Good And we don't want to shoot our powers but we can but all the collisions work and Exactly as expected. So kind of the game is starting really starting to come together a little bit now Okay, so I'm gonna go ahead and end that there. Thanks for watching subscribe for updates