0

到目前为止,我有这个,它做了两个圆圈,但一个在屏幕外。我想把它放在中心,让它们彼此分开。现在它做了两个循环,但我希望它做一个小圆圈,然后在屏幕中间的第一个周围做一个更大的圆圈。两者都需要不同。颜色。

def sun_and_earth():   
    import turtle  #allows me to use the turtles library
    turtle.Turtle()
    turtle.Screen()  #creates turtle screen
    turtle.window_height()
    turtle.window_width()
    turtle.bgcolor('grey')  #makes background color
    turtle.color("red", "green")
    turtle.circle(2, 360)  #draws a (size, radius) circle
    turtle.circle(218, 360)
    turtle.exitonclick()  #exits out of turtle window on click of window
4

1 回答 1

0

我想你可能对turtle库中的一些函数有一些误解。首先,turtle.window_height()返回turtle.window_width() 窗口的高度和宽度,所以(因为没有分配这些值)这两行什么都不做。同样,turtle.Screen()返回一个对象,所以该行再次什么也不做。

为了使您的圆圈居中,您需要使用该turtle.setpos()函数更改海龟开始的位置。这将改变海龟所在位置的 x 和 y 坐标。如果你开始海龟向下一半径,这将有效地使圆在 (0, 0) 处居中,因为圆的中心是(从文档中)向左一半径。

移动时请记住将笔从页面上移开,以免在两点之间意外画线,并在要再次绘制时再次将笔放回原处。

试试这个代码:

import turtle
turtle.Turtle()
turtle.bgcolor('grey')

# decide what your small circle will look like
smallColour = "red"
smallRadius = 5

# draw the small circle
turtle.color(smallColour)
turtle.penup()
turtle.setpos(0, -smallRadius)
turtle.pendown()
turtle.circle(smallRadius)

# decide what your large circle will look like
largeColour = "white"
largeRadius = 100

# draw the large circle
turtle.color(largeColour)
turtle.penup()
print(turtle.pos())
turtle.setpos(0, -largeRadius)
print(turtle.pos())
turtle.pendown()
turtle.circle(largeRadius)

turtle.penup()
turtle.setpos(0, 0)

我希望这会有所帮助,但是我认为您对龟的使用有一些误解,看一下教程或看一下文档可能是个好主意

祝你好运

于 2014-04-17T23:05:05.613 回答