In this video I show you how to animate text messages in python code to have an animated typewriter effect!
This means the text appears over a period of time where one additional character appears at a time based on the adjustable speed we give the program! We then look at how to cycle through a list of multiple messages and reset the scrolling typewriter effect!
This is really useful for back and forth dialogue in games, as well as instructions or menus! This is the basic way text is animated in games like Zelda and Pokémon and is a great and easy way to add spice to your games!
If you liked this video be sure to leave a like and subscribe to the channel for tons more great content! Let me know in the comments what you thought, if you have any questions, and what you'd like to see next on the channel!
Thanks for watching and goodluck with your code!
what's up you guys welcome back to the
channel and today I'm going to show you
how to have scrolling typewriter effect
messages in your games in Python using
[Music]
pame all right so let's get right into
it there's a lot of situations where you
could be creating games and you want
kind of that uh Pokemon style of like
you approach a character you trigger
something in your code and then you want
a message to show and it kind of does
like a type writer scrolly effect that's
sort of like they're talking to you
right so the text kind of appears one
word one character at a time there's a
really cool pretty easy way to do that
in py game uh using python so let's go
ahead and get into it as always anytime
you're using the pame module make sure
to import it uh you might have to run
pip to install it if you don't have it
already and then go ahead and do py
game. init just like that and then a few
things we're going to set up right in
the beginning this should be familiar to
anyone who's watched some of the
tutorials on the channel before set up a
p game uh game one thing we want to do
right in the beginning especially since
this is a tutorial on text is uh Define
a font and I'm going to use free Sands
bold.ttf with a font size of 24 just cuz
it's a built-in font almost all of you
will have it with your installation um
and 24 is a good size for Visible text
but not
overpowering and then we're just going
to set up a screen I'm not going to
worry about like defining width and
height separately just for this tutorial
because we're not playing around with it
or anything like that so just do py
game. display. set mode and then square
brackets and just give it a width and a
height I'm going to use 800 500 just a
simple popup rectangle and then we're
going to define a timer because we're
going to use that as our clock to
control the speed which will then
control how many characters show on
screen and how fast the next character
pops up um and then for now I'm just
going to make a message that's just uh
we'll show how to kind of cycle through
messages at at the end of the tutorial
but I'll just say check out this sweet
message and that'll be the the message
that we use uh for this tutorial and
then I'm going to go ahead and say that
my snip so what I show on the screen
well right in the beginning we want it
to be um empty so to do this we'll say
font. render and then an empty string
and then true and then white and this
little snip is what we'll be taking out
of the message as we scroll through it
so we'll have a counter um so to start
off the counter will be at zero and then
we'll have a SC like a text speed so to
start out I'm going to use three and uh
we're just going to keep track of
whether or not the full message is done
or not okay and so uh these sort of
things are all we have to do in our
initial setup and now everything else is
going to be inside of the game Loop so
that brings us to the main game Loop and
we just want to create a variable that
I'll call run you could call running or
active or whatever and set it equal to
true and then say while run so now we
have a while loop which is basically an
infinite Loop until something gets us
out of that Loop um and so inside of
this we want a screen dot fill and I'm
just going to fill with dark gray this
is where you'd put like your game
background or your map or
whatever and then timer. tick at a frame
rate I'm just going to use 60 you could
create a frame rate variable and then
I'm going to create kind of a spot for
our text to go so this would be like the
inventory or the text bubble or whatever
in your game um and I'm just going to
make it a black rectangle on the bottom
of the screen so to do that we're going
to give it four arguments we're going to
give it an X and Y start position 0300
is down um and all the way to the left
and then we'll make it the full width of
the screen and then 200 so it'll stretch
to the bottom you don't have to worry
about this rectangle too much and what
we're doing here just know it's a
default space to kind of differ
differentiate where I want to put the
text um and now let's come down below
this and let's handle how to get out of
this infinite wall Loop so we're going
to create some event handling and we
want to do for event in py game. event.
get just like this and then we want to
say if event. type is equal to py
game. quit all caps like that and if
that happens then we want to set run
equal to false and then we just want
want to uh do py game. display. flip
right down here at the end and then py
game. quit and so this display. flip is
what actually like throws all of our
stuff onto the screen so that you can
see it and then py game. quit outside of
the while loop means if somehow run
becomes false then it's time to just
quit our application so uh that's pretty
straightforward if I boot this up now
what you're going to see is just a box
so you can see this obviously isn't much
of a game but uh that's okay because I'm
here to show you how to do a very
specific thing um and what I'm going to
do now is I'm going to show you how to
get the text scrolling down here in the
bottom part so whatever your game is
whatever your application is um thank
you for bearing with me on setting up
the basics of Pi game now let's go ahead
and take a look at how we're going to do
this so we created that variable counter
which is a zero in the beginning right
now what I want to do is I want to check
and see if the counter is less than
speed times the length of our message
okay so the message the length of our
message is how many total characters are
on the screen the speed is going to be
how long it takes for the next character
to get written on the screen and so this
is basically saying have we written the
whole message onto the screen yet or not
and so if we haven't then we want to add
one to the counter and then we'll do
kind of an L if and say l if counter is
greater than or equal to the speed times
length of message then what we want to
do is we want to take our done variable
and set it equal to true and uh this is
um going to kind of matter more when we
have multiple messages and I show you
how to cycle through them but let's go
ahead and take a look at how to use that
counter and that speed to typewrite text
so snip equals font. render and if you
remember how we set this up in the
beginning snip equals font. render we
just had it as an empty string now what
we're going to do is we're going to say
okay actually take a piece of that
message and go from zero so the
beginning to the counter floor divided
by the speed so floor division basically
will uh divide by a number and round
down to the next whole number so you may
be thinking something that's a little
counterintuitive here is uh as you
increase the speed speed you'll actually
be slowing down the speed at which the
next character um gets drawn on so what
you could do if it makes more sense to
you is you could just do one divided by
speed and this way you'd be getting um a
faster you'd be getting a faster
increase as um as your speed were to
increase um basically making it in
inverse but uh I I'm just going to leave
it this way and if you have questions
about that just just let me know in the
comments below um I'm going to try not
to confuse you too much and I'll show
what I'm talking about by playing around
the speed once I get this done so this
is basically taking a snip of our
message based on how long we've had the
thing booted up and then we're going to
do screen. blit the snip and so blit is
a block transfer it's actually putting
the message on the screen and we're just
going to blit the snip at 10 310 so just
inside that black rectangle that we
created and what you'll see now as I
boot this up should be check out this
sweet message all right so that works
pretty well and if I added like this is
more words like this um then check out
this sweet message this is more words
okay I think that's pretty cool now
let's see what happens if we increase
the speed by a lot so I'm going to make
it 30 what you'll notice is this is
going to slow down a ton because what
it's really doing is now every 30 scans
and remember we have 60
FPS every 30 scans it draws one more
character onto the screen so that
actually means it's only doing two
characters per second right now so now
let's go the other direction it was at
three let's go ahead and take it down to
one so this is going to be 60 characters
per second and watch how fast this
happens check out this weet see so the
screen recording is so fast that might
uh or the frame rate is only like 20 FPS
so that might have not even covered the
whole thing so so there you go at two
you could see that's really fast and so
three I actually found was a pretty
enjoyable speed at
60fps um where it's not painfully slow
but it's also not so fast that you can't
even keep up um okay now let's go ahead
and that's obviously the basics let's go
ahead and do one more cool thing let's
say we had a list of messages so instead
of message this is going to be messages
and check out this sweet message this is
more words let's go ahead and put a
second message in here that is this is
another great message and then let's go
ahead and put one third one in here
that's just going to say isn't
this isn't back space apostrophe this a
great
tutorial maybe like And
subscribe just like that it's totally
not pandering because it's part of the
tutorial all right so now let's say we
have three
messages and we want to add one new
variable down here that's going to be
active message and we'll say we want to
start on zero we want to start at the
very beginning well what we need to do
now is we have to recreate that variable
message and set it equal to messages at
active message okay so the rest of our
code can stay the same but what we're
saying now is okay well we're pulling
one message out of a list of messages
and now we're actually going to use that
done variable again so this is pretty
cool we're going to come down into our
event handling and we'll just say when
we are done with a message if we press
enter we want to go to the next message
okay so let's just add a new event type
and say if event. type is equal to pame
Dot and now all caps key down then let's
say if event. key is equal to py game.
Kore return so that's the enter key um
for a lot of older keyboards the enter
key was return so pames module it still
is return but that's the enter key and
we'll say and done and let's just check
that we're not at the last message so
active message is less than the length
of messages okay so this is basically
saying the whole message has displayed
on the screen it's not the last message
in my list and I press enter well if
that's the case then what we want to do
is just want to add one to active
message like this and then we want
to done equals false we want to reset
that done variable and then we want to
reset the message because you remember
we defined that um we defined that in
the outside world so message is going to
be equal to again messages at active
message and then we just want to reset
the counter to zero so it knows it needs
to restart and that's all we have to do
to cycle through our list of messages so
let's go ahead and check check that out
it should look pretty cool check out
this sweet message this is more words
now if I press enter bam it restarts
this is another great message it is and
let's check out the last one isn't this
a great tutorial maybe like And
subscribe um yeah maybe like And
subscribe to the channel if you're
finding this useful if you're finding it
enjoyable and then if I press enter here
um it will say list index out of range
so I think what we need to do is say
active message is
less than the length of messages minus
one because the length of messages is
going to read two if there are only two
things in there but active message is an
index so it'll go zero and then one so
we actually want it to be less than the
length of messages minus one so that'll
fix that um and now if I were to pop in
here and just try to get through all the
messages there we go and if I press
enter too early nothing happens isn't
this a great tutorial maybe like And
subscribe and then if I press enter
nothing happens so there you go that's a
pretty basic tutorial you could
integrate this into like a world
Explorer game like a Pokemon or a Zelda
style thing or you could create a text
based tuto uh text space game or just
incorporate this into your other games
and apps or menus anything the
applications are really just up to you
and your creativity I hope you found it
useful if you did consider leaving a
like on the video subscribing to the
channel for tons more great content and
let me know in the comments below what
you'd like to see more of on the channel
what you'd like to see in a future video
as always good luck with your code and
thanks for watching thanks bye