Pygame Tutorial for Beginners - Python Game Development Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Alright guys, welcome to this Python game  development video series. And we will be   using the famous pi game library of Python to  create this school space in reader game. And   we are going to go really in depth into the spy  game and learn about a lot of different concepts.   And specifically, we will be learning a  lot about like new parts of Python that   you haven't explored yet. And new parts of pi  game, like moving characters, shooting bullets,   understanding game concepts like game loops and  stuff like that. So if you haven't ever created   a game, or you have only created a very basic  game, this would be a very good starting point   for you guys. So before we get started on all of  that cool stuff, learning about game development   and stuff like that, you should make sure that the  basic stuff is installed. So first basic stuff is   obviously Python. So if Python is not installed,  if you're using Python for the first time,   just go to python.org. And then click on downloads  and just download the latest version of Python.   After downloading Python, we are going to be using  ID which is basically a cooler way to write code   of Python. So I'm going to be using pi charm. And  if you don't want to buy the professional version,   although you have a 30 day trial, which you can  use for this course. But if you don't want to buy   it, just download the community version, which is  free, and you won't need more than the community   version. But if you're using something like  Sublime Text, or maybe the main Python terminal,   that's also fine, it won't matter. But if you want  to follow along completely, make sure that you   download pi charm. And thirdly, you can just have  a look at the PI game website that we are going   to be using to create our game. So you can just  have a look at it, it's not really necessary. But   you can just have a look at what has been done  with this PI game and look at the Games maybe   and you know, just kind of get ideas. So let's  get started. Make sure that you have downloaded   Python, after you have downloaded pi jam, it  will look somewhat like this. Now that we have   everything installed, we need to make sure that  we have this new project over here. So for that,   just go to file, click on new project. And then  right down the project name over here, I have   already created the project and I'm calling it  Space Invaders because this is the game that we'll   be creating, and probably have already played  this game when you were a child. But the main   idea is that there is a ship at the bottom and  is going to hit the enemies that are at the top   by shooting a bullet. And every time you hit an  enemy, your point increases and the enemies slowly   come down. If it touches the spaceship, you lose.  And the final score is what how many enemies you   have shot. So that's the basic idea of the game.  But you'll understand more as we go through the   videos. So after you have created this project,  we are going to create a new file over here and   we're just going to call it main because most  of our code is going to go inside this main dot   Python file. And after that, we need to install  pi game. So if you're using terminal, you can just   open up your terminal by typing in cmd. And you  can just type in pip install pi game. And this is   for people who are not using pi charm, or you're  using something else like Sublime Text or Python   terminal. But if you're using pi charm, what you  would do is you would go to File and then you   would go to settings and in settings, you would  go to project name, the whatever name you have   given if you want to follow along called spacing  reedham. And then go to project entrepreneur. And   over there you can see I've already installed  pi game. And if this PI game is not already   installed, just go to this plus sign over here  and search for pi game by game and then you can   select it and click on Install package and this  will install pi game onto your computer. Now I've   already installed pi game, so I'm not going to  install it again. And after that you can just type   in by import pi game. And if this doesn't show  an error, that means that pi game is installed on   your computer. So this is pretty much it. This was  a pretty small video, just an introduction video,   we're going to be doing a lot of cool stuff.  From the next video, we are going to be creating   our game, we are not going to go into theory and  stuff, we're just going to start with practical   stuff and actually start creating a game. So we  are going to be creating a game window next time.   So as you can see, this pie chart has a window  right now. So we are going to be creating this   kind of a window in the next video with a cross  button over here. So I'll see you over there.   And just to recap, what we did in the last  video was installed by game and in this video,   we are going to be creating our first Python  game window using pi game. So obviously the first   thing we need to do is import pi game into our  program. And hopefully you have already created   this mean.py file, if you haven't just created  right now. And first of all, we are going to   import pi game. And then we need to initialize pi  game to access all of the code methods and all of   the other stuff that is inside this PI game  module. So to initialize this PI game module,   what we need to do is just type in pi game.in  it so whenever from now on to create a game or   create a new game. Just make sure you add this  line otherwise your Python or pi game code is   not going to work. So let me just add a little  bit of comment. We're here saying initialize the   PI game. Alright, so after initialization, we have  to create our screen. And pi game makes it really,   really easy. It's just one line of code. It's  crazy. So we just want the right screen. And   this is our variable. And we can just write pi  game. And this is to access the methods that are   inside this pygame module. And we need to access  the display dot set mode method. And then inside   these brackets, we need to add another bracket. So  this is a tuple, make sure you add another bracket   inside it, otherwise, it's not going to work.  And our people mess up over here and then ask,   why is it not working? It's because you haven't  added another bracket over here. And then inside   it is going to take in two values, the height and  the width of the window that you want to create.   So I'm just going to give it for now a value of  800 pixels of height and 600 pixels of width.   So this should be good. And let me just add a  comment, create the screen. And let me just format   this properly. So this is all you need to create  a screen. But as you will see, after we run this   program that it has won me bad thing about it. So  as you can see the game window was created. But   after a few seconds is just went away. Let me show  it to you guys again. So after we click on Play,   it stays there for like one or two seconds, then  it goes away. And this is because our Python   program goes through these three lines, and then  just exits the program. And therefore you can't   see that screen for a long time. Because it just  takes one or two seconds for this program to run.   And that's it. So one way to bypass that is just  create an infinite loop over here. And this also   has problems. But just to show you guys that it  works, I'm going to do it. And I'm going to make   sure that my task managers open because this  will hand my computer. So make sure that your   task manager is also open. Because this will Hank  the window, at least that you're working on. But   just to show you guys, I'm going to run this  and you will be able to see that the window is   there. But it is totally hand. So you can see the  window is there. But it's nothing is working over   here. And this cross button is not working. Let me  just go back over here and close my Python window,   click on end task and it has been closed. Now why  is it hanging? Why is it not just closing down by   pressing this cross button? It is because we don't  have an event of a quit function over here. So   there is no way to quit the program. Our program  is just inside this while loop and is now able   to end and Windows make sure that that program  gets high. And that's why we need to add a quick   functionality to our PI game game window. Just  like this window has on pressing this close button   this window closes. So for that we are going to  be using something known as events. Now we'll get   into events more a little bit later. But for right  now just understand that event is anything that is   happening inside your game paint. By game window,  I just mean a window inside which your game is   being played. So anything happening inside that  game window is basically an event. So for example,   if you press arrow keys or anything on your  keyboard, you move your mouse that is an event   and even when you press this close button, that  is also an event in pygame. So this pressing of   the close button is actually known as a quit  event in pygame. So we are going to make sure   that we exit this loop when this close button is  pressed. So for that, what we are going to do is   we are just going to create a variable, let's call  it running and we are going to give it a value of   true. And inside this while loop we are instead of  just typing in while true to make it an infinite   loop, let's call it while running. And whenever  we want to break outside of this infinite loop,   we're just gonna turn the variable of running  into false, and it will automatically break this   while loop. So if you don't know the basics of  Python, or programming, basically, while true runs   infinitely and when this value of true is turned  to false, it comes out of the loop. But we're not   going to go into much into that because that's  just like very basic programming. So anyways,   after writing while running, what we need to do is  go through the list of events that are happening   inside our game window. So for that, we are going  to create a for loop and create a variable event.   And then we are going to loop through all of  the events that are happening inside our game   window. How do you do that pygame makes it really  easy. Just type in PI game dot event dot get and   then brackets and this makes sure that all of  the events that are there that are happening,   get into this pygame dot event dot gate. And then  we can loop all of these events using the for loop   and one by one check. If this cross button has  been pressed, and women whenever this cross button   is being pressed, we'll just turn this while loop  into false so this is pretty easy. So inside this   for event we're gonna check if the close button  has been pressed. And how do you do that just type   in if event dot type equals equals two, and then  we are going to check whether the event in PI game   is quit or not. If it's great, then we're going  to change the value of running into false. So   this is pretty easy. So we have changed the value  of true into false whenever the close button is   being pressed. And this close button is basically  a quit event. And we are going to loop through   all of the events to find this create event. So  we're going to go into more into events a little   bit later. But for right now, just understand this  is what we are doing. So let's just play this and   understand whether it's working or not. Let's see.  So right now you can see that we do system is not   initialized, it's giving us an error. It says pi  game dot event dot gate. Alright, so the problem   is that we have misspelled This is not pygame dot  small letter squared is just pi game dot quit. And   that's pretty much it. So when you are dealing  with a lot of syntax, you have to make sure that   you write the correct one but small problem, we  are going to click on play. And now you can see   that this is working and our window is not going  anywhere the computer is not hanging, it's looking   pretty good. Alright guys, let me just go through  this once more what we are doing over here. So we   have created our screen, we have initialized our  PI game. And then over here we have created a game   loop. So let's actually call it the game loop  over here, we're just going to add a comment   that says game loop. So this is an infinite loop,  which makes sure that the game is running always,   and the window doesn't close down. And we know we  want to do something like move a bullet or move   a play or something like that move a human being  inside a game window, we have to make sure that we   do stuff inside this while infinite loop which  only exists when this close button is pressed.   And we have implemented that close functionality  over here by pressing by getting the events all   of the events using this PI game dot event dot get  method. And then we are looping through all of the   events. So if more events are added, for example,  if somebody presses an arrow key to move forward,   that is also going to be looped in does pygame  dot event dot loop. And then we can add another   condition that if arrow key is pressed move  that player but more of that in the future.   For right now just understand, we looked at event  dot type equals two pi game dot quit and then we   change the running into false to make sure that  we come out of this while loop. And I know guys,   I'm revealing a lot of stuff right now. But this  is like the basics of game development. So I want   to make sure that you guys understand it properly.  Anyways guys, this is pretty much it for this   video. In the next video, we are going to change  the title and add some stuff into our game window.   So I'll really look forward to that. So right  now it's just blank. And it's just saying my game   window is not cool. So we're going to look into  that in the next video. I'll see you over there.   So in the last video, we learn how to create this  game window using pi game. So this window is a   little bit not personalized, because it says pi  game window over here. The background is weird,   the logo is not there. So in this video, we are  going to be learning about three main things.   So first thing is how to change the caption  of this window instead of pygame window will   make it something say something like Space  Invaders, we're going to learn how to change   the logo of this window right now is like  a snake or a turtle or something like that,   and how to change the background color of this  window. Now these three things are very basic,   but obviously very important when you're creating  a game. So let's get started on that. And first   thing we're going to do is obviously just comment  that so that you guys can come back to this code   and look what's happening. So we're going to  write a comment title and icon and after that,   we are going to change the caption or the title  of our window and pygame makes it very, very easy,   you just write it by game dot display. Because  we are meddling with the display of our windows   we have to write in on display, and then dot  set underscore caption and there it requires   the title of the window let's just call it  Space Invaders because that is what we are   creating. And then we are going to create the  image so you must have been seeing this UFO dot   png image over here we are going to be using this  image in just a second I'm going to show you how   you can get this image but before that, let me  just add this icon inside our program. So for   that this is also very easy. First of all, what  you can do if you want to get the same image is   that you can go to this website known as flat  icon.com and then search for spaceships or any   kind of icon so I've searched for spaceship and  I got this cute UFO icon. So to download this,   you can just click on Download collection and  make sure that you select PNG and then 32 by   32 pixels size. So whenever you're working  with an icon and pygame make sure you select   the 32 into 32. I haven't experimented with 24  or 16 pixels. It might work but I'm just going   to go with 32 into 32 pixels. You can select  any other icon if you want. So I selected this   icon and I just downloaded it into my project and  here is UFO dot png. Now to add this inside our   Python program it's pretty easy Just type in icon  equals two pi game dot image because we are using   an image, so we are going to be using the image  module that is inside pi game. And then dot load,   we want the image to load. And then we just  write the name of the image. So in my case,   it's UFO dot png. So we are going to use that UFO  dot png. And then the next line is pretty simple,   we are going to use the same dot display that  we did with the title. And we're just going to   write that over here. And then just said, snort  showing by this, I think it's set underscore   icon. So you can use that. And then you just  type in the variable name of icon over here,   that we have created. And this will make sure  that the icon has been added. So let's just run   this and see if it's working or not. So as you  can see, the icon is also working and the Space   Invaders is there. So this is looking pretty good,  we have learned how to add an image, not exactly   an image just icon over here and change the title  of our window. So this is looking pretty good. The   third thing we need to learn is how to change the  look or the background of our window. So for that,   we need to use something known as screen dot fill.  So we are going to go inside our while running   loop and over here we are going to write screen.  So anything that you want to be persistent inside   our game window that you want to make sure that  appears continuously, it can be an image or it   can be some text, it has to go inside this running  infinite while loop. That is where we are going to   change the color inside this running infinite  while loop. So we're going to just state screen   dot fill. And then inside this, we're going to  create a tuple make sure you add another bracket   inside it. So there are two brackets, and then  you give it three values of RGB. So we're going to   give it I'm going to explain what RGB is in just  a second, I'm just gonna give it a value of 000.   Right now, just to understand this RGB means is  red, green, and blue. And basically every color in   this world is made up of these three values. And  one more, you can see a PCT, but let's not go into   that. So RGB basically stands for red, green, and  blue. And this is the values these are the three   values that you can use to basically implement any  color on the screen. So this makes sure and this   value goes from zero to 255. So you can make it  255. But not more than that. And you can use it in   a combination. So they say we want just a pure red  color. So we'll say red, we want to 55 and then   green zero and blue zero. And similarly, if you  just want a green color, we can say zero to 55,   and blue as zero now what if we wanted a light,  light green color, we can lower down this value   to maybe like 150. So this will be a lighter  version of pure green. So this is how it works,   basically. And just to show you guys a more better  version of RGB, or what you can do with RGB. If   you haven't understood it properly, let's go to  Google. And you can just type in color to RGB   and go to any website. But I'm just going to go to  this reputable website. And over here you can see   these RGB values. So obviously, I told you red  is 255 00 line is 255 03, this, and similarly,   and so on. So let's go with this red color  right now. And you can choose any values. And   there are a lot more colors, you can even make  your own colors by trying out different three   values. So just experiment with it a little bit.  So I'm going to just choose the red color. So we   are going to go back to our code and I'm going  to write down to 55. But this is not going to   work even though we have tried, but this is not  gonna work just telling you in advance. So you   can see that it's not working, it's just a black  screen. So over here, we need to use something   known as update. So right now our display is not  updating. So that's why pygame provides a method   known as display update to make sure that if we  add anything inside our display window, it gets   updated inside this while running loop. So we for  that is pretty easy, just make sure that you add   it after every game. So it's pi game dot display.  So let's just try display dot update. And that's   it. So we have to add a couple of lines inside  your pipe icon game code. The first line is this   init line. And obviously you need to make a screen  but two lines that are always going to be there is   this init line. And the second line that's always  going to be there is this update line because you   want to update your screen because your players is  going to move away the bullets are going to moving   to Amy's going to move moving the score is going  to be changing. So you want to make sure that your   display is always updating and by display I just  mean the game window that we are working on. For   this program. It means this screen variable. So  anyways, so now we have updated it. Let's give   it a shot. Let's click on Play and as you can see  now it's in red color. So it looks good. We have   changed the title. We have changed the logo and  our screen is updating to read, I don't like it,   I just wanted to show you guys that you can change  the color and maybe we'll change it to something   a little bit more funky in the later videos right  now I'm just going to go with black that is 000.   And that's pretty much it. So guys, I'll see  you in the next video where we are going to be   finally adding images and our player that is maybe  a spaceship some kind inside our game window. And   so I'll see you there where we are going to learn  how to add images inside our game. So peace out.   So in the last video, we learned how to add  a caption and a logo and how to change the   background screen of our game window. In this  video, we are going to learn how to add an   image to our game window, which is pretty cool and  pretty easy because of pygame. But first of all,   let's look at the player PNG which we are  going to add inside our game window which   is kind of a spaceship which is shooting being  sort of its arms. So if you want this image,   you can just go to flat icon.com and download  this image. Obviously you can use any other image   you want. But I'm just going to stick with this  image. And you can go to download collection. And   obviously search for arcade space over here.  If you want this exact image and then choose   PNG and choose a 64 by 64 pixel. Obviously, you  can resize it in pygame. But I've seen that it's   better if you already get the image of the size  that you want. In this case, I want a 60 by 64 by   64 pixel version. So I'm going to take that, and  I've already added it to my project over here.   So after you have done that over here, then you  can come back and we can start adding the image.   But before we even start that, I just want to  discuss what is this 806 100 I have put over here,   I wanted to discuss this because this 800 is the  width and the 600 is the height, I think I might   have said something wrong about this. In the  starting videos. This 800 is actually the width   and not the height and the 600 is the height. So  if you look at our window, this is the x axis,   you can think of this window as an x and y axis  window. So if we go from left to right, this is   an X axis. And if you could go from top to bottom,  this is a Y axis. So it's always starts from zero   comma zero, which is at the top left corner,  and then we as we go on the right hand side,   the x value starts to increase. And on this top  right corner, the x value is 800. Because that   stands for width. And now if we go from top to  bottom, the value of y increases while the x   value remains the same. So over here, the value  is 600. But the value of x is zero. So if you   don't understand it a lot, don't worry about it.  Just think of it as this left, left to right is   basically x. and the value right now of this whole  window is 800 in terms of its width, and the value   from top to bottom is 600. And the value at the  bottom is 600. And if you want to go into into the   middle, it's approximately 300. Not approximately  exactly it's 300. So why I'm telling you this,   because we want to place our image in a particular  position. So that's why we need to give it in stat   coordinates. Obviously, you can experiment with  coordinates a little bit, but we'll go into it.   So let me just add a comment over here so that we  know that we are talking about the player and then   we need to add the player image which is pretty  easy like we have done it in the in terms of the   logo. So let's just copy and paste this over here  and change the UFO to player dot png. And then we   are going to give it the x and y axis. Where do  you want the image to be positioned. So let's   create two more variables. Let's call this player  x. And we're going to give it the value of 370.   And I'm going to go into why we give these values  to player x and player y. So these variables   basically represents the coordinates that we want.  And over this, we are going to give the buyer the   value of 480. So why have we done that. So if we  look at this window, we want to clear image to   appear somewhere over here, which is exactly the  half of the screen width. So we want it to appear   at the starting of the game, at least we want  it to appear in the middle. And we want it to   appear in the lower half of this window. So the y  axis should be closer to 600. Because this point   is 600. If you guys remember this lower part, so  you want it to appear a little bit above it. So we   can give it a value of about 480 or 450, whatever  you guys want. And then the x value we're giving   it 370 which is a little bit half less than half  of the width. So the width is 800. And if we go   a little bit less than this, this is 370. Because  we have to consider the size of the image itself.   If we give it exactly the value of 400 then it's  not going to fit perfectly. That's why we have   given these values so you can see that the 370 is  somewhat half of 800 and this purity is close to   600. Obviously when we change the values, you will  understand this concept a little bit better and   feel free to change this values and see the effect  it has. Now after this, we're just going to create   a function and we are going to call it player.  Now I'm going to go into why we are creating a   function. But let's just create a function for  now. And then we're going to use a method known   as screen dot blit. So grid basically means to  draw, so we are drawing an image of player on to   our screen. And this screen is actually known as  the surface of our game, we'll go into what it is   surface, which later I know I'm saying that  a lot, but it's not relevant to this video,   but just understand that we are drawing this image  of player dot png on our game window, by using   this method known as dot blade. And dot blade  blade actually means to draw. So we after loading   the image, you also need to draw it on the screen.  So that's why we are using this screen dot blade   method. And over here, it requires two parameters  or values. The first value is of the image itself.   So we are going to give it the image. And the  second thing it requires are the coordinates.   So we are going to give it the coordinates of  player x and player y. So the x and y coordinates.   After this, we are going to call it I'm just  going to ask you guys a question, Where do you   think we should call this player, we want this  player to appear in every frame of the screen,   we don't ever want this player to disappear.  If you guys said inside this while loop,   you guys are correct. So we are going to call this  player method inside over here. So let's just call   it and we're going to just call it over here, like  this, and make sure that display method is called   after this screen dot fill method. Otherwise,  this player won't appear on the screen. Why do you   think that is, this is because the screen is drawn  first. And then on top of the screen, we draw a   player or a spaceship. If the opposite was true,  then the spaceship will be drawn underneath the   skin, the screen, not the skin, but then it will  be drawn underneath the screen. So that is why we   always want it to be drawn after screen dot fill.  Actually, let's change let's remove this position   to from there to over here somewhere. So that we  guys know where this so because we are going to   be adding a lot of images and stuff. That's why  we need to make sure that this screen dot fill is   just above everything, and all the other things  and drawn on top of the screen. Alright guys,   so this is pretty much it. Let me just add another  comment, I don't know where it disappeared, the   standard for RGB, and red, green, blue, just to  make sure that you guys still remember what this   is. Alright, so this is looking pretty good. And  our player should appear on the screen. So let's   run it. And then we'll just go through all of this  stuff again to see what is happening. Alright,   so as you can see, our image has appeared on the  screen. So first, we loaded the image in display   an image variable, we pointed out the x and y  values and the x and y values were dependent   on the screen size that we gave over here. 806  100 because he wanted it to appear in the let   me just close this one, the old one. All right.  And because he wanted it to appear in the middle,   that is why we gave it the value of approximately  half of the screen. For example, if I gave it the   value of let's say just to experiment, let's say  we gave it the value of let's say 750 that is   close to right hand side, and we reloaded it.  And let's wait for it. Okay, as you can see,   it appeared on the right hand side. So you can  see that the x value has increased. And similarly   if we decrease it to, let's say about 30. I'm  just showing you this to guys so that you guys   can understand the x and y positions clearly,  you can see that it appears on the left hand   side. So let's change it to back to about 370.  And similarly in y if we change this to maybe   30 you will see that it appears on the top of the  screen instead of at the bottom of the screen. So   as we get go down, we are getting closer to that  600 value. So we are going to from zero to 600.   So let's get back to 480. And hopefully now you  guys have understood properly the meaning of this   x and y axes. So anyways, after that we created  this function known as player and inscribed   that we drew this image onto the screen and  it required two values the image the x and   y coordinates and then to make sure that this  player is always shown on the screen we added   it to this wild game loop infinite loop. So guys,  this is pretty much it for this video. I might be   repeating a lot of things a lot of times that  might be a little bit irritating for some guys,   but hopefully you guys understood it properly.  In the next video we are going to learn how to   move this player from left hand side to right  hand side using our arrow keys on a keyboard.   That's going to be really exciting. Let's wait  for that. So as in the next video, peace out   In the last video, we learned how to add an image  inside our game window. In this video, we are   going to learn how to move this image left, right  or top or bottom. So basically we are concerned   about the movement of an object inside our game  window. Or anything that we draw it can be image,   it can be an text after you watch the 3d game  and understand how the mechanics of movement   in a game works. So just to make sure that you  guys understand it, I have created an image so   that you guys can understand what we are going  to do, and the basics of movement of an image   or anything that you want. So obviously, we have  created a game window. This is argument though it   is 800 wide, that is 800x axis, and then it's  600 is height. That is you can see it on this   as y axis. And our spaceship currently is at  370 pixels. As we have added this over here,   that is the player x s ad 370 pixels. Now if we go  back to our image, what if we wanted this image to   move from over here to over here, we can just add  five pixels to our already existing 370 pixels and   this image will move in the right hand direction.  And now what if we added five pixel continuously,   so this five pixel will be added continuously,  this image will continue to move towards the   right hand side. So for example, we can add  a functionality which adds five pixels to our   existing 370 pixels in a while loop, some kind of  a while loop. And this will happen continuously   after every iteration of our loop. Similarly,  if we want to move it to the left hand side,   we can just decrease some value from our 370  pixel value. That is we can decrease some x   player x value and we will move towards the left  hand side. And similarly if we want to move up,   we can subtract some y values. So currently, the y  value of this is 480. So if we subtract some value   from this 480, it will move towards up direction  and if we add some value, it will move in the   downward direction. So adding and subtracting  values from x and y coordinates have a current   position of the object or in this case, patient  can lead to a movement of that object. And we have   to add it to a while loop because we want it to  happen continuously. So if we go back to our code,   we need to add one thing before I can show  you guys how we can add values to our player   x and player by or we how we can subtract it. So  before that we need to make some changes, because   currently there is no way to manipulate the x and  y coordinates from our while running loop. And if   you didn't understand what I meant by this over  here, don't worry about it. When we actually write   the code, you'll understand what is happening and  what I mean by increasing the x value to move it   towards the right hand direction. Basically, if  you move it very, very quickly, it won't seem that   the image is moving, it will just seem like that  the spaceship is continuously moving. Because of   the effect of persistence. In our eyes, we can't  see things that are happening very, very quickly.   Like magicians do, they create illusions by moving  things really really quickly. And the persistence   in it tells us that the image hasn't been erased  and then created over here, it just assumes that   the image has moved on the right hand direction  if you're adding the five value to our already   existing x coordinate. So anyways, we need to do  something so that we can manipulate the value of   player x and player y from the running y Lu. So  what we can do is go to this def player function   and over here at x comma y, so that we can send  in the values of the new x&y coordinates that we   want. And then instead of player x and player  y, we can just add x and y. So that the values   that we send to this player instead of the last  value is that it caught from player x and player   y. Instead of that the new values that we send in  of x and y are being shown on the screen are being   drawn on the screen. So now we have to go back to  the place where we are calling this function. So   over here, right now let's just write in player  x and player y. And you will see that there has   been no change. So let me just exit this first and  then play it again. Let's play it. So you will see   that there has been no change because you're not  manipulating the player x and player y value right   now. So let's go through this once as to what's  happening over here. So first, it goes to this   code, it creates the spaceship, then it takes the  coordinates of the x and y of your player or the   spaceship, and then there is this player exchange.  Let me remove this right now we don't need it. And   then if we go down, we have created this function  of death player x comma y. And then this is being   drawn on the screen. So this code hasn't been  executed yet because it is inside this wandering   while running loop. But the program just sees  that this exists. Now when we go down over here,   then this player thing is called and it takes the  player x and player y value from here and sends   it to this Deaf player function and it Inside  x and y, currently the 370 and 480 exists. So   this image is being drawn at this coordinate.  Now what if we change the value inside a while   running function. So for example, let's say we  increase the value of player x by something small,   this let's, let's increase continuously by five.  So this will move really, really quickly, and you   won't be able to see the image moving towards the  right hand side. But I just want to show it to you   guys for the speed. So you can see how quickly it  moved, I hope you call it, this will slow it down,   I'm just going to give it the value of 0.1. so  that you guys can see it properly. And you will   see that it is moving in the right hand side  direction slowly. So why is this happening.   So every time this while loop is running, the  player x coordinate increases by 0.1. And this   value is sent over here and this function player  function, and then it is sent to this new player   function. And a new image is basically drawn on  a new coordinate and the original image is being   removed. So for example, let's also just, let's  make it 0.2. And this time, we are also going to   print out the value of player x so that you guys  can see that the player x value is increasing from   our original value. So here, you can see it being  increased by 0.1. And it's going on, let's stop it   right now. And let's actually try it to move it in  the left hand left hand side direction, so let's   give it a minus value of 0.1. So now we want to  decrease the value. So instead of increasing the   value, we want to decrease the value and move it  towards the left hand side. So let's play it again   this time, and hopefully, let's just remove the  player of the print function because we don't need   it. So now let's play it. And you can see it's  moving towards the left hand side. And similarly,   if we do instead of the x coordinate, we do the y  coordinate and decrease its value is going to go   towards the up direction. So let's reload it.  And you will be able to see that it's moving   towards the up direction. And similarly if we  add stock, it is going to go down. So hopefully   you guys understood the mechanics of a movement of  an object. Just to reiterate, why is this movement   happening? Why is when when we are decreasing, the  value in y is going up. So because at the bottom,   the value of y coordinate is 600. So if we  decrease the value, it is going up. So over here,   it's about 480. And if we decrease it more, it's  going to go towards up direction. And similarly   if we increase it, it's gonna go below more  than 600 coordinates. So right now you can see   there are some, some fallacies with this system.  Right now, we can't control where this is going,   and you will be able to see that it just goes  outside the screen. So we need to do a couple   of things. First, we need to make sure that we can  control the movement of the spaceship in the left   and the right hand direction using an arrow keys  on the keyboard. And the second thing we need to   do is add a boundary so that the spaceship  doesn't go beyond our game window right now   is just disappearing by going outside the game  window. So we'll be handling these two things   in the next and the coming videos. In the next  video, we are going to learn how to add keyboard   support to our game. So basically, we are going to  control our spaceship using arrow keys. And that's   going to be really exciting. So I'll see you in  the next video. This is pretty much it for this.   And the last video we learned about the movement  mechanics of the objects in our game window. So   for example, if you do player y equals two  minus equals to 0.1, then it will move in   the other direction. So if I run this man, you  can see that our spacious is moving in upward   direction. But right now we don't have any way to  control the spaceship, we can't control it using   our keyboard. And this is exactly what we will be  learning in this video. So what we'll be doing is   that when we press a left arrow key on a keyboard,  the spaceship is going to move towards the left   hand side. And when we press the right hand side  arrow key, then a spaceship is going to move on   the right hand side. So what is the algorithm  to do that? So first of all, we are going to   check whether any each stroke is being pressed  on our keyboard, and after we check that we are   going to check whether the keystroke pressed is  either left hand side arrow key or the right hand   side arrow key. So we are going to do that. So  let me just write down an if condition. Let me   just remove this over here, we don't need it. So  after that, let me just tell you one more thing.   So any keystroke that is pressed on a keyboard is  actually an event. And as we have discussed before   that an event is anything that is happening inside  our game window, any kind of input control. So for   example, if we move a mouse, we click somewhere  we close this is Windows. So this is a quit event.   And similarly when we whenever we press any key  stroke on our keyboard, it is a key stroke event.   So for example, every event gets logged inside  this pygame dot event dot get and we loop through   all the events that are being stored inside it.  And then after every iteration. So for example,   let's say in the first iteration of this while  loop, a keystroke is not being placed but on the   10th iteration. A keystroke is being pressed and  it will get stored inside of spy game dot event   dot get And we'll be able to loop through that.  And check whether a keystroke has been pressed   or not. So, inside this, I'm just going to write  an if condition, which says actually before that,   let me just add a comment so that you guys  understand what we are doing. I'm going to write   if key stroke, the stroke is pressed, check,  whether is right, or left. So this is basically   what we'll be doing. Not keystroke or keystroke.  Oh, god, I'm gonna mess up my spelling. Alright,   keystroke. Can't get this feeling right. All  right. So anyways, how do you check whether   keystroke is being pressed or not by gay makes  it really, really easy. It's kind of an event,   which is pygame dot key down. So this basically  check if any keystroke it doesn't have to be left   or the right it can be alphabet, a even backspace  or space, it basically checks if any keystroke is   being pressed. So first, we are going to do  that we are going to write if event dot type,   which we did inside this if condition. So we  have to go actually inside this for loop. So   we'll just press this tab button over here. Let's  press this. So now we are inside this event for   loop. And now we are looping through all the  events that are there. And if the event dot   type is equals equals to pi game dot key key down,  then this means that a keystroke has been pressed   on a computer. And now we have to check whether  the keystroke that was pressed is left. Alright,   so this is also pretty easy. We just again  type event dot this time we type in dot key   because we want to find out the key that is being  pressed. And we just type in PI game dot key left,   key left. And this will just check whether the key  that has been praised is the left arrow or not.   If that is the left arrow, it says for right now  print. left arrow is pressed. And after that I'm   going to write again, I'm just going to copy and  paste this over here. Like this, and over here,   I'm just going to write right. And instead of  K underscore left, I'm going to add the right.   Alright, so basically what this does is it checks  with any keystroke is pressed and then it checks   whether the keystroke that is being pressed,  is left or right. And after that we are going   to check when the keystroke is being released.  So after pressing down on the left arrow key,   when we remove our finger from that left arrow  key, that is known as the removal of the keystroke   or the key up. So key down is pressing that key  or pressing any button on the keyboard and key up   is basically releasing that press. So we're going  to check for that over here if event dot type is   equals equals to pi game dot key up. So hopefully  we'll be able to find that key up over here. And   then over here, we're just going to check if  it was the left key or the right arrow key,   and then we are going to print something so  that is pretty simple. We can just copy this   from here. Let's copy this, paste this and we are  going to give a condition that if the left or the   right arrow key was pressed, so we can just copy  this from here and put this colon mark. And then   over here, we're just gonna write key keystroke  has been released this series so you guys know   what is going on. And I want you to write this  code down and then actually try it out yourself   so that you guys know what's happening behind the  scenes. So now we can just press on Play and over   here is going to do nothing. But on our print  screen over here we are printing all of these   things. So let's press the left arrow key and see  if something happens. So we have to first activate   our window over here, and then we can press the  left arrow key and we can see that the left arrow   is pressed. And when I remove my finger, it says  keystroke has been released. Now what if I keep   pressing on a left arrow key if I keep pressing,  it will just say left arrow is pressed and the   keystroke has been released. And similarly if I  press the right arrow key, it says right arrow is   pressed. But if I press any other key, it prints  nothing. So let's actually create a statement   over here to let's say, a keystroke is pressed  so that we know when any other key is pressed,   it also runs. So over here, we can just press the  left arrow key again, as you can see, a keystroke   is pressed left arrow key is pressed and keystroke  has been released. But if you press something   else, let's say enter, it just says a keystroke  is pressed and not the left and the right arrow   key. And that means that our keyboard strokes are  working. So now we can just minimize it and we're   going to create a new variable over here. And this  will signify the change that we want an x so if we   want the X to move in right hand direction,  we need to add value to this 370 existing   value and we wanted to move on the left hand side  direction we need to decrease value from eight. So   I'm going to just call it over here create a new  variable And call it player x underscore change   equals two and right now I'm going to give it a  value of zero. And inside our if conditions when   the left key is pressed, I want it to increase  by 0.1. And when sorry, decreased by 0.1, and   we're gonna change this 0.1 number, it basically  signifies the speed with which you want the object   to move. And in this case, it's spaceship,  how quickly you want the spaceship to move,   and we are going to change it later. And even in  this video, we will see how it goes if it moves   too slowly or too fast. But anyways, right, now we  are going to decrease the value when the left key   is pressed and increase the value when the right  key is pressed. So what kind of change Do we want   in our x, when we want the left arrow keys press,  so we're going to just right over here, player x,   and it's called change, and we want to decrease  the value by 0.1, let's say and when the right   arrow key is pressed, we want to increase this  change by 0.1. So let's remove this minus value   from over here. And when the key is released,  we want the spaceship to stop moving. And that   is why we want the change to be zero when our  keystroke is released. So over here, we are just   going to paste this over here. So this is looking  pretty good. Let's actually remove this comment   from here. And then we can just add this player  exchange to our existing player x x coordinate,   so I'm just going to write here x plus equals  two, and then player exchange. So doesn't matter   whether we are adding over here because we have  given a term value of minus 0.1. And over here   we are given it a positive value. So whenever  something is added, and the value is negative,   so I'm just going to give you an example. Alright,  so that you guys can understand this arithmetic   properly. You might be thinking that why are we  adding over here because we want to subtract 0.1,   right, so let's say our current value is  370. Let's actually take a smaller value,   let's take our player x coordinate is five. And  then when we do plus equal to So this basically   means equals to five plus, and then the value that  is player x underscore change. So right now this   value is minus 0.1. If the key left is activated,  so this value will we actually minus 0.1. So after   this arithmetic is processed, this will turn  actually into, so let me just add arrow key,   this will actually turn into five equals to  five, minus 0.1. And similarly, if we add,   then the addition is pretty, pretty obvious. So  we can just add 0.1. And this will remain the   same just 0.1. And the value will be 5.1. So this  is how this operator over here works. So we don't   need to worry about whether we are subtracting or  adding if we have added our arithmetic operator   over here, in this case, it smiles. So this should  be working, let's run our main.py file and see if   it is working. So let's activate it. And over  here, when you press a left arrow key, our ship   is moving. And when we press the right arrow  key, our ship is moving and we when we release   the keystroke, it is still moving. Why is that the  case. So let's go over here and see. All right,   so here the place x underscore change is 0.1.  So we have to change it to value zero, because   we don't want it to move, you can actually change  it to 0.1 to see how it works, how the spaceship   is stopping. So let's try it again. Now when we  move on the right hand direction, and we release,   the keystroke, the ship actually stops. So  this is looking pretty good. But Asli, this,   the speed is a little bit slow. So I'm going to  increase the value to 0.3. And now if we run it,   our speed should be sufficiently well. So this is  looking pretty good in future will change 0.3 to   a higher value when we use the clock concept. But  we are not using it right now. So this is looking   pretty good as patient is moving perfectly. And  just to recap, what we did in this video was first   we checked whether any keystroke has been pressed  on the keyboard. And then if the left keystroke is   pressed, then we increase or decrease the value  of player exchanged by 0.3. And if our right   key is present, we increase the value by 0.3. And  whenever our keystroke has been released, we make   sure that the player x coordinate stops changing.  And that is why we have given it the value of zero   to make sure that the spaceship stops and doesn't  go on. And then we just use this arithmetic thing   over here which increases the value of player  x or decreases the value of player x depending   upon the value of this player x underscore change.  So this guy's is looking pretty good in the next   video. If you have any queries please ask them  and questions. So this is pretty much it for this   video. In the next video, we are going to give  our spaceship an enemy because right now is the   space it's looking very lonely for a spaceship.  It's just going in the space and it's not finding   any planets or any enemies. So it's thinking about  its existence and the meaning of life. So what's   happening with me, so we are going to give it a  meaning in the next video. So I'll see you whether   in the last video we learn how to move our  spaceship using our keyboard and the left   hand The right hand side direction. And this  video I was about to give it some enemies.   But then I realized our spaceship is just  moving out of the screen when you go to the   left and when we go to the right. So why is  this happening. So when it goes to the right,   this right border of the spaceship of our game  window, it actually reaches the 800 width that   we have said. And when it goes beyond this 800,  it goes to maybe 810 and 105 pixels. So this no   restriction right now to the spaceship movement  in the left and the right hand side direction.   And similarly, when it moves to the left hand  side, it goes in a little bit lesser than zero   angles goes into minus territory, because this x  coordinate is an integer this that's why it can   go to negatives. And even it can go to minus  450, or something. So what what should we do,   so what we are going to do is that when it teaches  the zero coordinate, then we are going to any time   it goes into negative stuff, we are going to  make sure that it stays at zero by using an if   condition. So we are going to basically tell our  spaceship that, hey, if your x coordinate reaches   zero or less than zero, make sure it turns back  to zero. And similarly on the right hand side,   we are going to tell our spaceship, if your x  coordinate reaches this 800x coordinate path,   then I don't want you to go beyond it and change  your x coordinate to 800 again, so every time it   goes beyond 800, it's going to just delete itself  and go back to 800. But it's going to happen so   quickly that the eyes won't see it. And it will  look like that the spaceship can't go beyond 800.   So let's do that. That's right. And if condition  just beyond this arithmetic operator we have over   here. And as I said, we are just going to write  player x, if this player x becomes less than zero,   that is the x coordinate becomes less than zero,  then we are going to change the value of player   X to zero. And similarly if our let's write f l  f, player x becomes greater than or equal to 736   pixels. So why are we taking this 730 by 36 pixels  instead of 800 pixels. So because our pixel,   we have to take into consideration the size  of our spaceship. So if you remember the PNG   size of the spaceship is 64 by 64 pixels. So the  width of this spaceship is 64 pixels. That is why   we are subtracting 64 from 800. And that means  736 pixels. So if we chose any other number,   for example 800, then it will stop at this point  and not before that, but we wanted to stop over   here and not allow any part of spaceship to  appear beyond the game window. So that's why   we are choosing this 736 pixel number, we  just subtracted 64 from 800. And over here,   we are just going to change the value of whenever  it goes beyond 736 we want it to again go seven to   two back to 730 6x coordinate. And this will mean  that it will look that it cannot go beyond what is   actually but it's just deleting that spaceship and  creating a new one sn 36x coordinate picks it. So   let's check this out. And hopefully it works well.  let's reload it and see if it works or not. So as   you can see when I press the right arrow key  doesn't move beyond this. And feel free to try   it yourself and change the x coordinates just  experiment with it a little bit. So the last   video is pretty long. And this video is pretty  short. So that's good. So in the next video,   we're finally going to create an enemy  for a spaceship so and see you over there.   The last video we learned how to create boundaries  for our spaceship so that it doesn't go out of   bounds. And this video we are going to give it  our first enemies have already downloaded an enemy   image which looks like a happy Space Invader. It's  not going to be so happy when we shoot it with our   spaceship. But anyways, let's get this icon from  flat icon calm just like we always have and search   for Space Invaders. And I'm going to go with this  red space and you can select whatever you want   and download the 64 by 64 picture as usual, and  obviously in the PNG format. So now we can go back   to our code and just like we did with a player,  we can add this enemy dot png inside our code. So   let's do that. Let's just copy this whole code and  just add it below the player change it to enemy.   So we're gonna change everything to enemy so we  just have to rename everything. It's called an   enemy and then I'm just gonna copy and paste this  in the remaining areas. So let's just copy and   paste everything and instead of the player dot png  we are going to call it the enemy dot png and make   sure you rename the PNG file otherwise you tried  if you read enemy dot png is not gonna work. But   anyways, so we need to also create this function  again. So let's just copy and paste this too.   So we're gonna paste this over here instead of  player. We're gonna call it enemy and everything   else should come in the same. Let's format it  properly. Another thing is in PI charm if you want   to format everything properly if you have written  code that's not formatted and doesn't look good,   what you can do is you can just press Ctrl, Alt,  and L, and it will format everything properly   inside your code. So it's just Ctrl L. So as you  can see, it says imports optimized, it will just   make your code look good. So again, the command  is Ctrl, plus alt, plus L on your keyboard. So   anyways, now that we have the function of the  enemy, let's just go down below and add the enemy   over here so that we can call the function while  our while loop is running. So let's add the enemy   over here. And instead of the player x, let's just  call it enemy. And instead of the player x, we're   going to call enemy x. And instead of player y,  we're going to give it a value of and revive. So   right now you can see that the coordinates are the  same as all the spaceships. So this should overlap   our spaceship. So let's see if it's overlapping  or not. So right now, it's not overlapping. Let's,   let's actually check out why is that happening.  So this is because inside our enemy function,   we have not renamed this to enemy dot m g. So  let's do that. Let's rename it enemy dot image,   and then just reload it, and this should work  properly. So now you can see that the enemy is   on top of the spaceship, but he's not we want, we  want the enemy to be like somewhere on the top. So   maybe in the middle, but on the top, so we can  let the enemy x remain the same. But I'm going   to change the enemy y to something else. Let's  give it a value of 50. I've already tried it   out and 50 looks pretty good. So let's see how  it looks. Alright, so our spaceship is moving,   and we have an enemy. But the problem  with this enemy is that it's not moving,   it's very boring. So we're going to fix  that in the next video. But for right now,   what I want is that this enemy should appear in  the random places. So whenever we kill an enemy,   it is going to appear again or in a game terms is  going to respond again. So I wanted to respond in   different places, maybe on different x axis.  So maybe like maybe over here, over here,   update scale, and maybe in y should also appear  in different places. So maybe next time over here,   and it's same over here. So you want it to appear  in different places. So every time it reloads.   Basically, we want it to appear in different  places. And even at the starting of the game,   it should not be constant, it should be totally  random. So what we are going to do is we are   going to make this enemy x and enemy y actually  random. So we have to first write down the line   that actually helps us randomize values. So we  are going to import a package known as random. And   this is pretty easy to do. So let me just minimize  this over here and then scroll down and over here,   we are just going to write instead of 370, we're  just going to write random dot Rand int. So this   is a method inside the random package to choose  a random integer between two values. And then we   have to give it the value that we want. So we want  a starting value and an end value. So in our game,   we want it to appear anywhere between zero and  800. So we are going to give it the starting   value of zero, and the end value of 800. And  similarly with why we wanted to give a value   between maybe 50 and 150. So let's change this to  5050 should be the starting point. So the minimum   height should be 50. And then it can go down over  here, let's say 150. It's approximately 150. So   I'm going to give it a value of 150. Alright guys,  so this is looking pretty good. Let's just try   it again and see if it works. So let's run main.  Alright, so this is working it respond over here,   let's reload it to see if it responds somewhere  else. Let's just reload it and see where this time   it appears. This time it appears over here. Let's  try the third time third time is a charm. And it   always works. So this time it appears over here.  So our program is looking pretty good. We have   created an enemy and it is responding in different  places. So this is just looking pretty good. In   the next video, we are going to make sure that our  enemy moves. So right now our enemy is stationary,   it's not moving from the left or the right and  it's not going down. So we are going to make sure   that we rectify that in the next video is going  to be really exciting. So I'll see you over there.   So in the last video, we created our enemy  and made sure we applied randomization to our   enemies so that when it gets destroyed and created  again it should appear in random places. So as you   can see when we reload our program is going to  respond in a different place. So in this video,   we are going to make sure that our enemy is moving  so imagine that there are multiple enemies right   now there is just when one enemy and we haven't  created multiple enemies, so there is not any   problem. But imagine there are multiple enemies  and they're moving from left to right. And they're   also when they hit the boundary they come a  little bit down. So that's what we are going to be   focusing on today. We are not going to be creating  multiple enemies but we are going to be focusing   on the movement of this one single enemy and then  we can apply it to multiple enemies. But let's for   right now just focus on one enemy. So what exactly  do we want from this enemy? What kind of movement   Do we want. So what we wanted to do is move from  right to left in x direction. So for example,   this enemy will move in the right hand side. And  when it hits the boundary that is about 736 or 800   pixels, it should change its direction and move  towards the left hand side, and when it reaches   the left hand side, then again when it hits that  zero coordinate x coordinate direction, again   changes direction and move towards the right hand  side. So this is the movement in the x direction.   Now there is also a movement in the y direction.  So whenever it hits a boundary, whether it can   be right boundary or the left boundary, then it  should come a little bit down. So that imagine   there are multiple enemies, and we are trying to  shoot all of those enemies down. So how will the   game get over. So whenever these enemies hit the  boundaries and come down at one point of time,   they are going to hit the spaceship as they are  coming down, and then we'll make it Game Over,   like in the classic Space Invaders, so we need to  execute the x movement. And as well as we need to   execute the Y movement, this always is going to  be very easy, because you already know how to move   the spaceship, and we are going to do the same  thing that we did in the player. So first of all,   we will just scroll up, let's minimize this over  here. So we're going to slow up and because our   enemy is going to have the exchange as well as  our y movement. So I'm just going to copy this   over here and also make sure that we add our  by movement. Now enemy exchange over here,   we are going to give it the value of 0.3. Maybe  in future we'll speed it up because a spaceship is   also at the speed of an exchange of 0.3. Maybe  we will change it upon future. But right now   I'm just going to give it a value of 0.3. No, I'm  not giving it a value. So for example, in player,   we haven't, we have just given it a value of zero  over here. And below over here, we have changed   its direction by typing in negative and positive,  we are not going to do this with our enemy   V is going to provide the value of 0.3 over here.  Now if you scroll down, we're just going to do   the same thing that we did with our player. So  for example, over here, we made sure that if it   hit the boundary, so this is the algorithm  basically, you can call it the algorithm,   but it's pretty simple. So whenever it hits the  zero coordinate, we change its value to zero. And   whenever it gets greater than says 36, we make  sure that it coordinates Kate to send 36 again,   and this prevents the spaceship from going out of  bounds. So this is a kind of way of checking the   boundary. So we can do the same thing with our  enemy. So let me just copy and paste this over   here. And instead of everywhere player, we're just  going to call it enemy. So it should be in small.   So over here, also, let me just copy actually and  me and paste it everywhere, this is going to make   sure that we save up some time, not a lot of time.  But so let's face this enemy over here, over here,   I wish there was a quicker way to do it, but  it's not. So anyways, now I'm just going to add a   comment over here so that you guys can understand  what we are talking about over here. So checking   for boundaries of spaceship, so it doesn't go  out of bounds. And over here we're gonna write,   maybe enemy movement, enemy movement. And this  looks pretty good. Instead of restricting us, what   we want is whenever a boundary is hit, we want it  to go someplace. So for example, when it hits that   left boundary, we want to increase the value of  x so that it starts going in the right hand side   direction. And similarly when it hits this right  hand side boundary, then we want to decrease the   value of x. And this will keep on happening  because it's inside our while loop. So now   we are going to use the same arithmetic operation  that we use with our players. So if we scroll up   a little bit, you can see when we wanted to change  directions we gave, when we wanted to move towards   the left, we gave it a value of minus and when  you wanted to move towards the right, we gave it   a positive value of 0.3. And then we just added  because of this arithmetic property. So we are   going to do the same thing over here. So instead  of enemy x equals to zero, what we are going to   do is we are going to write and meet exchange  equals to 0.3. And so that basically when it   hits this left hand side that is zero coordinate,  it can start going towards a positive direction   that is it should start increasing the x value.  And similarly we want when we want it to go left   hand side we are going to change it to a minus  value. So we're going to give it a value of minus   and this should be enough guys. So actually, if I  scroll up, you can see that I've put the value of   enemy x underscore change equals 0.3. We can leave  it to zero or we can leave it as 0.3. It doesn't   really matter. Let's run it and see if it works.  Let me just close down the previous implementation   so that we don't get confused. And I'm just  gonna run it again run main. And hopefully the   enemy will start moving. So you can see that the  enemy is moving from left hand side to right hand   side. And actually, this looks pretty awesome.  So now you can see that it's not moving in the   y direction is just moving in the x direction.  So what we want is that when it hits the right   boundary, it should move downwards. Or even if it  hits the left boundary, it should move a little   bit downwards. So what I'm going to do is I'm  going to go over here, and I'm going to add any y   change equals 240. So that we can move it down by  40 pixels. So why When do we want it to move down,   we want it to move down when it hits the boundary,  that is the geo coordinate or the 736 coordinate,   so we can just increase the value of y over here.  And that should be enough. So I'm just going to   write in me, y plus equals two, and then we want  the enemy y change, just let it let's just paste   over here. And then we can do the same thing over  here. Let's not erase that, we'll just add it over   here. So now let's just reload it and see how it  looks. Now you can see when it hits the boundary,   it actually goes a little bit down. So you can  imagine multiple enemies, when they're hitting the   boundary they are going down. So it's gonna be a  little bit difficult for a spaceship to eliminate   all of them, but it's going to try its best.  And when this enemy reaches the spaceship, we   are going to give it our game over. And depending  upon how many enemies we can hit with our bullet,   which we'll be creating mostly in the next video  or in the coming videos. Depending on how many   enemies he can eliminate with that bullet, the  score is going to be according to that. So you   can kind of get the idea of the game with this  video as to how the game was going to look. So   today we are going to be learning about adding  background image to our PI game. So right now   if we open up our space invader game, you  will be able to see that we have an image   we have in spaceship, but it's happening in the  black background, which doesn't look that good,   we need to add some kind of a space background  to make it very cool. So what I'm going to do is,   I'm already downloaded an image of a background  that is background dot png. And it's some what   looks like this. So how did I get this image. So  what I did was I went to this website fault free   pic.com p ik and then free free pick.com. And  I searched for this text which says colorful   spaceship background. Obviously, feel free to get  an image from wherever you want. But make sure you   get an image that's 800 to 600 pixels wide is that  size, because our game window is of that size.   As you can see, we are made again window of this  area. So what I did was I went over there and you   can choose any of these images. But the image that  I chose was this one, and then I used illustrator,   so free pick is a website was going to give you a  lot of Illustrator Photoshop stuff, and you have   to edit it out to make it your own. So I removed  all of the weird stuff that we didn't want,   for example, this rocket, this text and stuff.  So I removed all of that stuff. And if you know   illustrator, feel free to make a new image on  your own. So what I'm going to do for you guys,   if you're not able to get this image on your own  is that I'm going to attach it to the description   or attach it somewhere so that you guys can  get this image, just look at this videos,   text. So what I'm going to do is after we download  this background image to our project folder,   make sure you do that, then we're just going to  add it to our windows. So over here, how do we   add images to our program is just by pygame dot  image dot load. So I'm going to do that. So I'm   going to add a comment, let's say and we are going  to write just background, just write background.   And then I'm going to create a variable called  background, I'm going to do the same thing   that we did with the wrist. So I'm just going to  copy and paste it over here. And instead of UFO,   I'm going to write background. So obviously my  images of 800 into 600 pixels. So make sure it   is of that size. So now that we have added it,  if we run it right now is not going to appear   because it just appears for a second and then  it disappears, we want it to persist. So how do   we make it persist is by adding it to our while  loop. So we need to make sure that we go to our   while loop over here and we are going to add it  below this screen dot fill because if we don't,   then this black color is going to be on top of our  background image which we don't want. So what I'm   going to do is I'm just going to add another  comment over here. It's gonna say background   image, and we forgot the B's. So let's add B and  then over here, we're just gonna do the screen.   Just typical thing we do when we want to add an  image which is screen, dot blit. So basically   draw the background image and then we need the  variable of the image so that is background.   And then we need to add the coordinates of our  image so we just wanted to appear from the top   left corner severe Going to add zero comma zero.  And that should be pretty good. So let's run our   main.py program, let's run main. And you will  be able to see that our background is there.   But the problem with a background and NP and  a spaceship is now that they are moving very,   very slowly. And why is this so? Pause this video  and figure it on your own territory, pretty cool.   But I'm just gonna explain it over here. So our  background, PNG is actually 226 pixels, sorry,   226 kB, so it's a little bit heavy. But what  happens is when this while loop is running, every   iteration of this while loop previously used to  happen really, really fast. But now because it has   to load this hairy background image, the iteration  of this while loop, every iteration is a little   bit more slow. So that is why whenever we are  increasing the value over here, so for example,   we are increasing the value of player, it is too  small. So whenever this while loop is going, it's   just increasing the value by 0.3, which is very  slow. And that's why it's not able to move around   a lot. So what we are going to do is we are going  to increase the value to five of our players. And   I'm going to change the value of our enemies.  So let's see where as enemies. So here are our   enemies, I'm going to give it a value of let's  say, four, I just want these enemies to be a   little bit slower than aspasia. Because when they  are going to be multiple enemies, then we don't   want them to appear like really, really fast. So  let's reload it now and see if it works. So it's   still slow. Why is this happening? a spaceship  has increased its movement, but the enemy is still   slow. So let's see if somewhere our old values are  still persisting. So enemy we have changed the x   value over here. So let's go up and see where  we have not changed the value. So over here, the   enemy exchange is still 0.3. So we have to change  it over here to let's make it four. And now we can   reload it and you will be able to see that the  enemy is moving at a good speed. So now we have   the background image, we have our spaceship and  we have our enemy. The only thing missing is the   bullets because we don't have the bullets right  now to kill this enemy. So in the next video,   we are going to make our bullets or get started  with making our butt so i'll see you are there.   In this video, we are going to learn how to shoot  our bullet inside our game window. So as you can   see, I'm shooting a bullet and the enemy. So right  now the bullet is not hurting the enemy because we   haven't implemented a collision yet. But more  than that in the future videos. In this video,   we're just going to learn how to shoot a bullet.  So let's get started. But before we get started   with the code, I just wanted to explain a couple  of things. And the first thing I wanted to explain   is what's the starting position of the bullet.  So when we are in the middle of the window that   is at 370 pixel x coordinate approximately,  it's shooting from the top of our spaceship,   but if we move left, it still shooting at the x  coordinate of our spaceship, it doesn't matter   where our spaceship is, the x coordinate of the  bullet is going to be always equal to that of   our spaceship, just the starting positions. For  example, if you shoot the bullet right now, even   if we move our spaceship, the bullet is not moving  with our spaceship, just the starting position of   the bullet is equal to that of our spaceship. So  that is the first thing and the second thing is   that as we shoot the bullet, the bullets of y  value is decreasing. So because at the bottom,   the value is 600 y coordinate, and as we go up the  value of y decreases and add the top is actually   zero. So at the top, the y coordinate of y of a  bullet is going to be zero. So we have to decrease   the value from about 480 pixels. So because this  480 coordinate y coordinate and we are going to   decrease it as we go towards the top. So right now  you can see that it's disappearing at the top. So   the third thing I want to talk about before we go  into code, because it's so important to understand   is the state of the bullet. So right now when  you can't see the bullet its state is at ready.   But when you can see the bullet state is going  to be at file. So right now it's not appearing   on the screen. That is That means that we are  reading the bullet for fire. And when it shoots,   you can see the bullet and that means the bullet  has been fired. So let's not do between that   firing period anything. So this is what it means.  Obviously, we are going to understand this more as   we write down the code. So let's get started  with that. So the first thing we need to do   is add an image of a bullet inside our project  folder now already added as you can see on the   left hand side bullet dot png, and it kind of  looks like this. So how can you get this image   so you can just go to flat icon.com like we have  done previously and searched for this bullet. And   you can just download this bullet from here. And  obviously you can choose any bullet that you want,   and even through Google or something but make  sure when you download this image it's 32 by   32 pixels. This bullet is not the same as I'm  using but if you want you can just go to the   sixth or seventh page of this flat icon.com and  you will find the same image but feel free to use   this image it doesn't make a lot of difference.  So now we have downloaded this bullet PNG make   sure it's 32 by 32 pixels. Let me just close  this Photoshop. This is the background, we made   the last video. So after adding the bullet dot  png, we are going to add it into our byte code.   So over here just below the enemy, we're just  going to copy this enemy because we don't want   to create everything again. And instead of enemy,  we're just gonna write bullet. And we are going to   replace everything of our enemy with our bullets.  So let's call it a bullet. And then I'm just   going to copy and paste everything. So politics  bullet why and just paste it but at exchange,   and some of these values we are not going to use.  So for example, bullet is not going to move in the   x direction. So using this x underscore change is  useless. But still, we are going to just keep it   over here just for this sake of conformity.  But anyways, in this bullet x and bullet y,   we are going to give the value of zero bullet  x because we are going to be changing the value   inside our while loop. But bullet y is going to be  at 480 pixels. Why is that because our spaceship,   as you can see as clear why is at 480 pixels.  So at that level, the bullet is always going to   be short. And so if I run this main program over  here, you can see that a spaceship is over here.   And what's happening to her enemy, let's reload  it. Let's see if we have done something to enemy   No. So as you can see, when we move, our spaceship  is not moving in the y direction, so we need to   shoot our bullet from its top of his nose, which  is at 480 pixels of y coordinate. That's why we   have code and this 480 over here. And then we are  going to add another variable which is going to be   bullet state. So this is the state I was talking  about a few seconds ago. So we are going to add   a variable bullet state and we are going to give  it a value of ready. So let me just add a comment   actually over here so that you guys remember what  this release date is what is y est. So readystate   basically means you can't see the bullet on the  screen. And fire. So bullet is currently moving.   Alright, so this is the state we are going to be  using to basically differentiate between whether   the bullet is in motion or it's in a state of  constant and it's not being fired right now is   going to help us decide the movement dynamics of  our bullet. So now we have created another bullet   state equals ready and now we are going to create  a function known as fire bullet that is going to   be called whenever we press our spacebar. So in  our game, when we press our spacebar of bullet   is being fired, so that is what you want. And  pressing the spacebar when it gets fired. So and   what happens when we press spacebar, we want this  function of fire underscore bullet to be called.   And we are going to do the same thing over here we  are going to call the x and y variables. And just   below that, we are going to access this bullet  underscore state variable equals to ready inside   our function. Now how do you do that, we need  to create this bullet underscored state function   as global so that it can be accessed inside this  function. So I'm just gonna write global and then   call it state. And now we can access the value  of this bullet state from inside this function.   So after this, after we press space, the bullet  is actually being fired. So now you can see it   on that screen. So we need to change the state of  the bullet equals to file. So now it's not ready,   it's actually being fired currently in motion.  After that, we just need to make sure that it   appears on the screen. So I'm going to write  skinning dot plate, we need to draw the bullet   on the screen. And then I'm going to write the  name of our bullet image bullet image. And then   we are going to write the coordinates of where do  we want it to appear. So we just wanted to appear   on x comma y the values that you are getting  from the function. Now we want this values to be   at x plus 16 and y plus 10. Why is that? So let me  just format everything properly. And you can press   Ctrl Alt plus L if you're using pi charm to make  sure that it looks good. So why are we doing this   Xpress 16 why plus 10. This is to make sure that  the bullet appears on the center of our spaceship,   if you don't do plus 16 is going to appear on  the left hand side of the spaceship a little   bit. I've already tried it out. So that's why a  note section is the correct value. And this Why   is just make sure that it appears a little bit  above spatial just to give it an illusion of the   bullet being fired from the top of the nose and  not from the center of the spaceship. And now   before we go into the while loop, I just want  to change the value of change in y because I'm   pulling is moving in the right direction. And  this is the value that we give to the enemy.   We don't want the bullet to move this fast we want  it to move at the speed of 10. So let's close this   window now. And let's just go below and what we  want over here is that if an arrow key of space,   not an arrow key, if a keystroke or space on  our keyboard is pressed, then we want to call   that fire underscore bullet function. So let's  create that over here so that we can just copy   and paste this line. And then at the end, we can  just add the value of space, okay, underscore   space. And this is going to make sure that we can  capture the value of space. And what we want when   you press a space, we want the Firebird function  to be called. And now what are the values inside   this. So right now we are going to give it  a value of clear eggs. But you'll understand   why this is strong. But this is just like a logic  error. So I'm not gonna worry about it too much,   but I'm gonna tell you why it's strong. And we  are going to fix this in this video. So let's try   it if event dot key pygame dot k underscore space  fire underscore bullet player x comma, y. So let's   run this right now and see how it looks. Obviously  it's not complete, not even close to complete,   but we're gonna it's gonna look how it looks. So  right now you can see bullet is not there. But   when we press the space, nothing is happening and  the enemies appearing for some reason. Over here,   if you speak, if you've seen yet near the  spaceship, the enemy is appearing, because we   haven't changed the name of our image. So let's go  to the bullet. And over here instead of enemy we   are going to call it bullet. I know when I was  recording this and you I was just writing the   slide, you guys must have screamed that, hey, this  should be bullet dot png not enemy dot png word,   no worries, we have changed the value. And if we  come back, you can see that you can't really see   the bullet over here. But it's actually appearing  but it's appearing so fast that you can't see it.   So actually, let me just change it to enemy  again so that you guys can at least see it.   It's happening behind the screen. So let me run  this. And when we press face, you can see that   the enemy is appearing at near the spaceship, you  can see this, this is happening. So this is how   the bullet is also appearing. But right now you  just can't see it. So let's change this back to   bullet dot png. To put a dot png so we need to  do a couple of things. So why is it appearing   and disappearing so quickly, what we need to do  is we need to make sure that it persists in the   background. And when we need anything to persist  in pygame, we added to our infinite while loop. So   right now this code is only being called when we  press the spacebar we want this bullet to appear   and not disappear really really quickly. So we  are going to add another comment over here. And   we're going to call this bullet movement bullet  movement. And over here, we are going to just   change the values and first check the maybe the  state of our bullet. So right now our state is   when you press the spacebar, then the state  changes to fire. But at default, the state   always remains at ready. So as you can see that  the bullet underscore state is equal to ready. And   when we press the spacebar, the fire underscore  grid function is called and inside the function,   the state changes to file. So outside over here we  can write and condition if bullet state is fire,   then we can actually move the bullet in front of  the spaceship and make sure it decreases in y.   So we're going to add some values and we're going  to call this Fireblade function again over here,   and we're going to get rid of value of clear x,  and we're going to give it a value of y. And just   below this, we are going to decrease the value of  our bullet just like we discussed at the starting   of the video, minus plus minus equal minus equal  to bullet y change which is currently 10. So let's   run this program now and see if it works. So this  is our spaceship and what's happening to our enemy   y is going crazy. Let me just reload this what's  what's up with this enemy. Alright, so now you can   see that the bullet is moving. But this this there  are two problems in this first, we can only fire   one bullet. So you can see after we have fired one  bullet, we can't fire another bullet. So that's   one problem. And the second problem is that when  we fire the bullet, when we move our spaceship,   the bullet is moving with aspiration. Just to  give you guys another view, when you press space   the bullet is going and when we move the spaceship  the bullet is actually moving with the spaceship.   So we will be solving both of these problems in  the next video. For right now I'm just going to   give you a recap so that you guys can understand  properly what is going on. So let's go The moment   you press the spacebar, so when you press the  spacebar, this event of spacebar is triggered and   this fire grid function is called. And over here  we sent two values that is the current position of   player x and a bullet y value. The bullet y value  is for at y coordinate and the player x is the   movement where the spaceship is the x coordinate  of the spaceship and that is partly the reason   why the bullet is moving in the direction of the  spaceship but we are going to tackle that in the   next video. So for right now we are calling this  fire bullet function whenever the space key is   pressed, and we're transferring the value of  player x and bullet y to our fire underscore   unit function. Now this fire blade function is  due to changing the state of our bullet state   underscore to fire so that when we execute this  y loop All this code can be executed. So we are   finding firing the bullet and to find the ability  first need to show it on the screen. So we are   blurting it out or drawing it on the screen. And  we have changed the value of 16. And why. So just   to show you guys why we have changed the values,  let me actually remove the x and y values, so that   you guys can see the difference. So I'm just going  to turn play and I'm going to shoot a bullet. As   you can see, the bullet is being shot a little  bit from the left hand side and we want it to   be shot from the middle of the screen. That is why  I did that. So the middle of the spaceship not in   the from the middle of the screen to be exact. But  anyways, after we did this fire in this corporate   function has been called the bridge state has  been returned to fire. And then we go to this   infinite while loop which is running but it wasn't  running because before running the spacebar or   before pressing the spacebar, the bullet state  was ready. So all of this code was not getting   executed. But after pressing the spacebar, the  bullet state has been changed to fire. So now we   can put blade this out on the screen by calling  this fire unders component function. And we can   start actually changing the location of the bullet  by moving it in the upward direction by reducing   the value of y. So guys, this was the logic of  our bullets. In the next video, we are going to   tackle the two problems that we have discussed  in this video. So I'll see you over there.   The last video we learned how to shoot a  bullet. But there are a couple of problems   with our program right now we can shoot only  one bullet at a time. And that said that's it   for the whole game has a pretty big problem. And  the second problem is that the bullet is always   following our spaceship doesn't matter where it  goes, bullet will follow it and we want it to   have an individual path. So for example, we wanted  to have, we want the bullet to have the starting   point of our spaceship, the x coordinate our  spaceship, but when we move the spaceship,   that bullet should continue on its original x  coordinate and not of our current spaceship. So   these are two problems we are going to solve.  Let's solve the first problem first of having   multiple bullets. So for that, what we are going  to do is we are going to add an if condition.   So right now, if you watch over here, when the  bullet goes up, it goes to the zero coordinate,   and then we are not doing anything to a bullet. So  it just keeps on going into negative values. And   we are not removing the bullet from our screen.  So what we need to do is as soon as it crosses   the zero coordinate, we need to make sure that it  resets to the value of 480. So as soon as soon as   it goes above zero coordinate, two things are  going to happen first is going to reset to 480   pixels of y. And the second thing is because you  can't see it on the screen anymore, because it   has gone beyond zero y coordinate, we are going  to change the state of a bullet to ready again,   so that we can fire it the next time. So let me  just add an if condition this beneath this bullet   movement, which says if bullet y is less than  equals to zero because above zero is going to   be negative values. And over here we are going to  do two things. First is we are going to reset the   state of the bullet that is the bullet Why should  be reset it to 480 again, and the second thing is   we need to change of the bullet state. So bullet  state is going to be ready again. And let's make   sure that we don't misspell anything. And we are  going to format this properly. Press Enter and   now let's reload it and you will be able to see  that our bullet is multiple. So right now you can   see we are short one bullet, then we can shoot  another bullet and it keeps on happening. And   now that we have fixed the first problem, let's  focus on the other problem. And that is that the   bullet is following our spaceship. So why is this  happening? This is happening because in our code,   we are still using the player x value. So if we go  to our space condition, so after pressing space,   we are using the player x value. And even  in our bile if condition code of over here,   we are still using the player x value. So it's  alright that we want the starting position of our   bullet to be at the spaceship that we just removed  this recording thing. So at the starting position,   we want the bullet to be at the x coordinate  of a spaceship. So when you press space,   the bullet starts from the spaceship and not  from somewhere else. So for example, over here,   we want to x coordinate of a bullet to be that  of the x coordinate of our spaceship. But when   we move after firing the spaceship, the x  coordinate shouldn't follow the spaceship.   So that is the problem that we are having. So how  do we solve that. So instead of using the bullet,   the space player x value everywhere, which is just  use when you press the spacebar, one value of the   x coordinate just should be just saved. So we're  gonna do that we're just going to write bullet x,   we haven't used this variable yet. And we are  going to save the value of player x over here.   And then instead of player x, we are going to send  the value of bullet X to our fire bullet function.   And we are going to do the same thing over here  in our if condition so instead of player x,   you're going to use the value of bullet x and  that should solve the problem. So let's run our   main program again and see if it works. So this  is a spaceship. And now if we move our spaceship,   the bullet is not following our spaceship. So,  we had what we have done is when the spacebar is   pressed, then we are saved the current spaceship x  coordinate inside our bullet X variable. And then   we are using that everywhere instead of just using  player x. So now that we have fixed that there is   one more problem to fix, which I've just realized.  So the problem is that after pressing if we press   the spacebar again, so for example, we press  the spacebar, the bullet is going. And then if   you press the spacebar multiple times this bullet  is going to change just keep on moving according   to our spaceship. So multiple spacebar changes  its x coordinate. And why is that happening. So   while the bullet is a moving condition, it's in  fire condition. Even in that condition, we can   press our spacebar, so we have to make sure that  we can only press the spacebar when it's in ready   condition. So we're going to add a if condition  over here, which says if bullet state is ready,   let's add double quotes for the string. If  the police state is steady, only then we can   fire another bullet, otherwise we can't fire the  bullet. So after our bullet goes above this zero   coordinate or our bullet is in ready state only  then we can fire another bullet. So now if you   play space, all the errors have been fixed. Except  this first one, I don't know what happens to is   sometimes. Alright, so let's shoot. And you can  see that after it has crossed the zero coordinate,   and it resets to 40 y coordinate again, only then  we can shoot another bullet. So this is looking   pretty good guys, we have finally completed the  bullet module. This was a pretty long module   because there were a lot of complications in it  and a lot of things to understand. So let me just   recap so that you guys can understand everything  properly. So if anybody has understood everything   perfectly, you guys can just stop this video.  So what happens when you press the spacebar,   when you press the spacebar First, it checks  whether a bullet is already on the screen or not.   If it's not on the screen, then it makes sure that  it gets the x coordinate of our spaceship, let me   just add a comment so that you guys can understand  get the current x coordinate of the spaceship. So   it gives the current x coordinate of the spaceship  and stores it inside this variable bullet x and   then it fires the bullet using this bullet X  variable. And this bullet Y variable basically   which we have said before that is 10 because we  want the bullet to move at the speed of 10 or   change its y value at every vile iteration at 10.  So after we do that it goes to this fire bullet   function and over there it changes the state to  fire and then it creates that bullet. After it   changes its state to fire it goes to this while  loop iteration I checked, it says if bullet state   is fire, and it keeps on firing the bullet. If we  don't have this fire underscore bullet statement,   then the bullet won't appear on the screen. So  anything that we want to be persisted inside, our   game needs to be included inside our while loop.  That is why we have this function inside our while   loop. If this statement was not there, the bullet  wouldn't be there. So that is why we need to make   sure that the bullet is continuously appearing on  the screen after every iteration of while loop. So   after it fires a bullet it keeps on changing  its y coordinate. Because of this statement,   it happens that it goes above the screen. So  after it goes above the screen this if condition   is activated, and it resets the bullet to 480 y  coordinate and then it resets the bullet state to   ready again. And then we can press the spacebar  again and everything happens again. Basically,   it's saved again the x coordinate of a spaceship  but I'm not going to go into all of that. So guys,   that is pretty much it for this video.  In the next video we are going to create   multiple enemies. So right now there is only  one enemy so it's very easy for our spaceship,   or maybe depending upon what I want. And  maybe I'm going to teach you guys how to   shoot this enemy. So right now this enemy  can pass through the bullet and nothing   is happening to him. So we are going to  probably discuss that in the next video.   So in the last video, we created multiple  bullets, instead of shooting just one bullet,   we can shoot 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  responding 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 do We'll be doing is that let me   just actually reload this game so that we can see  the bullet and the enemy properly. So what I'm   going to 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  conclusively say that the bullet has collided.   So the bullet has an X and Y coordinates where  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, 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 in distance   between two coordinates. And we're gonna take you  to this website math planet.com. And over here,   there is this equation, which says distance  equals to x coordinate minus x coordinates of   other objects squared plus y coordinate of one  object and y coordinate of the object square,   and then a square root of the whole thing. And  this x two and x one doesn't 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 in mind. And   let's go back to our code, and just beneath this  fire underscore bullet function, we are going to   create a new function, and we're gonna 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're gonna  do a square root. So to do a square root, or do   any kind of mathematical operations, you need to  import something known as math. So if you go up,   I haven't imported math yet. So I'm just gonna  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.sq Rt. So this is the basically the function  or the method for doing square root, and then   apply some brackets. Now we need the Nv x and the  bullet x, and we need to subtract it according to   the formula. But before we even that, we need to  square it. So if we go back to our equation, you   can see after the square root, the x two minus x  one is being squared. So we also need to implement   that mathematical formula. So we're just gonna  write math dot POW. So this stands for power,   and then it requires two values, the x and the y.  So we're going to do that. So we're going to just   write comma two, because we want it to be squared,  and then over here, we are going to give it the   value of enemy x minus enemy y. Alright, 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 y two minus   y one and two squared. So we are going to go  back over here and inside our math.pi. So this   is this part math.pi x two, and this is enemy,  not any y. This is politics, sorry about that.   And then we are just going to write plus, and  then we are going to write math dot POW again,   because we need another power and requires to two  values first is the par value, so we want it to be   squared again. And then we just need to subtract  the anyway when you move y and me y minus bullet   y. And this should give us the value. Obviously,  we need to close this Math Math dot square root,   so we're 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 two minus x one or  x minus x one minus x doesn't matter, squared it   and then math.pa y two minus y one squared. So  just to make sure that these two are separate,   we are going to apply another bracket over here.  So I'm going to also do this with this one. Let's   not blunder Alright, so there is some problem  over here. So this is the problem. So let's   apply a bracket over here instead of 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 is the  collision has occurred, else 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 the   Save condition. We could have just return false  over here, but I just wanted to make sure that   you guys understood. All right, let's format  it properly obviously immunity format meeting   in PI charm, make sure you press Ctrl Alt plus L  LS for I don't know Lula something. So make sure   you press Ctrl Alt plus L if you're using Python,  it should format everything beautifully. Alright,   so our escalation 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 going to stand   for collision so that we know that we are  writing the code for collision. And over here,   we can just check whether a collision is happening  or not. So I'm going to create a variable called   collision and then I'm going to call this  escalation function. And over here, we are   going to set the value of enemy x and y. 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 here we  are just going to write bullet x comma y. Alright,   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. Alright,   so this is looking good. And then we're going  to write a condition if condition. So what do   you want us to do if the collision is occurring?  Basically, that is what our program is asking. So   what do we 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 reading. And then the last   thing we want to do is obviously increase the  score. So I'm just going to 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  that's looking pretty good. So I'm going to just   scroll down, and over here, I'm going to write  score score by missing the variable of score   squared plus equals to one. So this will increase  the value by one every time we hit our enemy. And   after that, we are just going to print out our  score. So I'm just going to 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   going to 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. All right,   all right, so the score is five. So what one  thing you must have noticed is that the enemy   is still on his current path. And it's not  responding to its default location. So we   need to fix that. So what we are going to do is we  are going to 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 and y. So I'm just  going to copy this, and I will paste this inside   our F 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'll  see whether we do that or not, because this is not   such a complicated program. But maybe we will. So  now what will happen is that the enemy x and the   enemy y will get restarted or respond to a default  location, and will respond to a random integer   that is between zero to 800. And why is 50 to 150.  Obviously, we 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. Alright, so our enemy is actually   just going back to its new location. Alright,  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 responding properly, our enemies  respond 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're trying it on your own. If an  enemy response 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   respond our enemy or just be when you're creating  the movie or giving it a value of zero to 800. And   but inside our code we have written in over here  that if the enemy x is greater than 736, make sure   you change its y value. So there will 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 that  we don't increase the value till 800 only it is   736 Just for safety, we are going to write 735. So  obviously, we need to make sure that the same 35   also goes in when a collision has occurred. So I'm  just going to copy that over here too. Alright, so   this looks good. We have also fixed an error. So  this is looking pretty good. Alright. So Alright,   look enemies 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 going to 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 is pretty  fun to play. While I'm recording this video i'm   also playing. But 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 have  bullet coordinates. And we have used all of these   four coordinates in this formula, which we got  from typing and 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 squad,  make sure that you import math. And after that,   we made the square root. And then inside this  week, using the dot POW function, we squared it   just like we have done in this formula x two minus  x squared squared. So we needed to make sure that   we squared 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, all  obviously, I will upload all this code on GitHub,   so you'll find it somewhere over there. And by the  end of this series, I'll upload it after recording   all the videos. So you can wait for it a little  bit or you can find it in the description or   attachments or something. So math.pi and e x minus  bullet x, you guys understand all of this stuff,   and then how this 27 network 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 collision. 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 going to 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, we first of all,  we want to reset the vials of blood 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  one and we printed out the score and then we made   sure that the enemy respond to its new location.  Alright 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. Alright guys, that's 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 there.   In this video, we are going to learn how to create  multiple enemies. And in the previous video,   we learned about how the collision system works.  So for example, if we shoot the enemy is going   to respond to a new place, as well as increase  our score. So how do we make sure that multiple   enemies appear on a screen so for example, six  enemies so what we can do one idea is that we can   store all of these six enemies in a list or an  array but in Python is called a list. So we are   going to store all of the enemies in a list and  display them one by one on the screen. But it's   going to happen so fast that it will look that  all the six enemies are appearing simultaneously   on the screen. So we are going to turn all of  these variable into a list variable and how do   you do that you simply copy the variable name  and equal to and brackets. So this signifies   an empty list. There is nothing inside this list  and we are going to use a for loop to put values   inside this list. But we have to do it with all  of the variables. So let's do that. So let's do   it with enemy x and then enemy y. So I'm gonna  copy this, copy this paste over here. Let me   just copy everything first and then I'm going to  add the square brackets not the neatest way but   the quickest way and then I'm just gonna copy  all of these square brackets quickly. Alright,   and how do you add values inside a list so  right now this is an empty list and how do you   add values to a list is by using a method known  as dot append. So instead of equals two we are   going to use dot append over here. But even before  that we are going to create a variable known as   num of enemies which stands for number of enemies  and we can change this variable whenever we want   to increase the number of enemies. So we are going  to create a for loop for i in range and inside   this we are in a port number of enemies. So this  loop for right now is gonna run six times and six   of our enemies are gonna be created. Obviously you  we want all of these values to be out added to the   list that we have created of all the variables.  So what we are going to do instead of equal to,   we are going to write dot append. And this is  going to add our current value inside our list.   So for example, the first enemy image is going to  be stored inside this enemy image list. So we are   going to do all of this with all of the variables.  And you don't need to do it with any new via   change because these are like constant values,  but whether just for the sake of simplicity,   we are going to do it with all of them.  Alright, so let me just do it with all of them,   I'm going to copy this dot append everywhere. So  just to do it quickly, again, not the neatest way,   but the quickest way and that is what we want,  just the quickest way. And let me just add the   practice after everyone so that there are no  errors. And it looks neat. Alright guys, so   this is looking pretty well. So whenever our code  is going to run, it's going to go through this,   and it's gonna create six number of enemies.  But if you scroll down inside our while loop,   you can see that we don't specify which enemy x  do we want to move. So this is the code that we   are using for the enemy to move towards the left  hand side or the right hand side and a downward   movement. But if we don't specify which enemy x  we are talking about, because if you notice above,   we have declared six enemy x. And because we  have created a variable called as enemy x we have   created an empty list. So each time this for loop  is going to run is going to enter a new value of   enemy x and scientists and we x list. So we need  to make sure that our program understands which   enemy x we are talking about. So we are going to  create another for loop over here and we're going   to do the same thing that we did over there number  of enemies. And the idea is pretty intuitive,   you just need to change every value over here to  basically inside a square bracket just specify   which value you're talking about. And this is a  way to reference something inside a list obviously   if you know less you probably already know about  this. So anyways, we are going to do this we are   going to I this I bracket everywhere. So let me  just copy and paste this everywhere so that our   program knows which anyway we are talking about  which enemy wide change we are talking about. Let   me just copy this everywhere. All right, this is  looking pretty good. And then we also need to make   sure that a collision is also inside our enemy.  So we are going to copy all of this code from   over here. And we are going to place this inside  our while loop because we need to make sure that   we calculate the collision for every let me just  paste this so you can see over here that currently   our collision is it's not inside our for loop but  actually inside here it's actually inside a for   loop but make sure that it's not inside this  lF condition or the if condition otherwise,   it's not going to work a lot of people make this  mistake that they don't pace the collision part   correctly or don't pace anything properly. So  anyways after making sure that you have pasted   is probably inside our for loop we are also going  to change these let me just press it here alzette   we are going to make sure that our Python  program knows which and the x coordinates   we are talking about. So let me just paste it  over here. Alright, so this is looking pretty   good. We also need to make sure that we add  it over here. So basically, wherever we are   talking about the enemy or the coalition, you  need to make sure that our program knows which   coordinate or which enemy we are talking about.  And then the last thing we need to do is whenever   we are calling the enemies for example over here  calling the enemy function and this is where we   will basically blitting the enemy we also need  to make sure that we add which image do we want   to be plated. So right now it's not specifying  so we are going to paste this inside our ennemi   for loop and make sure it's not post pasted inside  the if condition it's pasted inside our loop. So   indentations matter and then we are going to send  the value of i according with specifying which x&y   coordinates we want to be printed on the screen.  So basically which enemy we are talking about.   So if we go up to our enemy function over here  you can see we haven't specified the AI value so   we need to specify the value and also the which  image enemy image do we want to draw? Obviously,   you will be thinking there's only one image but  actually there are six enemy images because we   have created a list of enemy images too,  as you can see over here. Alright guys,   so this is looking pretty good. I think almost  all of the code is done. So let me just play it   and then we will figure out what we have to recap  and if there are any errors, we can solve them.   So let's play it and see if it works. Alright,  so this is looking pretty good. We have created   six enemies and when we showed them so let's try  and shoot one of them amateur bad player. Alright,   so when we are shooting them, our collision is  also working. It's they are responding. Alright,   so this is looking pretty good. So how is  this game going to end whenever one of these   Space Invaders come and hit the bottom of  the spaceship or basically hit the 480 by   access then we are going to end the game so we  just keep shooting and increase the score. And   right now there is no place where we are  displaying the scores. In the next video,   we are going to make sure that we display  the score on the left hand side top corner.   All right, guys, welcome back. So in the  last video, we created multiple enemies,   and our game is looking real, real good. So the  score system is working, but the score is not   being currently displayed on our game window.  So we'll fix that in this video, we will make   sure that the score appears on the top left  corner of our screen. So how do you do that,   so that is simple, we use a font object that is  inside pygame. So pygame is actually going to help   us create font or text on the screen. So the first  thing we need to do is go to this word image. And   we are going to actually remove the score variable  that we've created over here. So instead of that,   we are going to just press enter and write score  so that we know we are creating score under this   statement. And then right now we are going to  create a variable known as core value, and we're   going to give it a value of zero. And obviously,  you can see with this red symbol, there is some   error with say a score plus one, we are doing  it over here. So we need to change this to score   value. And then we can remove the print statement  because we don't need it down. So let's just go up   now. And then we have to use the score object or  the PI game font object. So that is pretty easy.   You just create a variable known as font. And  obviously you probably know already, what font is,   font is just the type of text or the style of  text that is displayed how much the weight of   the line often is, is actually dependent upon  the font, what kind of font you use. So first,   we are going to define what kind of font we want.  So how do you do that, we just use the pygame dot   font method that is inside pygame pygame dot font  dot capital F font and inside this we are going   to give it two values. The first is which font Do  we want. So inside pygame, there is one free font   which is known as free. cents bold. So right now  we are just going to use this font and dot TTF is   the extension of the font. And then we are going  to give it a size. So we're just going to give it   a size of 32. And then after that we are going to  give it the x and y coordinates of where we want   this text to appear. So we want it to appear  in that top left corner somewhere, but not   just attached the screen a little bit away from  the scheme. So we are going to give it a value of   text text the x coordinate of 10 pixels, and then  text y of that is the y coordinate also of 10. Now   just like we did with these player player enemies,  we have to create a function that is going to show   this score value on the screen. So let's do that  we are going to create a function which we are   going to call show score. Pretty easy stuff. And  inside this function, we have to give it the X   and Y value just like we have done with everything  else. And one difference is that for example, you   can see how we are plotting the image inside this  def player. Instead of creating the player trying   the player on the screen, we are going to render  the text on the screen. So I'll just show you   what I mean by that. So we're going to create a  variable of score. And inside this we are going to   write font dot render. And we're going to require  some values inside it. So this is the what I mean   by instead of bleeding, we are going to render the  text of show the text on the screen. So using this   font object we have created over here we are now  right font on trend. And there is going to require   a couple of values the first value is what text Do  you want. So we want our text to be first of all   score. And then we want the score value in front  of the text. So we are going to write it actually   we have to convert it into a string. So right  now, this score value is an integer, so we have   to use typecasting and inside this. So basically  we are converting this integer value to a string   by writing str in front of it. This concept in  Python is known as typecasting and actually, in   programming This concept is known as type casting.  So inside this we are gonna write score value. And   then after that the second value is required is  true. So we can display it on the screen. And   then the third value requires is what is the color  of the font that you want, you can show whatever   color you want. Obviously, we have to use the RGB  values, which we have discussed before. But I'm   just going to use the white whether that is 255  or eight 255 green, and then 255 blue. So this   is looking pretty good. Let me just format this  properly. And then we are going to show it on the   screen and how do you show it on the screen.  So first, we are going to render the score   called render the text. And then we have to blit  this text on the screen or draw this text on the   screen. That is why I said this is a little bit  different. First you have to render then you have   to break it on the screen. So we are going to use  the same concept we have over here. So actually,   let me just copy this from here and paste it and  instead of image we are going to render our score.   So this time is not an image it's a font or a  text and it requires X and Y value which we are   providing through this function. And now one  last step. That we have to do is pretty easy,   we just have to go down and anything that you  want to persist on our screen obviously has to   be called inside our while loop. So I'm just gonna  call our show score function and x, y is text x,   and text y. Alright guys, so this is looking  pretty easy. So let me just run it and show you   that it works. And hopefully it will run without  any errors. So our score is displayed on the top   left corner. And when we shoot someone, let me  just show someone as you can see, our score is   increasing. So let me just shoot a couple of more  enemies or space invaders. Alright guys, so this   is looking pretty good. Our score is increasing  and resolving respond and our game is being   created. So one last thing I want to tell you guys  is that how do you add extra fonts. So right now,   if we go to our fonts, so for example, here is  a font, this is the only font that is inside our   pilot. So let me just close this, how do you add  other fonts inside this, so what you can do is   you can go to a website or something and download  fonts. And they will give you a font that is of   extension dot TTF. And you can just download this,  place it inside your project structure, and just   write the name of the font. So for example, what  you can do is you can go to this website known as   d a font. And this gives a lot of fonts for free.  So you can download just any any font that you   want. And we're going to give you a zip file, just  extract that zip file, and it's going to give you   a dot TTF file, then paste that TTF file inside  the Space Invader project folder, and just write   the name over here. And then you can use that  font inside your plugin. So this is pretty easy.   And this transfer the font size. So let me just  increase for sign to something crazy 70 maybe it's   not that crazy. But as you can see the score has  increased to 70 says, but let's keep it let's keep   it small 32 pixels. Alright guys. So that's pretty  much it. So let's just to recap a little bit,   first we create the score value, we added a font  object, then we created the x and y coordinates   of the font where we want it to appear. And  then inside a show score, we did two things,   we rendered the font and then we blurted it or  drew it on the screen, and render we applied or   gave it three values, the score the true and the  color of our text, so you can change it to green.   So let me just show it to you. Yes, RGB. So GB  should be 255. And then we can make both of these   values are zero red and blue. And this should make  it green. Let's play it. And as you can see, it's   green. But we want to invite color white, I think  looks good on any game. So let me just change it   to 255 again. Alright, let me just give a space  over here. And then we do it on the screen. And   then the last thing we did was before changing the  value of our scores, so after collision, we need   to make sure that the score value is increasing  by one after we hit the enemy by a bullet. And   the second thing is we call the show on the score  function that is pretty standard. Alright guys,   so in the next video, we are going to learn  how to end the game. So right now the game   is not ending is just keep on going even if the  enemy hit our spaceship. So one thing I noticed   is that the enemies are not moving very fast.  So we'll also increase the speed of the enemy   a little bit in the next video. And we'll make  these minor changes. So I'll see you over there.   Alright guys, welcome back. And hopefully  you're having a great day. And today we are   going to learn about how to add sounds and music  to our PI game. And currently there are no sounds   inside our game. So it looks pretty simple.  But what we are going to do is we are going   to add three types of sounds. The first sound  is the background sound that's going to play   continuously in the background. The second is  the shooting of the bullet sound. In this case,   I call it laser dot wave. And the third  one is explosion dot wave that is going   to be produced when our laser bullet hits our  enemy. And just to show you guys how it sounds,   I'm going to play this main dot Python file,  which I have already changed the code off so that   you guys can see what's happening inside again. So  I'm just gonna play it and you can see Alright, so   you must have seen there are three sounds first on  continuously goes in the background. Second is the   laser or the bullet sound. And the third is when  our bullet hits our enemy. Now how can you get all   these three files? Let me actually make sure that  the code is original and the last one that we have   used. And now how do you get these three dot WAV  files. So what I want you to guys to do is I want   you to go to github.com arthropod Space Invaders  by game and you will be able to get all the code   that I've written till now and along with it you  will be able to get the.pe files that I have been   talking about after downloading these three files  or maybe the whole code and you can download them   by clicking on this clone or download and done  download zip. Make sure you add the sound files   to your project by going to Space Invader and just  pasting them over there. Now let's get started   with adding background music into our game. So  first thing we need to do is import something   Known as a mixer from our pygame Library. And to  do that we just right from pi game, import mixer,   import this mixer. And what does this mixer This  is basically a class that helps us handle all   kinds of music inside our game, whether it can  be repeating our music, loading, music, anything   that want. So you have to look inside this class  whenever you want to do anything with sounds or   music. And then just below this background, we  are going to add a background sound. So let's   write our comments. so that you guys can know in  future what we are talking about. And then we're   just going to use the mixer library by writing  mixer dot music dot load pretty easy, because   we want to load the file and then we are going to  play the file. And inside this, we're just going   to write background dot WAV, which is the name of  our file, as you can see on the left hand side,   and then we're just gonna play this sound by  writing mixer dot music dot play pretty easy.   And now we want this file to play continuously.  Because right now, if you don't add anything   inside this dot play, this file is going to play  once and then just stop, but we want it to play   in a loop because this is a game and can go on for  long. We don't want it to play just once. And how   do you do that inside this mixer class, you just  add minus one and it's going to play on loop. So   now let's just test it out and see whether it's  working or not. As you can hear the background   music must be going on. But we haven't yet added  the sound to the experience town so that must not   be there. So that's why we are going to be working  on adding the bullets are now but one thing I want   you guys to notice is that currently we are using  mixer dot music because we want this file to be   played continuously in the background. So this  is kind of music. But as something is very short,   for example our bullet sound then we have to use  mixer dot sound. So we are going to come below   where we have a bullet shooting. So how are we  going to catch the moment when our bullet is being   released. So we are going to do that inside our  let's go to above let's go to this if bullet state   is ready. So over here we can release the sound  of our bullet going out from our spaceship. So   because over here we are getting the x coordinate  of the spaceship and then we are shooting the   bullet over here so we can somewhere over here at  our bullet Stan. And how do you do that is also   very easy. Just below this we are going to write  mixer dot sound. So in this case, it's not mixer   dot music, it's mixer adult sound. And we're  gonna first create a variable let's create a   variable and call it bullet underscore sound just  to make sure that we know where our sound is,   and makes a dot sound and inside this you have  to give the file of the sound so and that's in   our case is laser dot WAV. And then just like we  did in the music case, we are going to play it so   we're just gonna write bullets sound, not state  will it sound dot play, and we don't want it to   be played inside a loop. That's why we haven't  added minus one. So now our bullets is making   a sound now we just need to add a sound to our  collision. And where are we catching collision   let me just copy this first and we are going to  go below I just want you to guess first where we   are going because this is pretty obvious we are  going to this part where if collision we want a   sound p plate and that sound is off explosions.  So we're just going to copy and paste this over   here because most of the things are going to  be same instead of bullet we're going to write   explosion sound. And then I'm just going to copy  and paste this just below this play and instead of   laser dot wave we are going to go with explosion  dot wave. So that's this is pretty much it. Let's   actually play our game one more time and see if  everything is working properly. So let's play this   Alright guys, so everything is looking really  good. So are all of the three sounds are playing   obviously Feel free to walk around and play  around and add more sounds maybe remove and   add your own songs. That's going to be fun. In  the next video we are going to learn about how to   make sure that our game gets over when our enemy  hits our spaceship. So I'll see you over there.   In the last video, we learn how to add sounds  to our game and in this video we are going to   show the game over a screen. So I removed all of  the sounds for this video because it's going to   be difficult to explain what's going on if I add  the sound. So what we're going to do is when many   of these enemies come close to our spaceship  maybe over here, we're going to remove all of   these enemies or maybe just get them out of the  screen. And we are going to show a big test just   like we are shown over here the score zero we  are going to show a big text of game over over   here in the middle of the screen. And when is this  pixels gonna head without these enemies gonna come   close to the spaceship when they're gonna hit  the y axis of 440 pixel. So if you remember at   the top at zero pixel at the bottom is 600 pixel  and adjust at the starting of the spaceship it's   about 480 pixel, but just by experimenting, I  found out that 440 is perfect. So when I use   that 440 pixel As a limit, so when our enemies  come over here, we are going to show game over   for our player. And the final score is going to  be shown over here. So what we are going to do   is we are going to come to our enemies where we  are moving our enemies basically. So we'll come   down and come to enemy movement. So enemy  movement was over here. And then over here,   we are just going to add a text. So let's add  a text and just call it Game Over, because we   are going to be writing the game over code just  below this. And what do you want is when whenever   an enemy lets enemy right, and whenever enemy  Why have I so whenever one of the enemies and   doesn't need to be all of the enemies when one of  the enemies come to for 40 pixels. And actually,   just to test it out, I don't want the enemies to  come to the bottom. So I'm going to make it very,   very small, maybe 200 pixels. And just when we are  ending the game and we are finalizing the game,   I'm going to change it again to 440 pixels. So  whenever one of the enemies come to 200 pixels,   then what we want to do, we want to move all  the enemies, so we are on create another for   loop for j n. And then we just want to copy  all of these, because we want to move all   the enemies out of the screen, that's how I'm  going to create another for loop inside this   big for loop. And then under this and is going to  change the axis of enemy y j equals to 2000. And   this should make sure that these enemies go below  the screen to 2000 y pixel. And then after this,   I'm gonna just make sure that we display  the game over text. So how do you do that,   I'm just going to create a function. And this  function will be called underscore game over text.   And we are going to call this function and then we  are going to break out of this loop. So let's make   sure that we are not doing anything weird with the  brackets and over here we are just going to write   break. And this looks pretty good. So what we are  doing is whenever an enemy, it doesn't need to be   all the enemies when one of the enemies reaches  is greater than 200 pixels in this case, but it's   actually going to be 440. And then we are going to  collect all of the enemies inside this for loop.   And we want each of the enemies to be moved out of  the screen. And we are doing that by setting the   y axis to 2000. It is not going to be shown on the  screen. And then we're calling this game over text   function which is going to display the game over  text on the screen. And then we are breaking out   of this loop because we don't need this for i in  range loop anymore. So now let's create this game   over underscore test functions. So we are going to  go where we have added our score. So let's just go   over here. So you can see we have added our score  over here. So we are going to do doing basically   the same thing. And we are going to write game  over text over here. And then we're going to copy   the same thing that we have done over here. And  instead of font we are going to just write game,   or we can write or underscore font, and we want  the font to be bigger. So this is the size of the   font, we are going to make it 64 by 64 pixel The  size is bigger. And then we are going to create   our function. So just below this show underscore  function, we are going to create our game over   next function. And we are going to do the same  thing that we did previously with our show under   score function. And then over here, let's just  copy and paste this line. So we are going to copy   this paste over here. And then instead of score,  let's just call it over underscore text. And then   we don't need all of this stuff, we just need to  be displayed game. And let's make it in capitals,   just to make sure it looks good. Game over. So  this is looking good, then we just need to blurt   it all drawn on the screen, we're gonna copy and  paste this over here, let's just press enter and   paste this over here. And then instead of score,  we are going to write over underscore text. And   we are not because we are not going to use the  x and y values, we can use it but because our   game worst takes is going to be displayed at one  place. So I'm going to remove this and instead   of that, I'm just gonna write 202 50 pixels.  And this 200 comma 250 pixel is basically the   middle of the screen, you can try out different  values and see what works for you. But basically,   I've divided the both of these values by two and  made it a little bit less both the x and y axis.   And that's how I got to the center obviously, you  to change the values a little bit so that it looks   good. Anyway, so this is looking pretty good right  now let's run our main.py file and see if it's   working. And you have to remember that we have  made it 200 pixels. And you can see that again,   what else is being displayed, but it's not in the  middle and it's not big. So why is that happening.   And this is because we have used the same font  over here that we have created previously,   this font, we need to change this. And because  we have created a new font that is known as   the Oh font. So we're just going to copy this  and paste this instead of font don't render we   are going to paste over underscore font, because  this is our new font. And this font for used for   a score. That's why it's so small. let's reload  it and hopefully this time it will work. So let's   wait for it to come to 200 y axis and after that  is going to show the game or screen so now you   can't hit any of the enemies so the score is going  to remain constant. So let me just show it to you   again. Let me just hit A couple of enemies. And  now we have hit our enemies. So let's wait for our   200 pixel to get over. And it's going to happen  anytime soon. All right, it's your end game over.   And now because you can't hit any more enemies,  your score is going to remain constant. Obviously,   you can add more buttons like play again and do  stuff like that. But by now you already know how   to do it. So I'm not going to go into it again.  So now finally, let's change is now that we know   that our code is working instead of 200. Let's  change it to 440 pixels. And make sure you add the   sounds again and make it a complete game. So guys,  this is pretty much it. For this video series, we   created our space invader games from scratch. And  if you were a beginner who didn't know anything   about pi game or creating games, now you have  a basic understanding of how a game is created.   It has been wonderful hanging out with you guys.  So thank you for watching and I'll see you around
Info
Channel: freeCodeCamp.org
Views: 1,299,379
Rating: undefined out of 5
Keywords: pygame, python, pygame tutorial, pygame course, python game development, gamedev, python game, code a python game, pygame tutorial for beginners, space invaders
Id: FfWpgLFMI7w
Channel Id: undefined
Length: 135min 49sec (8149 seconds)
Published: Tue Oct 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.