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

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 创建一个数据库(要创建的表和记事本的代码截图))
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口