Incrementing Shortcuts (Video Version)
-
0:01 - 0:04Remember the three steps
of making an animation? -
0:04 - 0:07We start out by making some variables
outside the draw loop, -
0:07 - 0:08and then inside the draw loop
we use those variables -
0:08 - 0:10in our drawing code.
-
0:10 - 0:12So we've got a variable
for the x position, -
0:12 - 0:14one for the y position,
one for the ballWidth, -
0:14 - 0:16and one for the ballHeight.
-
0:16 - 0:18And then, at the very end,
-
0:18 - 0:20we change those variables
a little bit every time, -
0:20 - 0:23so x is going to be
the old value of x, plus 1, -
0:23 - 0:25so it's going to be increasing every time;
-
0:25 - 0:28y is going be the old value of y, minus 2,
-
0:28 - 0:30so y will be decreasing every time;
-
0:30 - 0:35ballWidth is going to get the old value
of ballWidth times 0.99. -
0:35 - 0:39So since 0.99 is less than 1,
we're going to see ballWidth decreasing. -
0:39 - 0:42And ballHeight is going to be
the old value of ballHeight -
0:42 - 0:46divided by 1.01, which is greater than 1,
-
0:46 - 0:48and so we're also going to see
ballHeight decreasing. -
0:48 - 0:50So if I press Restart, you can see
-
0:50 - 0:54all of these attributes
of the ball changing. -
0:54 - 0:57So if you look at
these four lines of code, -
0:57 - 0:59you'll notice that
they all follow a similar pattern. -
0:59 - 1:01We've got a variable,
then an equals sign, -
1:01 - 1:06then the same variable, some operator --
plus, minus, times, divide -- -
1:07 - 1:09and some number, okay?
-
1:09 - 1:12And this pattern is so common
in programming, -
1:12 - 1:14and programmers are so lazy,
that they decided, -
1:14 - 1:17"Hey! Since we use this pattern so much,
-
1:17 - 1:19"Shouldn't there be
an easier way to type it?" -
1:19 - 1:22And so they made a shortcut,
and the shortcut goes like this: -
1:22 - 1:25Instead of saying "x gets x plus 1",
-
1:25 - 1:29I could say "x plus equals 1." Got it?
-
1:29 - 1:36And instead of saying y gets y minus 2,
I could say "y minus equals 2." -
1:37 - 1:41And instead of saying
ballWidth gets ballWidth times 0.99, -
1:41 - 1:48I can say -- you guessed it --
"ballWidth times equals 0.99" -
1:48 - 1:50Finally, instead of saying
-
1:50 - 1:54ballHeight gets ballHeight divided by 1.01
-
1:54 - 1:57we can say
"ballHeight divides equals 1.01." -
1:57 - 1:59So for all of these, what it does is
-
1:59 - 2:02it takes the value of the variables,
-
2:02 - 2:04so ballWidth, and then this operator,
-
2:04 - 2:06and then multiplies it by 0.99.
-
2:06 - 2:09So it's going to say,
"ballWidth times 0.99" -
2:09 - 2:12and then store it back
in the variable, ballWidth. -
2:12 - 2:14And if I press Restart you can see
-
2:14 - 2:16our animation looks the same as before.
-
2:16 - 2:18And now you get to be lazy too!
- Title:
- Incrementing Shortcuts (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:
- 02:20
Jenny Lam edited English subtitles for Incrementing Shortcuts (Video Version) | ||
Jenny Lam edited English subtitles for Incrementing Shortcuts (Video Version) | ||
Kirstin Cosper edited English subtitles for Incrementing Shortcuts (Video Version) | ||
Kirstin Cosper edited English subtitles for Incrementing Shortcuts (Video Version) | ||
Kirstin Cosper edited English subtitles for Incrementing Shortcuts (Video Version) | ||
Kirstin Cosper edited English subtitles for Incrementing Shortcuts (Video Version) | ||
odduse_of_language edited English subtitles for Incrementing Shortcuts (Video Version) | ||
kramtark edited English subtitles for Incrementing Shortcuts (Video Version) |