How To Use Joysticks In Pygame

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello folks in this video we'll look at how to use a joystick controller with pygame by the end of the video you will have a player rectangle that you can fully control with the joystick we'll begin with the starter code where I create the game window to find a helper function for displaying text and create a clock to limit the game speed to 60 frames per second I then create a player rectangle and position it at The X and Y coordinates I also specify a color variable which will be used later once we get the joystick connected up next is the game Loop where I draw the rectangle and have my event handler for closing the game down the first thing I will do is initialize the joystick module I'll go up to the top and I'll paste in this code which says pygame.joystick dot init now we can begin adding and keeping track of joysticks within the game so to do that I'm going to come down here and I will create an empty list to store the joysticks this will just be joysticks is equal to empty square brackets now whenever a joystick gets connected I'm going to add it into this list but how do we know if a joystick has been connected well for that there is an event within Pi game which means I need to go into my event handler and I will add an additional event by saying if event DOT type is equal to pygame dot Joy device added this event will be triggered every time I connect the joystick so let's print this event out and see what it says now when I run it I've already got my joystick connected so straight away it's triggered an event down here it gives me a device index and a guid what I'm interested in is the device index that's the value that's assigned to each individual joystick as it gets connected one thing to keep in mind is that this event gets triggered whether you have the joystick connected before you launch the game or if you're connected during so right now I've got the joystick connected I run it and already it's detected this event now I can disconnect the joystick and I can reconnect it again and you'll see that it triggers a second time the device index is the same because I've got the same number of joysticks connected as before now I can use this value here to create a joystick object I'll assign it to a variable called joy and I'll say it's equal to pygame dot joystick dot joystick with a capital and what I pass in here is that event dot device index once that's done I can add this into my overall joysticks list by saying joysticks dot append Joy now that I've got this joystick created we can start having a look at some of the methods that come with the module first of all I'm going to paste in some code up here now this is my draw text function from before but the important thing that is outputting onto this screen is this pygame.joysticks dot get count method this will return the number of joysticks that are connected so it says controllers 1 if I disconnect it it drops a zero reconnect it goes back to one there are a few other things that we can call from this module I'm going to paste them all in down here so what I'm doing is iterating through each joystick within that joysticks list and then I'm outputting some information on each specific one the joystick module has a bunch of different methods but I'm just showing a few of the common ones here so we've got get power level which will tell me how much battery charge I've got get name which will tell me what kind of controller it is and then get the number of axes with get underscore no Maxes if we run this you can see a whole bunch of information being displayed now the one that I find quite interesting is this controller type so it detects the type of controller I've got connected which is a PS4 controller and it's a six axis PS4 controller now this is Handy for getting some information back from the joystick but how do we actually get button inputs how do we control things on the screen with the joystick well there are two ways to do this we can either go into the event handler or we can get button inputs directly by checking the state of each individual button pygame is assigned a number value to each of the buttons that are on the controller so you can check for each specific button by passing in the number of it to start with I want to check check for the X circle square and triangle buttons we'll do that by iterating through all the joysticks in our list so I'll say for joystick and joysticks and I'll add a comment to say change player color with buttons the first button that I want to check is the cross so I'll say if joystick dot get underscore button and then here I just pass in the value the pi game assigns to the Cross which is zero and if that's the case then I change the call variable which is the color of that rectangle to Royal Blue now I can do the same for the rest of the buttons so I'm just going to paste this code in and it checks for the X circle square and triangle and it changes the color according to each one if I run this I'm going to have my rectangle on the screen but as I press the button you can see that it's changing color so that's working pretty well so now let's have a look at moving the rectangle around the screen if we have a look at those button assignments again from the documentation we can see that 11 through to 14 give us a directional buttons so that's the buttons that we want to check within here I'm going to stay within the same for Loop and I'll add a comment to say play layer movement with joystick and just as I did before I'm going to go to that specific joystick and I'll say get underscore button and I'll pass in number 14. this will allow me to move to the right and to do that I just adjust my X variable so I'll increase the X variable by 5 pixels I can then repeat this for the rest of the directional buttons with pasted code here so going to the left is going to decrease the X variable by 5 pixels and then I've got the same for y for moving up and down once these X and Y variables are updated if we go and have a look up here the player's rectangle is updated based on those values so we'll use that to move the rectangle around if I run this again and I start pressing the d-pad buttons you can see the player rectangle moves around now we've got directional input with d-pad I want to have a look at how to handle the analog stick input if we look at the documentation again we can see that each of the analog sticks is broken up into two axes so the left analog stick has a horizontal axis which is Axis 0 and then a vertical axis which is access one we can use these to see how far to move the player so we go back into here and we'll say player movement with analog sticks and I'm going to separate those two axes into their own variables so we'll say horiz move for horizontal movement is equal to joystick dot get underscore access and it's axis zero and now I can repeat this for axis one so I'll change this to vert move and change it to access one now let's just print one of these out to see what it actually looks like so I'm going to print the horizontal one out for now and you can see straight away I'm getting a value here so if I move to the left it goes to -1 and if I move to the right it goes to almost plus one but you can see that between those two values there's a whole host of different values depending on how far across I push the analog stick and we can use that to adjust how fast this rectangle moves across the screen I can get rid of this print statement now and I will say Y is increased by vertical movement multiplied by 5 and then X is increased by horizontal movement multiplied by five the reason I multiply by five is so that it's the same as the d-pad movement and now we run this code again and you can see I'm able to now move around this rectangle with the analog sticks so if I push it just slightly it moves slowly and if I push it all the way it moves a lot faster now this gives me much smoother control over the rectangle and I can still use the d-pad if I want to but there is one small thing to note if I let go of everything you can see that the rectangle is actually moving a tiny bit so why is that happening well if I print this value out again so let's print out vertical move this time you can see that it doesn't actually go to zero so what I need to do is ignore these very small values that are very close to zero to do that I will add a threshold so will remove this print statement and above this y I'm going to say if the absolute value of my vertical movement is greater than let's say 0.05 well in that case that means that I am actually holding down the analog sticks so I'm going to move and then I do the exact same thing for the horizontal movement I'll say you have absolute value of the horizontal movement is greater than 0.05 then I increase the x value if I run this again you'll see that the rectangle is now completely stationary but as soon as I put in any input it moves around and that's how you get input from the joystick within pygame I know I haven't covered all of the buttons but the rest of them work in the same way as either the analog sticks or the digital input buttons that I've looked at already so you can expand this and add full controller functionality to your games if you found this useful then please leave a like and I'll see you in the next one
Info
Channel: Coding With Russ
Views: 7,107
Rating: undefined out of 5
Keywords: Python, Python tutorial, python beginner tutorial, pygame tutorial, pygame beginner tutorial, pygame tutorial for beginners, python game, python game tutorial, pygame, programming, game, coding, python for beginners, python programming, joystick, controller, ps4, xbox
Id: MLl9wzQ2Bcg
Channel Id: undefined
Length: 8min 16sec (496 seconds)
Published: Sun Apr 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.