美文网首页
turtle库绘图 以及 Module 'turtle

turtle库绘图 以及 Module 'turtle

作者: 小星star | 来源:发表于2019-02-12 19:21 被阅读1次

在vscode中写代码,pylint报这个错,但是实际运行却又没有问题
Module 'turtle' has no 'fd' member
主要原因是:turtle并没有向外暴露 fd这个方法,新建一个Turtle对象,然后使用setup方法更为推荐,这样写的话,编辑器也会有代码提示

m = t.Turtle()
m.fd(50)

turtle常用函数

penup()              提起画笔   海龟在空中
pendown()            放下画笔  海龟在地上
pensize(width)       画笔宽度   海龟腰围
pencolor(color)      画笔颜色  海龟身上带的颜料 
pencolor("red")      或者 pencolor(0.63, 0.13, 0.94) 或者pencolor((0.63, 0.13, 0.94))
forward(d)           简写fd(d) 海龟向前爬行  d 行进距离,可以为负数
backward(d)          简写bd(d) 海龟向后倒退
circle(r, extend)    海龟 绕着圆心D,根据半径r做圆周运动。圆心D默认是在海龟左侧r距离的位置,extend是绘制角度,默认是360度整圆
setheading(angle)    简写seth(angle)  海龟的朝向角度
left(angle)          海龟向左转
right(angle)         海龟向右转
image.png

蟒蛇绘制实例

import turtle

turtle.setup(650, 350, 200, 200)
turtle.Screen
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
    turtle.circle(40, 80)
    turtle.circle(-40, 80)
turtle.circle(40, 80/2)
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40 * 2/3)
turtle.done()
image.png

Turtle讲解

  1. turtle的绘图窗体


    image.png
    image.png
  2. turtle有两套坐标体系
    空间坐标体系 和 角度坐标体系
    其中二者都包含下面两种体系,所以细分下来是四种体系。

    • 绝对坐标 (咱们人类的视角)
    • 海龟坐标 (海龟的视角)

    空间坐标中的 绝对坐标

    turtle.goto(x,y)

    image.png
image.png

空间坐标中的 海龟坐标

turtle.circle(r, angle)
turtle.fd(d)
turtle.bk(d)

image.png
image.png

角度坐标中的 绝对角度

turtle.seth(angle) #set h 设置h

image.png
image.png

角度坐标中的 海龟角度

turtle.left(angle)
turtle.right(angle)

image.png image.png
  1. RGB色彩空间
    两套表示 0-255 或者 0-1(小数)

    turtle.colormode(mode)
    1.0 : RGB小数模式
    255:RGB整数模式

    image.png

相关文章

网友评论

      本文标题:turtle库绘图 以及 Module 'turtle

      本文链接:https://www.haomeiwen.com/subject/qmokeqtx.html