15 Video Views·Apr 22, 2025
This video is part four 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
game over okay so
second thing i think we should do is add
some
restart text
so i will call that
restart text make the
make the text say press to restart
and let's put that same
x we'll probably want to scooch it back
since there's more text here but let's
put it a
100 lower on the screen let's put it 230
and we also are going to want to drop
we'll get to this after we check and
make sure it looks okay
but we're also going to want to draw a
rectangle ooh
game over
because we're just drawing game over
text in two places it's
important if you're going to copy paste
stuff like that to um
to actually run it like i just did and
check
because you can't just assume everything
you drew looks good and like you can see
here i'm going to scoot it to the right
a little bit and i'm going to scoot it
up a little bit
you can't
always really envision what something's
going to look like until you just run it
so okay we also want to draw a
rectangle which i'll call
i'll just call it restart button because
you can't use um like a click
collide
event you can't do collision with text
for some reason
you need a rectangle
so
i'm going to make a rectangle that is
just a pie game dot
draw.rect and we're going to put it
right at the
restart text and we're going to just
make it a black rectangle that gets
drawn behind the text but this way there
will be something to check whether or
not the person is clicked on
okay so pygame.draw wrecked and then
we want to put on the screen we want to
be black and
we want it to
be
at 70 150. so again we're giving it the
exact same coordinates as the text and
then
that block of text is probably a hundred
long and um let's just go ahead and say
that it's
20 tall
but the idea is if you're clicking
anywhere in
the rectangle area that says press to
restart we can assume you want restart
because the x out of the game button is
way in the top right
uh okay so we need a new event type now
because this is the first time the
players need to click so we want to say
if event dot type
is equal to
pi game and then this is going to be
mouse bound button down we'll just do
when you click down on it
some people would do click up and that's
when you release the mouse click but
that hey user preference
so now let's do if
i think we called it restart button and
then
so restart button dot
collide point and we can do a nice thing
here which is event dot position so uh
the click in this case has an x and y
coordinate and that's considered the
position of the event
and uh that is what we're checking here
and so we are saying all right well
they've clicked the restart button so
what do we want to do well the most
obvious thing is we want game over to be
equal to
false because they just
restarted it
but in that scenario i actually think
that what we should do
is make game over equal to
false to even uh or true to even click
because there's no reason to be clicking
on anything if game over is not true
but then you also gotta think about
pretty much everything we did to
initialize this game
um all of this stuff should
come back into play right like if speed
changed if direction changed if anything
changed we want to get reset when you
hit restart
so
you could create a little function do
that i'm just going to put it all under
the button again and let's go ahead and
see if this is going to last restart now
press to restart
okay
so that's cool
it moved
but
um but game over oh it's because it's a
double equal sign alrighty
let's try that again
all right so that's pretty sweet um we
just keep getting uh closer and closer
to like a full functioning game this is
getting uh pretty fun i think the next
step to do will be let's go ahead and
create some score tracking
so uh score is obviously equal to
zero when the game starts and score is
going to
score is going to also get reset to zero
when we hit the restart button and then
i think in check collisions
right we already have a we already have
a function that checks
the
uh
whether or not something has collided
and that's also when we want the score
to update
so this is pretty cool we'll
just kind of
leach off of that function and add some
score calculations onto it and we just
need to make sure where we call that
function
which was
that was check game over there was
ball x direction yeah okay
and then score
and then we also need to pass it in but
then we should draw that onto the screen
so let's go to where we draw our
rectangles and everything and let's
steal this two lines where we
define
font and everything
and let's just call this score text
right
score
and for the text let's uh say score
colon
and then um let's add
string
str
conversion of the score you could use an
f string if you want
um that's just formatting everything
inside of it to be a string that's kind
of a shortcut you can do that if you
want
but if you're doing stuff like that you
know plenty about python you don't need
to be here at all so
let's go ahead and see how that looks
just kind of slapping it on the screen
here
score oh we got three there and we got
seven there
okay let's take a look at why
that's updating by three or four
if ball
collides with the player ball x
direction equals one
why don't we say and
and ball x
direction
is
equal to negative one
meaning it was moving to the left and
then here we'll say and it equals
positive one
and this hopefully will act kind of like
a one shot and now that code will only
happen once
so that appears to be doing it um i'm
guessing just like the computer is able
to scan so many times
that it kind of viewed like two or three
different scans
where the ball was still colliding with
the player even though it was changing
direction
so it's just a symptom of computers
being very fast sometimes you want to
build in a little extra check that will
keep track of
things like that
okay
you