< Return to Video

4. INTERACT

  • 0:01 - 0:04
    Remember all those cool exciting projects we saw earlier in the first video?
  • 0:04 - 0:08
    They were all animated and exploding and colors changing and moving,
  • 0:08 - 0:09
    all sorts of stuff happening.
  • 0:09 - 0:14
    Everything that we've done in this set of tutorials up until now is static drawings
  • 0:14 - 0:16
    that don't move, that don't change.
  • 0:16 - 0:20
    But what we're going to do now is look at how we can animate things.
  • 0:20 - 0:22
    How can we make things change?
  • 0:22 - 0:26
    Well first of all, we should realize, like, what does it mean for a program to animate?
  • 0:26 - 0:31
    What we had before was stroke, fill, rectangle, stroke, fill, ellipse.
  • 0:31 - 0:34
    We execute all those lines of code and we get to the end and voila,
  • 0:34 - 0:35
    we have our perfect drawing.
  • 0:35 - 0:37
    Now we want to do something different.
  • 0:37 - 0:40
    A program that animates runs continuously over time.
  • 0:40 - 0:45
    It starts, it runs, and it reruns and it reruns and it reruns.
  • 0:45 - 0:48
    It has to continuously draw the circle, draw the circle, draw the circle.
  • 0:48 - 0:52
    So we need a framework, we need a structure, we need some way of doing this besides
  • 0:52 - 0:55
    just having our linear list of instructions.
  • 0:55 - 1:01
    The way we're gonna do that is by having two sections of code, two blocks of code.
  • 1:01 - 1:03
    One, we're going to call "setup."
  • 1:05 - 1:12
    Setup is all the code that happens once at the start.
  • 1:13 - 1:16
    All the code that goes in setup happens once at the start.
  • 1:17 - 1:22
    Then we're going to have another block, another section of code that's going to be called "draw."
  • 1:22 - 1:28
    The code that goes in draw loops continuously over and over again.
  • 1:28 - 1:32
    So we might, the program is going to run forever and ever.
  • 1:32 - 1:34
    It's going to do some things once, then it's going to run forever and ever.
  • 1:34 - 1:35
    You can think about programming a game.
  • 1:35 - 1:38
    If you were making a game, you might have some things at the beginning,
  • 1:38 - 1:41
    we would set the scoreboard to 0, we would set the player's name,
  • 1:41 - 1:44
    we would set the player's location to the beginning spot, and then,
  • 1:44 - 1:48
    after that beginning setup, after that initialization,
  • 1:48 - 1:53
    then the things move and they change and they check and the ball hit the puck
  • 1:53 - 1:57
    or whatever the game might be, they draw all the elements of the game continuously.
  • 1:57 - 1:59
    This is the structure we're going to use.
  • 1:59 - 2:03
    The question of course then is, how do we type this into our code?
  • 2:03 - 2:06
    Do we just type "setup" and put a line underneath it?
  • 2:06 - 2:11
    No, there's new syntax, new strange symbols that we have to write very precisely.
  • 2:11 - 2:12
    And it looks like this.
  • 2:13 - 2:23
    Void setup with parentheses with curly bracket open, end curly bracket.
  • 2:23 - 2:25
    By the way, the curly brackets might actually be a character you've
  • 2:25 - 2:27
    never had a chance to use before.
  • 2:27 - 2:30
    This is a time. It's shift something on your keyboard, so if you can't find it,
  • 2:30 - 2:32
    pause this video and find those curly brackets.
  • 2:32 - 2:36
    And the curly brackets are crucial because they mark the beginning and end of setup,
  • 2:36 - 2:42
    meaning any code that goes inside here is the code that's going to happen
  • 2:42 - 2:43
    in the beginning of the program.
  • 2:44 - 2:47
    Now, to write draw, we follow exactly this syntax again.
  • 2:48 - 2:57
    We say void, draw, () another start curly bracket and another close curly bracket.
  • 2:57 - 3:00
    So we're now writing two blocks of code.
  • 3:00 - 3:02
    We put code in setup that happens once at the beginning,
  • 3:02 - 3:04
    code in draw that loops over and over again.
  • 3:04 - 3:07
    You might also be asking yourself the question like,
  • 3:07 - 3:09
    "yeah, I kind of get why the curly bracket is there, the parentheses,
  • 3:09 - 3:14
    maybe I'm willing to understand that I have to have parentheses, but what is this 'void?'"
  • 3:14 - 3:15
    What does void mean?
  • 3:15 - 3:18
    At this point, I really just have to say that void is something you should just memorize,
  • 3:18 - 3:21
    it's part of the definition of setup and draw.
  • 3:21 - 3:23
    You have to say void draw, void setup.
  • 3:23 - 3:26
    The more important thing that we should be looking at together is,
  • 3:26 - 3:31
    we wrote all this code, stroke, fill, background, rect, ellipse, all these things.
  • 3:31 - 3:33
    Where do they go? Do they go in setup do they go in draw?
  • 3:33 - 3:35
    Well, here's the thing.
  • 3:35 - 3:36
    What goes in setup?
  • 3:36 - 3:40
    What is something that you might do at the beginning of your program?
  • 3:40 - 3:43
    There's a function that we haven't actually taught you yet.
  • 3:43 - 3:46
    It's a function that's secretly been happening but we didn't put it in yet.
  • 3:46 - 3:48
    Now it's time to actually put it in.
  • 3:48 - 3:52
    That function is called "size," and it's something that makes sense to go in setup.
  • 3:52 - 3:59
    Size (); takes two arguments, a width and a height.
  • 3:59 - 4:01
    500, 400.
  • 4:02 - 4:03
    This makes sense.
  • 4:03 - 4:06
    This is defining the size, in pixels, of your canvas.
  • 4:06 - 4:11
    The thing that you're drawing to is 500 pixels wide and 400 pixels high.
  • 4:11 - 4:12
    Now this makes sense.
  • 4:12 - 4:16
    When your program first starts, this is something that you would do at the beginning.
  • 4:16 - 4:18
    You would set up the size of your canvas.
  • 4:19 - 4:20
    We can look at what would we put in draw?
  • 4:20 - 4:22
    Well in draw, we might put all of our drawing code.
  • 4:22 - 4:24
    We would say, "hey, I want to draw the background.
  • 4:24 - 4:28
    And I'm gonna have a black background. And then I'm going to set a fill.
  • 4:28 - 4:32
    And then I'm going to set a stroke. And then I'm gonna draw a shape,
  • 4:32 - 4:34
    like ellipse, blah blah blah."
  • 4:34 - 4:37
    Now I'm making this messy here because this is anything your heart imagines.
  • 4:37 - 4:38
    Anything you want to draw.
  • 4:38 - 4:41
    Any colors, any shapes, all of your drawing code.
  • 4:41 - 4:45
    So what we're actually going to do right now is switch back over to the code editor,
  • 4:45 - 4:48
    and in the code editor, I'm gonna add void setup and void draw.
  • 4:48 - 4:53
    See how those are two separate functions now? Two separate blocks of code?
  • 4:53 - 4:56
    Now we're gonna put size in setup, there it is.
  • 4:56 - 4:58
    Notice how it's indented slightly.
  • 4:58 - 5:02
    This is the convention to have code inside a block of code have a slight indentation.
  • 5:02 - 5:06
    Now we're gonna put code in draw. I'm gonna put a background and
  • 5:06 - 5:07
    I'm gonna put a couple different shapes.
  • 5:07 - 5:09
    Ready?
  • 5:09 - 5:12
    It's a program, remember, that runs once at the beginning and loops over and over again.
  • 5:12 - 5:15
    So when we run it, something really exciting should happen, right?
  • 5:15 - 5:17
    OK, here we go, we're gonna run that code.
  • 5:17 - 5:18
    There it is.
  • 5:19 - 5:21
    Alright, yeah, I don't know how you feel,
  • 5:21 - 5:23
    I'm not even looking at it because there's no window, really,
  • 5:23 - 5:26
    I'm just in a video, but I know what it's doing, and it's static.
  • 5:27 - 5:28
    It's not changing.
  • 5:28 - 5:30
    So this might feel a little bit disappointing,
  • 5:30 - 5:34
    we spent all this time to add setup and draw, but our program isn't animating.
  • 5:34 - 5:35
    Why isn't it animating?
  • 5:35 - 5:39
    The reason why it's not animating is, well, inside of draw,
  • 5:39 - 5:42
    we're drawing the same shapes over and over again, so if we're always saying,
  • 5:42 - 5:45
    "Put a rectangle here, draw the background, put a rectangle here,
  • 5:45 - 5:47
    draw the background, put a rectangle over here,"
  • 5:47 - 5:49
    that rectangle's never going to move.
  • 5:49 - 5:51
    So now, we need a new concept.
  • 5:51 - 5:58
    We need to figure out, how can we vary the way the shapes are drawn each time
  • 5:58 - 6:00
    through this draw loop?
  • 6:00 - 6:02
    We might be drawing an ellipse.
  • 6:02 - 6:08
    And that ellipse might be at some x, some y, with some width and some height.
  • 6:08 - 6:11
    The values that we've been using are numbers like 250, 200.
  • 6:11 - 6:15
    And if the values are 250, 200, the ellipse is always at that location.
  • 6:15 - 6:17
    Always and forever.
  • 6:19 - 6:21
    Remember this thing, you know this thing that you've got on your computer,
  • 6:21 - 6:23
    it's called a mouse and you can move things around?
  • 6:23 - 6:25
    And you usually see, I'm gonna do a terrible job of drawing this,
  • 6:25 - 6:27
    this little mouse pointer on your screen?
  • 6:27 - 6:32
    Well, the mouse exists at some x and some y, right?
  • 6:32 - 6:34
    This is an x, y location where the mouse is.
  • 6:34 - 6:40
    What if I could say, draw the ellipse not at 250, but at the x location of the mouse,
  • 6:40 - 6:44
    wherever it might be, and the y location of the mouse, wherever it might be.
  • 6:45 - 6:49
    The fact is, we can do that! The way we do that is by writing a word.
  • 6:49 - 6:56
    The word is "mouse x mouse y."
  • 6:56 - 6:59
    Now this isn't magic. It's not just magic that this happens.
  • 6:59 - 7:02
    Processing knows, this computer programming environment
  • 7:02 - 7:04
    knows that when you write the word "mouse x,"
  • 7:04 - 7:10
    you mean, it stands in for the number that is currently the x value of the mouse.
  • 7:10 - 7:15
    We have now learned a really important fundamental thing in computer programming.
  • 7:15 - 7:18
    A fundamental concept. A variable.
  • 7:18 - 7:20
    Words that stand in for numbers.
  • 7:20 - 7:23
    A word that holds a value, this is a variable.
  • 7:23 - 7:27
    There's lots more to variables than this, but we've got to start here.
  • 7:27 - 7:30
    We now know that we can use the variables "mouse x" and "mouse y."
  • 7:30 - 7:32
    So I'm gonna bring the code editor back up,
  • 7:32 - 7:37
    and you're gonna see that there's an ellipse being drawn just at 250, 200.
  • 7:37 - 7:40
    There it is. It's looping, but it never changes.
  • 7:40 - 7:44
    Now I'm gonna replace that line of code with the ellipse at mouse x, mouse y,
  • 7:44 - 7:45
    and I'm gonna run it again.
  • 7:45 - 7:49
    OK? Take your mouse, move it over there, are you moving that circle?
  • 7:49 - 7:52
    We're drawing the circle over and over again, but every time we draw the circle,
  • 7:52 - 7:55
    we're drawing it at an updated location, wherever that mouse is.
  • 7:55 - 8:00
    This is the power of looping an animation loop with variation inside that loop.
  • 8:00 - 8:03
    If we can draw a circle at a different location,
  • 8:03 - 8:06
    if we can draw a circle with a different size, with a different color,
  • 8:06 - 8:09
    things can change, things can animate.
  • 8:09 - 8:13
    Just as an exercise, try swapping mouse x and mouse y,
  • 8:13 - 8:16
    so type mouse y where mouse x was, and mouse x where mouse y was.
  • 8:17 - 8:21
    Run the code yourself and see what happens.
  • 8:21 - 8:22
    Interesting, right?
  • 8:22 - 8:24
    This is a very important point.
  • 8:24 - 8:27
    One of the things that you can do is, these are just numbers.
  • 8:27 - 8:31
    Even though it makes sense intuitively to have mouse x for the x value of something,
  • 8:31 - 8:34
    you can also use mouse x for the value, the red value of a color.
  • 8:34 - 8:37
    You can use mouse y for the blue value of a color.
  • 8:37 - 8:41
    You could also say something, like you can do little mathematical operations.
  • 8:41 - 8:46
    Something you can always say are things like "mouse x plus 50"
  • 8:46 - 8:50
    which would draw your shape 50 pixels over from where the mouse was.
  • 8:50 - 8:55
    You could also do things like say "mouse x divided by 3,"
  • 8:55 - 8:59
    which would then take the value of mouse x, divide it by 3, and use that number.
  • 8:59 - 9:02
    So this is something you can really play with,
  • 9:02 - 9:05
    and in our example that I'm going to put below for you,
  • 9:05 - 9:08
    you'll see some examples of using the mouse values in
  • 9:08 - 9:11
    slightly more complicated mathematical operations.
  • 9:11 - 9:14
    OK, so we're done with this part of the tutorial,
  • 9:14 - 9:17
    but there's one little last piece I think that would be important to show you,
  • 9:17 - 9:21
    and it's something that's gonna go into the example that I'm gonna leave you with.
  • 9:21 - 9:24
    If you've noticed, right, we only ever had size in setup.
  • 9:24 - 9:27
    All of our drawing stuff goes in draw. That makes sense.
  • 9:27 - 9:31
    Look below, that example that's there right now, size is in setup, everything else is in draw.
  • 9:31 - 9:36
    Let's just try, just as an experiment, what happens if we take the line of code
  • 9:36 - 9:39
    called "background," and move it from draw to setup?
  • 9:39 - 9:40
    We're gonna move it there.
  • 9:40 - 9:44
    That means background is gonna be here, in setup.
  • 9:45 - 9:51
    That means background is only being drawn once at the beginning.
  • 9:51 - 9:52
    Never ever again.
  • 9:52 - 9:55
    What do you think's gonna happen when we run this code?
  • 9:55 - 9:56
    Try to guess.
  • 9:56 - 9:57
    And now we're gonna run it.
  • 9:57 - 9:59
    Move your mouse over.
  • 9:59 - 10:01
    Notice that the circle is leaving a trail.
  • 10:01 - 10:05
    It's because we've never erased the background.
  • 10:05 - 10:08
    So we're seeing every circle that's being drawn, and we're seeing every circle
  • 10:08 - 10:12
    that's being drawn in draw over and over and over and over again.
  • 10:12 - 10:15
    This is something that you can use, you can make this decision,
  • 10:15 - 10:17
    "do I want to draw the background always?
  • 10:17 - 10:21
    Or do I want to put the background just once in setup to create a painting program?"
  • 10:21 - 10:25
    So go forth, make your program animate, make things move,
  • 10:25 - 10:29
    make things change color if you can, and we're gonna add one more piece,
  • 10:29 - 10:31
    we're almost to the end of this tutorial series,
  • 10:31 - 10:33
    but we've got one more step we're gonna do in
  • 10:33 - 10:35
    learning the basics of programming in this tutorial.
  • 10:35 - 10:39
    OK, have fun, see you in a bit.
Title:
4. INTERACT
Description:

These videos are designed to be viewed as part of "Hello Processing" a tutorial for code.org's Hour of Code.

http://hello.processing.org/

more » « less
Video Language:
English
odsosu edited English subtitles for 4. INTERACT
odsosu edited English subtitles for 4. INTERACT

English subtitles

Revisions