Python繪圖之(5)turtle 畫一朵花


操縱海龜繪圖有着許多的命令,這些命令可以划分為兩種:一種為運動命令,一種為畫筆控制命令
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

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, 10, 40, 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()

  執行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM