首页 > 其他分享 >如何在IDLE中使用海龟画图

如何在IDLE中使用海龟画图

时间:2024-10-13 19:52:19浏览次数:5  
标签:turtle 画笔 range 画图 IDLE 设置 海龟 left

大家看别人使用海龟画图是不是非常羡慕,可是自己虽然会python但不知如何下手,今天就让我来教你吧!

导入海龟画图库

海龟画图是导入了turtle库来进行编写程序的,而turtle这一个库却是在IDLE安装时就一同下载了的,所以你用的IDE如果不是IDLE,那另当别论。其实我也不知道~

import turtle

海龟画图的使用方法

海龟画图的指令简单易懂,不过需要会一些英语就可以半小时速成了!你不会不会英语吧,小学白读了吗?

来吧,这是我整理的一些turtle模块代码如何使用的集合:

import turtle:导入turtle模块(早就讲过了!)
turtle.forward(步数):海龟移动步数
turtle.left(角度):海龟向左旋转
turtle.right(角度):海龟向右旋转
turtle.pencolor("颜色英文"):海龟画笔颜色调整
turtle.fillcolor("颜色英文"):海龟画笔填充颜色调整
turtle.begin_fill():(与上一句搭配使用),开始填充
turtle.circle(圆的直径):绘制一个圆形
turtle.end_fill():结束填充
turtle.done():画图结束,接下来的程序都将无效
turtle.bgcolor("颜色英文"):将背景填充为某一种颜色
turtle.setup(窗口大小, height, x, y): 设置窗口大小和窗口左上角在屏幕中的位置。 
turtle.title(): 设置窗口的标题。 
turtle.screensize(width, height, color): 设置画布大小,背景颜色。 
turtle.done(): 绘图结束后,不自动关闭窗口。
turtle.pencolor(color): 设置画笔颜色。  
turtle.width(): 设置画笔宽度。  
turtle.speed(int): 设置画笔的速度,传入1~10的数字,1最慢,10最快。传入其他值会更快,但是没有鼠标移动的动画效果。
turtle.penup(): 提起画笔,提起后移动画笔不会留下痕迹。
turtle.pendown(): 落下画笔,开始绘图前先将画笔落下。
turtle.setx(value): 设置画笔的x轴坐标。
turtle.sety(value): 设置画笔的y轴坐标。
turtle.towards(x, y): 设置画笔指向的点。
turtle.setheading(): 设置画笔的方向。与towards()配合可以设置画笔指向某个点,如- - - setheading(towards(0, 0))可以设置画笔指向原点。
turtle.pos(): 返回画笔当前的坐标。鼠标移动一段时间后可以print()打印此函数获取鼠标位置。
turtle.heading(): 返回画笔当前的方向。
turtle.backward(distance): 画笔后退一段距离。
turtle.goto(x, y): 移动画笔到指定坐标。

好,那么接下来就是实战了!

import turtle
for j in range(1,5):
    turtle.forward(100)
    turtle.left(90)

​

这个代码会让你画出一个正方形,重复循环了四次移动一百步,左转90度,就是一个正方形了!

运行时会弹出一个新窗口运行你的代码!

如果我们做一个三色盘子呢?

你不是酷毙了?

import turtle
turtle.bgcolor("black")
turtle.pencolor("blue")
turtle.speed(100000)
turtle.ht()
for i in range(1,366):
    for i in range(1,4):
        turtle.forward(300)
        turtle.left(120)
    turtle.left(1)
turtle.bgcolor("pink")
turtle.pencolor("red")
for i in range(1,366):
    for j in range(1,5):
        turtle.forward(200)
        turtle.left(90)
    turtle.left(1)
turtle.bgcolor("purple")
turtle.pencolor("yellow")
for i in range(1,366):
    for j in range(1,6):
        turtle.forward(100)
        turtle.left(72)
    turtle.left(1)

网上还有大佬竟然做出了小猪佩奇和老虎!

让我们欣赏一下他们的作品!

from turtle import*

def nose(x,y):#鼻子
    penup()#提起笔
    goto(x,y)#定位
    pendown()#落笔,开始画
    setheading(-30)#将乌龟的方向设置为to_angle/为数字(0-东、90-北、180-西、270-南)
    begin_fill()#准备开始填充图形
    a=0.4
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            left(3) #向左转3度
            forward(a) #向前走a的步长
        else:
            a=a-0.08
            left(3)
            forward(a)
    end_fill()#填充完成

    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    pendown()
    pencolor(255,155,192)#画笔颜色
    setheading(10)
    begin_fill()
    circle(5)
    color(160,82,45)#返回或设置pencolor和fillcolor
    end_fill()

    penup()
    setheading(0)
    forward(20)
    pendown()
    pencolor(255,155,192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160,82,45)
    end_fill()


def head(x,y):#头
    color((255,155,192),"pink")
    penup()
    goto(x,y)
    setheading(0)
    pendown()
    begin_fill()
    setheading(180)
    circle(300,-30)
    circle(100,-60)
    circle(80,-100)
    circle(150,-20)
    circle(60,-95)
    setheading(161)
    circle(-300,15)
    penup()
    goto(-100,100)
    pendown()
    setheading(-30)
    a=0.4
    for i in range(60):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            lt(3) #向左转3度
            fd(a) #向前走a的步长
        else:
            a=a-0.08
            lt(3)
            fd(a)
    end_fill()


def ears(x,y): #耳朵
    color((255,155,192),"pink")
    penup()
    goto(x,y)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50,50)
    circle(-10,120)
    circle(-50,54)
    end_fill()

    penup()
    setheading(90)
    forward(-12)
    setheading(0)
    forward(30)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50,50)
    circle(-10,120)
    circle(-50,56)
    end_fill()


def eyes(x,y):#眼睛
    color((255,155,192),"white")
    penup()
    setheading(90)
    forward(-20)
    setheading(0)
    forward(-95)
    pendown()
    begin_fill()
    circle(15)
    end_fill()

    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()

    color((255,155,192),"white")
    penup()
    seth(90)
    forward(-25)
    seth(0)
    forward(40)
    pendown()
    begin_fill()
    circle(15)
    end_fill()

    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()


def cheek(x,y):#腮
    color((255,155,192))
    penup()
    goto(x,y)
    pendown()
    setheading(0)
    begin_fill()
    circle(30)
    end_fill()


def mouth(x,y): #嘴
    color(239,69,19)
    penup()
    goto(x,y)
    pendown()
    setheading(-80)
    circle(30,40)
    circle(40,80)

def body(x,y):#身体
    color("red",(255,99,71))
    penup()
    goto(x,y)
    pendown()
    begin_fill()
    setheading(-130)
    circle(100,10)
    circle(300,30)
    setheading(0)
    forward(230)
    setheading(90)
    circle(300,30)
    circle(100,3)
    color((255,155,192),(255,100,100))
    setheading(-135)
    circle(-80,63)
    circle(-150,24)
    end_fill()


def hands(x,y):#手
    color((255,155,192))
    penup()
    goto(x,y)
    pendown()
    setheading(-160)
    circle(300,15)
    penup()
    setheading(90)
    forward(15)
    setheading(0)
    forward(0)
    pendown()
    setheading(-10)
    circle(-20,90)

    penup()
    setheading(90)
    forward(30)
    setheading(0)
    forward(237)
    pendown()
    setheading(-20)
    circle(-300,15)
    penup()
    setheading(90)
    forward(20)
    setheading(0)
    forward(0)
    pendown()
    setheading(-170)
    circle(20,90)

def foot(x,y):#脚
    pensize(10)
    color((240,128,128))
    penup()
    goto(x,y)
    pendown()
    setheading(-90)
    forward(40)
    setheading(-180)
    color("black")
    pe

标签:turtle,画笔,range,画图,IDLE,设置,海龟,left
From: https://blog.csdn.net/yhcychcyhchy/article/details/142902664

相关文章

  • pandas 画图
    当然,以下是一些使用Pandas的df.plot()方法绘图的例子:线图:importpandasaspdimportnumpyasnp#创建数据t=np.linspace(0,10,100)x=np.sin(t)y=np.cos(t)#创建DataFramedf=pd.DataFrame({'x':x,'y':y})#绘制线图df.plot(x='t',y=......
  • 【JS】requestIdleCallback实现分块执行
    点击按钮后,执行一个耗时较长的dom操作,页面很长时间没有响应,给用户一种卡死的现象<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0">&......
  • 04_手工画图剖析Elasticsearch核心概念:NRT、索引、分片、副本等
    课程大纲1、lucene和elasticsearch的前世今生2、elasticsearch的核心概念3、elasticsearch核心概念vs.数据库核心概念1、lucene和elasticsearch的前世今生lucene,最先进、功能最强大的搜索库,直接基于lucene开发,非常复杂,api复杂(实现一些简单的功能,写大量的java代码),需要深入......
  • YOLOv8训练损失、mAP画图功能 | 支持多结果对比,多结果绘在一个图片(科研必备)
    鱼弦:公众号【红尘灯塔】,CSDN博客专家、内容合伙人、新星导师、全栈领域优质创作者、51CTO(Top红人+专家博主)、github开源爱好者(go-zero源码二次开发、游戏后端架构https://github.com/Peakchen)YOLOv8训练损失、mAP画图功能|支持多结果对比,多结果绘在一个图片(科研必备)......
  • python画图|横向填充两条线之间的区域
    前述已经学习了《python画图|竖向填充两条线之间的区域》,点击下方链接直达:python画图|竖向填充两条线之间的区域-CSDN博客现在我们尝试更换填充方向,转向横向填充。【1】官网教程首先点击官网链接,直达教程:https://matplotlib.org/stable/gallery/lines_bars_and_markers/fi......
  • 解决 Python IDLE 横向显示文字的方法
    用pythonIDLE编写代码的时候,遇到中文字体是横向的,如何解决?解决办法,亲测有效。解决办法保存之后,之前的中文横向就竖向显示了。......
  • python画图|多个填充区域
    前述已经学习过两条线之间的填充,以下链接可以直达:python画图|竖向填充两条线之间的区域-CSDN博客现在尝试同时在横向和竖向进行填。输入以下代码:importmatplotlib.pyplotasplt#引入matplotlib模块画图importnumpyasnp#引入numpy模块做数学计算t=np.arange(0.0......
  • 软件设计画图,流程图、甘特图、时间轴图、系统架构图、网络拓扑图、E-R图、思维导图
    目录一、流程图二、甘特图三、时间轴图四、系统架构图五、网络拓扑图六、E-R图七、思维导图一、流程图是一种用符号表示算法、工作流或流程的图形。用不同的图形表示不同含义,如椭圆表示开始和结束、菱形表示判断等。画图工具WPSoffice应用市场--文档处理--本地流......
  • 箭头→箭头 matlab画图
    closeallt=linspace(-4,4,1000);pdf=normpdf(t)cdf=normcdf(t)plot(t,pdf,"b",'LineWidth',2);holdonplot(t,cdf,'k','LineWidth',3);N=5y=rand(N,1);x=zeros(N,1)-4;scatter(x,y,60,'r','fille......
  • 使用腾讯云 GPU 云服务器完成 blender 的动画图片渲染
    步骤1:准备环境系统环境blender在Windows上运行较为稳定易用,所以建议使用腾讯云的WindowsServer2019,已经预装好驱动前往blender官网安装blenderDownload—blender.org如果您希望只渲染,我们开发了一个专门用来blender渲染的软件:ArSrNaBlender渲染助手:https://www.arsrna.cn/app......