ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python画图画字-turtle

2022-01-30 18:00:30  阅读:304  来源: 互联网

标签:turtle 图画 python seth 50 fd 90 circle


Turtle库是Python语言中一个很流行的绘制图像的函数库
使用之前需要导入库:import turtle

turtle绘图的基础知识:

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

画笔控制函数

turtle.penup() 别名turtle.pu()
       画笔抬起,不留下痕迹
turtle.pendown() 别名turtle.pd()
       画笔落下,留下痕迹
turtle.pensize(width) 别名turtle.width(width)
       画笔宽度
turtle.pencolor(color)
       color为颜色字符串或者rgb值
     eg:turtle.pencolor("purple")颜色字符串
       turtle.pencolor(0.63,0.13,0.94)RGB的小数值
       turtle.pencolor((0.63,0.13,0.94))RGB的元组值
turtle.goto(x, y) #将画笔移动至(x,y)处

运动控制函数

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

方向控制函数

turtle.setheading(angle) 别名turtle.seth(angle)
      改变行进方向
angle:改变方向的角度(绝对坐标下,绝对角度)
turtle.left(angle)
turtle.right(angle)
      angle:当前方向上转过得角度(海龟角度)

实例:
画祝福字

# coding=utf-8
# HappyBirthday
import turtle


def move(angle, length):
	# 画笔控制函数
	turtle.penup()  # 画笔抬起,不留下痕迹
	# 方向控制函数
	turtle.seth(angle)  # 改变行进方向
	# 运动控制函数 
	turtle.fd(length)  # 向前行进


# prepare
# 宽高开始X开始Y
turtle.setup(1300, 400, 100, 100)  # 设置窗体的位置和大小
# 画笔控制函数
turtle.penup()  # 画笔抬起,不留下痕迹

turtle.fd(-350)  # 向前行进
turtle.seth(90)  # 改变行进方向
turtle.fd(50)  # 向前行进
turtle.pendown()  # 画笔落下,留下痕迹
turtle.pensize(10)  # 画笔宽度
turtle.pencolor("green")  # 画笔颜色,为颜色字符串或者rgb值
turtle.seth(0)  # 改变行进方向
# turtle.hideturtle()  # 隐藏画笔
turtle.speed(5)  # 笔的移动速度参数范围0.5——10,范围之外为0,最快,不设置速度为最慢
# 呀
turtle.fd(100)
# 生
turtle.pencolor("green")
# turtle.circle(r,extent=None)
# 根据半径r,绘制一个extent角度的弧度
#       r:默认圆心在海龟左侧r距离的位置
turtle.circle(50, 90)
turtle.circle(50, -30)
turtle.seth(0)
turtle.fd(100)
turtle.fd(-50)
turtle.left(90)
turtle.fd(30)
turtle.fd(-60)
turtle.left(90)
turtle.fd(50)
turtle.fd(-100)
turtle.fd(50)
turtle.left(90)
turtle.fd(50)
turtle.right(90)
turtle.fd(60)
turtle.fd(-120)
# 日
turtle.penup()
turtle.fd(-30)
turtle.pendown()
turtle.seth(90)
turtle.fd(100)
turtle.seth(0)
turtle.fd(70)
turtle.seth(-90)
turtle.fd(50)
turtle.seth(180)
turtle.fd(70)
turtle.seth(-90)
turtle.fd(50)
turtle.seth(0)
turtle.fd(70)
turtle.seth(90)
turtle.fd(50)
# 移动
move(0, 30)
# 快
turtle.pensize(8)
turtle.circle(30, 15)
turtle.pendown()
turtle.circle(30, 60)
turtle.penup()
turtle.seth(0)
turtle.fd(13)
turtle.seth(90)
turtle.pendown()
turtle.fd(40)
turtle.fd(-50)
turtle.penup()
turtle.seth(0)
turtle.fd(13)
turtle.pendown()
turtle.seth(-180)
turtle.circle(20, -90)
turtle.circle(20, 90)
turtle.penup()
turtle.fd(13)
turtle.pendown()
turtle.seth(-90)
turtle.fd(60)
move(0, 40)
move(90, 80)
turtle.pendown()
turtle.seth(0)
turtle.fd(30)
turtle.seth(90)
turtle.fd(30)
turtle.fd(-30)
turtle.seth(0)
turtle.fd(20)
turtle.seth(-90)
turtle.fd(35)
turtle.seth(0)
turtle.fd(10)
turtle.fd(-30)
turtle.seth(90)
turtle.fd(35)
turtle.fd(-35)
turtle.seth(0)
turtle.fd(-25)
move(-90, 50)
move(180, 25)
turtle.pendown()
turtle.seth(0)
turtle.penup()
turtle.circle(50, 20)
turtle.pendown()
turtle.circle(50, 70)
turtle.seth(-90)
turtle.circle(50, 60)
# 移动
move(0, 50)
move(90, 45)
# 乐
turtle.pensize(10)
turtle.pendown()
turtle.fd(40)
turtle.seth(0)
turtle.circle(50, 60)
turtle.circle(50, -25)
move(-90, 15)
turtle.pendown()
turtle.fd(30)
turtle.seth(0)
turtle.fd(-25)
turtle.fd(65)
turtle.fd(-40)
turtle.seth(-90)
turtle.fd(60)
turtle.seth(135)
turtle.fd(20)
move(135, 10)
turtle.pendown()
turtle.seth(-135)
turtle.fd(20)
move(0, 70)
turtle.pendown()
turtle.seth(135)
turtle.fd(20)

画爱心

import turtle
def curve():
	for i in range(200):
		turtle.right(1)
		turtle.forward(1)
turtle.color('pink')
turtle.write('aaaa', align='center')
turtle.begin_fill()

turtle.left(140)
turtle.forward(111.65)
curve()
# turtle.letf(120)
turtle.left(120)
curve()
turtle.forward(111.65)
turtle.end_fill()
turtle.hideturtle()
turtle.done()

标签:turtle,图画,python,seth,50,fd,90,circle
来源: https://www.cnblogs.com/xiaowng/p/15857293.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有