forest_2003 2021-12-27 16:49 采纳率: 100%
浏览 128
已结题

python里面turtle.tracer的问题

中间有一段时间卡顿,最后图片会显示完整,但是不知道怎么修改
问题来源是turtle.tracer(0)这个代码的问题



#圣诞树
import turtle as t
import random,time,datetime,math

t.hideturtle()
t.setup(1000,600,100,0)
t.tracer(0)

def bg(size):
    t.colormode(255)
    a=random.randint(30,70)
    b=random.randint(10,25)
    c=random.randint(10,25)
    t.bgcolor(a,b,c)
    t.pendown()
    t.color("yellow","yellow")
    t.begin_fill()
    for i in range (5):
        t.forward(size)
        t.right(144)
    t.end_fill()

def koch(size,n):
    if n==0:
        t.fd(size)
    else:
        for angle in [0,60,-120,60]:
            t.left(angle)
            koch(size/3, n-1)

def main(size):
    t.pendown()
    t.color("white","white")
    t.begin_fill()
    level=2
    koch(size,level)
    t.right(120)
    koch(size,level)
    t.right(120)
    koch(size,level)
    t.end_fill()

def xuedi1():
    t.penup()
    t.goto(-530,-260)
    t.pendown()
    t.pensize(87)
    t.color("white")
    t.goto(530,-260)
    t.pensize(5)
    t.goto(530,-220)
    t.left(90)

def xuedi2():
    t.circle(random.randint(0,6),180)
    t.right(180)


for i in range (75):
    t.penup()
    t.goto(random.randint(-485,485),random.randint(-100,290))
    bg(10)
    t.penup()
    t.goto(random.randint(-485,485),random.randint(-290,290))
    main(10)
    t.penup()
    t.goto(random.randint(-485,485),random.randint(-290,290))
    main(10)
    time.sleep(0.025)
xuedi1()
for i in range(200):
    xuedi2()
    time.sleep(0.025)


def wjx():
    t.penup()
    t.goto(225,250)
    t.pendown()
    t.pensize(25)
    t.color("yellow","pink")
    t.begin_fill()
    for i in range (5):
        t.forward(50)
        t.right(144)
    t.end_fill()

def sdssg(angle):
    t.penup()
    t.goto(250,250)
    t.colormode(255)
    a=random.randint(90,100)
    b=random.randint(100,150)
    c=random.randint(30,80)
    t.color(a,b,c)
    t.begin_fill()
    t.pendown()
    for i in range(6):
        t.setheading(0)
        t.right(90+angle)
        t.forward(80)
        t.left(90+angle)
        t.forward(20)
    t.penup()
    t.goto(250,250)
    t.pendown()
    for i in range(6):
        t.setheading(0)
        t.right(90-angle)
        t.forward(80)
        t.left(90-angle)
        t.forward(-20)
    x=angle
    long=240-960*(math.sin(math.radians(x)))
    t.forward(long)
    t.end_fill()

for i in range (4):
    sdssg(35-5*i)
    time.sleep(0.5)
wjx()
def write():
    t.penup()
    t.goto(-400,200)
    t.pendown()
    t.pencolor("white")
    t.write("Happy",font=("arial",66,"normal"))

    t.penup()
    t.goto(-350,100)
    t.pendown()
    t.pencolor("yellow")
    t.write("Christmas",font=("arial",66,"normal"))
write()
t.done()
运行结果及报错内容
我的解答思路和尝试过的方法

怎样可以使前面的画图加快,后面的慢下来,又不会中间卡顿,这个是中间一段代码出现的问题

  • 写回答

3条回答 默认 最新

  • 关注

    修改完毕,如果有可能的话少用点sleep(),他会阻塞进程

    
    import turtle as t
    import random,time,datetime,math
    t.setup(1000,600)
    t.hideturtle()
    
    t.tracer(0)
    
    
    def bg(size):
        t.colormode(255)
        a = random.randint(30,70)
        b = random.randint(10,25)
        c = random.randint(10,25)
        t.bgcolor(a,b,c)
        t.pendown()
        t.color("yellow","yellow")
        t.begin_fill()
        for i in range(5):
            t.forward(size)
            t.right(144)
        t.end_fill()
    
    
    def koch(size,n):
        if n == 0:
            t.fd(size)
        else:
            for angle in [0,60,-120,60]:
                t.left(angle)
                koch(size / 3,n - 1)
    
    
    def main(size):
        t.pendown()
        t.color("white","white")
        t.begin_fill()
        level = 2
        koch(size,level)
        t.right(120)
        koch(size,level)
        t.right(120)
        koch(size,level)
        t.end_fill()
    
    
    def xuedi1():
        t.penup()
        t.goto(-530,-260)
        t.pendown()
        t.pensize(87)
        t.color("white")
        t.goto(530,-260)
        t.pensize(5)
        t.goto(530,-220)
        t.left(90)
    
    
    def xuedi2():
        t.circle(random.randint(0,6),180)
        t.right(180)
    
    
    for i in range(75):
        t.penup()
        t.goto(random.randint(-485,485),random.randint(-100,290))
        bg(10)
        t.penup()
        t.goto(random.randint(-485,485),random.randint(-290,290))
        main(10)
        t.penup()
        t.goto(random.randint(-485,485),random.randint(-290,290))
        main(10)
        time.sleep(0.025)
    xuedi1()
    for i in range(200):
        xuedi2()
    
    
    def wjx():
        t.penup()
        t.goto(225,250)
        t.pendown()
        t.pensize(25)
        t.color("yellow","pink")
        t.begin_fill()
        for i in range(5):
            t.forward(50)
            t.right(144)
        t.end_fill()
    
    
    def sdssg(angle):
        t.penup()
        t.goto(250,250)
        t.colormode(255)
        a = random.randint(90,100)
        b = random.randint(100,150)
        c = random.randint(30,80)
        t.color(a,b,c)
        t.begin_fill()
        t.pendown()
        for i in range(6):
            t.setheading(0)
            t.right(90 + angle)
            t.forward(80)
            t.left(90 + angle)
            t.forward(20)
        t.penup()
        t.goto(250,250)
        t.pendown()
        for i in range(6):
            t.setheading(0)
            t.right(90 - angle)
            t.forward(80)
            t.left(90 - angle)
            t.forward(-20)
        x = angle
        long = 240 - 960 * (math.sin(math.radians(x)))
        t.forward(long)
        t.end_fill()
    
    
    for i in range(4):
        sdssg(35 - 5 * i)
    wjx()
    
    
    def write():
        t.penup()
        t.goto(-400,200)
        t.pendown()
        t.pencolor("white")
        t.write("Happy",font = ("arial",66,"normal"))
        t.penup()
        t.goto(-350,100)
        t.pendown()
        t.pencolor("yellow")
        t.write("Christmas",font = ("arial",66,"normal"))
    
    
    write()
    t.done()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月4日
  • 已采纳回答 12月27日
  • 创建了问题 12月27日

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序