Python之turtle库画各种有趣的图及源码(更新中)

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。

1、安卓小人

#!/usr/bin/env python
import turtle
aj=turtle.Pen()
y=0
aj.speed(5)
#turtle.screensize(200,800)
turtle.bgcolor("black")
#aj.shape("turtle")
def head():
    aj.color("green")
    aj.fd(160)
    x=aj.xcor()
    aj.seth(90)
    aj.begin_fill()
    #aj.color("green")
    aj.circle(x/2,180)
    aj.end_fill()
    aj.penup()
    aj.goto(33,37)
    aj.pendown()
    aj.dot(13,"black")
    aj.penup()
    aj.goto(126,37)
    aj.pendown()
    aj.dot(13,"black")
    aj.penup()
    aj.home()
    aj.pendown()
    aj.hideturtle()
    aj.fd(160)
    aj.seth(90)
    aj.circle(x/2,60)
    aj.right(90)
    aj.pensize(5)
    aj.fd(30)

    aj.penup()
    aj.home()
    #aj.pendown()
    aj.hideturtle()
    aj.fd(160)
    aj.seth(90)
    aj.circle(x/2,120)
    aj.right(90)
    aj.pensize(5)
    aj.pendown()
    aj.fd(30)
    aj.penup()
    aj.home()
    aj.penup()

def body():
    aj.pensize(0)

    aj.home()
    aj.showturtle()
    aj.goto(0,-7)
    aj.pendown()
    aj.begin_fill()
    aj.fd(160)
    aj.right(90)
    aj.fd(120)
    aj.right(90)
    aj.fd(160)
    y=aj.ycor()
    aj.right(90)
    aj.fd(120)
    aj.end_fill()

def legs():
    aj.penup()
    #turtle.color("red")
    aj.goto(33,-169)
    aj.pendown()
    aj.pensize(32)
    aj.fd(43)
    aj.penup()
    aj.goto(130,-169)
    aj.pendown()
    aj.fd(43)
    aj.penup()

def hands():
    aj.home()
    aj.pensize(30)
    aj.goto(-18,-77)
    aj.pendown()
    aj.left(90)
    aj.fd(65)
    aj.penup()
    aj.goto(179,-77)
    aj.pendown()
    aj.fd(65)
    aj.penup()
    aj.hideturtle
    aj.fd(100)
    aj.hideturtle()
    aj.circle(100)
    aj.circle(100,360,59)
    aj.reset()
    turtle.bgcolor("black")
    turtle.pencolor("green")
    turtle.hideturtle()
    turtle.goto(-300,0)
    aj.hideturtle
    turtle.write("Thank you for watching....", font = ("Bodoni MT Black", 28, "bold"))
    turtle.penup()
    turtle.goto(-40,-170)
    turtle.pendown()
    turtle.pencolor("yellow")
    turtle.write("Developed by 一个超会写Bug的安太狼", font = ("Palatino Linotype", 22, "bold"))

head()
body()
legs()
hands()
turtle.done()

效果图:
在这里插入图片描述

2、龙形曲线(Dragon Curve)

又叫分形龙,是一种自相似碎形曲线的统称,因形似龙的蜿蜒盘曲而得名。

# -*- coding: utf-8 -*-

from turtle import *
length = 5
angle  = 90
setup(1280,720)
up()
goto(300,-100)

down()
def draw_path(path):
    for symbol in path:
        if symbol == 'f':
            import random
            colormode(255)
            color(random.randint(0,255),random.randint(0,255
  • 79
    点赞
  • 287
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 20
    评论
### 回答1: 使用Pythonturtle库可以轻松地绘制各种图形,包括线条、圆形、矩形、多边形等等。通过turtle库提供的函数和方法,可以控制海龟的移动和绘制,实现各种有趣的效果。例如,可以使用turtle库绘制一个简单的正方形: import turtle turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.done() 这段代码会绘制一个边长为100的正方形。其turtle.forward()函数表示向前移动一定距离,turtle.right()函数表示向右旋转一定角度。最后,调用turtle.done()函数表示绘制完成,等待用户关闭窗口。 除了绘制基本图形,turtle库还支持更高级的绘图功能,例如填充颜色、设置线条宽度、绘制文本等等。可以通过查阅turtle库的文档,了解更多的绘图方法和技巧。 ### 回答2: Pythonturtle库是一个基于Windows图形界面的函数库,可以使用户在屏幕上绘制图形。使用turtle库画图可以增加程序的趣味性,同时也能够让程序员更好地理解数学基础知识,比如坐标系、角度、线性代数等。 在开始使用turtle库之前,我们需要引入turtle库模块。下面是一个最基本的turtle库绘图代码实例: ``` import turtle turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.done() ``` 上述代码实现了在屏幕上绘制了一个边长为100的正方形。其turtle.forward(n)表示向右前进n个单位,turtle.right(n)表示向右旋转n度。turtle.done()用于保持图像窗口不关闭,直到用户手动关闭窗口为止。 turtle库绘图的基本操作包括:前进、后退、旋转、抬笔、落笔、改变颜色、改变线宽等。此外,我们也可以借助turtle库绘制各种图形,如圆形、三角形、正多边形、心形等。 下面是一个使用turtle库绘制五芒星的实现代码: ``` import turtle turtle.penup() turtle.goto(0,0) turtle.pendown() turtle.color('red','yellow') turtle.begin_fill() for i in range(5): turtle.forward(200) turtle.right(144) turtle.end_fill() turtle.done() ``` 上述代码实现了在屏幕上绘制了一个红黄相间的五芒星。其turtle.penup()和turtle.pendown()分别用于抬笔和落笔。turtle.color('red','yellow')用于设置线条颜色为红色,填充颜色为黄色。turtle.begin_fill()和turtle.end_fill()用于标识需要填充颜色的区域。 绘制图形时,我们可以使用代码循环进行绘制,这可以大幅度加速绘制的过程。下面是一个绘制螺旋线的实现代码: ``` import turtle step = 2 angle = 10 turtle.speed(0) for i in range(360): turtle.forward(step) turtle.right(angle) step += 0.05 angle += 0.5 turtle.done() ``` 上述代码实现了在屏幕上绘制了一个螺旋线。其,step表示每次前进的距离,angle表示每次右转的角度。turtle.speed(0)用于加速绘制速度。通过实验调节angle和step的大小,我们可以绘制出各种不同的曲线形状。 总之,使用turtle库画图可以让程序更具趣味性和可视化。无论是初学Python的新手还是有经验的程序员,都可以通过绘制各种形状来提高自己的编程水平。 ### 回答3: Pythonturtle库是一个非常有趣的绘图工具,它简单易用但又足够强大,可以用于绘制各种图形。在Python使用turtle库,可以让我们探索计算机图形的奥妙,同时也可以增强我们的创造力和编程能力。 要使用turtle库编写程序,首先需要导入turtle库。可以使用以下代码导入: ```python import turtle ``` 在导入库后,可以创建一个画布和一个画笔来开始绘画。使用以下代码创建一个画布: ```python turtle.setup(width, height, startx, starty) ``` 该函数接受四个参数,分别是画布的宽度、高度、起始坐标。需要注意的是,如果这四个参数都不提供,将使用缺省值。 创建画笔后,就可以使用turtle库提供的各种绘图函数来绘制各种形状和图形。一些常用的函数如下: ```python turtle.forward(distance) turtle.backward(distance) turtle.right(angle) turtle.left(angle) turtle.goto(x, y) turtle.circle(radius) ``` 这些函数可以让我们实现各种基本形状,例如直线、曲线、圆等。 为了实现更多复杂的绘图,我们可以使用循环和条件语句,来使代码更加流畅和可扩展。通常,我们会使用while循环或for循环来生成一系列图形,如以下示例: ```python for i in range(4): turtle.forward(100) turtle.right(90) ``` 这段代码会绘制一个正方形,使用四次循环来实现。 总的来说,Pythonturtle库提供了一个有趣、易用、功能强大的绘图工具,可以让用户通过代码来实现各种图形,并增强用户的编程能力和想像力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BugMiaowu2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值