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