guhusf 发表于 2021-3-11 11:04:38

各位大佬,这个为什么错了呀,怎么修改呢

import turtle
turtle.setup(800,800)
turtle.pensize(5)
turtle.pencolor('green')
turtle.speed('fastest')
turtle.penup()
turtle.goto(50,-200)
turtle.pendown()
turtle.fillcolor('green')
turtle.begin_fill()
turtle.fd(200)
turtle.seth(130)
turtle.fd(220)
turtle.rt(130)
turtle.fd(120)
turtle.seth(130)
turtle.fd(200)
turtle.rt(130)
turtle.fd(100)
turtle.seth(130)
turtle.fd(300)
turtle.lt(100)
turtle.fd(300)
turtle.lt(130)
turtle.fd(100)
turtle.rt(130)
turtle.fd(200)
turtle.lt(130)
turtle.fd(120)
turtle.rt(130)
turtle.fd(220)
turtle.lt(130)
turtle.fd(200)
turtle.rt(90)
turtle.fd(100)
turtle.lt(90)
turtle.fd(85)
turtle.lt(90)
turtle.fd(100)
turtle.end_fill()
turtle.penup()
turtle.goto(260,-200)#右下第一个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.penup()
turtle.goto(244,-28)#右边第二个圆
turtle.pendown()
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(210,130)#右边第三个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-180,130)#左边第三个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-214,-28)#左边第二个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.penup()
turtle.goto(-230,-200)#左边第一个圆
turtle.pendown()
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-5,325)#最上面的星星
turtle.seth(80)
turtle.pendown()
turtle.fillcolor('tomato')
turtle.begin_fill()
turtle.pencolor('tomato')
while True:
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    if abs(pos())<1:
      break
end_fill()

巴巴鲁 发表于 2021-3-11 11:06:37

pos没有定义,很明显啊

guhusf 发表于 2021-3-11 11:17:48

巴巴鲁 发表于 2021-3-11 11:06
pos没有定义,很明显啊

怎么定义啊,这些程序都是照着书抄的,还不会自己写{:5_92:}

巴巴鲁 发表于 2021-3-11 11:24:23

pos()改成turtle.pos()
这是turtle库的pos函数:abs(pos())<1: 可理解为 画笔所处位置的坐标,距离原点(0,0)的距离的绝对值小于1,顾名思义就是用来查看 画笔经过若干次循环后 是否回到原点,回到原点即为真(True)。
若回到原点,便可用 break 跳出循环,使画笔停止循环运动。

逃兵 发表于 2021-3-11 11:31:31

import turtle
turtle.setup(800,800)
turtle.pensize(5)
turtle.pencolor('green')
turtle.speed('fastest')
turtle.penup()
turtle.goto(50,-200)
turtle.pendown()
turtle.fillcolor('green')
turtle.begin_fill()
turtle.fd(200)
turtle.seth(130)
turtle.fd(220)
turtle.rt(130)
turtle.fd(120)
turtle.seth(130)
turtle.fd(200)
turtle.rt(130)
turtle.fd(100)
turtle.seth(130)
turtle.fd(300)
turtle.lt(100)
turtle.fd(300)
turtle.lt(130)
turtle.fd(100)
turtle.rt(130)
turtle.fd(200)
turtle.lt(130)
turtle.fd(120)
turtle.rt(130)
turtle.fd(220)
turtle.lt(130)
turtle.fd(200)
turtle.rt(90)
turtle.fd(100)
turtle.lt(90)
turtle.fd(85)
turtle.lt(90)
turtle.fd(100)
turtle.end_fill()
turtle.penup()
turtle.goto(260,-200)#右下第一个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.penup()
turtle.goto(244,-28)#右边第二个圆
turtle.pendown()
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(210,130)#右边第三个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-180,130)#左边第三个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-214,-28)#左边第二个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.penup()
turtle.goto(-230,-200)#左边第一个圆
turtle.pendown()
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-5,325)#最上面的星星
turtle.seth(80)
turtle.pendown()
turtle.fillcolor('tomato')
turtle.begin_fill()
turtle.pencolor('tomato')
while True:
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    if abs(turtle.pos())<1:
      break
turtle.end_fill()

guhusf 发表于 2021-3-11 11:42:54

逃兵 发表于 2021-3-11 11:31


最上面画笔还是会一直循环

guhusf 发表于 2021-3-11 11:44:24

巴巴鲁 发表于 2021-3-11 11:24
pos()改成turtle.pos()
这是turtle库的pos函数:abs(pos())

谢谢大佬,但是加了break还是会一直循环啊

巴巴鲁 发表于 2021-3-11 11:48:05

guhusf 发表于 2021-3-11 11:44
谢谢大佬,但是加了break还是会一直循环啊

我觉得挺好看的,动态效果{:10_256:}

逃兵 发表于 2021-3-11 12:02:03

guhusf 发表于 2021-3-11 11:42
最上面画笔还是会一直循环

一直不满足条件,不会跳出,这里类似于死循环,可以加个print(abs(turtle.pos()))来监控

import turtle
turtle.setup(800,800)
turtle.pensize(5)
turtle.pencolor('green')
turtle.speed('fastest')
turtle.penup()
turtle.goto(50,-200)
turtle.pendown()
turtle.fillcolor('green')
turtle.begin_fill()
turtle.fd(200)
turtle.seth(130)
turtle.fd(220)
turtle.rt(130)
turtle.fd(120)
turtle.seth(130)
turtle.fd(200)
turtle.rt(130)
turtle.fd(100)
turtle.seth(130)
turtle.fd(300)
turtle.lt(100)
turtle.fd(300)
turtle.lt(130)
turtle.fd(100)
turtle.rt(130)
turtle.fd(200)
turtle.lt(130)
turtle.fd(120)
turtle.rt(130)
turtle.fd(220)
turtle.lt(130)
turtle.fd(200)
turtle.rt(90)
turtle.fd(100)
turtle.lt(90)
turtle.fd(85)
turtle.lt(90)
turtle.fd(100)
turtle.end_fill()
turtle.penup()
turtle.goto(260,-200)#右下第一个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.penup()
turtle.goto(244,-28)#右边第二个圆
turtle.pendown()
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(210,130)#右边第三个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-180,130)#左边第三个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-214,-28)#左边第二个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.penup()
turtle.goto(-230,-200)#左边第一个圆
turtle.pendown()
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-5,325)#最上面的星星
turtle.seth(80)
turtle.pendown()
turtle.fillcolor('tomato')
turtle.begin_fill()
turtle.pencolor('tomato')
while True:
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    print(abs(turtle.pos()))
    if abs(turtle.pos())<1:
      break
turtle.end_fill()

逃兵 发表于 2021-3-11 12:04:04

或者你可以修改跳出条件,闪十下就跳出循环

import turtle
turtle.setup(800,800)
turtle.pensize(5)
turtle.pencolor('green')
turtle.speed('fastest')
turtle.penup()
turtle.goto(50,-200)
turtle.pendown()
turtle.fillcolor('green')
turtle.begin_fill()
turtle.fd(200)
turtle.seth(130)
turtle.fd(220)
turtle.rt(130)
turtle.fd(120)
turtle.seth(130)
turtle.fd(200)
turtle.rt(130)
turtle.fd(100)
turtle.seth(130)
turtle.fd(300)
turtle.lt(100)
turtle.fd(300)
turtle.lt(130)
turtle.fd(100)
turtle.rt(130)
turtle.fd(200)
turtle.lt(130)
turtle.fd(120)
turtle.rt(130)
turtle.fd(220)
turtle.lt(130)
turtle.fd(200)
turtle.rt(90)
turtle.fd(100)
turtle.lt(90)
turtle.fd(85)
turtle.lt(90)
turtle.fd(100)
turtle.end_fill()
turtle.penup()
turtle.goto(260,-200)#右下第一个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.penup()
turtle.goto(244,-28)#右边第二个圆
turtle.pendown()
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(210,130)#右边第三个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-180,130)#左边第三个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-214,-28)#左边第二个圆
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.circle(10,360)
turtle.penup()
turtle.goto(-230,-200)#左边第一个圆
turtle.pendown()
turtle.circle(10,360)
turtle.end_fill()
turtle.penup()
turtle.goto(-5,325)#最上面的星星
turtle.seth(80)
turtle.pendown()
turtle.fillcolor('tomato')
turtle.begin_fill()
turtle.pencolor('tomato')
n = 10
while n:
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    turtle.fd(50)
    turtle.rt(144)
    n-=1
turtle.end_fill()
页: [1]
查看完整版本: 各位大佬,这个为什么错了呀,怎么修改呢