Terrific Text Part 1
-
0:00 - 0:03So we've been doing
a lot of drawing so far. -
0:03 - 0:05But we haven't talked about
something as simple as say, -
0:05 - 0:08writing your name;
that's what this lesson is about, text. -
0:08 - 0:10So why might you want to use text?
-
0:10 - 0:12Well maybe we want to show
the score in a game -
0:12 - 0:15or have characters talk,
or just to make our name grow -
0:15 - 0:16and shrink and change colors.
-
0:16 - 0:19So let's go ahead and try and say "hello."
-
0:19 - 0:24Say "text(hello)" and we have
this weird error message that pops up, -
0:24 - 0:27saying that "hello is not defined,"
what does that mean? -
0:27 - 0:29Well, the problem is that the program
-
0:29 - 0:31thinks that "hello" is a variable,
-
0:31 - 0:34and it kind of makes sense
from the program's perspective -
0:34 - 0:36because maybe "hello" could be a variable,
how would it know? -
0:36 - 0:39So how do we tell our program
that it's actually text? -
0:39 - 0:41So that's easy, we just have to remember
-
0:41 - 0:43to put these quotations marks around it.
-
0:43 - 0:45And that says
that no, this is not a variable, -
0:45 - 0:46and in fact this is text
-
0:46 - 0:48or we want you to display it as text,
-
0:48 - 0:50and you can remember this
by thinking of a book, -
0:50 - 0:52where all the characters talk,
-
0:52 - 0:54and there are quotes
around what they are saying. -
0:54 - 0:56And similarly, when you want
the program to say something, -
0:56 - 0:58that text has to have
these qutoes around it. -
0:58 - 1:01And this is actually really important
and easy to mess up. -
1:01 - 1:04So i'm just going to say it again,
anytime you want to use text -
1:04 - 1:06in your program you always have to use
-
1:06 - 1:08these quotation marks around it.
-
1:08 - 1:10Otherwise you're going to get
some really weird error messages. -
1:10 - 1:13And remember, if you do see
those weird error messages, -
1:13 - 1:15just be sure to double check
that you are remembering to use quotes. -
1:15 - 1:17Great! so now we're using quotation marks,
-
1:17 - 1:19and we don't get an error anymore.
-
1:19 - 1:21But you might have noticed
that nothing is happening, -
1:21 - 1:22and it's still blank,
-
1:22 - 1:25but what is kind of strange is
if we set a "background," -
1:25 - 1:29something, say just a red,
then we see that it is actually there. -
1:29 - 1:33It's just in white, so the problem is
if we think about it, -
1:33 - 1:36that we were writing white text,
onto a white background, -
1:36 - 1:38and that's why we couldn't see it.
-
1:38 - 1:40So that seems a little bit silly,
why were you writing -
1:40 - 1:43white text onto white background?
-
1:43 - 1:46Well, we could just change it,
because we learned how to set -
1:46 - 1:47the fill of something.
-
1:47 - 1:50And just like we can set the fill
of a rectangle or a line, -
1:50 - 1:52we can set the fill of text to anything
-
1:52 - 1:54just like before, and then there it is!
-
1:54 - 1:56It shows up without
needing the background, -
1:56 - 2:01so let's look a little bit more
to how this text thing is working. -
2:01 - 2:02The first part, is obvious enough,
-
2:02 - 2:04it's just whatever text we want to write.
-
2:04 - 2:07The next part, if we change it,
-
2:07 - 2:09we can see that it's basically
just how far over, -
2:09 - 2:12and the next one
is just how far up and down. -
2:12 - 2:15That probably looks really familiar
from when we were just -
2:15 - 2:16drawing rectangles.
-
2:16 - 2:18One thing that's a little bit tricky
-
2:18 - 2:20is that text has these two coordinates
-
2:20 - 2:24specify the lower left part,
so this corner of the text. -
2:24 - 2:27While with rectangles
it's the upper left, this corner. -
2:27 - 2:30And that can seem like it was
just designed to confuse you, -
2:30 - 2:33but it's just something
you kind of have to remember. -
2:33 - 2:35And we can even experiment
and see it for ourselves, -
2:35 - 2:39by we can set this to say, "height,"
and we can see that, -
2:39 - 2:42yeah, it is setting the height
to be this lower left coordinate. -
2:44 - 2:46Or we can set it to zero,
-
2:46 - 2:50and you think what should we expect then?
-
2:50 - 2:53And we don't see it at all,
but if we slowly start increasing this, -
2:53 - 2:56we can see that, yeah it is kind of
just like peeking out there. -
2:56 - 3:00Because again, that lower left coordinate
is what we're specifying, -
3:00 - 3:02not the upper left.
-
3:02 - 3:06Okay, so enough of analyzing
this text thing, -
3:06 - 3:07let's go ahead and make it better.
-
3:07 - 3:10For example,
let's start to make it bigger, -
3:10 - 3:12we can do that with "textSize"
which just tells the program -
3:12 - 3:14how big to draw the text.
-
3:14 - 3:16And we can make it "30,"
which is pretty big, -
3:16 - 3:18we can make it even bigger,
-
3:18 - 3:20or we can make it really,
really, really, really small. -
3:20 - 3:23Whatever we want, so let's go ahead
and draw your name -
3:23 - 3:26and maybe a little message
about yourself underneath. -
3:26 - 3:29Since I don't know your name,
I'm just going to draw my name. -
3:29 - 3:31You can switch it to yours in a moment.
-
3:31 - 3:34So using what we just learned,
we can say "text("Sophia")" -
3:34 - 3:36and there my name is.
-
3:36 - 3:38And then maybe want to put
a little message underneath, -
3:38 - 3:43like, "I like puppies
and guitars and coding." -
3:43 - 3:47So that's great,
except that we obviously need -
3:47 - 3:49to change the positions
so they don't overlap. -
3:49 - 3:52And oh no, no,
that's kind of a long string of text, -
3:52 - 3:55so let's change it
to be a smaller text size. -
3:56 - 3:59And there we go, that's pretty good.
-
3:59 - 4:01Except, I don't know,
it's kind of boring having -
4:01 - 4:04them both be blue,
so let's just change the "fill," -
4:04 - 4:07and let's make it maybe a nice,
hmm I don't know, -
4:07 - 4:10maybe a nice, like, purple.
-
4:10 - 4:13Alright, and there you go,
that's all there is -
4:13 - 4:16to drawing text and changing
colors and changing size.
- Title:
- Terrific Text Part 1
- Description:
-
This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/ - Video Language:
- English
- Duration:
- 04:19
![]() |
Kirstin Cosper edited English subtitles for Terrific Text Part 1 | |
![]() |
Kirstin Cosper edited English subtitles for Terrific Text Part 1 | |
![]() |
Kirstin Cosper edited English subtitles for Terrific Text Part 1 | |
![]() |
Kirstin Cosper edited English subtitles for Terrific Text Part 1 | |
![]() |
Kirstin Cosper edited English subtitles for Terrific Text Part 1 | |
![]() |
Kirstin Cosper edited English subtitles for Terrific Text Part 1 | |
![]() |
Kirstin Cosper edited English subtitles for Terrific Text Part 1 | |
![]() |
Kirstin Cosper edited English subtitles for Terrific Text Part 1 |