turtle은 간단한 움직임을 반복하는 프로그램을 사용하여 복잡한 모양을 그릴 수 있습니다.
In Python, turtle graphics provides a representation of a physical “turtle” (a little robot with a pen) that draws on a sheet of paper on the floor.
It’s an effective and well-proven way for learners to encounter programming concepts and interaction with software, as it provides instant, visible feedback. It also provides convenient access to graphical output in general.
Turtle drawing was originally created as an educational tool, to be used by teachers in the classroom. For the programmer who needs to produce some graphical output it can be a way to do that without the overhead of introducing more complex or external libraries into their work.
Tutorial
New users should start here. In this tutorial we’ll explore some of the basics of turtle drawing.
Starting a turtle environment
In a Python shell, import all the objects of theturtlemodule:
from turtle import *
If you run into aNomodulenamed'_tkinter'error, you’ll have to install theTkinterfacepackageon your system.
Basic drawing
Send the turtle forward 100 steps:
forward(100)
You should see (most likely, in a new window on your display) a line drawn by the turtle, heading East. Change the direction of the turtle, so that it turns 120 degrees left (anti-clockwise):
left(120)
Let’s continue by drawing a triangle:
forward(100)
left(120)
forward(100)
Notice how the turtle, represented by an arrow, points in different directions as you steer it.
Experiment with those commands, and also withbackward()andright().
펜 제어
Try changing the color - for example,color('blue')- and width of the line - for example,width(3)- and then drawing again.
You can also move the turtle around without drawing, by lifting up the pen:up()before moving. To start drawing again, usedown().
The turtle’s position
Send your turtle back to its starting-point (useful if it has disappeared off-screen):
home()
The home position is at the center of the turtle’s screen. If you ever need to know them, get the turtle’s x-y co-ordinates with:
pos()
Home is at(0,0).
And after a while, it will probably help to clear the window so we can start anew:
clearscreen()
Making algorithmic patterns
Using loops, it’s possible to build up geometric patterns:
for steps in range(100):
for c in ('blue', 'red', 'green'):
color(c)
forward(steps)
right(30)
- which of course, are limited only by the imagination!
Let’s draw the star shape at the top of this page. We want red lines, filled in with yellow:
color('red')
fillcolor('yellow')
Just asup()anddown()determine whether lines will be drawn, filling can be turned on and off:
begin_fill()
Next we’ll create a loop:
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
abs(pos())<1is a good way to know when the turtle is back at its home position.
Finally, complete the filling:
end_fill()
(Note that filling only actually takes place when you give theend_fill()command.)
How to…
This section covers some typical turtle use-cases and approaches.
Get started as quickly as possible
One of the joys of turtle graphics is the immediate, visual feedback that’s available from simple commands - it’s an excellent way to introduce children to programming ideas, with a minimum of overhead (not just children, of course).
The turtle module makes this possible by exposing all its basic functionality as functions, available withfromturtleimport*. Theturtle graphics tutorialcovers this approach.
It’s worth noting that many of the turtle commands also have even more terse equivalents, such asfd()forforward(). These are especially useful when working with learners for whom typing is not a skill.
You’ll need to have theTkinterfacepackageinstalled on your system for turtle graphics to work. Be warned that this is not always straightforward, so check this in advance if you’re planning to use turtle graphics with a learner.
자연은 남겨야 할 것과 남기면 안 되는 것을 구분합니다. 지워야 할 것과 지우지 않아야 할 것, 그 지혜를 계절은 분명히 가르칩니다. 그러나 인간은 필요 없이 남기는 게 많습니다. 많은 축적, 무분별한 미련이 오늘날 모든 모순과 불화의 원인이 아닌지. 우리가 가는 길은 진정한 제자리로 돌아오기 위한 길입니다. 가야 할 때를 알고 간다는 것은 가난한 심령을 말합니다. 뒷모습이 맑은 사람은 그 영혼이 환할 것입니다. 이 지상을 떠날 때 나도 아름다운 뒷모습을 보여줄 수 있을까요.
- 김수우, 윤석정의 《백년어》 중에서 -
* 꽃은 자신을 떨구어 말끔히 지워냄으로써 열매를 잉태합니다. 매미와 뱀은 허물을 벗고 새 몸을 얻습니다. 버리는 것과 얻는 것은 모두 자연의 섭리입니다. 버려야 얻습니다. 이를 거스르는 것이 역리(逆理)이고 이를 따르는 것이 순리(順理)입니다. 순리를 따르는 삶을 산 뒤에 맑고 아름다운 뒷모습을 남기고 가는 인생이 진정한 승리자입니다. 위대한 승리자입니다.