Pygame Tutorial 6 Keyboard Input Controls Key Pressed Event

alright guys welcome back in 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 to minus equals to zero point one then it will move in the up direction so if I run this man you can see that a spacious is moving in upward direction but right now we don't have any way to control this spaceship we can't control it using a 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 red 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 checked that we are gonna have check whether the key stroke 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 our mouse we click somewhere we close this window so this is a quit event and similarly whenever we press any key stroke on our keyboard it is a key stroke event so for example every event gets locked inside this PI game dot event dot ket 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 then it will get stored inside the spy game not even dot gate and we'll be able to look through that and check whether a keystroke has been pressed or not so inside this I'm just gonna 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 keystroke keystroke is pressed check whether is right or left so this is basically what we will be doing Thank You stroke of keystroke oh god I'm gonna mess up my spelling something all right keystroke can't get this feeling right it's all right so anyways how do you check with your keystroke is being pressed or not my game makes it really really easy it's kind of an event which is I game dot key dumped 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 not ready if event dot type which we did inside this if condition so we have to go actually inside this for loop so if we will just press this 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 all right so this is also pretty easy we just again type event dot this time we type in dot key because you want to find out the key that is being pressed and we just type in PI game dot key left ok left and this will just check whether the key that is being pressed is the left arrow or not if that is the left arrow it's just for right now print the left arrow is pressed and after that I'm gonna write again I'm just gonna copy and paste this over here like this and over here I'm just gonna write right and instead of key underscore I'm gonna add the right alright so basically what this does is it checks whether 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 Araki that is known as a 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 are going to check for that over here if event dot type is equal C equals 2pi game dot key up so hopefully we'll be able to find that key up over here and then over here we are 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 we can just copy this from here and put this colon mark and then over here we are just gonna write keystroke has been released just 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 print and play and over here is gonna do nothing but on 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 wait our window over here and then we can press the left arrow key and you 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 the 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 print 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 press left arrow keys 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 are going to create a new variable over here and this will signify the change that we want an egg so if you want the extra move in right hand direction we need to add value to this 370 existing value and if we wanted to move on the left hand side direction we need to decrease value from it so I'm going to just call it over here create a new variable and call it player X underscore change equals 2 and right now I'm going to give it a value of 0 and inside our if conditions when the left key is pressed I want it to increase by 0.1 and when sorry decrease by 0.1 and we don't change the 0.1 number it basically signifies the speed with which you want the object to move and in this case a spaceship how quickly you want the spaceship to move and we are going to change it later and even in this video we'll 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 placed 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 key Express so we're gonna just right over here player X and let's go 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 gonna paste this over here so this is looking pretty good let's actually move 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 clear X plus equals two and then player exchange so doesn't matter whether we are adding over here because we have gained a term value of minus 0.1 and over here we are giving it a positive value so whenever something is added and the value is negative so I'm just gonna 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 this zero point one right so let's say our current value is 370 let's say actually you take a smaller value let's take out clear x coordinate is 5 and then when we plus equal to so this basically means equals to 5 plus and then the value that is player X and the score change so night now this value is minus 0.1 if the key left is activated so this value will be 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 zero point one and similarly if we add then the addition is pretty pretty like obvious so we can just add zero point one and this will remain the same just zero point 1 and the value will be five point one so this is how this operate over here works so we don't need to worry about whether we are subtracting or I think if we have added our arithmetic operator over here in this case it's minus so this should be working let's run our main dot fire file and see if it is working so let's activate it and over here when you press the 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 does go change is zero point one so we have to change it to value zero because we don't want it to move you can actually change it to zero point one to see how it works how the spaceship is stopping so let's try it again now when you move on the right hand direction and we release the keystroke the ship actually stops so this is looking pretty good but astley this just speed is a little bit slow so I'm going to increase the value to zero point three and now if we run it our speed should be sufficiently well so yeah this is looking pretty good in future we'll change is zero point three to a higher value when we use the clock concept but we are not using it right now so this is looking pretty good a spaceship is moving perfectly and just to recap what we did in this video was first we checked whether any keystroke has impressed on a keyboard and then if the left keystroke is pressed then we increase or decrease the value of player exchange by 0.3 and if our right key is play then we increase the value by point three and when our keystroke has been released you 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 guys this is looking pretty good in the next video if you have any queries please ask them in questions so this is pretty much it for this video in the next video we are gonna give our spaceship and enemy because right now is the space is 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 gonna give it a meaning in the next video so I'll see you who are they

Loading