【问题标题】:Can we add shape in turtle.shape()? [closed]我们可以在turtle.shape() 中添加形状吗? [关闭]
【发布时间】:2021-03-31 17:38:23
【问题描述】:

我想编写白板程序,所以我想把海龟的形状变成笔。 我想知道 turtle.shape() 中的海龟是否有一些东西可以进一步添加 - 比如钢笔 - 形状? 如果我们有它,我们如何添加它?

【问题讨论】:

    标签: python python-3.x turtle-graphics python-turtle whiteboard


    【解决方案1】:

    添加新海龟光标形状的关键是屏幕方法register_shape()(又名addshape())。您可以使用多边形(单个或多个)或图像文件(传统上是 *.GIF,但最近还使用 *.PNG,具体取决于 tkinter 的底层版本)来定义新形状。

    一旦注册了一个形状,就可以使用turtle shape() 方法将光标更改为新的形状。基于海龟文档:

    from turtle import Screen, Turtle
    
    screen = Screen()
    screen.register_shape("custom.gif")
    
    turtle = Turtle()
    turtle.shape("custom.gif")
    

    但是,图像不会随海龟一起旋转。为此,您可以定义一个基于多边形的形状:

    screen.register_shape("right_triangle", ((-10, 10), (-10, -10), (10, -10)))
    
    turtle = Turtle()
    turtle.shape("right_triangle")
    

    虽然多边形图像的方向可能不是您期望的那样,所以您可能需要旋转乌龟或调整多边形坐标。

    【讨论】:

      猜你喜欢
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 2012-08-02
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多