Python Turtle:使用circle()方法绘制同心圆
我正在展示一个用Python的海龟模块绘制的孙子图案, 他要求看同心圆。 我想用乌龟的画笔画会更快 而不是写我自己的代码来生成一个圆。哈我被卡住了。 我看到这个圆是从海龟的背开始的 当前位置及其绘制方向取决于海龟的电流 运动方向,但我不知道我需要做什么才能得到 同心圆。 在这一点上,我对高效的生产方式不感兴趣 同心圆:我想看看我要做什么才能得到它 这种工作方式:Python Turtle:使用circle()方法绘制同心圆,python,turtle-graphics,Python,Turtle Graphics,我正在展示一个用Python的海龟模块绘制的孙子图案, 他要求看同心圆。 我想用乌龟的画笔画会更快 而不是写我自己的代码来生成一个圆。哈我被卡住了。 我看到这个圆是从海龟的背开始的 当前位置及其绘制方向取决于海龟的电流 运动方向,但我不知道我需要做什么才能得到 同心圆。 在这一点上,我对高效的生产方式不感兴趣 同心圆:我想看看我要做什么才能得到它 这种工作方式: def turtle_pos(art,posxy,lift): if lift: art.penup()
def turtle_pos(art,posxy,lift):
if lift:
art.penup()
art.setposition(posxy)
art.pendown()
def drawit(tshape,tcolor,pen_color,pen_thick,scolor,radius,mv):
window=turtle.Screen() #Request a screen
window.bgcolor(scolor) #Set its color
#...code that defines the turtle trl
for j in range(1,11):
turtle_pos(trl,[trl.xcor()+mv,trl.ycor()-mv],1)
trl.circle(j*radius)
drawit("turtle","purple","green",4,"black",20,30)
从文件中: 中心是海龟左侧的半径单位
所以,当你开始画一个圆的时候,无论海龟在哪里,这个圆的中心是向右的一段距离。在每个圆之后,只需向左或向右移动一些像素,并绘制另一个圆,其半径根据海龟移动的距离进行调整。例如,如果您绘制一个半径为50像素的圆,然后向右移动10像素,您将绘制另一个半径为40的圆,并且这两个圆应该同心。您可以这样做:
import turtle
turtle.penup()
for i in range(1, 500, 50):
turtle.right(90) # Face South
turtle.forward(i) # Move one radius
turtle.right(270) # Back to start heading
turtle.pendown() # Put the pen back down
turtle.circle(i) # Draw a circle
turtle.penup() # Pen up while we go home
turtle.home() # Head back to the start pos
这将创建下面的图片:
基本上,它将海龟向下移动一个半径长度,以使所有圆的中心点保持在同一点上。因此,现在我给你一个可以画同心圆的精确代码
import turtle
t=turtle.Turtle()
for i in range(5):
t.circle(i*10)
t.penup()
t.setposition(0,-(i*10))
t.pendown()
turtle.done()
在这一点上,我对高效的生产方式不感兴趣
同心圆:我想看看我要怎么做才能达到这个目的
工作
为了解决OP的问题,对其原始代码所做的更改非常简单:
turtle_pos(trl, [trl.xcor() + mv, trl.ycor() - mv], 1)
trl.circle(j * radius)
变成:
turtle_pos(trl, [trl.xcor(), trl.ycor() - mv], 1)
trl.circle(j * mv + radius)
包含上述修复和一些样式更改的完整代码:
import turtle
def turtle_pos(art, posxy, lift):
if lift:
art.penup()
art.setposition(posxy)
art.pendown()
def drawit(tshape, tcolor, pen_color, pen_thick, scolor, radius, mv):
window = turtle.Screen() # Request a screen
window.bgcolor(scolor) # Set its color
#...code that defines the turtle trl
trl = turtle.Turtle(tshape)
trl.pencolor(pen_color)
trl.fillcolor(tcolor) # not filling but makes body of turtle this color
trl.width(pen_thick)
for j in range(10):
turtle_pos(trl, (trl.xcor(), trl.ycor() - mv), True)
trl.circle(j * mv + radius)
window.mainloop()
drawit("turtle", "purple", "green", 4, "black", 20, 30)
你能发布你当前的代码吗?def turtle_pos(art,posxy,lift):if lift:art.penup()art.setposition(posxy)art.pendown()def drawit(tshape,tcolor,pen_color,pen_color,pen_thick,scoolor,radius,mv):window=turtle.Screen()#请求屏幕窗口。bgcolor(scoolor)#设置其颜色#…定义范围(1,11)内j的turtle trl的代码:turtle_pos(trl[trl.xcor()+mv,trl.ycor()-mv],1)trl.circle(j*radius)drawit(“turtle”,“purple”,“green”,4,“black”,20,30)--抱歉,不知道如何格式化。在当前的Python turtle下,这个答案似乎不再有效。引用的文档仍然是一样的,但它是伪造的,因为中心相对于海龟的位置取决于海龟的头部。如果使用默认的初始标题,报价就完全错误了。切换到“logo”模式,仅对初始标题有效。但您的具体示例目前是错误的,因为您必须以相反的方式移动才能使圆同心。这对任何其他标题都不起作用。