Python-turtle库知识小结

Turtle库是Python语言中一个很流行的绘制图像的函数库

使用之前需要导入库:import turtle

• turtle.setup(width,height,startx,starty)
  -setup() 设置窗体的位置和大小
  相对于桌面的起始点的坐标以及窗口的宽度高度,若不写窗口的起始点,则默认在桌面的正中心
  窗体的坐标原点默认在窗口的中心
• 绝对坐标
  ○ turtle.goto(100,100):指从当前的点指向括号内所给坐标
• 海龟坐标,把当前点当做坐标,有前方向,后方向,左方向,右方向
  ○ turtle.fd(d):指沿着海龟的前方向运行
  ○ turtle.bk(d):指沿着海龟的反方向运行
  ○ turtle.circle(r,angle):指沿着海龟左侧的某一点做圆运动
• 绝对角度
  ○ turtle.seth(angle):只改变海龟的行进方向(角度按逆时针),但不行进,angle为绝对度数
• 海龟角度
  ○ turtle.left(angle)
  ○ turtle.right(angle)

画笔控制函数

1、turtle.penup() 别名turtle.pu()
    画笔抬起,不留下痕迹
2、turtle.pendown() 别名turtle.pd()
     画笔落下,留下
 3、turtle.pensize(width) 别名turtle.width(width)
     画笔宽度
 4、turtle.pencolor(color)
   color为颜色字符串或者rgb值

运动控制函数

1、turtle.forword(d) 别名turtle.fd(d)
      向前行进
      d:行进距离,可以为负数
    2、turtle.circle(r,extent=None)
      根据半径r,绘制一个extent角度的弧度
      r:默认圆心在海龟左侧r距离的位置

方向控制函数

1、turtle.setheading(angle) 别名turtle.seth(angle)
      改变行进方向
    2、angle:改变方向的角度(绝对坐标下,绝对角度)
    3、turtle.left(angle)
    4、turtle.right(angle)
      angle:当前方向上转过得角度(海龟角度)
turtle画国旗例子:

import turtle
def draw_rectangle(x,y,width,height):
    """"绘制矩形"""
    turtle.goto(x,y)
    turtle.pencolor('red')
    turtle.fillcolor('red')
    turtle.begin_fill( )
    for i in range(2) :
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)
    turtle.end_fill( )

def draw_star(x,y,radius):
    """"绘制五角星"""
    turtle.setpos(x, y)
    pos1 = turtle.pos()
    turtle.circle(-radius,72)
    pos2 = turtle.pos()
    turtle.circle(-radius, 72)
    pos3 = turtle.pos()
    turtle.circle(-radius,72)
    pos4 = turtle.pos()
    turtle.circle(-radius, 72)
    pos5 = turtle.pos()
    turtle.color('yellow','yellow')
    turtle.begin_fill()
    turtle.goto(pos3)
    turtle.goto(pos1)
    turtle.goto(pos4)
    turtle.goto(pos2)
    turtle.goto(pos5)
    turtle.end_fill()


def main():
    """主程序"""
    turtle.speed(12)
    turtle.penup()
    x,y = -270, -180
    # 画国旗主体
    width, height = 540,360

    draw_rectangle(x, y, width, height)
    # 画大星星
    pice = 22
    center_x, center_y = x + 5 * pice, y + height - pice * 5
    turtle.goto(center_x,center_y)
    turtle.left(90)
    turtle.forward(pice * 3)
    turtle.right(90)
    draw_star(turtle.xcor(),turtle.ycor(), pice * 3)
    x_poses, y_poses = [10, 12, 12, 10], [2,4,7,9]
    # 画小星星
    for x_pos, y_pos in zip(x_poses,y_poses):
        turtle.goto(x + x_pos * pice, y + height - y_pos * pice)
        turtle.left(turtle.towards(center_x,center_y) - turtle.heading())
        turtle.forward(pice)
        turtle.right(90)
        draw_star(turtle.xcor(),turtle.ycor(),pice)
    # 隐藏海龟
    turtle.ht()
    # 显示绘图窗口
    turtle.mainloop()

if __name__ == '__main__':
    main()
  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值