17 Video Views·Apr 22, 2025
This video is part two 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
up and down controls
all set so
next thing i want to do is get the
computer to track the ball and then i
want to get the ball moving
uh so let's see for that
um i think we should make the computer's
location uh the next thing that we
update
so we're going to have to create a new
function and call it
um
i'll call it update ai because that
sounds hardcore
but for this
we are going to be passing in the ball's
y coordinate
and the computer's current y coordinate
and then we're going to get the
computer's y coordinate back so based on
where the ball is the computer is going
to update um
where it is accordingly so
let's go ahead and create that function
and we called it
update ai great name
i always use ai instead of computerized
opponent because it makes me feel really
advanced
um obviously you can probably tell from
my code that i am not really advanced
but hey
works for me
okay so um just so i don't forget what
i'm passing back i always uh kind of
write the last line
as well so uh we are going to pass in
ball y
and let's go ahead and just in here
let's make a computer speed equal to
we'll give them three they can't be as
fast as the player
as the player you should always behave
like you are the code's god
i guess
okay so computer speed of three and then
what do we wanna do we wanna check if
the
computer's y coordinate
now you gotta think um it's 30 pixels
wide right and really the way a computer
would play this is to get into the
optimal placement so it doesn't just
want the ball somewhere over the paddle
it wants it dead in the center so
there's room for error so let's say if
the computer
plus 15 which again think about the
coordinate system going from zero at the
top to 300 at the bottom
so plus 15 is going to be in the middle
of the paddle
and if that
is greater
than the ball's y position and we'll say
plus 5 because again the ball is 10 wide
so what do we want to do if the computer
mid section is farther down the screen
than the
ball's mid section is we want to
computer y minus equal
computer speed right okay and then
we will say else
computer
let's do l if
um
so
l if computer y plus 15 is less than
ball underscore y plus five and in that
case that's saying that the
we need to go down the screen
so then we want computer
y
plus equal to
computer speed
all righty and i actually think that's
gonna handle it um but what you'll see
is this isn't really going to do much
yet because the ball isn't moving so i'm
just going to set the ball y equal to
zero when i start this up we should see
the computer move to get to it
bam
and then it freaks out because it's just
bouncing around but cool so um it
appears that that works oops it appears
that that's working pretty good but once
we get the ball moving and bouncing um
it should work a lot better
so let's go ahead and um
oops let's go ahead and move on to
um the ball
so
i will create a function
that i'm going to call
update
ball
update ball i'm not completely sure yet
what all it's going to need passed in
and what all um it can do without so
let's just start writing it and we'll
kind of flesh it out as we go
so
update
ball
and
inside this function it's going to
basically take a look at uh let's let's
separate into x movement and y movement
and say uh
if
ball
x direction so we'll need to create a
variable saying whether the x uh
is right or left
so um if it's equal to one that means
that we're telling it to go right
and the x coordinate is less than
what do we want
less than
290 i suppose let's see we're going to
start by drawing it to bounce off all
the walls
and then we'll handle the collision with
the paddles second so if ball x
direction is equal to 1 and the ball x
coordinate is less than 290 then we are
going to say ball
x plus equal to
and let's do
another variable called ball speed so
what i'm seeing here already is we're
going to need to pass in ball x
direction
ball y
direction
ball x coordinate
and
ball y coordinate and then ball
speed as well so obviously we haven't
made a lot of those variables yet we're
going to create the function for
updating it and moving it and then we'll
go ahead and kind of work backwards to
create the variables
um
that's how i want to do it there's
obviously other ways to do it so
code it however you want
but let's just go ahead and say
if the direction is 1 and the ball is
less than 290 and then let's say if
direction is negative one and x is
greater than zero
then the ball is going to move to the
left
by the speed
okay
so now we've got back and forth handled
let's take care of up and down and say
if ball
wide direction actually we also need to
handle
what happens when it hits those extremes
so
my mistake
let's do a
l if
ball x
direction
is equal to one so we're telling it it
should go right but now
ball x is greater than or equal to that
290 marker that means it's hit the right
wall so what we need to do
is
change ball x direction
okay
so we're gonna go ahead and multiply it
by negative one and that's why i like
using one and negative one rather than
zero and one um you can actually
update uh direction just by multiplying
times uh minus one which is really handy
like when you want it to collide with a
paddle you say well whatever direction
you were moving moving the opposite now
so
that is pretty cool
and then for moving to the left we're
going to you need an and
for moving to the left we're going to
add if ball x direction is equal to
negative one
and then it's gone all the way to the
left we'll flip it just the same
okay
so now we need to sort of do the same
thing
for ball y
well pretty much exactly the same thing
for ball y
so i am going to go ahead and copy this
guy completely and we're going to change
some x's to y's
and then uh i think we should handle the
ball's speed in the y direction
different
than the speed in the x direction
it's just my sense of things
you can make them one variable if you
want uh
i think it'd be a little more fun to
have x and y
um update it slightly different times so
you get kind of
slightly different
paths of travel
okay
so hopefully what we've done is we've
just created a ball that bounces around
in
the
extremities in the
off of all the walls
and what we want to do
is just return
ball x and ball y for now i think we'll
see we might need to return more
but
this should get the ball moving get the
ball rolling
i know that was bad
okay so we need to go create four
variables
um
ball x
uh ball x direction
and we'll start it out moving to the
right ball
y direction
we'll start it out moving down
and
ball speed
we'll make it two
and ball
y
speed
which
we'll make that uh
we'll make that two as well for now
paul why speed but do we pass that in
yep okay
all righty well i'm just gonna run it
and kind of let the code tell me what i
did wrong
so it looks like we get into the bottom
right and it tries to bounce back to the
left once but then immediately tries to
go back to the right so let's take a
look here
at our
ball control
if it's going to the right and it's less
than 290
just add it on
but then if it's going to the right and
it's greater than or equal to 290
multiply times negative one
um if it's negative one call x minus
equal ball speed
um i don't know why it would be this
but i'm gonna just try
oh yes i do okay
stupid mistake no surprise there but we
aren't actually able to update
ball x direction
and ball y direction
um
unless those exist outside of this
function so right this every time this
gets called it pulls these variables in
independently
and uh we weren't returning them so what
we need to do
is not just bring back ball x and ball y
but also ball x direction and ball y
direction
and now
it should bounce around all happy like
all right sweet
we could try to follow it
uh it's bouncing straight into the
corners which is rad so there you can
see why like i kind of like the idea of
doing a different x and y speed it'll
give you a little more interesting of a
bounce pattern but the other cool thing
that you can see right now is the ai
is tracking it and following it so we
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
you