Return to Video

Incrementing Shortcuts (Video Version)

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

more » « less
Video Language:
English
Duration:
02:20

English subtitles

Revisions