English feliratok

← Drawing a Square - Programming Foundations with Python

Beágyazókód kérése
6 Languages

Showing Revision 4 created 05/25/2016 by Udacity Robot.

  1. Okay. Time to draw the square. But this time,
  2. using the computer. So, I created a new Python
  3. file and called it mindstorms.py. You could've called it
  4. something else. And then, inside it, I created a
  5. function called draw_square, which, for now, is empty. Now,
  6. I know there is this module in Python called
  7. turtle. This is the actual thing that moves around
  8. and draws stuff on the computer. By the way,
  9. I encourage you to find out why this thing
  10. is called turtle and not a dog, or a cat.
  11. One way to start to draw is by saying, turtle.Turtle.
  12. Now, this syntax is a little weird and we will
  13. understand it by the end of this video, but
  14. it suffices to say that this is how we grab
  15. out turtle. I will call it brad. Now, the first
  16. thing we want to do is move brad forward. And we
  17. can use the forward function for that. This function takes in
  18. a number, which is the distance we want to move forward.
  19. Now, I just want to run this code, and see if it's
  20. even working. But, you know what, the one thing we forgot,
  21. is that we still don't have a screen on which this
  22. turtle, aka brad, is going to move around. In other words,
  23. we need the red carpet equivalent in our program. I know
  24. that is done by adding a window screen, and this window is
  25. going to have a red background, and I want to be able to close
  26. this window any time I click on it. Now, let's save and run this program.
  28. All right. It seems that we have a window with a red background. And a turtle
  29. named brad, which has moved 100 steps forward. Okay, so I am back at the code,
  30. and now I'm going to finish making the square. So, after brad has moved 100
  31. steps forward, I want to turn it right, and I want to do so by 90 degrees. So, I
  32. can say, brad.right 90, and because I want to
  33. do these two steps right here, four times to do a square, I'll just
  34. copy paste them a few times. So, I'm going to copy these two lines
  35. and I'm going to paste them. Second time,
  36. third time, fourth time Let me save and run,
  37. alright, it seems like we have our first square.