 Okay, welcome back to space arena the ultimate python turtle graphics game tutorial part 12 in this part We're gonna deal with player and enemy collisions And this is gonna be very similar to what we did in the last part where we dealt with missile and Enemy collisions, but it's gonna work just slightly differently and you'll you'll see what happens here So let's go ahead and find the part of the code Where the collisions are registered because we've already done that part. We did that a while back so what we want to do here and come all the way down is We're looking at is instant sprite enemy and the that that enemy is active We did the missile thing last time So what we want to do here is We'll go ahead and just do this what we're gonna do Sprite health Minus equals let's say they both lose 10 and then player dot health minus equals 10 And let's go test that see what happens. I'm kind of curious myself There's a red thing She tell you what I'm gonna go ahead and change this the size so we're not bouncing off the screen all the time here It's really in my nerves as well. So it's 300. So the so screen is 800 by 600 so we'll make it 700 by 500 So kind of so that way we're not chasing after stuff. That's not on the screen Okay, so did you see there I collided now I've got less power I've got less health And you can see now because my ship has a hundred health and this now only has 20 We changed that a video or two ago in the last video So you see I can just go ahead and ram. I can just go ahead and ram the enemies until I Now my health is at zero. Let's see what happens here if we go into further Okay, so we never really dealt with so you see how it's doing the same thing that did in the last video where the Players health is now less than zero. So let's go ahead and deal with that just like we dealt with it in the Enemy class. So we added this self and this reset method. So let's go ahead and see if I can find the player Okay, so the player doesn't have an update method so I'm gonna go ahead and copy the update method from the and the reset method I guess from the The enemy since we already did that and so let's go ahead and pop that in there Okay, so Now with the player we can't now. This is the nice thing about using objects I don't have to update this at all because we're using self now the thing with the player is when the player Resets the player doesn't really have a state. The player state is always going to be active But what we can do is we need to self dot x equals zero self dot y equals zero self dot health equals self dot max health So we basically reset everything back to the beginning. I'm gonna say self dot heading Equals 90 because that's the way we started it Let's let's go ahead and do self dot dx equals zero self dot dy equals zero and We'll kind of figure out see if that does what we need it to do Did we do live? Let's see if we did live player. We did lives. So also do self dot lives minus equals one Now we'll have to deal later with if we have zero lives, but we're not gonna deal with that for now Do that later in the tutorial. Let's go ahead and test that that's gonna take some time because Because the player has a lot of health Let's go ahead and It's funny. It's hard to die when you want to See make sure this works Okay, and now the player went back to the center. It's not moving it is it has full health and It is ready to Fight the enemies again, and we haven't dealt with power-ups. Obviously now the other thing I wanted to do is if you recall from the original game when to The enemy and the player collided there was a bouncing effect Let's go ahead and put that in because that that's pretty darn cool So I'm gonna put that in the sprite class So I can have different sprites bounce against each other if need be but I think we only use it with a player And enemies here, but let's go ahead and just do it So I'm gonna go ahead and put that doesn't really matter where you put it to be honest But I'm gonna put it here before updates. I'm gonna say deaf. I'm gonna bounce self and other Okay, so it might be the player and enemy. It could be enemy and player could be enemy in any way We can have the enemies bouncing against each other if we wanted to too. So watch what I do actually To explain the physics of it. It's kind of interesting. One of my students Sarah. Thank you very much Sarah She actually figured this out a few months ago in class and I've been very grateful for that. She figured out how this works So basically what happens is if you want two objects to bounce like somewhat realistically is You need to exchange their DX and dy values okay, so think about that so if Two objects are heading towards each other and They hit each other and you have a perfectly elastic collision They will bounce back in the other direction So they basically borrow the momentum of the other person and then momentum of a is transferred to be the momentum of B is Transferred to a so here's I'm gonna do that. I'm gonna do temp underscore DX equals self dot DX I'm gonna do temp underscore dy equals self dot dy And then I'm gonna do self dot DX equals other dot DX self dot dy equals other dot dy and I'm gonna do Other dot DX equals temp underscore DX don't screw up the underscores and other dot dy equals Temp underscore dy now those of you who've done a lot of Python Well, we'll recognize the fact that there is an easier way to do this in Python But I'm gonna write it out like this. So in case you're coming from another language It makes a little bit more sense what we're doing. So so temp DX equals self So basically we're holding the value of self DX and dy because we're about to change it here to the others DX and dy and the others DX and dy Takes on the temp values, which was the original DX and dy So then down here, I actually have to call that my main game loop So if there is a collision and I can put at the end doesn't matter. I'm gonna say player dot bounce With the sprites the player was going to bounce. Let's test that out. This starts giving me like a cool effect. Oh My gosh, that's so cool Okay, you see how they're bouncing now Oops Can you see how it's just very subtle, you know, if there's if there's a very little difference in speed There's just a little bit of a balance. Do you see that? Okay, and And now we've got Okay, we still got our weapons working Okay, that's how that that one's flying around because we hit it really hard. But yeah, so that that is the bounce effect Okay, which is very very cool. It gives us a bit more fun in physicsy type type feel So so we created a bounce method and again, it just Basically it exchanges the DX and dy values and that that was it again. Thank you to Sarah for figuring that out for me Yeah, so I think that's about I think go back and watch that again if you need to but it's not that it's not that crazy It's not that difficult But you also see how now the player has a way of dying it loses lives. It goes back to the center So we're starting to put into place all those little game Game elements that make a game into a game other than just a kind of fun demo. Okay, stay tuned for more