< Return to Video

More While Loops (Video Version)

  • 0:01 - 0:04
    Now that you've learned
    the basics of loops,
  • 0:04 - 0:06
    let's go ahead and walk through
    making an awesome loop drawing
  • 0:06 - 0:07
    like this one,
  • 0:07 - 0:10
    Balloon Hopper floating
    through a beautiful sky.
  • 0:10 - 0:16
    As I go through writing the program from scratch, I want you to think to yourself about how you might do it on your own, because pretty soon, you will be.
  • 0:16 - 0:20
    So first, it always helps to break down your program into steps.
  • 0:20 - 0:27
    We'll start by drawing these balloons, which are just the same ellipse repeated over and over and over, and that sounds like a job for a loop.
  • 0:27 - 0:34
    Then, we'll take on these lines, and then finally we'll add Balloon Hopper herself.
  • 0:34 - 0:38
    All right, so here's a blank slate. Which can be pretty intimidating.
  • 0:38 - 0:44
    Sometimes, it helps to make things just a little bit more friendly by adding a background right away, just to get into the swing of things.
  • 0:44 - 0:50
    Now, since we wanna draw a loop, your first thought should be those loop questions that we covered last time.
  • 0:50 - 0:57
    And we'll do them pretty quickly this time, so if you need a review, just revisit "Intro to While Loops".
  • 0:57 - 1:01
    Now, the first question: "What do we want to repeat?"
  • 1:01 - 1:06
    So let's try drawing that first balloon. Because we wanna be repeating those balloons.
  • 1:06 - 1:15
    You can go ahead and do that, maybe like that, and well, you know, that's kind of small, it's not quite in the right place,
  • 1:15 - 1:17
    so let's just try moving it a little bit.
  • 1:17 - 1:25
    And this is just part of programming, where you try something, you realize it's not what you wanted, and then you try again and eventually you get closer and closer.
  • 1:25 - 1:34
    Okay, now, we need to add color probably, right? So we didn't think of that when we were thinking about the steps, so we can just say that's part of drawing the balloon.
  • 1:34 - 1:41
    All right? And next, we need to think about how exactly we want to be changing this balloon during our loop.
  • 1:41 - 1:48
    Well, we want the balloon to be drawn across the screen, right? We want it to be drawn like here, and then here, and then here,
  • 1:48 - 1:51
    so we want the computer to do it, because my drawing's really bad.
  • 1:51 - 1:58
    So we can fake that a little bit just by changing this first number, which as you remember controls the x-position: the position sideways.
  • 1:58 - 2:03
    But, I mean like, that's pretty lame, right? Like that's not really like that cool picture we had before.
  • 2:03 - 2:11
    So instead, let's say that we're going to call this x, for the x position, and we're going to make a variable that's about what it was before.
  • 2:11 - 2:16
    And now, we're gonna be changing that variable inside of our loop, so we'll use a while loop,
  • 2:16 - 2:22
    and then inside that loop we'll say that x is going to change each time, maybe by, say, 20.
  • 2:22 - 2:27
    If we just move that ellipse inside, being very careful, of course, not to move that variable declaration inside,
  • 2:27 - 2:30
    because then we'll just think about what would go wrong.
  • 2:30 - 2:34
    And it's actually worth trying on your own, if you're curious.
  • 2:34 - 2:39
    Okay, well, so, now our third loop question is "How long do we wanna repeat?"
  • 2:39 - 2:44
    So we can think that maybe we wanna just keep going until we're basically run off the side of the screen
  • 2:44 - 2:47
    So maybe all the way until x is less than like 400.
  • 2:47 - 2:51
    So now this is cool cause somethings happening, right? But it's not really quite what we envisioned.
  • 2:51 - 2:56
    So just like before, you know, we just have to go through it and slowly improve it to get it to where we imagined.
  • 2:56 - 2:59
    So first, the ellipses are really squished together, so let's fix that.
  • 2:59 - 3:02
    Okay, so that's good to give them a little bit of breathing room.
  • 3:02 - 3:09
    But you know, maybe they're still going a little bit too far off the side of the screen, so if we change the ending point, we can start disappearing
  • 3:09 - 3:17
    the ellipses that appear, you know, here, because we're saying you know as soon as x gets to like here maybe, stop drawing.
  • 3:17 - 3:19
    And that's what this part of the while loop says.
  • 3:19 - 3:25
    Okay? And we can also say "Well maybe we wanna change the ellipses a little bit," do we wanna move them all down,
  • 3:25 - 3:34
    do we want to, you know, change their size a little bit again, and the nice thing about the while loop is that we can do all of them all at the same time.
  • 3:34 - 3:37
    All right. So perfect.
  • 3:37 - 3:42
    Well, now looking at these balloons, it would be nice to put strings on them. If we like them, then we need to put strings on them,
  • 3:42 - 3:44
    otherwise they're gonna float away.
  • 3:44 - 3:51
    So we need a line for each one. We think that we wanna put that line starting at about the center of each of those balloons,
  • 3:51 - 3:56
    just to make it easy, and they're all coming down at kind of about the same point, maybe like that.
  • 3:56 - 3:59
    So how can we make the program do that, instead of having to draw it?
  • 3:59 - 4:05
    So we can think of, if we wanna be repeating something, we definitely wanna put it inside this while loop, so let's go ahead and make that line
  • 4:05 - 4:11
    And if we want it to be at that center of the ellipse, well then we need to have it start at these two coordinates, so we can do that.
  • 4:11 - 4:17
    And you can say well let's just have it end, you know, kind of wherever. At that was actually pretty close!
  • 4:17 - 4:24
    But, again, it's not perfect - you might sense a theme - so we need to fix it. So first let's fix this ugly thing.
  • 4:24 - 4:30
    Which is that, the string that we drew is actually overlapping with our balloon, which isn't right at all.
  • 4:30 - 4:35
    We really want that ellipse to be covering the line, which we can do just by changing that ordering.
  • 4:35 - 4:39
    You kinda see a lot of things you've learned are probably coming together here.
  • 4:39 - 4:45
    All right, so this is good, but maybe we wanna change the color of those lines, and how do we do that if we were really making this program on our own?
  • 4:45 - 4:47
    Well we'd go and look at the documentation.
  • 4:47 - 4:50
    Or we'd watch the documentation video if we didn't know how to do that.
  • 4:50 - 4:55
    So, we can go ahead and use stroke() to set the color of those lines,
  • 4:55 - 5:00
    and maybe make them like, I dunno, maybe like, that color?
  • 5:00 - 5:10
    And it's beautiful! Now, last, all we need to do is draw Hopper. And that just requires putting her in as an image, like this,
  • 5:10 - 5:17
    and you can see the documentation for how I figured that out, and just moving her around so that, you know, she's kind of holding those ballons like that
  • 5:17 - 5:18
    and floating through the sky.
  • 5:18 - 5:27
    And there you have it! We're done! You can try decorating the balloons, you know, you can think about maybe adding things to this loop to make those balloons a little bit cooler,
  • 5:27 - 5:31
    and you can even use a loop in your next drawing.
Title:
More While Loops (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:
05:31

English subtitles

Revisions