16 Video Views·Apr 22, 2025
This video is part three in a five part series rebuilding the classic video game PONG from start to finish using Python and Pygame! This series covers everything from absolute scratch to a fully functioning game start to finish and includes tips and ideas for creating some customization and potentially an even more advanced version of the game!
In episode one we define the game board size, draw the rectangles to act as the player and the computer paddles, then draw the ball onto the screen, and write the programming for the player to move up and down with keyboard keys!
In episode two of the series we write the code for AI movement of the opposing paddle as well as handle the ball's movement to bounce off of walls using collision detection and setting speeds to vary the difficulty and ability of your opponent!
In episode three of the series we create actual collision detection between the ball and the player and computer's paddles and make the ball bounce the opposite direction on collision but then if it hits the left or right walls we create the code for game over tracking!
Episode four of the series handles restarting the game after game over conditions have been met, and resetting everything on the board back to starting positions, as well as tracking score during the game based on how many times the ball has ricocheted!
Episode five is the final part of the series and handles code for a color changing ball that randomly picks a color to change to every bounce, as well as incrementing the speed to increase as the scores get higher so the game gets progressively more difficult as it goes on!
This entire series is meant to help with general python skills as well as be a fun game building exercise. We cover drawing buttons, labels, text and all kinds of widgets and shapes into the Pygame GUI, defining player controls from clicking the mouse or exit button to typing specific keystrokes!
If you found this useful be sure to check out the channel for a ton of additional python and coding tutorials and practice! Thanks for watching and good luck with your code!
check out the channel:
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
what's going on you guys welcome back to
the channel and today we've got kind of
a fun one for you we're going to learn
to build the classic game pong the game
that pretty much started
modern video games we're going to learn
to recreate that in python using pygame
i haven't put in the code yet to handle
like bouncing off of the paddle and
bouncing off of your paddle
but you can tell like your computerized
opponent is going to be tracking it
pretty good
so let's actually do that next that's
going to be um
kind of the bulk of how pong is played
right it's actually the ball bounces off
the paddles and um look when they made
uh pong back in the day they didn't have
anything pre-built
pie game kind of has the benefit of
um
having a module to delet detect
collision between different objects
um so
what we're gonna do is in the event
handler
uh
next kind of in that same area we're
going to
check for collision so let's get into
that
and
let's go ahead and create another you
could probably use this update ball
um
function
to
do the same thing but that function has
already got a lot going on so i think it
would be wise to
uh
i think it'd be wise to do a check
collision um separate
and we'll we'll make two
check collision
player
and we'll do check
collision ai and these are going to look
pretty similar but for the sake of not
having to pass in like 10 000 parameters
per
um
not having to pass in 10 000 parameters
per uh function i think separating mount
makes more sense so
we will do this we will say player
i think we can just do player
and we might be able to just do ball and
then we will need
ball x direction
and i think all we want if there is
collision is we want to flip
the ball x direction
so we can just
call x direction so i think we can do it
this way
where we pass in the ball
we pass in the computer here and then we
pass in the ball x
direction but let's go ahead and write
the function and i'm sure while we're
writing the function that will probably
uh direction
right in the function that'll tell us if
we missed something it always does
okay and we called this
check collision with player
just check collision player
okay and the order of the variables
let's go ahead and just steal these to
be accurate
and we are returning
ball x direction
so what i'm going to do is if
and then this is a built in pie game
function
it is if
ball
dot collide
and then with
player okay
and then all i'm gonna do is say ball x
direction
and then
times negative one uh
i
think that that is all we have to do to
handle bouncing off the paddle because
this collide rect
is checking um
is checking the rectangle of the object
that you called and then it's also
checking the rectangle of the object
that you put inside of the parentheses
so really if it hits the
player or it hits the computer
we just want it to go the opposite
direction
so
that might do it
i called it check collision ai yeah
that's hardcore
i haven't changed this name yet
okay so let's go ahead and run this see
if it bounces off the paddles
okay
so
we do return
ball x direction
but it hits the right paddle
and it just
keeps bouncing into it
let's see
i don't know if this will fix it or not
but let's try combining these
let's say check collisions
ball player
ai
uh computer
stupid
and let's go ahead and tweak our
function
to be check collisions
pretty sure this isn't going to be it
but
gotta troubleshoot
okay so
let's just say if it collides with the
player
that we want the
direction to be equal to one
and then l if the ball collides with
the
computer
then we want the x direction
to be equal to
negative one bounce back to the left
and that will do it
all right not super confident that
that's going to fix it and apparently it
did
wow
all right so um
look we made it guys you can you can
bounce off of the paddles you can um
you can play against the computer like i
said you could set up the exact same
controls for s and w
uh for like up and down arrow and then
they could control the right guy
if you want to but right now this is the
basics of pong working and
now i'm just going to kind of add a few
more things that i think make the game a
little bit more fun
but right here you just created you know
the essence of pong
so what i want to do next
before patting myself on the back too
much i want to get rid of the fact that
it can just bounce off the left and
right walls
i want to make that game over so i'm
going to start
that process by creating a variable that
i will call game over
and it's going to be equal to false
right when the game starts
and we're going to create a new function
that will call once per game loop and
i'm going to call it check
game over
okay
and
we're going to get the game over
variable back from there so game over is
going to be equal to check game over and
what we're going to do is all we're
going to pass in is ball x we don't need
anything else because all we're going to
do is check if the game has gotten all
the way to the left or not
and
then let's go to where the
ball updates
right um what do that is
update ball this function
and we'll say if
game over is false or if not game over
okay so
um that should make sense um
now we're saying the ball is only going
to update if game over is not true
and
let's go ahead and take care of the code
that's actually going to
enter a game over scenario
so we called that function
check game over and we're passing in
ball x
and i think we need to pass in game over
as well actually
because the current status of that is
going to make a big difference
all right and what we will say is if
ball x is equal to zero which is when it
hits the left wall
or
and actually let's say less than or
equal to because if it's jumping around
by multiple pixels uh it's just better
to catch it this way so if it's less
than or equal to 0
or ball x is greater than or equal to
290 because it's 10 pixels wide so
that's when it hits the right wall
we will say
game over is equal to true
and let's go ahead and add another
condition here and say and game over
is equal to false there's no reason to
run this code if uh game over is true
so
then
we will return game over
and i think
that will handle that
so let's go ahead and see i'm gonna let
it hit the left wall here real quick
and you can see the ball stops moving
let's go ahead and add it to the ai too
because the computer gets a little
confused
um so let's move this guy back in there
and i'm okay with being able to move the
player while game's over it's not going
to get you any bonus points it's not
going to do anything for you
um
but the yeah so there we go the computer
should stop moving the ball should stop
moving
nothing should happen
so that's good let's go ahead and put in
some restart logic because obviously
this hopefully is a game that you enjoy
playing you're not going to play just
once
so uh let's handle that
and
let's make a
uh
let's make another if statement and say
what we want to do if game over is true
and we're going to
create
some text now that i will say is game
over text
and
actually something that we didn't do at
the beginning of this game that i try to
do at the beginning of every game is i
just try to define a font um because any
text that you want to draw onto the
screen in the game you have to use
pygame's very specific method of putting
text on the screen
so that starts by
defining a variable i call font or off
net apparently
because i can't type
fortunately typing is not a requirement
for coding
okay so font equals pie game dot font
dot
font
and then uh you can use any font you
want a lot of times using a fun font can
really change like the theme of your
game
i am using a free one that comes built
in with python
okay
and then font size is the second uh is
the second variable i'll just say 20.
that's kind of normal
game font but you can play around as
much as you want
but okay so we defined font as a
variable and now in our
game over text we're gonna you have to
do it in two steps you have to render it
and then you have to draw it onto the
screen with the blip function
so game over text is going to be equal
to font dot render
and then
game
over so we're telling them they lost
and
second variable true
we're gonna make it white on a black
background okay
and uh let's see the second piece that i
was talking about is screen dot blit you
have to draw the text onto the screen so
what are we drawing game over text
and then you give it coordinates for
where to draw it onto so i'm going to do
8130 that's kind of high in the middle
of the screen
and again that's just something you can
play around with
but
let's see how that looks
game over okay
you