æ纵海é¾ç»å¾æç许å¤çå½ä»¤,è¿äºå½ä»¤å¯ä»¥åå为两ç§:ä¸ç§ä¸ºè¿å¨å½ä»¤ï¼ä¸ç§ä¸ºç»ç¬æ§å¶å½ä»¤
1. è¿å¨å½ä»¤:
forward(degree) #åå移å¨è·ç¦»degree代表è·ç¦»
backward(degree) #åå移å¨è·ç¦»degree代表è·ç¦»
right(degree) #åå³ç§»å¨å¤å°åº¦
left(degree) #å左移å¨å¤å°åº¦
goto(x,y) #å°ç»ç¬ç§»å¨å°åæ 为x,yçä½ç½®
stamp() #å¤å¶å½åå¾å½¢
speed(speed) #ç»ç¬ç»å¶çé度èå´[0,10]æ´æ°
2. ç»ç¬æ§å¶å½ä»¤:
down() #移å¨æ¶ç»å¶å¾å½¢,缺çæ¶ä¹ä¸ºç»å¶
up() #移å¨æ¶ä¸ç»å¶å¾å½¢
pensize(width) #ç»å¶å¾å½¢æ¶ç宽度
color(colorstring) #ç»å¶å¾å½¢æ¶çé¢è²
fillcolor(colorstring) #ç»å¶å¾å½¢çå¡«å
é¢è²
fill(Ture)
fill(false)
lucy : 梦æ³ç
§è¿ç°å®ï¼é²èï¼éæ¥é£éï¼
draw_flower1.py
[python] view plain copy
# -*- coding: cp936 -*-
import turtle
import math
def p_line(t, n, length, angle):
"""Draws n line segments."""
for i in range(n):
t.fd(length)
t.lt(angle)
def polygon(t, n, length):
"""Draws a polygon with n sides."""
angle = 360/n
p_line(t, n, length, angle)
def arc(t, r, angle):
"""Draws an arc with the given radius and angle."""
arc_length = 2 * math.pi * r * abs(angle) / 360
n = int(arc_length / 4) + 1
step_length = arc_length / n
step_angle = float(angle) / n
# Before starting reduces, making a slight left turn.
t.lt(step_angle/2)
p_line(t, n, step_length, step_angle)
t.rt(step_angle/2)
def petal(t, r, angle):
"""Draws a è±ç£ using two arcs."""
for i in range(2):
arc(t, r, angle)
t.lt(180-angle)
def flower(t, n, r, angle, p):
"""Draws a flower with n petals."""
for i in range(n):
petal(t, r, angle)
t.lt(p/n)
def leaf(t, r, angle, p):
"""Draws a å¶å and fill it."""
t.begin_fill() # Begin the fill process.
t.down()
flower(t, 1, 40, 80, 180)
t.end_fill()
def main():
window=turtle.Screen() #creat a screen
window.bgcolor("blue")
lucy=turtle.Turtle()
lucy.shape("turtle")
lucy.color("red")
lucy.width(5)
lucy.speed(0)
# Drawing flower
flower(lucy, 7, 60, 100, 360)
# Drawing pedicel
lucy.color("brown")
lucy.rt(90)
lucy.fd(200)
# Drawing leaf
lucy.rt(270)
lucy.color("green")
leaf(lucy, 40, 80, 180)
lucy.ht()
window.exitonclick()
main()