Return to Video

Drawing a Square - Programming Foundations with Python

  • 0:01 - 0:03
    Okay. Time to draw the square. But this time,
  • 0:03 - 0:07
    using the computer. So, I created a new Python
  • 0:07 - 0:11
    file and called it mindstorms.py. You could've called it
  • 0:11 - 0:13
    something else. And then, inside it, I created a
  • 0:13 - 0:17
    function called draw_square, which, for now, is empty. Now,
  • 0:17 - 0:20
    I know there is this module in Python called
  • 0:20 - 0:24
    turtle. This is the actual thing that moves around
  • 0:24 - 0:26
    and draws stuff on the computer. By the way,
  • 0:26 - 0:28
    I encourage you to find out why this thing
  • 0:28 - 0:30
    is called turtle and not a dog, or a cat.
  • 0:32 - 0:35
    One way to start to draw is by saying, turtle.Turtle.
  • 0:35 - 0:39
    Now, this syntax is a little weird and we will
  • 0:39 - 0:42
    understand it by the end of this video, but
  • 0:42 - 0:44
    it suffices to say that this is how we grab
  • 0:44 - 0:48
    out turtle. I will call it brad. Now, the first
  • 0:48 - 0:51
    thing we want to do is move brad forward. And we
  • 0:51 - 0:54
    can use the forward function for that. This function takes in
  • 0:54 - 0:57
    a number, which is the distance we want to move forward.
  • 0:57 - 0:59
    Now, I just want to run this code, and see if it's
  • 0:59 - 1:03
    even working. But, you know what, the one thing we forgot,
  • 1:03 - 1:06
    is that we still don't have a screen on which this
  • 1:06 - 1:09
    turtle, aka brad, is going to move around. In other words,
  • 1:09 - 1:13
    we need the red carpet equivalent in our program. I know
  • 1:13 - 1:16
    that is done by adding a window screen, and this window is
  • 1:16 - 1:19
    going to have a red background, and I want to be able to close
  • 1:19 - 1:23
    this window any time I click on it. Now, let's save and run this program.
  • 1:23 - 1:28
    [BLANK_AUDIO]
  • 1:28 - 1:33
    All right. It seems that we have a window with a red background. And a turtle
  • 1:33 - 1:37
    named brad, which has moved 100 steps forward. Okay, so I am back at the code,
  • 1:37 - 1:42
    and now I'm going to finish making the square. So, after brad has moved 100
  • 1:42 - 1:47
    steps forward, I want to turn it right, and I want to do so by 90 degrees. So, I
  • 1:47 - 1:53
    can say, brad.right 90, and because I want to
  • 1:53 - 1:58
    do these two steps right here, four times to do a square, I'll just
  • 1:58 - 2:02
    copy paste them a few times. So, I'm going to copy these two lines
  • 2:04 - 2:06
    and I'm going to paste them. Second time,
  • 2:06 - 2:12
    third time, fourth time Let me save and run,
  • 2:16 - 2:18
    alright, it seems like we have our first square.
Cím:
Drawing a Square - Programming Foundations with Python
Leírás:

more » « less
Video Language:
English
Team:
Udacity
Projekt:
UD036 - OOP
Duration:
02:20
Udacity Robot edited Angol subtitles for 03-06 Drawing a Square
Udacity Robot edited Angol subtitles for 03-06 Drawing a Square
Udacity Robot edited Angol subtitles for 03-06 Drawing a Square
Cogi-Admin edited Angol subtitles for 03-06 Drawing a Square

English subtitles

Felülvizsgálatok Compare revisions