turtle --- 龜圖學 (Turtle graphics)¶
原始碼:Lib/turtle.py
介紹¶
龜圖學是由 Wally Feurzeig,Seymour Papert 與 Cynthia Solomon 於 1967 年開發的,一個 以 Logo 程式語言撰寫的廣受歡迎的幾何繪圖工具。
This is an optional module. If it is missing from your copy of CPython, look for documentation from your distributor (that is, whoever provided Python to you). If you are the distributor, see 可選模組的需求.
Get started¶
Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the
command turtle.forward(15), and it moves (on-screen!) 15 pixels in the
direction it is facing, drawing a line as it moves. Give it the command
turtle.right(25), and it rotates in-place 25 degrees clockwise.
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.
教學¶
New users should start here. In this tutorial we'll explore some of the basics of turtle drawing.
啟動一個烏龜環境¶
在 Python shell 中,引入 turtle 模組中所有物件:
from turtle import *
If you run into a No module named '_tkinter' error, you'll have to
install the Tk interface package on your system.
基本繪圖¶
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 with backward() and
right().
Pen control¶
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, use down().
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 coordinates 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 as up() and down() 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(