Python Pygame Tutorial - Episode 2! Creating Object Movement, Handling Collisions + Changing Speeds!

LeMaster Tech
183 followers
16 Video Views·Apr 23, 2025
Learn to develop video games using python and pygame!! This is episode 2 in an intro to Pygame series from LeMaster Tech. This episode covers moving objects, handling collisions at walls!
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
this will be episode 2 in our pie game
tutorial series um and we will be
picking up directly where we left off
with the previous video
um so the code looks just like this and
the screen looks like this so if you
didn't check that one out
we will be starting from a 600 by 600
screen with a blue background and a
green ball we drew on it
and actually i was
uh
gonna change this back to just being a
variable green so if you didn't check
that one out be sure to go and check
that out before uh jumping in but uh
what we're gonna work on today is adding
motion to the ball
so we're gonna we're gonna kind of have
this ball bounce um off the extremities
and be constantly moving because
ultimately the goal will be once we
create a player
to be dodging the ball at all times so
we're going to work a little bit on the
motion today but first one thing i want
to change real quick you see here it
just says pie game window
something we can add real quick is just
a
pie game dot
display
dot set caption
and uh this is where we can actually
define
a title for our window so i'll go ahead
and call it ball bouncer this is all you
need right here to create a title
and you see up top
it updates a title you can also update
the icon
if you want to bring in a little png
that describes your game
this is a pretty simple game so i'm not
going to worry about that too much for
now but let's go ahead and get into it
so one thing all three of these draws
and display updates we actually don't
want those
inside of the for loop handling events
we want these to be drawing at all times
and when we add the ball motion that'll
kind of become clear why
so one thing we're going to add into the
game now is a clock
we're going to create a timer
and we're going to use pie games
built-in
pie game dot time
dot clock function
and um we are going to while in the
running loop we are gonna use timer dot
tick
and what we're doing here is we're
creating a universal frame rate and i'll
go ahead and say we want to run at frame
rate and that'll be a variable we need
to go and define up here now
so what we did there different
when you just have a while loop in
python um
the code is going to update as fast as
it possibly can unless you put in a
timer
that's going to slow it down to a
specified number of updates per second
so we use a frame rate of 60 and what we
really said here is we want this to run
at 60 clicks per second
and that becomes useful when you start
having motion and character interactions
and controls and things because you can
very quickly load up an old machine and
it'll run much slower than it would on
new program that can be unfair inside of
a game and it just looks kind of bad
so
that's that's why the timer that's why
the frame rate and then the next thing
we're going to do is we're going to
create a new function and we're gonna
call it
update
ball
position
okay
and we're gonna run that every second
and what that's gonna be is our
constantly in motion
ball so we called it update ball
position
and we don't have to pass any variables
in if we define everything as a global
object inside of here so to do that we
need to call out um
we need to call out some variables
that i believe
will actually want to change our circle
from being drawn at 300 300 to a couple
variables i'll call circle x
and
circle y
and this way we can update the position
of the ball based on these variables
so we'll need to come up into our game
variable section
and say 300 and 300 and we're defining
the initial starting point
and then
we're also going to do something that
i'm going to call circle x
direction
and
i'll just call that a one to start off
and a circle
y
direction
and i will call that a one to start as
well
and you're going to see what all four of
these variables are for inside of our
function here so if we come down what
we're going to do is we're going to take
all four of these that we just made
and we are going to define them as
global so we can mess with them inside
of a function but then we can call those
variables outside of the function and
still be referencing them so this series
is absolutely not uh like a beginner
intro to python in general it's an intro
to pie game so if you're unfamiliar with
things like globals and creating
functions and calling variables i
suggest you start with a more basic
tutorial series on just basics of python
this is more specific to pygame and
assumes you kind of know what a lot of
these things are
there's one of those playlists on this
channel for intro to python i can go
ahead and link that
below or you can check it out on the
channel
otherwise what we're doing so we're
calling in the circle x circle y in both
directions so that we can start
writing some code checking to see
how we should move the ball so first
thing we're going to do we'll handle our
motion in the x direction
and the reason i created that direction
variable is it's going to tell us
whether we need to be bouncing left or
bouncing right so i'll just say if it's
positive
and we're going to kind of use it as a
speed parameter as well so that's why
i'm just checking if it's positive i'm
not checking if it's 1 per se
but so if it's positive we then need to
check that our
circle has not gone past the right
boundary
so um that's going to be if the circle
is less than 570 right because the
screen window
is 600 pixels wide and then our circle
has a radius of 30 pixels so when the
center of the circle which circle x is
looking at
is within 30 pixels of the right edge
then it's time to stop it but so if it's
less than 570 we can just go ahead and
add
our circle x direction variable to the
position of circle x but then otherwise
so now we're saying it has gone far
enough to the right that it needs to
shift directions so we're going to then
um
multiply the circle x by minus one the
direction variable and what we're doing
there is we're saying okay go all the
way until you hit one extremity to the
right and at that point update and move
back to the left
so i'm gonna go ahead and run this just
uh so we can take a look at it
built-in function has no attribute tick
what have i done
to my
have i done to my timer
oh
silly
just need a few parentheses sometimes so
what we're going to see here it should
move to the right and
okay so we reversed the direction but
now we haven't put the code in yet
handling while it moves to the left and
bounces back so
if we look at our update ball position
function all we're doing is saying well
while the direction is positive
go to the right until you hit the right
extremity
so what we need in next is going to be
our else condition so this will be
l if circle direction is negative and
i'm doing this because if we ever want
to stop the ball completely like in a
game over scenario that would be a time
to make direction equal to zero and then
it won't move left and it won't move
right
and this time we're saying well it's
moving backwards we want to check if
it's greater than 30 now so it hasn't
hit the left extremity and again we can
keep the rest of this in place that
adding the direction and then
multiplying times one
and actually one thing i'm going to do
just for these
examples is i'm gonna increase this up
to five so the ball should move quite a
bit faster now
because what we just said is per second
move five pixels so this might look a
little goofy on the recording i think
the recording takes um images at 20
frames per second uh it looks pretty
smooth on my machine and if you are
running it following along on yours it
should look pretty good at 60fps too
it's just the screen capture software
probably has it jumping around a little
bit but the ball is bouncing back and
forth uniformly left to right so the
next thing to do will be basically add
this exact same code
and we want to do it in the y direction
this time
so let's go ahead and
just copy in all the exact same code and
starting here just change all your x's
to y's
and since we made the screen the same
size in the left and the right and our
object is uniform we don't have to
change the
30 or the 570 we can just keep those
exactly as is
and uh so i'll go ahead and run this and
what you'll see is actually going to be
um
kind of boring because it's going to
shoot straight to the corner and then
straight back up to the corner um
because it's a it's a rect it's a square
screen it's moving to the left and to
the right at the same speed and it
starts in the middle
um but the reason i made these two
variables is you now have control of the
speed the ball moves in the x direction
and the y direction independently from
one another so i can move it in the y
direction twice as fast as in the x
direction and then we actually start
getting
some motion that's a little more
satisfying
and so that's just the basics of how to
get
an object um
displaying on the screen and updating uh
bouncing off of your extremities and
what we'll do in the next couple videos
is we'll start adding in like a scoring
system and we can create a player to
avoid the ball and we'll eventually add
collision saying you've lost once the
ball has hit you so
keep an eye out for future tutorials
coming but for now we've created an
object on the screen and we handle
collision with the extremities of the
screen
and that's pretty cool so thanks for
checking this video out if you have any
questions about what you saw here or
suggestions for ways this could have
been cleaner code go ahead and let me
know in the comments
and if you found this or anything on the
channel useful um
feel free to leave a like and a
subscribe on the channel that really
helps me out a lot
and as always good luck with your code
and thanks for watching thanks bye
