How to Track the Mouse Position in PyGame! (Python GUI Tutorial)

LeMaster Tech
181 followers
8 Video Views·Apr 21, 2025
This quick and easy tutorial shows how to track and use the mouse position in your PyGame games and applications. This is very commonly needed in games involving buttons or any type of aiming!
Be sure to check out the channel for a ton of other great python and pygame tutorials! And let me know in the comments if you have questions or what you want to see in a future video!
what's up you guys welcome back to the
channel in today's video i'm going to
show you how to locate and use the mouse
position inside of the pi game module
using python
[Music]
so there are a ton of games that you
might want to make that need to have the
mouse position known to to use your game
you might want this for buttons or you
might want this for like point-and-click
shooters in any case we're going to set
this up pretty quickly so i'm going to
kind of roll through setting up
the bare minimum to get a pie game
screen together so you need to import pi
game if you don't have it installed run
pip and install pi game
and then do pygame dot init set up a
width and a height for your screen i'm
just going to use 500 500 because it's
pretty easy as a default
choose a frame rate for your game 60 is
very standard set up a timer to actually
run the game at that frame rate so
pygame.time.clock
and then
set up the screen to use
the height and width that you made so
pygame.display
dot set mode and then width comma height
just like that okay now just uh choose a
variable to be your running variable and
i'm just going to call mine run
set equal to true to start
and then say wall run so while we're
running
we're going to do all this stuff every
iteration of the game so
let's just start by uh filling the
screen with white and again all i'm
doing and the reason i'm not explaining
this a ton is i am setting up the bare
minimum to get a pie game window to open
up so that we can talk about the real
meat of the game but it's important that
you know how we got there so we're just
going to say timer.tick at the frame
rate which means run this code 60 times
per second
and then we need to handle some
exit conditions so we don't have an
infinite loop and so we'll say for event
in pygame dot event dot get
if event dot type is equal to pygame dot
quit all caps just like that then we
want to set run equal to false
pi game dot display dot flip
so that's drawing everything onto the
screen and then pygame dot quit just
like that so if i run this
we get just this plain 500 pixel by 500
pixel white rectangle just like this and
that's all we're gonna do for this
tutorial so that i can show you guys now
how to get the mouse position and it's
really easy because pygame has a
built-in mouse module so if we have this
mouse position variable that we want to
populate and let's say we want keep
wherever the mouse currently is in this
mouse position variable
it's
dot mouse dot get
pause like that and um that's literally
it to get the mouse position so let me
just print onto the screen the mouse
position variable so now i'm gonna run
the code and you're gonna see in the
terminal in the bottom i'll try to zoom
in make this a little bigger you'll see
in the terminal on the bottom now if i
go oop
i gotta get to the bottom of this
program
oh boy that was a stupid way of doing
this
so now if i run this code you're going
to see in the bottom of the screen there
whenever i move this mouse we get an x
and y coordinate and it's all relative
to our pi game window so i'm up near the
top left and we're getting really small
numbers i move right and that first
coordinate updates because that's the x
coordinate i move down and that second
coordinate is increasing because that's
the y-coordinate so that's all we need
for getting the mouse position out of
our game now let's say you wanted to use
that in the game so pygame.draw let's
just put a circle on the screen
let's make it red so it's really easy to
see
and for x and y coordinate we're just
going to give it mouse position and
we're going to make it a radius 10 mouse
now if we run this you're going to see
as long as the mouse is on the screen
there's this red circle that follows us
around so this is super useful if you
want like a character that's always
pointing or looking at the mouse
it's also useful if you want like a
point and click a point and shoot kind
of game and then
obviously for buttons it's really useful
because you need to know where the mouse
is
so this is a really easy way to
constantly be checking the mouse
position
last thing i'll talk about in this video
just because it is related in that pie
game.mouse
package there's this get pressed as well
and it's checking the three buttons so
i'm gonna show this i'm gonna print
pygame.mouse.getpressed in the bottom of
the screen and i'm gonna disable this
printing the mouse position so you guys
can see um so if you can see the bottom
of the screen i'll try to make this big
but i've gotta get to the bottom of my
terminal there we go i'm gonna click the
left mouse
uh position in my game and you'll see
the the first uh box there goes to true
now i'm gonna click the scroll wheel the
middle button of my mouse and the middle
one goes to true and then i'm gonna
click the right one and go to it goes to
true so if you wanted to check okay
where is the mouse and is the left
button clicked then you would have this
variable like left
clicked is equal to
pygame dot mouse dot get pressed oop get
pressed and just like that but then you
would say okay zero and that's the left
click button so that's how you would
check if the
left had been clicked or not and i think
i'm going to leave this tutorial here i
want to keep this one short and simple i
don't want to get it too jumbled with
other concepts i hope this is what you
were looking for if you searched for
basically how to do these types of
concepts
feel free to check out the channel for
tons more python and pygame tutorials
let me know in the comments if you had
any questions or what you'd like to see
in a future video thanks so much for
checking out the channel and as always
good luck with your code and thanks for
watching thanks bye
