Python Pygame Tutorial - Episode 3! Creating and Tracking Score, and Drawing Text Onto Screens!

LeMaster Tech
183 followers
14 Video Views·Apr 23, 2025
Learn to develop video games using python and pygame!! This is episode 3 in an intro to Pygame series from LeMaster Tech. This episode covers creating and tracking a score variable and defining and drawing text into a game in pygame!
Be sure to check out our channel for tons of additional python tutorials and practice problems and be sure to like and subscribe if you find our content useful!!
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
If you want to see anything specific on the channel or have technical questions about anything leave a comment on our of our videos and we will get back as soon as we can!
Thanks for watching and supporting the channel!
hello and welcome back to the channel
today's video will be part three in our
um pygame
tutorial series
and uh if you haven't watched the
previous two videos we are going to be
starting with code that we already wrote
in those two videos
so
i recommend
going back and checking those out or at
least figuring out how to create your
own
bouncing object if you'd like to follow
along with this tutorial
otherwise if you're just here to
see the concepts that we talk about in
the tutorial then uh that's fine too
let's go ahead and get right into it
so last time we got to this point where
we have a ball that we're drawing we're
drawing on screen and we set up the code
so that it bounces
off of the walls whenever it hits stuff
and we're going to go ahead and in this
video we're going to create a score
value and we're going to update it so
that our score
increases every time the ball hits a
wall
and ultimately what this game is
supposed to be is like a dodging game
where the player once we create the
player
the player is going to try to avoid the
ball and every time the ball hits a wall
it's gonna count the score up so
let's go ahead and create a score
um
let's start by creating a score value
which is going to be zero the variable
but then also um what we need to
do is is define something in pi game to
be able to put text on the screen we
actually have to define a font
so um
we're going to say that our font is
equal to
pi games built in pi game dot font dot
capital f font
and then this needs a couple arguments
um
it needs the name of the font so i'm
gonna say
freesandbold.ttf that's one of the ones
that
um is just built into most python
installs although you do have to spell
it correctly
and then a font size and we'll say 20.
and so this is um this is
kind of weird but uh in pie game there's
no way to actually put text directly on
the screen you have to define it as a
font and then you have to do what's
called rendering the text and then um
and then you actually draw it onto the
screen so
um it uses the pygame.blit function
which we'll see in a little bit
but i don't really want this to be any
of these bright neon colors we've been
using the red blue and green so i'm
gonna go ahead and make white and black
real quick and white is two five five
two five five zero
and black is
zero zero zero and i think this would
look good as white text on a black
background so
um so okay this score um starting out at
zero well we already created code that
checks if the ball is hitting a wall so
what we have to do is add the global
we have to add the global
variable
score inside of our update ball
function and anytime we change direction
that's how we know that the
um
score should be incremented because that
means the ball just hit a wall so we're
gonna do this score incrementer add one
to the score
every time the ball hits a wall so north
wall south wall east wall and west wall
um
are all going to count as one point for
the score and the important thing to
understand in this video is it doesn't
matter
what your event that adds one to the
score if it's killing an enemy if it's
jumping over an obstacle if it's
whatever your score incrementer is
collecting coins
you put score increment code
at the same point as the event that
causes that so that's what we're doing
here just because the ball changing
direction is us adding one to the score
and then we come down to where we draw
our game screen
and this is where we do the
display score
so now we're going to take
display score
now we are going to
use font dot render so this is pi game's
built-in um
rendering tool for putting text onto a
screen
once you've defined a font and we'll do
font dot render and then the caption
that we want is score and then a space
plus
the string conversion of our score
variable
so what we should see on screen is the
word score colon space and then the
number of the actual score
and then it asks for
whether it's gonna be aliased or not
i've always put a true for that i don't
actually
know what happens if you put false i've
always put true
and then it'll ask for text color and
background color and since we defined
white and black we'll throw those in
and then you use like i said before
screen dot blit and blit is just sort of
pie games um it it stands for block
transfer
i believe but it's pie games like
actually drawing it onto the screen
function so you have to do that anytime
you want to put something on the screen
and let's say we want to blit display
score and we want 10
and 10. so that's positional um actually
this needs to be a tuple
but these are coordinates of where it's
going to draw it on
so
excuse me
let's take a look if we caught
everything here when we run this if we
have a score
in the top left
yep that counts up every time the ball
hits a wall
so this is going to seem pretty
pointless and you can tell right now if
we just let this run for forever
the score is going to increment for
forever
um
because we haven't created the game over
conditions the actual like fail
conditions yet um because we haven't
created a player yet
but this is kind of important to create
the framework around your game before
you start creating the functional
playing of the game
and something else to understand about
pygame some people start from the player
and build the world around the player
some people create the world
and then just insert the player into it
both are totally valid excuse me
both are totally valid there's no
correct way and incorrect way of
creating a game if at the end of the day
it's fun to play and it works
so this is just how i went about
creating this particular game so
real quick this video is not too long
yet i'll add a few things in here that i
just think would be kind of cool
we're going to go ahead and draw another
circle inside of that first circle
and i'm going to make this guy
red and i'm going to put it just inside
the circle and i'm actually going to
fill it in it's not going to have a line
width
and what i'm doing here is i'm creating
the object i'm just making a little more
interesting looking i'm drawing a red
circle after i draw the green circle
but i'm using the same central
coordinates
so now when i run this you should see
yeah the ball is much more vibrant i
mean it really stands out at you it's
not just an open circle
um
you know that that sort of thing is kind
of cool
obviously
you can just create
various colors with various rgb values
and play around with the aesthetics it
doesn't just have to be red green and
blue you can mess with all of the rgb
colors
um
but so that's that's kind of a nice
feature
one thing that you could do if you're if
you're just tweaking the game and you
don't really love the way it's playing
you can keep messing around with the
speeds
um just by changing the x and y
direction
and
again i know i've mentioned this in
previous videos when i do this run it
runs pretty smooth on my computer if
you're following along with the code it
should be running pretty smooth on yours
the screen capture software really only
catches like 20 fps
but so that's it for this one we added a
score parameter we went over how to put
text on the screen
and uh stay tuned for future videos
we'll get into adding the player adding
game over conditions um creating buttons
and
text events and
we'll go through a lot more so for now
if you have any specific questions about
what you saw here today or you'd like to
see anything in particular in the future
be sure to let me know in the comments
and
as always thanks for checking out the
channel and good luck with your code
thanks bye
