乌龟中半径较小的圆形

Circle shape in turtle with smaller radius

这是我在 Whosebug 上的第一个问题,请耐心等待。我想让我的乌龟的形状是一个小圆圈。但是,默认圆圈对于我的项目来说太大了。有没有办法让它变小?我公司不让我加载gif文件做乌龟的形状。

到目前为止,我尝试的是使用 gif 文件:

import turtle 

screen = turtle.Screen() 
screen.register_shape('circle1', 'circle.gif')
t = turtle.Turtle() 
t.shape('circle1') 
t.forward(10) 

这可行,但使用了我公司不允许的 gif 文件。有没有一种方法可以不用 gif 来实现?

你可以做的是输入你想要的圆的大小对应的多项式。 register_shape不仅可以接收文件名,还可以接收坐标。

screen.register_shape(name, coordinates) 是使用该函数的另一种方式。

如果要使圆变小,请绘制相应大小的圆并将其附加到列表中。然后将该列表转换为元组,这可以成为您的新形状。

一般来说,您可以绘制任何形状,附加坐标并使用它。

这是一个圆的例子:

具体例子

import turtle 

def drawCircle(radius,iterations, t): # returns tuples 
     coords = [] 
     for i in range(iterations): 
          t.forward(radius) 
          t.right(360.0/iterations) 
          coords.append((t.xcor(), t.ycor()))
     return tuple(coords)

t = turtle.Turtle()
screen = turtle.Screen()
screen.register_shape('myCircle', drawCircle(1, 72, t))

t.shape('myCircle')

一般示例

import turtle


def drawShape(t): 
   coords = [] 
   while # turtle move 
       coords.append((t.xcor(), t.ycor()))
   return tuple(coords) 

t = turtle.Turtle()
screen = turtle.Screen()
screen.register_shape('myShape', drawShape(t))
t.shape('myShape')

注意当您有自定义形状时,turtle 会自动为您填充。所以这不会是一个圆圈,而是一个实心圆圈。

使用 Python 提供的光标集,包括 'circle',您可以使用 shapesize() 方法(又名 turtlesize)调整光标的大小:

from turtle import Screen, Turtle

screen = Screen()

turtle = Turtle()
turtle.shape('circle')
turtle.shapesize(0.5)  # make the cursor half the default size

turtle.forward(100)

screen.exitonclick()

上面,我们将其 relative 大小调整为 default 大小。如果我们想将其调整为特定像素大小,我们首先要知道提供的光标基于 20px x 20px 的正方形,并相应地进行调整:

from turtle import Screen, Turtle

CURSOR_SIZE = 20

screen = Screen()

turtle = Turtle()
turtle.shape('circle')
turtle.shapesize(15 / CURSOR_SIZE)  # make the cursor 15 pixels diameter
turtle.color('black', 'white')  # make it an open circle this time

turtle.forward(100)

screen.exitonclick()