This video is the full 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 part 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 part 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 part 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!
part 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!
part 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!
00:00 Intro 00:40 Set Up PyGame 6:10 Draw the Game Pieces 11:00 Add Player Control 16:03 Add AI Ball tracking 19:58 Add Ball Movement 28:35 Add Collision With Paddles 34:55 Create Game Over Conditions 41:52 Add Restart Code 47:00 Add Score Tracking 49:30 Increasing Difficulty 52:38 Extras and Add-Ons 55:50 Goodbye
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
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 players 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 sdw 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 0 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
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 going to be two equal signs
pie game dot key up there we go and if
it equals k up i'll go ahead and copy
the kw and chaos i'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 gonna 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 one 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 gonna 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 rad
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
uh so let's see for that
um i think we should make the computers
location uh the next thing that we
update
so
we're gonna 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
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 kind of write
the last line
as well so
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 codes 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
um 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 five 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
going to handle it
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 gonna 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
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
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
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 1 and x is greater
than zero
then the ball is going to move to the
left
by the speed
um
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
you can actually
update uh direction just by multiplying
times
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
uh 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
balls speed in the y direction different
than this b 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 at 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 uh extremities in the uh 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
alrighty 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
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
um 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
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
so
what we're going to do is in the event
handler
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 uh 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
ball 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
direct
while writing 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
oh 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 pedal
and it just
keeps bouncing into it
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
got 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 gonna 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
um so what i want to do next
before paying 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
um 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 uh
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
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 uh
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
it's just better to catch it this way so
if it's less than or equal to zero
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
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 games 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 um
nothing should happen
uh 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
let's handle that
and
let's make a
let's make another if statement and say
what we want to do if game over is true
and uh we're going to
create um
some text now that i will say is game
over text
and uh 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
uh okay so font equals pie game dot font
dot
font
and then
you can use any font you want a lot of
times using a fun font uh 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 with
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
um
and then you give it coordinates for
where to draw it on to 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 so uh 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 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
ah 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.draw.rect
and we're going to put it right at the
restart text and we're gonna 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 um
we want to put on the screen
we want it 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 100
long and
let's just go ahead and say that it's
20 tall
but the idea is if you're clicking
anywhere in
uh 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 bout 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 uh 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 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 got to 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
uh 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 um 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
um 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
say score
colon
and then
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 uh keep track of
things like that
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
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
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
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 ten as your score
um so this way the ball will go faster
in the x direction
um
every
ten 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'd 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
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
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 uh 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
um 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 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
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
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
fall 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
um let's go ahead i'm gonna just start
out by
increasing the ball's
initial speed to 3 and then 2 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 um
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
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
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 the computers 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