且先摘要
24.1. turtle
— Turtle graphics
文本,打造一個 TurtleArt 相似之環境︰
24.1.2.2. Methods of TurtleScreen/Screen
Window control
turtle.
reset
()
Delete the turtle’s drawings from the screen, re-center the turtle and set variables to the default values.
>>> turtle.goto(0,-22) >>> turtle.left(100) >>> turtle.position() (0.00,-22.00) >>> turtle.heading() 100.0 >>> turtle.reset() >>> turtle.position() (0.00,0.00) >>> turtle.heading() 0.0
Settings and special methods
turtle.
mode
(mode=None)
Parameters: | mode – one of the strings “standard”, “logo” or “world” |
---|
Set turtle mode (“standard”, “logo” or “world”) and perform reset. If mode is not given, current mode is returned.
Mode “standard” is compatible with old turtle
. Mode “logo” is compatible with most Logo turtle graphics. Mode “world” uses user-defined “world coordinates”. Attention: in this mode angles appear distorted if x/y
unit-ratio doesn’t equal 1.
Mode | Initial turtle heading | positive angles |
---|---|---|
“standard” | to the right (east) | counterclockwise |
“logo” | upward (north) | clockwise |
>>> mode("logo") # resets turtle heading to north >>> mode() 'logo'
Methods specific to Screen
turtle.
setup
(width=_CFG[“width”], height=_CFG[“height”], startx=_CFG[“leftright”], starty=_CFG[“topbottom”])
Set the size and position of the main window. Default values of arguments are stored in the configuration dictionary and can be changed via a turtle.cfg
file.
Parameters: |
|
---|
>>> screen.setup (width=200, height=200, startx=0, starty=0) >>> # sets window to 200x200 pixels, in upper left of screen >>> screen.setup(width=.75, height=0.5, startx=None, starty=None) >>> # sets window to 75% of screen by 50% of screen and centers
Turtle motion
- Move and draw
turtle.
forward
(distance)turtle.
fd
(distance)-
Parameters: distance – a number (integer or float) Move the turtle forward by the specified distance, in the direction the turtle is headed.
>>> turtle.position() (0.00,0.00) >>> turtle.forward(25) >>> turtle.position() (25.00,0.00) >>> turtle.forward(-75) >>> turtle.position() (-50.00,0.00)
-
backward()
|bk()
|back()
turtle.
backward
(distance) -
Parameters: distance – a number Move the turtle backward by distance, opposite to the direction the turtle is headed. Do not change the turtle’s heading.
turtle.
rt
(angle)-
Parameters: angle – a number (integer or float) Turn turtle right by angle units. (Units are by default degrees, but can be set via the
degrees()
andradians()
functions.) Angle orientation depends on the turtle mode, seemode()
.
turtle.
left
(angle)
turtle.
lt
(angle)
Parameters: | angle – a number (integer or float) |
---|
Turn turtle left by angle units. (Units are by default degrees, but can be set via the degrees()
and radians()
functions.) Angle orientation depends on the turtle mode, see mode()
.
>>> turtle.heading() 22.0 >>> turtle.left(45) >>> turtle.heading() 67.0
Turtle state
Appearance
turtle.
shape
(name=None)
Parameters: | name – a string which is a valid shapename |
---|
Set turtle shape to shape with given name or, if name is not given, return name of current shape. Shape with name must exist in the TurtleScreen’s shape dictionary. Initially there are the following polygon shapes: “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”. To learn about how to deal with shapes see Screen method register_shape()
.
>>> turtle.shape() 'classic' >>> turtle.shape("turtle") >>> turtle.shape() 'turtle'
然後使用 Python3 互動 Shell ,寫個打招呼程式︰
pi@raspberrypi:~ $ python3 Python 3.4.2 (default, Oct 19 2014, 13:31:11) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import turtle >>> turtle.setup(width=800, height=600) >>> turtle.shape('turtle') >>> turtle.mode('logo') >>> >>> def square(): ... for repeat in range(4): ... turtle.forward(100) ... turtle.right(90) ... >>> >>> square() >>>
確認符合預期也︰