python : turtle 画一朵花

转自 # http://blog.tibarazmi.com/draw-flower-using-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

# -*- 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, r, angle, p)
    t.end_fill()

def main():
 
    window= turtle.Screen() #creat a screen
    window.bgcolor("white")
    window.title("draw a flower")
    lucy= turtle.Turtle()
    lucy.shape("turtle")
    lucy.color("red")
    lucy.width(3)
    #lucy.speed(10)
 
# Drawing flower
    flower(lucy, 7, 60, 100, 360)
 
# Drawing pedicel
    lucy.color("brown")
    lucy.rt(90)
    lucy.fd(200)
 
# Drawing leaf 1
    lucy.width(1)
    lucy.rt(270)
    lucy.color("green")
    leaf(lucy, 40, 80, 180)
    lucy.rt(140)
    lucy.color("black")
    lucy.fd(30)
    lucy.lt(180)
    lucy.fd(30)

# Drawing leaf 2
    lucy.rt(120)
    lucy.color("green")
    leaf(lucy, 40, 80, 180)
    lucy.color("black")
    lucy.rt(140)
    lucy.fd(30)
    lucy.ht() # hideturtle
    window.exitonclick()
 
main()



  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值