Pygame Tutorial 13 Collision Detection

all right you Python game developers welcome back to this Python PI game video series in which we are creating a space invaders game so in the last video we created multiple bullets instead of shooting just one bullet began showed multiple bullets at the enemy and we don't have to you know just just shoot one bullet during the whole game but as you can see our bullet is going through the enemy and it's not colliding and nothing is happening when the bullet hits the enemy not the score is increasing and the enemy is also at the same place it's not respawning anywhere which we want so in this video we are going to learn about the collision concept how to make sure that the two objects have collided and that's a very important concept in gaming so what we will doing is that let me just actually load this game so that we can see the bullet and the enemy properly so what I'm gonna be doing is we are going to calculating the distance between the bullet and the enemy and if the distance is short enough then we are gonna conclusively say that the bullet has collided so the bullet has an x and y coordinates when it goes up through the screen and similarly this enemy also has an x and y coordinate when it goes through the screen on left to right or up and down so we are going to use all of these four coordinates that is the x coordinate and the x coordinate of our bullets and the y coordinates of both our bullet and an enemy to calculate the distance between them so now what I want you guys to do is go to google and type and distance between two coordinates and you're going to take you to this website match planet comm and over here there is this equation which says distance equals to x coordinate minus x coordinates of other object squared plus y coordinate of one object and y coordinate of other object Square and then a square root of the whole thing and this x2 and x1 does not matter whatever is at front because and by the end of the day we are squaring this so obviously you must have learned this in school or in college but if you haven't don't worry about it just go through this article a little bit you just need to execute this code in this equation in our Python so even if you don't understand what this is doing we are going to learn how to actually implement our equation in Python so that is going to be really helpful for you guys so keep this equation and mind go back to our code and just believe this fire underscore bullet function we are going to create a new function and we are going to call it is coalition so this function is basically going to help us with define whether a collision between the bullet and an enemy has occurred or not so he's going to take four values the x-coordinate of the enemy the y-coordinate of the enemy the x-coordinate of the bullet and similarly the y coordinate of our bullet and now we are going to create a variable known as distance so this distance will store the distance between the bullet and our enemy and how do we calculate this distance obviously using this equation so distance equals two and then we are gonna do a square root so to do a square root or do any kind of mathematic operations you need to import something known as math so if you go up a hand imported math yet so I'm just going to write import math so I'm going to import that and then first of all we want to do the square root so let me go over here and just write math dot s qrt so this is the basically the function or the method for doing square root and then apply some brackets now we need the nvx and the bullet X and we need to subtract it according to the formula but before even that we need to square it so if we go back to our equation you can see after the square root the x2 minus x1 is being squared so we also need to implement that mathematical formula so we are just going to write Matt dot pee-eww so this stands for power and then it requires two values the X and the y so we are going to do that so we are not just write comma two because we wanted to be squared and then over here we are going to give it the value of enemy X minus n mu Y all right so this is looking good we have executed the first part of our equation now we have to execute the second part of the equation that is plus y2 minus y1 and the squared so we are going to go back over here and inside our math dot power so this is this part math dot by x2 and this is enemy not anyway why this is bullet X about that and then we are just going to write plus and then we are gonna write Matt dot power again because we need another power and requires two your two values first is the path value so we want it to be squared again and then we just need to subtract the enemy a when you mean why so and me why - bullet why and this should give us the value obviously we need to close this melt math dot square root so we are going to apply another bracket but looks like it's an extra bracket so what we are doing is math dot POW we are doing X 2 minus X 1 or X minus X 1 minus X 2 doesn't matter squared it and then math dot paw y2 minus y1 squared so just to make sure that these two are separate we are going to apply and at the bracket over here so I'm going to also do this with this one let's all right so there is some problem over here so this is the problem so let's apply a bracket away yeah I just love that and this is looking pretty good so now what we are going to do is we are going to write an if condition if this distance is less than 27 pixels then return us the value of true that as the collision has occurred yes return us the value of false so we're going to write F condition and return false I mean we didn't need to write this F condition we could have just returned false over here but I just wanted to make sure that you guys understood all right let's format it properly obviously mutti format anything in python make sure you press ctrl or + L LS for I don't know lose something so make sure you press ctrl alt class L if you are using PI job it should format everything beautifully alright so RS collision function is complete so now what we are going to do is we are going to scroll down and we are going to write inside our while infinite loop over here just below this enemy X or even the politics we are going to add a comment that's gonna stand for collisions so that we are know that we are writing the code for collision and over here we can just check whether a collision or is happening on so I'm just going to create a variable called conservation and then I'm gonna call this escalation function and over here we are going to set the value of enemy x and if you buy and over here in all of this code inside this while loop we are manipulating the value of x and y of enemy and the bullet so this is the right place to call this escalation function so over here we are just going to write bullet x comma bullet Y all right so this is looking good so this is collision is going to store the value of true and false and if the collision has occurred is going to store the value of collision of true in collision all right so this is looking good and then we are going to write a condition if coalition so what do you want us to do if the collision is occurring basically that is what our program is asking so what do you want after a collision has occurred so the first thing that we want is to reset the bullet to its starting point so we are going to reset the bullet Y at 480 and then because the bullet is not being shown anymore we want to change the state of the bullet to ready so let's change the state of the bullet to ready by writing bullet state equals to ready and then the last thing we want to do is obviously increase the score so I'm just gonna create a new variable let's say let's actually just go over here so that it's close by and we are going to write the value of score equals to zero all right so this is looking pretty good so I'm gonna just scroll down and over here I'm gonna write score score by missing the variable of score square plus equals to 1 so this will increase the value by 1 every time we hit our enemy and after that we are just going to print out our score so I'm just gonna print it out on the screen so now let's just run our program and see if everything is working or not so I'm just gonna run the main program and over here we are going to shoot our bullets and hopefully if it hits and I'm not a bad player as you can see the score increased by one so let's try to hit it again and as you can see the score is increasing by one but when we are not hitting it the score is not increasing so let's try to make our score at least five or all right so the score is 5 so what one thing you must have noticed is that the enemy is still on its current path and it's not least pointing to default locations so we need to fix that so what we are going to do is we are gonna right change the value of player X the enemy X and the enemy by over here so I'm just gonna scroll up and check out where we have declared the value of enemy X&Y so I'm just gonna copy this and I'm gonna paste this inside our if collision obviously like this code can be optimized we can create a new function for all of this stuff that is happening over here but maybe we can do that in the later videos all the optimization classes and functions we will see whether we do that or not because this is not such a complicated program but maybe we were so now what will happen is that the enemy X and the enemy Y will get restarted or respawn to a different location and we respond to a random integer that is between 0 to 800 and Y is 50 to 150 obviously we have already discussed what this does it basically gives a default position to the enemy so let's give it a shot again and we're gonna run the mean again and see if it works so let me just wait for it all right so our enemy is actually just going back to its new location all right let's just wait for the enemy to come back a little bit down so that we know that our code is working properly so let's shoot it alright so our game is respawn properly our enemies respawn properly and it looks good so one thing you might have noticed that when it comes to this right-hand side of the screen it just drops down even if you are trying it on your own if an enemy respawns towards the right-hand side of that window of a window it just goes down so why is that happening that is happening because when we respawn our enemy or just be when we are creating then we are giving it a value of 0 to 800 and but inside our code we have written and over here that if the enemy X is greater than 736 make sure you change its Y value so therefore whenever our enemy is responding with a greater value than 736 it's immediately going down without a chance to go left that's why we need to make sure that over here when we are creating the enemy we do increase the value till 800 only till 736 and just for safety we are going to write 735 so obviously we need to make sure that the 735 also goes in when a collision has occurred so I'm just gonna copy that away do all right so this looks good we have also fixed an error so this is looking pretty good all right so all right look enemy is responding properly let me just shoot it again all right so alright guys so this is pretty much it for this video in the next video we are gonna learn how to create multiple enemies so right now there is only one enemy so the game is pretty easy you can just play it really easy and actually it's pretty fun to play when I'm recording this video also playing well anyway guys let me just recap actually quickly so that you guys can understand so if you already understand the video just go to the next video but for people who have not understood properly so we have created a function called is collision where you have four coordinates to off enemy coordinates and to off bullet coordinates and we have used all of these four coordinates in this formula which we got from typing in distance between two coordinates this formula and then we try to execute this formula inside our code obviously before you shall start doing this man thought Scott make sure that you input math and after that we made the square root and then inside this week using the dot power function we squared it just like we have done in this formula X 2 minus X square squared so we need you to make sure that we square it and make sure that the brackets are correctly placed because if you guys are typing the code it might like it might you might mess up somewhere so make sure you guys are typing this code properly line by line all right obviously I'll upload all this code on github so you'll find it somewhere over there and by the end of this series I'll uploaded after recording all the video so you can wait for it a little bit or you can find it in the description or attachments or something so Matt power enemy X - politics you guys understand all of this stuff and then how this 27 number came to be so I just tried and tested what was the proper number for distance between enemy and bullet to call it a cool and I just by trial and testing I came on this number if the distance between the bullet and the enemy is less than 27 I'm gonna call it a collision and then if the collision has occurred it will return true and obviously we are calling this function over here if collision so if a collision has occurred what do we want from a program first of all we want to reset the Y of the bullet to 480 that is the starting position of the spaceship and then we are going to change the state of course to ready because you can't see the bullet anymore and when you can't see the bullet anymore the state is ready and then we increase the score by 1 and we printed out the score and then we made sure that the enemy respawn to its new location all right guys so this is basically a way to just to track back a little bit this is just a way to kill the enemy and then make sure that we also create a new enemy all right guys this is pretty much it for this video thank you for hanging out in the next video we are going to learn how to create multiple enemies so I'll see you over there

Loading