zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然python编程:turtle模块绘图(4)

    import turtle
     
    bob = turtle.Turtle()
     
    for i in range(1,5):
        bob.fd(100)
        bob.lt(90)
        
    turtle.mainloop()

    import turtle
    import math
     
    def circle(t,r,a):
        #传入接口arc,在arc中完成大部分计算
        arc(t,r,a)
        
    def arc(t,r,a):
        #计算边的长度,同时设定边的个数
        length = 2 * math.pi * r * abs(a) / 360
        n = int(length / 4) + 3
        step_length = length / n
        step_angle = a / n
        polygon(t, step_length, n)
    
    def polygon(t, length, n):
        #画出图形
        for i in range(n):
            t.fd(length)
            t.lt(360/n)
            
    bob = turtle.Turtle()
    radius = 50
    angle = 360
    circle(bob, radius, angle)
    
    turtle.mainloop()

    import turtle
     
    def draw_spiral(t, n, length=3, a=0.1, b=0.0002):
        #Draws an Archimedian spiral starting at the origin
        theta = 0.0
        for i in range(n):
            t.fd(length)
            dtheta = 1 / (a + b * theta)
            t.lt(dtheta)
            theta += dtheta
    
    #create the world and bob
    bob = turtle.Turtle()
    draw_spiral(bob, n=1000)
     
    turtle.mainloop()

    import turtle
    
    def draw_diamond(turt):
        for i in range(1,3):   
            turt.forward(100)
            turt.right(45)
            turt.forward(100)
            turt.right(135)
            
    def draw_art():
        window = turtle.Screen() #视窗
        window.bgcolor("black") #背景颜色
        brad = turtle.Turtle()  #创建一个“乌龟”对象
        brad.shape("turtle")    #设定画笔图像是乌龟
        brad.color("red")       #设定画笔颜色是红色
        brad.speed("fast")      #画画速度是快
        for i in range(1, 37):  #调用diamond函数36次,即画36个花瓣
            draw_diamond(brad)
            brad.right(10)
        brad.right(90)          #画出枝干
        brad.forward(1000)
        window.exitonclick()
    draw_art()

    import math
    import turtle
    
    def polyline(t, n, length, angle):
        #Draws n line segments
        for i in range(n):
            t.fd(length)
            t.lt(angle)
            
    def arc(t, r, angle):
        #Draws an arc with given radius and angle
        arc_length = 2 * math.pi * r * abs(angle) / 360
        n = int(arc_length / 4) + 3
        step_length = arc_length / n
        step_angle = float(angle) / n
        t.lt(step_angle/2)
        polyline(t, n, step_length, step_angle)
        t.rt(step_angle/2)
    
    def petal(t, r, angle):
        #Draws a petal using two arcs.
        for i in range(2):
            arc(t, r, angle)
            t.lt(180 - angle)
            
    def flower(t, n, r, angle):
        #Draws a flower with n petals
        for i in range(n):
            petal(t, r, angle)
            t.lt(360.0/n)
            
    def move(t, length):
        #Move turtle(t) forwoard (length) units without leaving a trail
    #     t.pu()
        t.fd(length)
        t.pd()
        
    bob = turtle.Turtle()
    #draw a sequence of three flowers
     
    move(bob, -100)
    flower(bob, 7, 60.0, 60.0)
     
    move(bob, 100)
    flower(bob, 10, 40.0, 80.0)
     
    move(bob, 100)
    flower(bob, 20, 140.0, 20.0)
     
    bob.hideturtle()
    turtle.mainloop()

    import turtle
    
    t = turtle.Pen()
    turtle.bgcolor("black")
    #sides = eval(str(input("输入要绘制的边的数目,请输入2-6Dev数字! ")))
    sides = 6
    colors = ["red","yellow","green","blue","orange","purple"]
    for x in range(360):
        t.pencolor(colors[x%sides]) #随机颜色
        t.speed("fast")
        t.forward(x*3/sides+x)    #六边形长度依次增加
        t.left(360/sides+1)       #转动角度依次变化
        t.width(x*sides/180)      
        t.left(91)
        
    print ("结束")

    import turtle  
    from datetime import *
    
    # 抬起画笔,向前运动一段距离放下  
    def Skip(step):  
        turtle.penup()  
        turtle.forward(step)  
        turtle.pendown()
        
    def mkHand(name, length):  
        # 注册Turtle形状,建立表针Turtle  
        turtle.reset()  
        Skip(-length * 0.1)  
        # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。  
        turtle.begin_poly()  
        turtle.forward(length * 1.1)  
        # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。  
        turtle.end_poly()  
        # 返回最后记录的多边形。  
        handForm = turtle.get_poly()  
        turtle.register_shape(name, handForm)
        
    def Init():  
        global secHand, minHand, hurHand, printer  
        # 重置Turtle指向北  
        turtle.mode("logo")  
        # 建立三个表针Turtle并初始化  
        mkHand("secHand", 135)  
        mkHand("minHand", 125)  
        mkHand("hurHand", 90)  
        secHand = turtle.Turtle()  
        secHand.shape("secHand")  
        minHand = turtle.Turtle()  
        minHand.shape("minHand")  
        hurHand = turtle.Turtle()  
        hurHand.shape("hurHand")  
        
        for hand in secHand, minHand, hurHand:  
            hand.shapesize(1, 1, 3)  
            hand.speed(0)  
            
        # 建立输出文字Turtle  
        printer = turtle.Turtle()  
        # 隐藏画笔的turtle形状  
        printer.hideturtle()  
        printer.penup()
    
    def SetupClock(radius):  
        # 建立表的外框  
        turtle.reset()  
        turtle.pensize(7)  
        for i in range(60):  
            Skip(radius)  
            if i % 5 == 0:  
                turtle.forward(20)  
                Skip(-radius - 20)  
                 
                Skip(radius + 20)  
                if i == 0:  
                    turtle.write(int(12), align="center", font=("Courier", 14, "bold"))  
                elif i == 30:  
                    Skip(25)  
                    turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))  
                    Skip(-25)  
                elif (i == 25 or i == 35):  
                    Skip(20)  
                    turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))  
                    Skip(-20)  
                else:  
                    turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))  
                Skip(-radius - 20)  
            else:  
                turtle.dot(5)  
                Skip(-radius)  
            turtle.right(6) 
    
    def Week(t):     
        week = ["星期一", "星期二", "星期三",  "星期四", "星期五", "星期六", "星期日"]  
        return week[t.weekday()] 
    
    def Date(t):  
        y = t.year  
        m = t.month  
        d = t.day  
        return "%s年%d月%d日" % (y, m, d)
    
    def Tick():  
        # 绘制表针的动态显示  
        t = datetime.today()  
        second = t.second + t.microsecond * 0.000001  
        minute = t.minute + second / 60.0  
        hour = t.hour + minute / 60.0  
        secHand.setheading(6 * second)  
        minHand.setheading(6 * minute)  
        hurHand.setheading(30 * hour)  
          
        turtle.tracer(False)   
        printer.forward(65)  
        printer.write(Week(t), align="center", font=("Courier", 14, "bold"))  
        printer.back(130)  
        printer.write(Date(t), align="center", font=("Courier", 14, "bold"))  
        printer.home()  
        turtle.tracer(True)  
        # 100ms后继续调用tick  
        turtle.ontimer(Tick, 100) 
        
    def main():  
        # 打开/关闭龟动画,并为更新图纸设置延迟。  
        turtle.tracer(False)  
        Init()  
        SetupClock(160)  
        turtle.tracer(True)  
        Tick()  
        turtle.mainloop()
        
    if __name__ == "__main__":  
        main()

  • 相关阅读:
    378. Kth Smallest Element in a Sorted Matrix
    295. Find Median from Data Stream
    857. Minimum Cost to Hire K Workers
    373. Find K Pairs with Smallest Sums
    767. Reorganize String
    无序列表
    有序列表
    缩写
    设计者详细信息
    强调
  • 原文地址:https://www.cnblogs.com/tszr/p/11851475.html
Copyright © 2011-2022 走看看