Return to Video

Variable Expressions (Video Version)

  • 0:02 - 0:03
    We're back with Winston,
  • 0:03 - 0:07
    and now we have both
    an x and a y variable
  • 0:07 - 0:09
    for Winston's position.
  • 0:09 - 0:11
    So, now we can make Winston
    hop up and down
  • 0:11 - 0:14
    and have a little Winston party!
    Wooo!
  • 0:14 - 0:16
    Very nice.
  • 0:16 - 0:19
    Let's review what this code does
    before we keep going.
  • 0:19 - 0:22
    We have these x and y variables
    at the top
  • 0:22 - 0:25
    that store the center position
    of Winston's face,
  • 0:25 - 0:29
    which we use in this line here
    when we make the ellipse for his face.
  • 0:30 - 0:32
    Then, we position the eyes
    and the mouth
  • 0:32 - 0:34
    relative to the center of the face.
  • 0:34 - 0:39
    So, we subtract or add to x and y
    in order to make sure that the eyes
  • 0:39 - 0:43
    are 50 pixels away and 100 pixels away.
  • 0:44 - 0:45
    For example, the mouth
  • 0:45 - 0:49
    is 50 pixels to the right
    of the center of the face,
  • 0:49 - 0:54
    and 40 pixels below
    the center of the face.
  • 0:56 - 1:00
    So, let's go through and see
    what else we can store as variables.
  • 1:00 - 1:03
    To do that, I'm going to go through
    each line of code
  • 1:03 - 1:07
    and look for what we call
    hard coded numbers.
  • 1:08 - 1:11
    Those are numbers
    that are just straight numbers,
  • 1:11 - 1:14
    not variables
    or dependent on variables.
  • 1:15 - 1:18
    Let's see.
    In our first ellipse call here,
  • 1:18 - 1:22
    we have 300 and 300
    for the width and height.
  • 1:23 - 1:27
    So, we can make a variable
    for these instead, like faceSize.
  • 1:27 - 1:33
    So, faceSize = 300,
    and then we can pass faceSize in here,
  • 1:33 - 1:36
    and right now it would pass 300
    as the value.
  • 1:37 - 1:41
    Cool. Now, let's keep going.
  • 1:42 - 1:45
    Everything in here
    is using x or y or eyeSize,
  • 1:45 - 1:49
    but here for the mouth,
    we once again have 150
  • 1:49 - 1:51
    and 150 for the width and height.
  • 1:51 - 1:56
    So, we can make a mouthSize variable,
    say mouthSize = 150,
  • 1:57 - 2:00
    and we'll go pass mouthSize here.
  • 2:00 - 2:02
    It'll just pass 150 right now,
  • 2:02 - 2:04
    because that's what
    the variable is equal to.
  • 2:05 - 2:10
    OK, so now that we've done that,
    we can easily change the faceSize here,
  • 2:11 - 2:16
    and we can easily change the mouthSize,
    and we can move it like that,
  • 2:17 - 2:19
    and we can change the eyeSize again.
  • 2:19 - 2:21
    OK, so that's cool.
  • 2:21 - 2:24
    But, there's something
    I don't really like about that,
  • 2:25 - 2:27
    and that's when I change the faceSize.
  • 2:29 - 2:34
    I actually want everything else to change
    relative to the faceSize,
  • 2:34 - 2:37
    so if I make the faceSize
    really small like this,
  • 2:38 - 2:41
    I want his eyes and his mouth
    to be really small, too.
  • 2:41 - 2:44
    If I make the faceSize
    half of its original size,
  • 2:45 - 2:48
    the eyes and the mouth
    should also be half of their size.
  • 2:48 - 2:50
    Otherwise he just looks really silly,
  • 2:50 - 2:53
    because his eyes and his mouth
    are way too big for his face.
  • 2:53 - 2:55
    They're not even connected anymore.
  • 2:56 - 2:59
    So what we want to do
    is somehow make these variables,
  • 3:00 - 3:02
    mouthSize and eyeSize,
  • 3:02 - 3:05
    be dependent on this variable,
    faceSize.
  • 3:06 - 3:09
    So, let's bring it back to what it was.
  • 3:09 - 3:11
    The way we can do this
  • 3:11 - 3:17
    is we can make these values
    be based off the faceSize values.
  • 3:17 - 3:22
    So, we can say
    mouthSize = faceSize/2.
  • 3:22 - 3:24
    We are using a fraction of the face;
  • 3:24 - 3:28
    we're saying take one half
    of the face size.
  • 3:28 - 3:30
    If you're not familiar with fractions,
  • 3:30 - 3:32
    there's tons of videos on Khan Academy
  • 3:32 - 3:36
    that you can use
    to review how fractions work.
  • 3:36 - 3:41
    All right. Now for eyeSize.
    It's around faceSize/4.
  • 3:41 - 3:44
    It's not perfect,
    but it's pretty good.
  • 3:44 - 3:47
    Oh, 7.
    There we go, that's better.
  • 3:47 - 3:49
    See, if you get the fraction
    wrong at first,
  • 3:49 - 3:51
    you can always fix it later.
  • 3:51 - 3:54
    Just fiddle with that number
    until something makes sense.
  • 3:54 - 3:58
    OK, so now if we resize the face again,
  • 3:58 - 4:03
    see how the eyes and the mouth
    are actually resizing along with it?
  • 4:03 - 4:04
    It's pretty cool.
  • 4:04 - 4:07
    But, there's still something wrong.
  • 4:07 - 4:09
    The eyes and the mouth
  • 4:10 - 4:13
    are still going off the face
    when we go really small.
  • 4:13 - 4:16
    The sizes are correct;
    the problem is the offset from the face.
  • 4:18 - 4:21
    What's happening here
    is that down here,
  • 4:21 - 4:24
    when we position the ellipses,
  • 4:25 - 4:31
    we have x - 50 and y - 50,
    and then x + 100 and y - 60,
  • 4:32 - 4:36
    So even if our face size
    is only 50 pixels,
  • 4:36 - 4:41
    we're still having the eye positioned
    at -50 pixels to the center,
  • 4:41 - 4:44
    which is going to make it off the face.
  • 4:44 - 4:48
    So, we need for 50 and 100
    and all these numbers here
  • 4:48 - 4:52
    all of these should also be fractions
    of the faceSize,
  • 4:52 - 4:56
    so that when the faceSize changes,
    the amount that the eyes are offset
  • 4:56 - 4:58
    and the mouth is offset,
  • 4:58 - 5:01
    those numbers should also change.
  • 5:02 - 5:05
    So, to show you what I mean,
    let's do the first eye.
  • 5:05 - 5:08
    x - 50. So, x - 50 pixels.
  • 5:08 - 5:12
    This means it should be 50 pixels
    to the left of the center of the face.
  • 5:13 - 5:15
    Well, what we actually want now
    is to use the fraction,
  • 5:15 - 5:20
    so it's going to be faceSize/6.
  • 5:20 - 5:22
    So, one sixth the size of the face.
  • 5:23 - 5:29
    Cool. And then 50
    will be also faceSize/6.
  • 5:30 - 5:31
    So, now if we resize,
  • 5:32 - 5:35
    notice how that eye
    is perfectly positioned.
  • 5:36 - 5:38
    Good eye, Winston! Good eye.
  • 5:39 - 5:41
    The other eye still needs
    some help though.
  • 5:41 - 5:48
    So 100; that'll be faceSize/3,
    so one third the size of the face,
  • 5:48 - 5:53
    and 60 is one fifth the size of the face,
    so faceSize/5.
  • 5:54 - 5:57
    Great. Let's resize it, very nice.
  • 5:57 - 6:00
    We still have a problem with our mouth,
  • 6:00 - 6:02
    so we'll go down to the mouth.
  • 6:02 - 6:07
    This one is maybe faceSize/6 again,
  • 6:07 - 6:10
    and this one is about faceSize/7.
  • 6:11 - 6:16
    All right, now everything is done
    proportionally. Let's check it out.
  • 6:16 - 6:19
    Woo! Now we can make Winston really small,
  • 6:19 - 6:22
    and his eyes and mouth
    are still inside his face!
  • 6:23 - 6:27
    I'm sure Winston
    is really happy about that.
  • 6:27 - 6:28
    All right! Yay!
  • 6:28 - 6:30
    So, let's review what we're doing here.
  • 6:31 - 6:33
    At the top, we have our variables.
  • 6:33 - 6:37
    We start off with a variable
    that's just storing a number: 200.
  • 6:37 - 6:40
    Then, we make our mouthSize
    and eyeSize variables
  • 6:40 - 6:43
    be dependent on that number
    as fractions of that number,
  • 6:43 - 6:46
    so that if faceSize is currently 200,
  • 6:46 - 6:48
    then mouthSize will be 100.
  • 6:48 - 6:50
    But, if we change faceSize to 300,
  • 6:50 - 6:53
    then mouthSize
    would suddenly be 150,
  • 6:54 - 6:56
    so it's always changing in proportion.
  • 6:57 - 7:00
    Then, down here,
    when we calculate our offsets,
  • 7:00 - 7:01
    we're also using fractions,
  • 7:01 - 7:06
    because we want the offsets
    to also be changing
  • 7:06 - 7:09
    proportional to the faceSize.
  • 7:09 - 7:10
    We basically just want to make it
  • 7:10 - 7:15
    so we just have this one variable
    that affects everything.
  • 7:15 - 7:18
    We can do that with variables
    and variable expressions.
  • 7:20 - 7:21
    So, now that we understand
  • 7:21 - 7:24
    how to make variables dependent
    on the values of other variables,
  • 7:24 - 7:26
    we can do way more with our programs.
  • 7:26 - 7:30
    Let's celebrate
    by making Winston huge!
  • 7:30 - 7:33
    Go Winston, come on,
    keep going! Keep going!
  • 7:34 - 7:39
    Never stop!!
    Keep going! Naaaaaa!
Title:
Variable Expressions (Video Version)
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:
07:42

English subtitles

Revisions