In this quick and easy tutorial we go over all the basics of using text and custom fonts in Pygame games with python code!! The most important part of using custom fonts is finding the .ttf font files that you want to use and getting them added to your project! From there, using the fonts in the games is pretty easy and straightforward once you get used to it!
Be sure to leave a like and subscribe for tons more great content in the future and let me know in the comments what you thought of this and what you want to see more of in the future!!
Thanks for watching and goodluck with your future projects!
what's up you guys welcome back to the
channel in today's video I'm going to
show you how to incorporate custom fonts
and text into your Pi game games
[Music]
so this is going to be a quick and easy
tutorial because I've been moving across
the country I'm still not completely
unpacked I moved from Pittsburgh
Pennsylvania to Colorado
so I am a little bit all over the place
but I wanted to keep bringing you
content on the channel so I thought I'd
go back to basics I've actually had a
lot of questions recently on what to do
if your text isn't working or you want a
different type of font in your Pi game
games so we're going to cover that in
this video I'll quickly discuss to you
what I have already on screen is
basically the bare minimum to have a pie
game game up and working so you want to
start by importing the pi game module
you can run pip install pygame if you
don't have it installed yet and then do
pygame.in it set up a clock and an FPS
just like you see here and then you're
going to use those inside this main game
Loop to just control the speed of your
game and then for this tutorial all I
need to you set up in the beginning
other than that is a font which we'll be
talking a lot in depth once I discuss
just this shell and then the screen you
just say
pygame.display.set mode and then in
square brackets give it width and height
and then inside the main game Loop I've
filled the screen with a background
color just so it'd be clear what was
going on and then I made text which
again we're going to talk about these
lines a lot so don't worry about it and
I just have the bare minimum of event
handling that just basically says if I
press the X in the pop-up window when I
load the game in it'll allow me to quit
and close the program so my computer
doesn't crash
so these 20 lines of code you see here
are really the bare minimum shell to
create a pygame window and just to show
you what's going on when I boot this up
it's just a very plain 800 by 500 screen
with check out this text on the screen
okay so now let's talk about actually
where that text comes from and how to
change the font that you're are using or
have multiple fonts so to set up a font
when you're creating a pi game game it's
this pygame dot font dot font with a
capital f where you actually initialize
it in the beginning so you could call
this my font or variable font or just
font is fine and then you want to point
it at a ttf file this is the most
important part of the tutorial
a lot of my videos I'll use freesands
bold.ttf which you see on the screen now
because most Windows applications and I
believe uh Mac and iOS applications have
freesands bold available on the system
by default so this should work for most
people most of the time while they're
building and designing a game
but as you saw in that window it's not
the coolest looking text it's very
generic so you might not want to build
out your entire game with freestands
bold
so uh for this tutorial I have two ttf
files that I got from the internet and
if you don't know where you get custom
fonts or new fonts um or and you don't
want to create one from scratch which is
totally reasonable
check out da font
d-a-f-o-n-t.com I get a lot of mine from
there they're not a sponsor I don't have
a partnership with them or anything it's
just thousands of fonts available for
free and they'll specify on there
whether it's a free download for
personal use or it'll say a hundred
percent free meaning you can incorporate
into games and apps and you don't have
to pay royalties or anything like that
so if you're trying to develop something
for a profit or commercial release make
sure you grab ones that say 100 free not
just free for personal use so you don't
have to get in trouble like paying
royalties anyways that's a long way of
saying however you go about it you need
to end up with this dot ttf file it's
like true type or text some true text
file I don't remember exactly what tdf
stands for but that's the file you need
you don't have to put it in a fonts
folder you could drop it straight into
your
game structure right underneath just the
fonts tutorial folder it's a little bit
cleaner to put your assets in subfolders
which is why I have the fonts folder
but basically in pi game games there are
a total of three steps to get text on
the screen it's a little bit complicated
compared to how simple and easy to use
pygame usually is in Python but
basically you have to Define your font
in the very beginning and you give it
two arguments you give it the font file
and then you give it a text size so this
is font size 52. I want it pretty big
for this tutorial but you can play
around with that as much as you want you
could even make this a variable and so
you could call this like font size and
then just every time you were to use the
the text in your game you could Define
font size or change the value of font
size right before that text so this is
just the ttf file and your font size but
then when you get to the point where you
actually want draw it on the screen
you make another variable call it my
text or my string or text or whatever
and you do font dot render and this is
font or whatever variable name you put
here so if you call this my font this
would be my font dot render and then you
give this one three arguments the first
one should be the actual text that you
want to draw on the screen so if you
want to include variables in this you
would just make it an F string and you
would put your variables you know inside
curly brackets or if it's just a string
then you just put the string there you
can put a variable here as long as it's
in string format so the first argument
is a string and then the second argument
is anti-alias which basically is a true
or false and as I understand it you just
put true and all of your lines will be
smoother so I always put true I don't
know a scenario where you'd want that to
be false so just put true for now and if
you you're in a scenario where false
makes it look better go for it and then
the third one is color pygame has a lot
of colors built in which I didn't
originally know when I started making
pie game tutorials some of my great
YouTube viewers point that out to me but
so for this uh tutorial because I have
an orange background and because it's
Halloween uh no it's it's October which
is Halloween month for those watching in
the future we'll do black text on an
orange background it'll be fun and
seasonal
um and then the last part in getting
text actually on the screen is this
screen dot blit where your block
transfer or actually drawing the text
onto the screen so this is the most
important part because if you just do
these first two steps it won't appear
anywhere so you'll have defined and
rendered your font but it won't appear
anywhere so you do screen.blit and then
you tell it what rendered font to draw
onto the screen what object and then you
give an x and y coordinate okay so
that's everything required to put text
on the screen which is how we end up
with check out this text at 100 pixels
over from the left and then 150 pixels
down from the top
um but the purpose of this video is what
if we want to change this font and it's
actually really easy so if you have a
ttf file in your project directory
somewhere all you need to do is when you
do this font.render in the very
beginning here you just need to make
sure you point it to the font that you
want it to be so I'm going to point to
font1.ttf I'll leave the font size at 52
and I'm going to just leave it in the
position it already was in but look
without changing anything else other
than actually that's not going to work
because we need to include the folder so
it needs to be fonts forward slash
font1.ttf so if I had this file up one
level in my folder structure that would
have worked but I put it in a special
um fonts folder so now
we'll see check out this text and this
was a cool kind of star warsy Vibe one
that I used in a uh platformer tutorial
that's also on the channel
um and I really like the style of this
text so uh that's my first font but
let's say you wanted two different fonts
in your game because you had like an
instructions font that was like a little
bit easier to read and a little bit
smaller and then you had like a title
font or a menu font it's really easy to
have multiple fonts you can have as many
fonts as you want sometimes you want
them just to be separated by font size
so you would do like Big Font little
font medium font
and so uh to do that you just say font 2
equals and then point it at the ttf file
you want and I called mine font 2.
and I'll give it a different font size
so you can kind of see that and then we
also need these two same sorts of lines
where we Define the text so we'll call
this one my text too and this is going
to be font two because remember it's got
to be whatever that variable name is
dot render and then we'll change it up a
little bit we'll say check out this text
as well we'll still have it be black and
then we'll move this one a little bit
left just so it has a different X and Y
start position so you can kind of
see how this works
and so this needs to be my text to but
just like this we have a second string
with us with a different font that's
actually uh that font size is a little
too big
um check out this text as well let's
make that one 36 just so it's
um just so I think the whole thing will
show up on the screen this way
okay so you can see this one's kind of
um I think squid games based uh off of
the Netflix show squid game but these
are just two kind of fun fonts you might
use this one sparingly because it's sort
of hard to read but hopefully this
tutorial is what you're looking for if
you clicked on this video and that's a
basic introduction of how to use
multiple fonts custom fonts and sort of
how text works in pi game games I hope
to get back to some long form tutorials
and sort of start branching out the
channel as I get settled in in Colorado
but until then if you like this video be
sure to leave a like subscribe to the
channel for tons more content and let me
know in the comments below what you
thought of this video and what you're
hoping to see more of in the future as
always good luck with your code and
thanks for watching until next time
thanks bye