14 Video Views·Apr 22, 2025
This video is part five 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
okay so uh yeah we're getting up there
in time on this video so let's just add
a few things
that will make this a better game
and uh for
the ball i think well one we don't
really need this ball x direction y
direction stuff anymore because it's
really all
um
based on colliding with the
with the paddles but we're going to
leave that in there
but one thing we do want to do is this
ball speed in the x and y direction
right
we have um we just start them out as two
and one but we don't do anything with
them from there and i think it would be
fun if it got harder as it went up
as the game went on so let's go ahead
and do
ball speed so this is in the x direction
right
and this is going to be equal to
let's say
2 which is what it starts at but then
let's add score
and let's use this function that's
called floor division
so um because we're updating something
in pixels and it needs to be an integer
this is always going to divide down to
the lowest whole number so this won't be
a one it'll be a zero until you're up to
the value of 10 as your score
so this way the ball will go faster in
the x direction
every
10 you know it'll go one faster and then
in the y direction
let's go ahead and say
that it equals to 1 because that's what
we started at right
and then
every 15 it'll go one faster so i'm just
winging it here but that seems like that
would be a good adjuster so let's see if
i can get a decent score going here
might actually start the ball a little
bit faster just because the game is
pretty easy to start but i mean that's
how games are supposed to be
so let's see if i can get up to like 15
or 30 here um
a big part of me being able to display
this is being able to play the game
decently
but you can kind of tell when we hit 10
it got a little faster left to right and
now we are above 15 and it definitely
sped up up and down we should get the
next yeah so it just sped up again in
the x direction
and here if i can get another six points
it'll speed up in the y direction again
and it's actually already getting pretty
tricky i'm definitely slightly better at
writing code than i am at playing video
games
but
uh you can probably tell i know the
screen capture isn't exactly at 60
frames per second so it might look a
little choppy but this game got really
fast you actually can see there i i won
the game um i beat the right player so
that's pretty sweet um what we had there
is we just had the net speed get higher
than the ai was able to move
so it's because we built a very simple
ai and so the speed that we give it in
the x and y direction is going to limit
when it can finish
so if you have watched this long in this
series of tutorials you definitely
deserve an easter egg or something fun
to reward you for your patience
so since now we have this fully
functioning game with restart logic you
play against computer let's just do
something fun to make this a little bit
unique it is 2021 uh or later it is not
1972.
and so let's just go ahead and do
something kind of fun where every time
the score increments
um let's uh let's mess with the ball
color too
so
um
actually we don't even need to pass it
in
we are just going to return another
variable that i'll call ball color and
every time the score increments
let's go ahead and say
ball color
equals
and let's
grab three random integers between zero
two five five and so you will need to
import the random module
um
to do this but
now i've just said okay well when the
score updates i'm just gonna generate a
new color with three random rgb values
so it could literally be anything it
could be terrible um
like if it is black on a black
background that's obviously not ideal
but uh those odds are pretty slim
so uh now let's see we've got this ball
color variable
and
towards the beginning because it does
have to
it does have to exist initially for us
to use this
let's say the ball starts as white
and
let's go ahead down to where we draw the
ball
ball equals here and instead of it just
being constantly white let's make it
equal to ball color
all right and that's probably gonna do
it um let's go ahead and run this and
see if we get a cool color changing ball
local variable ball color reference
before assignment yes
of course it is because if you're not
going to pass it in as a variable
then you do need to tell them
that you are referencing the global so
it has something to refer to and say
okay this is an existing variable
alrighty
so we've done some too many values to
unpack in
ball x direction score
oh yeah i don't think we're grabbing it
back from there yet
there we go
okay
alrighty so you can tell i'm getting a
little tired okay but the
ball changed color that was pretty sweet
let's go ahead i'm gonna just start out
by
increasing the ball's
initial speed to three and then two in
the y direction
and i'll make sure i do that in the
restart logic as well
but you can just tell like the game
starts out a little too easy which is
definitely something like you can do
if you want
but
three plus two there we go
um and let's take a look at this yeah so
it just starts out harder um i mean if
you're a fan of challenging games i
think this is a little more fun uh
you can tell that the colors are
changing and i think that's just kind of
fun it reminds me of like a loading
screen
um
but this is pretty sweet like we just
built of a full video game that was at
one point the height of video games that
people had to play which explains why
like in the 70s everyone wanted to nuke
everyone and stuff
um
but uh i'm you know i i'm pretty pleased
with this i think it's super fun uh i
one way that you could make it to where
like the computer is quite a bit better
and you're more challenged is you could
just up the computer speed you can mess
with colors you can mess with power ups
you can mess with anything you want this
is the root code of how to do it
hopefully you found this useful i know
this was quite a long tutorial but this
really is start to finish how to build
this entire game i had a ton of fun
doing it hopefully you found it useful
if you did if you're still here thanks
for watching the whole thing
if you uh would do me the favor of
smashing that like button subscribe to
the channel it would help me out a ton
and i really appreciate your time i hope
you had fun doing this and if there's
anything you want to see in the future
be sure to let me know about in the
comments below
and i'll get back to you as soon as
possible
really appreciate your support on the
channel and as always good luck with
your code and thanks for watching thanks
bye