Python turtle库的使用

直接打开Python app,出现命令行

turtle.home()  打开面板

turtle.position()  查看当前位置

turtle.heading()  查看当前方向

 

 

 

 

https://docs.python.org/3/library/turtle.html

 

https://docs.python.org/3/library/turtle.html#turtle.circle

turtle.circle(radius, extent=None, steps=None)
Parameters
  • radius – a number

  • extent – a number (or None)

  • steps – an integer (or None)

Draw a circle with given radius. The center is radius units left of the turtle; extent – an angle – determines which part of the circle is drawn. If extent is not given, draw the entire circle. If extent is not a full circle, one endpoint of the arc is the current pen position.

Draw the arc in counterclockwise逆时针方向 direction if radius is positive, otherwise in clockwise顺时针方向 direction.

Finally the direction of the turtle is changed by the amount of extent.

As the circle is approximated by an inscribed regular polygon, steps determines the number of steps to use. If not given, it will be calculated automatically. May be used to draw regular polygons.

>>>
>>> turtle.home()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0
>>> turtle.circle(50)
>>> turtle.position()
(-0.00,0.00)
>>> turtle.heading()
0.0
>>> turtle.circle(120, 180)  # draw a semicircle
>>> turtle.position()
(0.00,240.00)
>>> turtle.heading()
180.0

 

 

https://docs.python.org/3/library/turtle.html#turtle.setheading

turtle.setheading(to_angle)
turtle.seth(to_angle)
Parameters

to_angle – a number (integer or float)

Set the orientation of the turtle to to_angle. Here are some common directions in degrees:

standard mode

logo mode

0 - east

0 - north

90 - north

90 - east

180 - west

180 - south

270 - south

270 - west

>>>
>>> turtle.setheading(90)
>>> turtle.heading()
90.0

 

up和down函数,分别是提笔和落笔

 

https://docs.python.org/3/library/turtle.html#turtle.goto

goto 和 setpos是同一个作用

 

 

https://docs.python.org/3/library/turtle.html#turtle.pencolor

turtle.pencolor(*args)

Return or set the pencolor.

Four input formats are allowed:

pencolor()

Return the current pencolor as color specification string or as a tuple (see example). May be used as input to another color/pencolor/fillcolor call.

pencolor(colorstring)

Set pencolor to colorstring, which is a Tk color specification string, such as "red", "yellow", or "#33cc8c".

pencolor((r, g, b))

Set pencolor to the RGB color represented by the tuple of r, g, and b. Each of r, g, and b must be in the range 0..colormode, where colormode is either 1.0 or 255 (see colormode()).

pencolor(r, g, b)

Set pencolor to the RGB color represented by r, g, and b. Each of r, g, and b must be in the range 0..colormode.

If turtleshape is a polygon, the outline of that polygon is drawn with the newly set pencolor.

 >>> colormode()
 1.0
 >>> turtle.pencolor()
 'red'
 >>> turtle.pencolor("brown")
 >>> turtle.pencolor()
 'brown'
 >>> tup = (0.2, 0.8, 0.55)
 >>> turtle.pencolor(tup)
 >>> turtle.pencolor()
 (0.2, 0.8, 0.5490196078431373)
 >>> colormode(255)
 >>> turtle.pencolor()
 (51.0, 204.0, 140.0)
 >>> turtle.pencolor('#32c18f')
 >>> turtle.pencolor()
 (50.0, 193.0, 143.0)

 

>>> tup = (0.2, 0.8, 0.55)
>>> type(tup)
<class 'tuple'>

 

turtle.speed(speed=None)

Parameters

speed – an integer in the range 0..10 or a speedstring (see below)

Set the turtle’s speed to an integer value in the range 0..10. If no argument is given, return current speed.

If input is a number greater than 10 or smaller than 0.5, speed is set to 0. Speedstrings are mapped to speedvalues as follows:

  • “fastest”: 0

  • “fast”: 10

  • “normal”: 6

  • “slow”: 3

  • “slowest”: 1

Speeds from 1 to 10 enforce increasingly faster animation of line drawing and turtle turning.

Attention: speed = 0 means that no animation takes place. forward/back makes turtle jump and likewise left/right make the turtle turn instantly.

 

posted @ 2022-09-17 10:16  ChuckLu  阅读(113)  评论(0编辑  收藏  举报