Python Pygame Tutorial - Episode 5! Creating Collision Detection and 'Game Over' Ending Conditions!

LeMaster Tech
183 followers
14 Video Views·Apr 23, 2025
Learn to develop video games using python and pygame!! This is episode 5 in an intro to Pygame series from LeMaster Tech. This episode covers creating collision detection between the ball. object to be avoided, and the player's controlled character!
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 will be episode five in
our introduction to pie game with python
series on this channel
um and if you missed the first four
here's where the game is at today we
have a rectangle controlled by the
player
attempting to dodge a ball which
increments your score every time it hits
the wall so if you haven't been
following along and you're just here for
the concepts discussed in this specific
video that's fine but if you'd like to
build this game together as we build it
i suggest you go back and check out the
start of the series
because that's where we'll be starting
from
so today's video in particular is
regarding checking for player collision
with the ball
you could kind of see there i don't know
if i actually highlighted it you can
currently drive your player straight
through the ball and it doesn't end the
game it doesn't reset the score or
anything like that
the ball keeps bouncing and you can keep
controlling the player so what we're
going to do today is check for collision
with the two objects
and um
kind of create a stop scenario where you
can't move the player anymore and the
ball stops moving as well so to do this
i am going to
actually
come down into where we defined the
player and the ball before right this
outer circle which is in green
in the player here
which is the orange rectangle and i'm
going to start by calling these um
actually giving them variable names so
i'll call the ball the ball
and then i'll say the player is our
gamer um
and so the reason for doing that is
we're going to create a new function
down here
and we are going to call it check
collision so right after that we are
going to
new line
um
we are going to create a new function
called check
collision
and uh we have to
give it some data so it knows what it's
checking for and what it'll be checking
is actually our gamers
position and so
since we created these as variables now
they have the pi game attributes of
rectangles and circles so we can pass in
here their
center points and then use that to ch
check collision
in our
um check collision function that we're
going to create so i'll just pass in the
center x and center y
for both the player and the ball
center one
and okay now let's go ahead and actually
create this check collision um function
because right now it's not doing
anything we'll go up above our other
functions and create a new one
and we'll call it define check
collision and so what we just passed in
here right we passed in
the
players x
the players y
the
balls x and
the balls y
and our actual check collision function
is going to be checking based on the
center point of the two so
first let's check in the x direction
and let's say if the absolute value of
the player right because you could be
colliding from the left or from the
right so the player's x position
minus the ball's x position
could be a negative value if uh if
you're if the ball's coming in from the
left or from the right it changes which
direction but we just want an absolute
check
and um so our ball right we made that a
radius of 30 and our player is
30 pixels wide but we're checking based
on the center point so it's going to be
the radius plus half the width which is
going to be 45 for these characters
okay so that's checking that their x
parameters overlap
but what about the scenario where the
ball crosses the plane and your y's
don't overlap so you actually need
collision um you need the x and the y
parameters simultaneously to be inside
of those
parameters so we're going to do
basically the exact same formula and
check in the y direction that the player
y
minus ball y
is going to be less than and that'll be
55 because we made the rectangle
50 tall in the previous video so it's 25
plus the radius which is where we get
55.
and then we're gonna pass in a lot of
globals so that i don't have to pass
them all in as
variables into the function i'm just
going to define global player x
direction
global
player y direction
global
let's do
come on
uh and then global we called it circle
before now we're calling it ball that's
okay not a big deal
and then global let's say circle y
direction so this is because if they
collide which is what this if statement
is saying if that's true
then we want to set all four of those
equal to zero so i'm gonna go ahead and
copy these back down
and
create the logic
and
now we're saying okay well if the two
touch then
stop moving all four of them and what's
kind of cool the way we wrote the code
previously and this was intentional
um
we
wrote it so that if we just set the
direction equal to zero there's going to
be no more
changing direction or ability to control
because it's all math based off that
number either being positive or negative
and so when we set to zero it's not
positive or negative so we really kill
it and just freeze it in place
so that's why we're gonna make all of
those zero
absolute
so it's not happy with my formula here
probably because i have a closed
parenthesis for no reason there we go
okay and then what we'll also do is
we're going to call a gameover function
inside of here that i'm actually going
to create separately just in case in a
game if you want multiple different
scenarios that could cause
game over i like to sort of define that
function as its own because this is
specifically just the collision
condition
okay so let's go ahead and define game
over
and what
we're going to do inside of the game
over function
is um we're actually going to
display score
game over font
sorry we're not displaying score we want
to display game over
display
game
over
and that's going to be equal to
i know i did that in my notes because
we want to take the text we did before
to
draw the score onto the screen
and we're actually going to copy that up
and put that into game over but we want
to call this display game over rather
and then in here we're going to say game
over man
and we don't need to add the score to it
that's all we want to say
but let's go ahead and make this one red
on a black background it's a little more
intimidating
and i don't want to
i don't want to put this in 1010 anymore
i want to put it around the middle of
the screen so i'm going to pick like 70
and 300 because you have to remember
you're giving it a starting location and
this text is kind of long so it'll
stretch across the screen
and rather than just font.render let's
make a new font up here
let's make it a little bit bigger
so remember the font that we defined for
score is just size 20 but you want game
over to kind of pop so i'm to take this
we'll use the same freesands bold
but we'll make this guy 60
and now instead of font.render we'll do
game over font.render
okay
and let's take a look here
so once we see collision
um we're going to stop everything from
moving and then we'll put game over on
the screen
and let's see this should be about all
we need to do to check for collision and
end the game so let's take a look
okay i'm gonna give it a few
and then i'm gonna try to go get
collision here
and there we go
so we we see it stops but then it
actually stops the objects in motion i
can't control it anymore which is good
but there's a little bit of a gap and
that's just kind of because uh the scan
rate it's actually off by about one
pixel it hasn't moved that one
additional spot yet
um that's the sort of math that's really
easy to tweak you can come into our
check for collision
function and rather than this be a 45
and a 55. you can make it a 44 and a 54.
you can leave it as is if you want
i'm going to do it this way because i
think yeah there we go now
if you're gonna freeze frame on the game
being over you're gonna have some upset
players if they can take a screenshot of
it and clearly state like hey it didn't
touch so it's almost better to be one
pixel more forgiving than one pixel less
forgiving especially um with people
playing games
so that's it for this video there's how
to check for collision between two
objects and uh create a game over
condition
and in the next video we'll be taking a
look at some things like um
restarting the game and keeping track of
a high score so
hopefully you found this useful
hopefully this tutorial has been fun for
you
eventually we should have a pretty fun
game to make and if you have any
questions about what you saw in this one
or stuff you want to make sure you see
in future videos
feel free to let me know about in the
comments and uh if you found this or any
content on the channel useful
feel free to throw down a like or
subscribe i really appreciate it helps
channel out a lot
and as always thanks for watching and
good luck with your code thanks bye
