Python Pygame Tutorial - Episode 4! Create a Player Object, and Define Player Controls and Movement!

LeMaster Tech
183 followers
16 Video Views·Apr 23, 2025
Learn to develop video games using python and pygame!! This is episode 4 in an intro to Pygame series from LeMaster Tech. This episode covers creating a player object and defining player controls and movement in the code!
Be sure to check out our channel for tons of additional python tutorials and practice problems and be sure to like and subscribe if you find our content useful!!
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
If you want to see anything specific on the channel or have technical questions about anything leave a comment on our of our videos and we will get back as soon as we can!
Thanks for watching and supporting the channel!
hello and welcome back to the channel
today's episode is going to be episode 4
in our introduction to pie game tutorial
series on this channel we will be
picking up right from where we left off
at the end of episode three so if you
haven't been following along make sure
you check those out before diving in
here unless you're just here to kind of
see the concepts we cover in this video
in which case that's totally fine
so previously we built a game that is
just this fancy ball bouncing off the
walls and we created a score text that
increments every time it hits a wall so
today's video we're going to be creating
a player and handling player motion so
i've decided that we are going to make
the player an orange rectangle
and we're going to let it move in all
four directions based on the arrow keys
so we will be taking a look at how to do
that today
first i'm going to go ahead and
create
the color orange
like i always do i prefer my colors
defined above as rgbs so that when i
reference them
later i can just type in the color name
one less thing to think about and we're
going to have a section here where we
create uh player variables so we're
gonna
create a variable for the width of the
player we'll say the rectangle be around
30 and then we'll say height of 50
should be good
and then we will call out again these
are some variables which once you have
an object in mind
um
you you are always going to need a width
and a height and then especially for
rectangles or images or even circles
like you're gonna need coordinates for
it to start with so for player x we'll
say start in the middle of the screen
but then we don't want starting too
close to the circle because eventually
the goal will be avoid the circle
so we'll say 500 which is going to be
pretty close to the bottom of the screen
because the coordinate grid in pi game
actually starts at the top left
so
the bottom of the screen is 600 and the
far right side of the screen is 600
it for x and y so that's x and y and
then um because we're going to be moving
the player like before uh like the ball
we're going to make a player x and
player y
um direction
and we'll start those out as zeros
so the player doesn't start moving
initially
and then just to show something
different i know with the circle x and
circle y direction we combine speed and
direction into one variable but for the
player
i'm going to create another variable
called speed and a little bit later
we'll see
we'll see how to use that and it's just
kind of giving you guys two options for
ways to move stuff
but let's go ahead and draw the player
next so we're going to go down and let's
say
underneath our where we draw our circle
let's do pie game dot draw dot and this
will be wrecked
and then it needs surface just like
before
um
correct
put it onto the screen and then we will
make it orange which we have defined now
and then rectangles need four arguments
here they need
x and y starting coordinates so we
called that player x
player y
and then it needs width and height and
we called those player width
and player height
and if we wanted to we could give it um
a thickness like we did with the ball so
that it would just have a line
edge but
if we do it this way it should just be a
solid orange rectangle and that should
look pretty good so let's go ahead and
see if we've successfully drawn yep so
there's our player it doesn't do
anything if i click the arrow keys right
now and it doesn't do anything when it
collides with the ball
but we've drawn our rectangle onto the
screen successfully
so let's go ahead and handle
inputs from the player so when the
event
is
keys
so instead of the if event type being
quit which is how we exit the loop
we want if event type is equal to
pi game dot and it is key down and so
when the key down is pressed
um
we're gonna check what actual key it was
so event dot key
and let's say first if it was
pygame dot
the left key then what we actually want
to do is say the player x direction
is going to equal
negative one so that's moving to the
left in the coordinate grid system
and we'll go ahead and copy those two
lines of code down four times
and we'll say left right up down so left
right
and then
up and down so those are our arrow keys
i'm just picking those because i prefer
to control it a game like this it
certainly would be pretty common to do
that with wasd as well
so um you know that's kind of up to you
uh this is just how i'm doing it for
this video and then so when we go right
the player x direction will be
one when we go uh up we're actually
going to subtract one from the player y
direction
yeah because zero is at the top of the
screen
and then when we go down
we want positive one for the player
motion
okay and then the other thing we need to
handle here is we need to handle when
you release a key stop the motion
so if
the
key up
pie game dot key down pretty much the
opposite of this
then we check all these keys and if it's
left or right
we are going to stop the x direction and
if it's up or down we're going to stop
the y direction and so
for all of these we are going to be
saying well while i have the left key
held down i want the player to move to
the left while i have the right key held
down player move to the right
up down move up or down but then when i
let go of the key stop moving
if you don't include this key up stuff
it's gonna keep sliding in whatever
direction you last pressed until you
press another direction you could build
a game that way like a lot of times in
ice dynamic where like
you stop moving but you're on a slick
surface um that'll involve a continued
motion kind of like that
but for us this is how we're going to do
it and we are going to now create
another function just like we did with
the ball and we'll call it update player
position
and let's go up to where we created the
ball logic
and we'll create some player logic as
well so we'll say def
update player
position
okay pretty sweet
and we're going to have some globals for
this guy as well we're going to bring in
global
we're going to bring in the player x
we'll do global
we'll do global for player y
and this is just so i don't have to pass
all these parameters in
you could pass them in as
function arguments and then you could
return their results as function results
but if you reference everything globally
then you get to
control directly based off of outside
variables inside a function and that is
how i prefer doing this
so
uh player x player y
x direction and y direction should be
all we need for now
and we're actually going to set up some
code pretty similar to what we have down
in the
circle checker with the difference being
we're not really going to increment the
score for anything but we are going to
check
if the
player x direction is positive
then we are going to check and see if
the player
x is less than but this isn't going to
be 570 anymore it's going to be 600
minus the player width
so because now we can adjust the size of
that rectangle and this will still hold
true that's saying the right edge of the
screen minus however wide our actual
rectangle is so you can't go off the
screen
and when that's true then we are going
to update the player x by
the player x direction but then
multiplied by the player speed so you
remember
above we defined
player speed
um we are going to
uh
we are going to multiply the direction
by the speed and that's how we're
actually going to adjust the speed so
this is useful if you have a player that
like you grab an upgrade and you
immediately are twice as fast this way
you can just modify that speed variable
and you don't have to worry too much
about doing any math or checking what
the current value was
okay so let's go ahead and do this a
couple more times we're going to do it
for checking the player going to the
left
and so we'll say if the player x
direction is negative
then we want to check
we want to check that the player x is
just greater than 0 because the position
of the rectangle is based on the lower
left corner
so if it's greater than 0
that just means the whole rectangle is
going to be on the screen and we will
again um this part stays exactly the
same because left or right is going to
be handled by whether the direction is
positive or negative
and we can pretty much copy this code
exactly
for the y direction
so if player y direction is positive
um and then if the player y
is
less than 600 minus the height
player y
player y direction player speed
and again greater than zero player y
y direction
player speed
so
that pretty much should do it for
movement so let's go ahead and see if
we've captured it all here
i'm going to press the left arrow key
and i hold it down to slide left slide
to the right
that's pretty sharp when i let go of the
key it stops moving i can move up or
down
let's go ahead and check it up against a
wall yep perfect we get stopped at the
top stopped at the right
we can do both at same time stop to the
left stop to the bottom
there you go so that's pretty cool right
there
just a few lines of code and we've
created four directional player movement
now you will notice if the
rectangle slams into the circle right
now we don't have any game over
condition
the we can keep moving the score keeps
incrementing so we've created movement
we haven't handled collision yet
but that's all for this tutorial
hopefully you found it useful if you
have any questions about what you saw
here or
would like to see anything in particular
in the future just let me know in the
comments below otherwise if you found
this or anything else on the channel
helpful um
i'd really appreciate a like and
subscribe it helps the channel out a lot
and uh as always good luck with your
code and thanks for checking out the
channel thanks bye
