具有特定功能的循环圆

2024-10-03 23:26:08 发布

您现在位置:Python中文网/ 问答频道 /正文

回答

我需要创建一个名为“caterpillar()”的函数,这个函数用于连接我的draw\u circle()函数和draw\u line()函数来创建caterpullar。我被困在尝试创建三个圆所需的毛毛虫身体使用画圈在一段时间内循环。到目前为止,这是我的全部代码:

from turtle import *
import turtle


def moveto(x,y):
    penup()
    goto(x,y)
    pendown()




def draw_circle(xpos,ypos,radius,colour):
    moveto(xpos,ypos)
    circle(radius)
    turtle.fillcolor(colour)

def draw_line(x1, y1, x2, y2):
    penup()
    goto(x1,y1)
    pendown()
goto(x2,y2)

def draw_square(x,y,length,colour):
    moveto(x,y)
    forward(length)
    right(90)
    forward(length)
    right(90)
    forward(length)
    right(90)
    forward(length)
    turtle.fillcolor(colour)



def caterpillar():
    draw_line(0,30,-20,-15) # feelers
    draw_line(0,30,20,-15) # feelers
    draw_line(60,30,40,-15) # feelers
    draw_line(60,30,80,-15) # feelers
    draw_line(120,30,100,-15) # feelers
    draw_line(120,30,140,-15) # feelers


    for _ in range(3) : # 3 body circles
        xpos = 0
        ypos = 0
        radius = 30
        turtle.begin_fill()
        draw_circle(0,0,30,"green")
        turtle.end_fill()
        xpos = xpos + (radius*2)



caterpillar()

我被困在“for u in range(3)”下的最后一部分-我需要使用draw u circle函数在以下特定坐标处循环三个圆: Caterpillar

我已经被困在这个小时,任何帮助将不胜感激! 编辑: 还忘了提一下,我一直收到错误“文件”C:\Users\Rekesh\Desktop\caterpillar\1.py”,第48行,在caterpillar中 xpos=xpos+(半径*2) UnboundLocalError:赋值前引用了局部变量“xpos” 当我使用xpos=xpos时,我不确定是否需要它。你知道吗


Tags: 函数caterpillardeflinelengthforwarddrawturtle