26 Video Views·Apr 22, 2025
This video is part one 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 this episode specifically 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
so we're going to try to move pretty
quick and fit this whole game into one
video here so if you're unfamiliar with
python in general i recommend you start
with one of the very intro series that's
on this channel and if you like this and
you want to see more content like this
be sure to leave a like and a subscribe
to the channel and helps me out a ton
and without any further ado let's get
right into it
so uh with any game in python
if you're going to use pie game the
first thing you're going to want to do
is import pie game
this should be pretty familiar and then
um
the second line is just pie game dot in
it so you're actually calling the pie
game function
starting the whole module and then
really the the next thing you're going
to want to do is define your screen
uh you can't have a game without a
screen so that's going to be
screen equals pi game dot
display
dot set
mode and then you define a size in
pixels and put those inside of a
square bracket
okay
so now we've just created a screen
that's going to be 300 by 300 you can
play around the settings if you want
i i've made this before making this
video and 300 by 300 works pretty good
for pong but hey you can do whatever you
like
so then uh pygame dot display dot
set caption i like to do this second um
because you're just saying what you're
going to uh call the game so we will
just call it
pong
reboot that's pretty sweet and then
let's see
something i do in every game is i create
a timer because um if you watched the
first pygame tutorial series on this
channel i talked about this in more
depth but essentially
your game will run or could be limited
by the hardware that you're playing it
on unless you define an fps to play it
at
like a frame rate
and so if you want to do something where
it's running at a fixed fps like 60
frames is pretty common
then you need to go ahead and create
yourself a timer early
in your code and then also just create a
frame rate variable
60 30 whatever you want it to be
and we'll go ahead and use that pretty
soon
so then inside your
code you create your actual like game
loop which is going to be
create a variable i usually use running
almost always use running
and then while running is true
or you can just say while running if
you're using a boolean variable
so while running is true or equal to one
or whatever you want
to call it then we're going to go ahead
and do timer.tick at frame rate and so
python now knows that it's going to run
at that frame rate that's how many times
it's going to run per second
that's the built-in clock function
so that's pretty sweet this is our main
game loop and then you have to create
an event to get out of the loop right
because right now this looks like an
infinite loop there's no way for running
to ever
go false so
you create the event handler which is
going to start with for event in
pygame dot event dot get so that's a
generic pi game
function for grabbing any keystroke any
click any movement of the mouse that's
all going to be handled in the event
handler here but then the first one you
just want to make is
if
event dot type
is equal to and we will use the pie game
built in quit because every time it
draws a screen there's a small red x in
the top right corner and that's the quit
button
and so if they click that then we are
going to set running
equal to false
and then
that's all we're going to put in the
event for now in the event handler and
then outside the event handler let's go
ahead and put in
excuse me
pygame dot display dot
flip
and uh pygame dot quit and that's just
kind of um last stages the flip is is
how you uh draw everything onto the
screen
but let's go ahead and draw a background
first so um just just to have something
let's do screen dot fill
and let's put 0 0 0 in there so that's
an rgb value
and let's just see that might be all we
have to do just to get a screen to pop
up
fill invalid wrecked style object
let's take a look here
it looked like it worked
um
this might need to be in another set of
parentheses
i don't know it's just an rgb value
that looks okay all right i'm not sure
why we needed the second set of
parentheses but we did so put them in
there the reason um that took me a sec i
usually like to define some colors up
top so i can use those throughout and
just call them black white purple
whatever
so i'm going to make those now and
i recommend typically when you're
creating a game build yourself a library
of colors you want to use
create a font that's what i'll do next
here but then anytime you need a color
rather than rgb and trying to play
around with variables
you have a library and so if you really
want something to be a specific color
then just play around with the rgb
values but play around with them in your
variable definitions up here
okay so uh we drew the screen and that's
pretty sweet why don't we go ahead and
for the next thing um let's draw the
player and the uh like computerized
right paddle so we'll have the player on
the left and the computer on the right
for this game
uh all right so
let's go ahead right under where we did
screen dot phil and let's create a
rectangle that we'll call the player and
the player's location is going to be
equal to
pygame.draw.rect
and then
we're going to put it on the screen we
want to be a white rectangle and now we
give it the four arguments needed for a
pi game rectangle so this is going to be
let's make it five wide and then let's
make the height
or i'm sorry this we're giving it um
x and y coordinates first
so because in the game what you're
actually controlling when you move up
and down is your y coordinate we're
gonna uh create a new variable called
player y and that's what we'll control
but then for the size of the rectangle
let's go ahead and make it uh 10 and 40.
so you can play around with this after
you draw it onto the screen and kind of
figure out what works for you and if you
want change it
i'm going to create a little section
here that i'm going to call
game variables
and so we need player y
and let's go ahead and start with that
equal to
130 okay
so if i just go ahead and run this you
can see our
character yeah so i think that looks
decent for a paddle kind of retro
um
which is really what we're going for
anyways we're just trying to create a
one for one of pong we don't need to
make it super cool or advanced uh let's
just get it working first
so second thing to do will be draw the
computer paddle
uh you could make this two player i
don't have the you know mental capacity
to play both of these on a tutorial at
the same time but you could make one for
example that uses q and a or w and s to
go up and down and the other one uses
the arrow keys and two people could sit
at one laptop and play the game
but for now i'm gonna make it a
computerized opponent plus that will
give everybody a little bit of practice
um
drawing
a or
creating code as kind of an ai to play
against which is great practice as well
to think about how a computer would
handle the situation
and then so instead of five we want to
put this on the other side of the screen
so it's going to be at
285 right because it's 10 wide and we
want to be five away from the right just
like the other one okay
so when we run it we have player on the
left and computer on the right and it's
starting to kind of look like pong so
obviously one piece that's missing right
is the ball which you could make um
you could make it a circle
definitely if you want that's more like
an actual ball but since i'm going for
classic pong i'm gonna make it a real
little rectangle because that is what
the first pawn game looked like it was
just a little pixel
or a few pixels but you get it
you get it
okay so for
this ball we're going to draw it onto
the screen again
and uh let's go ahead and make it white
just like the player and everything else
and
i'm just uh you know in the game white i
don't know what color the player is
that's not important
okay so uh sorry bad joke
now let's go ahead and the ball is
actually going to be moving in the x and
the y direction right
so let's make those variables and uh
just to start
uh let's make it
10 and 10. i think that'll be a good
size but if that looks too big or
something we can change it later
but okay so now we need to create the
ball x and we'll put them pretty much
dead in the center of the screen
so that'll be 145 since this is
referring to their coordinates in the
top left as you look at the object and
they're 10 by 10 so if we want to be in
the middle uh
we go five off so there you can go
that's kind of um how you want the game
to look when it starts up
and that's what we've got here so uh
that's pretty cool let's keep going now
first thing i think we should do is
handle the player movement uh because
it's it's not going to be too hard and
if we start coding
the computers and the balls movement and
the player is powerless to do anything
then that just seems kind of weird to me
so let's go ahead and get into some more
event handling now um
and again if you're following along uh
you can use whatever keys you want for
up and down you could use the up and
down arrow keys that would make a lot of
sense um
i've played a lot of the keyboard games
that like a s d w are your four
directional controls and i just like
controlling the movement with my left
hand
so i'm gonna go with w and s for up and
down um hopefully that makes sense but
event dot
key
but you can do it however you want you
can follow the same format and then
obviously you're just gonna instead of
here where i say if event dot key equals
uh pygame dot and then k underscore w
you would use k underscore up arrow k
underscore e whatever you want um just
change the key out there
and uh if that is
if that is pressed then we are going to
want to
move down so i will
make a variable that i'm going to call
player y direction actually i can just
say player direction because we don't
move in the x and the y
so i'll just say player direction
is going to be equal to negative one
and let's go ahead while we're here and
let's do the opposite
and so uh you may be thinking like hang
on if i hit w i want that to be up if i
hit s i want that to be down yes that
does make sense except that it doesn't
because pi game
starts from 0 0 in the top left corner
and uh
it goes higher numbers as you go down so
um if you think about like your y
coordinate if you want to move up like
go from the middle of the screen to the
top you're going to be at like 150 as
your coordinate in the middle of the
screen but then zero at the top so move
up your direction is actually negative
one doesn't make a lot of sense but hey
i didn't make it
okay
so k underscore s is going to be
player
direction equal to one
all right and so now what we want to do
is we want to make sure that we set the
direction
equal to zero initially
because we do not want
the player to just start out moving
and then we also want the direction to
go back to zero if
they um lift off the key so let's see
if event type is equal to pi game dot
and now this is going to be key up
uh
it's got to be two equal signs
pi game dot key up there we go
and if it equals k up i'll go ahead and
copy the kw and chaos that'll save us
some typing time
and we're gonna set equal to zero and so
if you think about this if you hold w
down or you hold s down you want the
direction to stay up or down the whole
time but as soon as you let go of that
key you want direction to go back to
zero
so
that's pretty cool that'll do it and
then what we're going to do with that is
actually
let's go ahead and update the player y
variable
right so player
y
plus equals
and now um let's just say that the speed
is one like one pixel per counter which
is going to be a total of 60 pixels per
second
so
let's say it's one times
the player direction right so this is
why we handled it that way when
direction is zero your y doesn't change
at all but then when the direction is
negative you go up towards the top of
the screen when your direction is
positive you go down towards the bottom
of the screen so let's run that
and yeah so i'm holding w and then when
i let go it stops and i can go up and
down and what you'll see is like the
paddle moves very slowly so this is kind
of the first way you can start thinking
about potentially increasing or
decreasing
difficulty is with player speed so i'm
going to create a variable i'm going to
call it player speed and i'm going to
make it a little bit faster
than one i'm gonna make it i think three
uh now let's go five three is still a
little boring five is totally red
totally rad
yeah so i mean i'm flying now uh i mean
the idea of pong is that like it starts
off pretty slow but as the game goes on
it can get more progressively difficult
and uh obviously we can do a lot of
things with it today that you couldn't
have done back in 72 when it was made
but this is pretty sweet right here
we've got the up and down controls um
all set so uh next thing i want to do is
get the
uh
computer to track the ball and then i
want to get the ball moving
you