Exemplo n.º 1
0
def hands( freq=166 ):
    """Draw three hands.

    :param freq: Frequency of refresh in milliseconds.
    """
    global running
    now= datetime.datetime.now()
    time= now.time()
    h, m, s, ms = time.hour, time.minute, time.second, int(time.microsecond/1000)

    # Erase old hands.
    while turtle.undobufferentries():
        turtle.undo()

    # Draw new hands.
    hand( h*5+m/60+s/3600, .6*R, 3 )
    hand( m+s/60, .8*R, 2 )
    hand( s+ms/1000, .9*R, 1 )

    # Draw date and time
    turtle.penup(); turtle.home()
    turtle.goto( 0, -120 ); turtle.write( now.strftime("%b %d %H:%M:%S"), align="center", font=("Helvetica", 24, "normal") )

    # Reschedule hands function
    if running:
        # Reset timer for next second (including microsecond tweak)
        turtle.ontimer( hands, freq-(ms%freq) )
Exemplo n.º 2
0
def undo():
    global commandList
    print("The number commands that can be undone: ", \
turtle.undobufferentries())
    undoInput = input("""If you would like to undo just one command type \
"undo".\n\n
Alternatively, if you would like to retrace all the turtle's steps to the \
beginning then please type "reversetimeloop".""")
    if undoInput == "undo":
        turtle.undo()
        commandList.pop()
    elif undoInput == "reversetimeloop":
        while turtle.undobufferentries():
            turtle.undo()
        pass
    else:
        print("Command not recognised")
Exemplo n.º 3
0
def demo2():
    """Demo of some new features."""
    # 使用最慢速度 0最快,1-10逐渐加快
    turtle.speed(1)
    # 隐藏画笔(即海龟箭头),同 turtle.hideturtle()。此时只要画笔是按下状态,虽然隐藏了但是还是能绘制图形的
    #turtle.ht()
    # 显示画笔,同 turtle.showturtle()
    #turtle.st()
    # 设置画笔线条粗细为3,同turtle.width(3)
    turtle.pensize(3)
    # 设置海龟箭头当前方向角度(当前模式下海龟箭头当前位置指向(0,0)点的向量角度),turtle.towards(0, 0)其实是当前方向的反方向
    turtle.setheading(turtle.towards(0, 0))
    # 计算半径(海龟箭头当前位置到(0,0)位置距离的一半)
    radius = turtle.distance(0, 0) / 2.0
    # 海龟箭头右转90度,即以海龟箭头当前位置到(0,0)为直径画
    turtle.rt(90)
    # 画18次角度为10度的小段圆弧,画笔每按下画一小段就再抬起移动一小段
    for _ in range(18):
        switchpen(turtle.getturtle())
        turtle.circle(radius, 10)
    turtle.write("wait a moment...")

    # 等待2秒
    time.sleep(2)

    # 撤销所有
    while turtle.undobufferentries():
        turtle.undo()
    # 清屏并将画笔位置和方向恢复到初始状态并保持画笔形状不变,即位置到原点(0, 0),因为模式是"standard"方向恢复向右,"standard"/"world"模式方向也恢复到默认的向右,"logo"模式方向恢复到默认的向上
    turtle.reset()
    # 海龟箭头左转90度
    turtle.lt(90)
    turtle.colormode(255)
    # 定义等边三角形默认的边长
    laenge = 10
    turtle.pencolor("green")
    # 设置画笔线条粗细为3, 同turtle.width(3)
    turtle.pensize(3)
    # 海龟箭头左转180度
    turtle.lt(180)
    # i为-2 到15 共遍历18次,之所以i有负数,是因为内循环中turtle.fillcolor(255-15*i, 0, 15*i)中的15*i不能超过255(即colormode的值)
    for i in range(-2, 16):
        print("当前i=", i)
        # 后15次画的图形进行填充,每次填充色不同
        if i > 0:
            turtle.begin_fill()
            turtle.fillcolor(255 - 15 * i, 0, 15 * i)
        # 画等边三角形
        for _ in range(3):
            turtle.fd(laenge)
            turtle.lt(120)
        # 后15次画的图形填充完毕
        if i > 0:
            turtle.end_fill()

        # 每画完一个等边三角形,等边三角形的变成增加10像素,并且角度左转15度
        laenge += 10
        turtle.lt(15)

        # 达到 慢->快->慢->快 的效果 (速度值>10 则被置为0即最快速度)
        turtle.speed((turtle.speed() + 1) % 12)

    # 海龟箭头左转120度
    turtle.lt(120)
    # 画笔抬起
    turtle.pu()
    # 向前移动70像素
    turtle.fd(70)
    # 海龟箭头向右转30度
    turtle.rt(30)
    # 画笔放下
    turtle.pd()
    # pencolor设置成红色并且fillcolor设置成黄色
    turtle.color("red", "yellow")
    # 速度设置成最快
    turtle.speed(0)
    # 画图形并填充
    turtle.begin_fill()
    for _ in range(4):
        # 绘制1/4圆
        turtle.circle(50, 90)
        # 右转90度
        turtle.rt(90)
        # 前移30像素
        turtle.fd(30)
        # 右转90度
        turtle.rt(90)
    # 图形填充完毕
    turtle.end_fill()
    # 海龟箭头左转90度
    turtle.lt(90)
    # 画笔抬起
    turtle.pu()
    # 向前移动30像素
    turtle.fd(30)
    # 画笔放下
    turtle.pd()
    # 海龟箭头图标设置成海龟
    turtle.shape("turtle")
    # 获取(默认的)第一个Turtle实例对象
    tri = turtle.getturtle(
    )  # 下文中的 tri(默认的)第一个Turtle实例对象,可以将所有的tri替换成turtle,比如tri.lt(100) 可改成 turtle.lt(100)
    # 设置tri的(海龟箭头和)画笔大小的缩放模式为auto,此模式下(海龟箭头和)画笔随 pensize变化而变化.
    tri.resizemode("auto")
    # 创建一个新的Turtle实例对象并返回该对象实例
    turtle1 = turtle.Turtle()
    # 设置turtle1的(海龟箭头和)画笔大小的缩放模式为auto,此模式下(海龟箭头和)画笔随 pensize变化而变化.
    turtle1.resizemode("auto")
    # turtle1的海龟箭头图标设置成海龟
    turtle1.shape("turtle")
    turtle1.reset()
    turtle1.lt(90)
    # turtle1设置画笔速度为0最大速度
    turtle1.speed(0)
    # turtle1的画笔抬起
    turtle1.pu()
    # turtle1的画笔移动到坐标点(280, 40)
    turtle1.goto(280, 40)
    # turtle1的左转30度
    turtle1.lt(30)
    # turtle1的画笔放下
    turtle1.pd()
    # turtle1设置画笔速度为6
    turtle1.speed(6)
    # turtle1的pencolor设置成蓝色并且fillcolor设置成橘黄色
    turtle1.color("blue", "orange")
    # 设置画笔线条粗细为2, 同turtle.width(2)
    turtle1.pensize(2)
    # tri设置画笔速度为6
    tri.speed(6)
    # tri方向指向turtle1当前位置
    tri.setheading(tri.towards(turtle1))
    count = 1
    # 只要tri与turtle1之间距离>4像素进入循环
    while tri.distance(turtle1) > 4:
        turtle1.fd(3.5)
        turtle1.lt(0.6)
        tri.setheading(tri.towards(turtle1))
        tri.fd(4)
        if count % 20 == 0:
            # 当前位置拷贝一份turtle1实例的当前海龟箭头图标
            turtle1.stamp()
            # 当前位置拷贝一份tri实例的当前海龟箭头图标
            tri.stamp()
            # tri间断效果
            switchpen(tri)
            # turtle1间断效果
            #switchpen(turtle1)
        count += 1
    tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right")
    tri.pencolor("black")
    tri.pencolor("red")

    # 等待2秒
    time.sleep(2)

    # 此处的turtle.undobufferentries()等价于turtle.getturtle().undobufferentries()或tri.undobufferentries()表示只要第一个turtle实例可以撤销,就一直撤销,因为tri的操作数量相对于turtle1要多,为了两个对象都撤销完所以用了前者
    while turtle.undobufferentries():
        # tri对象撤销一步
        tri.undo()
        # turtle1对象撤销一步
        turtle1.undo()
    # tri对象向前移动50像素
    tri.fd(50)
    tri.write("  Click anywhere to exit!", font=("Courier", 12, "bold"))
    # 获取海龟屏幕TurtleScreen对象实例
    screen = turtle.getscreen()
    # 注册点击事件,点击海龟屏幕TurtleScreen任何位置,触发close_window函数
    screen.onclick(close_window)