Return to Video

Terrific Text Part 1

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

more » « less
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
Show all

English subtitles

Revisions