< Return to Video

2. SHAPES

  • 0:01 - 0:03
    Alright, we're ready to get started.
  • 0:03 - 0:05
    This is a very exciting moment,
  • 0:05 - 0:09
    'cause we're actually going to look at writing lines of code and drawing stuff to the screen.
  • 0:09 - 0:11
    So, what does it mean to write a line of code, right?
  • 0:11 - 0:13
    A line of code is an instruction.
  • 0:13 - 0:15
    What is the instruction we're going to start with?
  • 0:15 - 0:18
    The instruction we're going to start with is "draw a rectangle."
  • 0:18 - 0:22
    So let's see, what is a rectangle? I think we all know what a rectangle is, hopefully.
  • 0:22 - 0:24
    A rectangle is something that looks like this.
  • 0:26 - 0:30
    So what does it mean, then, to have an instruction that says "draw a rectangle?"
  • 0:30 - 0:36
    Well an instruction is text, and that instruction is going to look like this "rect."
  • 0:37 - 0:39
    A function is essentially that command, that instruction.
  • 0:39 - 0:44
    It's the word we use, and the function of "rect" is to draw a rectangle in the window.
  • 0:44 - 0:48
    So now, we have this function named "rect," we have this rectangle,
  • 0:48 - 0:50
    but we can't just say "draw a rectangle,"
  • 0:50 - 0:51
    we've got to be more specific, right?
  • 0:51 - 0:54
    I can't just tell you to "walk."
  • 0:54 - 0:58
    I might want to tell you to walk left or walk right or walk fast or walk slow.
  • 0:58 - 1:05
    To make a rectangle with "rect," we've got to say how wide should the rectangle be,
  • 1:05 - 1:09
    how high, how tall should the rectangle be, and where should it be on the screen?
  • 1:09 - 1:11
    Let's actually start for a moment with where should it be on the screen.
  • 1:12 - 1:13
    First, what do I even mean by "screen?"
  • 1:13 - 1:16
    You've got this computer screen, I'm in your computer screen, right?
  • 1:16 - 1:18
    The screen is a rectangle.
  • 1:18 - 1:22
    We're going to be drawing into a smaller rectangle inside your computer screen;
  • 1:22 - 1:23
    we're going to call that a canvas.
  • 1:23 - 1:30
    So I'm gonna draw a nice big rectangle here, and this is going to be the canvas.
  • 1:31 - 1:34
    The canvas is a coordinate system, meaning we can say
  • 1:34 - 1:37
    "hey, this point is the point 12, 27,"
  • 1:37 - 1:41
    or this point is the point 3, 102, those numbers aren't right,
  • 1:41 - 1:47
    but we're going to assign places in this canvas by their x coordinate and their y coordinate.
  • 1:47 - 1:52
    This being the x axis, and this being the y axis, right?
  • 1:52 - 1:56
    OK this is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  • 1:56 - 2:01
    so this would be at pixel number 9 the way that I drew this.
  • 2:01 - 2:03
    What do I mean by pixel?
  • 2:03 - 2:05
    We have these units of measurement in life, right?
  • 2:05 - 2:09
    In the world, we have inches and centimeters and miles and kilometers,
  • 2:09 - 2:12
    well on a computer screen, our unit of measurement is a pixel.
  • 2:12 - 2:16
    Each pixel is a little tiny dot that has a color on your screen.
  • 2:16 - 2:20
    This window has a width in pixels and a height in pixels, and in fact,
  • 2:20 - 2:25
    the ones that we are gonna work with in this tutorial are going to have
  • 2:25 - 2:30
    a width of 500 and a height of 400.
  • 2:30 - 2:34
    So if you think about it, if the width is 500 and this is the middle,
  • 2:34 - 2:38
    what would the middle pixel value be? 250.
  • 2:38 - 2:42
    I didn't really draw this terribly to scale, but if this were the middle,
  • 2:42 - 2:45
    what would the middle from 0-400 be? 200.
  • 2:45 - 2:50
    And in fact, we're going to need to plug these values into our rectangle function.
  • 2:50 - 2:53
    Now I need more space to write so I'm going to start writing rectangle down here again.
  • 2:53 - 2:56
    Rectangle, and now we're going to need parentheses.
  • 2:56 - 3:00
    Parentheses, and at the end, a semicolon.
  • 3:00 - 3:05
    Every line of code, every instruction that we write is going to end with a semicolon.
  • 3:05 - 3:07
    It's going to be the function name,
  • 3:07 - 3:12
    followed by parentheses with stuff in the middle that we'll see in a second, and a semicolon.
  • 3:12 - 3:15
    What goes in the middle? Well this is that stuff.
  • 3:15 - 3:17
    This is the parameters of the rectangle.
  • 3:17 - 3:21
    It's how wide is it, how high is it, and where is it x,y.
  • 3:21 - 3:25
    So by the way, the rectangle's coordinate is the top left of the rectangle.
  • 3:25 - 3:30
    It's x,y, which is at x = 250, y = 200.
  • 3:30 - 3:34
    250, 200.
  • 3:34 - 3:39
    So we are now adding these parameters to how we define a rectangle, with an x and a y,
  • 3:39 - 3:41
    and now let's give it a width.
  • 3:41 - 3:46
    Let's say the width of this rectangle is 150 and the height of this rectangle is 100.
  • 3:46 - 3:55
    So now we have an x, we have a y, and now we also have a width and we also have a height.
  • 3:56 - 3:57
    Whoa, big moment here!
  • 3:57 - 4:01
    This is a big moment in our computer programming lives
  • 4:01 - 4:06
    if you've never done computer programming before, this is your very first line of code.
  • 4:06 - 4:10
    We now have a function name, we have the arguments to that function,
  • 4:10 - 4:11
    where is that rectangle?
  • 4:11 - 4:15
    Where is its x, where is its y, what is its width, and what is its height?
  • 4:15 - 4:18
    As soon as we write this line of code and execute it,
  • 4:18 - 4:20
    we're going to see this rectangle on the screen.
  • 4:20 - 4:24
    OK, so now something really exciting is gonna happen, hopefully.
  • 4:24 - 4:27
    I'm gonna get much smaller, I'm suddenly in the corner of the window,
  • 4:27 - 4:29
    and below me, there's a code editor.
  • 4:29 - 4:32
    What's a code editor? A code editor is a place where you can type code.
  • 4:32 - 4:35
    It's just a text box; you can type and put stuff there.
  • 4:35 - 4:37
    What are you gonna put there? This.
  • 4:37 - 4:38
    This instruction.
  • 4:38 - 4:39
    You don't have to do the typing yet, you're gonna get your chance;
  • 4:39 - 4:42
    I'm actually gonna add that line of code for you there automatically.
  • 4:42 - 4:44
    Look, that line of code is there. Does it look the same?
  • 4:44 - 4:50
    rect (250, 200, 150, 100);
  • 4:50 - 4:51
    That's the line of code.
  • 4:51 - 4:56
    So now, where are we gonna see the results of that line of code? Right over here.
  • 4:56 - 4:58
    Right over here, that canvas is going to appear.
  • 4:58 - 5:00
    How big is that canvas going to be?
  • 5:00 - 5:04
    That canvas is going to be 500 pixels wide and 400 pixels high.
  • 5:04 - 5:07
    Notice, by the way, that there's nothing in the canvas right now,
  • 5:07 - 5:10
    but I'm going to add some rulers above and below.
  • 5:10 - 5:13
    Those rulers are going to show you where the pixels are,
  • 5:13 - 5:17
    so as you're trying to figure out where to draw stuff, those rulers will be helpful hints for you.
  • 5:17 - 5:20
    You can always toggle the rulers off with the toggle rulers button.
  • 5:20 - 5:22
    See? I toggled them off. See? I toggled them on.
  • 5:22 - 5:23
    Boy this is fun!
  • 5:23 - 5:26
    OK, now, uh, whoa, what do we gotta do?
  • 5:26 - 5:27
    OK, that code below? That rectangle?
  • 5:27 - 5:30
    It's time for us to run that code. I'm going to run it for you.
  • 5:30 - 5:34
    Do you see the rectangle there? That's the instruction, now you've got the rectangle.
  • 5:34 - 5:36
    We ran our first computer program; it's very exciting,
  • 5:36 - 5:39
    and now, now it's time for you to make a change.
  • 5:39 - 5:42
    To change the code and run your first computer program.
  • 5:42 - 5:45
    So try doing something like, just make the rectangle a little bit taller.
  • 5:45 - 5:49
    Instead of a height of 100, what if you give it a height of 200?
  • 5:49 - 5:51
    Change the number from 100 to 200.
  • 5:51 - 5:54
    Did you change that number?
  • 5:55 - 5:58
    Now look, I'm gonna add a button called "run this code,"
  • 5:58 - 6:02
    that button, if you press it, will execute the code that you've written.
  • 6:02 - 6:07
    Press that button. Do you see now that the rectangle is a little bit bigger?
  • 6:07 - 6:09
    Try making one more change.
  • 6:09 - 6:11
    Maybe move the rectangle to the left or right and run your code again.
  • 6:16 - 6:18
    You got it! Looks pretty good to me.
  • 6:18 - 6:22
    OK great, you know, you can always pause the video if you want more time to experiment.
  • 6:22 - 6:26
    So just pause the video, experiment, and hit play, and I'll keep talking.
  • 6:26 - 6:28
    You could also just pause the video if you wanted to stop listening to me talk;
  • 6:28 - 6:30
    that's quite OK with me.
  • 6:30 - 6:32
    OK, so this is really the fundamentals we've got here.
  • 6:32 - 6:35
    What I'm going to be asking you to do is make up your own design.
  • 6:35 - 6:37
    You can have more than one rectangle, right?
  • 6:37 - 6:39
    Below, I'm going to add 2 more rectangles.
  • 6:39 - 6:41
    Now there's 3 rectangles on the screen.
  • 6:41 - 6:45
    OK? So you can start to add them, put them in different places, but you know what?
  • 6:45 - 6:48
    You're probably going to feel quite limited to what kind of designs you can make
  • 6:48 - 6:51
    if all you could ever do is draw a rectangle,
  • 6:51 - 6:55
    so let's add one more shape to our set of shapes that we can draw.
  • 6:55 - 6:58
    The nice thing is we're just writing a new function, a new command,
  • 6:58 - 7:00
    and it's exactly the same thing as rectangle;
  • 7:00 - 7:03
    the only difference is we write the word ellipse,
  • 7:04 - 7:09
    and now we give the ellipse an x, the ellipse a y, the ellipse a width, and the ellipse a height.
  • 7:09 - 7:12
    With an ellipse, or at least a circle, when we think of a circle,
  • 7:12 - 7:15
    we think of a circle as having a diameter.
  • 7:15 - 7:21
    A diameter is the distance from one side of the circle to the other through the center.
  • 7:22 - 7:23
    So that's what a circle is.
  • 7:23 - 7:27
    But an ellipse can be something that's like a squashed circle that's very short,
  • 7:27 - 7:32
    or maybe very tall and thin, like this is an ellipse, or this is an ellipse.
  • 7:32 - 7:36
    And in these cases, we have a different height and a different width. Right?
  • 7:37 - 7:40
    The width and height, if they're equal, it's a circle, if they're different, it's an ellipse.
  • 7:40 - 7:45
    If you ever make these values equal, like if I say 200, 200,
  • 7:45 - 7:50
    and if I put this at the same location, 250, 200, we're going to get a circle.
  • 7:51 - 7:53
    Now where is that circle going to be?
  • 7:53 - 7:58
    When we drew our rectangle, we gave it an x, y location. 250, 200.
  • 7:58 - 8:03
    That's this point. This rectangle is drawn with that point being the corner of the rectangle.
  • 8:03 - 8:09
    A circle, an ellipse, is different. The x, y point we're defining is the center of that circle.
  • 8:09 - 8:16
    So here is our 200 pixel by, 200 pixel wide and 200 pixel tall circle.
  • 8:16 - 8:19
    It's right there. With the x, y at the center.
  • 8:19 - 8:21
    So our code editor now just has one rectangle.
  • 8:21 - 8:24
    It's that rectangle we drew below, and there it is in the canvas.
  • 8:24 - 8:29
    Let's add this line of code here, the ellipse. Let's put that ellipse there.
  • 8:29 - 8:30
    And I'm going to run it for you.
  • 8:30 - 8:32
    Look, there's the ellipse!
  • 8:32 - 8:34
    Now, notice something a bit strange here.
  • 8:34 - 8:35
    The ellipse is covering up the rectangle.
  • 8:35 - 8:39
    This brings up a very important point in terms of programming computer graphics.
  • 8:39 - 8:42
    The order that we write our lines of code is incredibly important, right?
  • 8:42 - 8:47
    Because if the rectangle is drawn first, the ellipse is drawn second, the ellipse is drawn on top of it.
  • 8:47 - 8:52
    Now, I'm going to swap those lines of code, I switch them, and run it again.
  • 8:52 - 8:54
    Notice now the rectangle is on top of the ellipse.
  • 8:54 - 8:58
    So how you choose to write the order of the shapes that you're drawing,
  • 8:58 - 9:01
    it's really an important point in creating your design.
  • 9:01 - 9:05
    Alright, I think we've really come to the end of this first tutorial,
  • 9:05 - 9:08
    and it's time now for you to do your first exercise.
  • 9:08 - 9:11
    I'm going to leave you with a code example that you can use to get started from.
  • 9:11 - 9:15
    So I'm filling in below a nice code example, you can see the design over here.
  • 9:15 - 9:17
    And what you should work on is try to make your own design.
  • 9:17 - 9:21
    You can make a self-portrait, or a creature or an alien.
  • 9:21 - 9:23
    Something friendly or scary, whatever you think.
  • 9:23 - 9:24
    You can design a building.
  • 9:24 - 9:28
    Any simple design that you can do with rectangles and ellipses.
  • 9:28 - 9:33
    You can use this example to build off of, or you can just erase it and start from scratch.
  • 9:33 - 9:35
    So take a little time to do this exercise.
  • 9:35 - 9:38
    When you're done, select that you're ready, and the next video will appear,
  • 9:38 - 9:40
    and we're gonna take the, in the next video, we're gonna do
  • 9:40 - 9:42
    what you're probably wondering about already, which is like,
  • 9:42 - 9:46
    "uh, can I make these red or blue or some color that I want?"
  • 9:46 - 9:49
    After you do your exercise we're going to introduce color.
  • 9:49 - 9:54
    So enjoy and I look forward to seeing you back, if you come back, for the next tutorial.
  • 9:54 - 9:56
    OK, bye bye.
Title:
2. SHAPES
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 2. SHAPES
odsosu edited English subtitles for 2. SHAPES

English subtitles

Revisions