python海龟作图红绿灯_海龟作图—用Python绘图
大家好,又见面了,我是你们的朋友全栈君。
一、关于Turtle
“turtle是一个简单的绘图工具。它提供了一个海龟,你可以把它理解为一个机器人,只听得懂有限的指令”
操纵海龟绘图有着许多的命令,这些命令可以划分为两种:一种为运动命令,一种为画笔控制命令。
二、运动命令
forward(degree)
#向前移动距离degree代表距离
backward(degree)
#向后移动距离degree代表距离
right(degree)
#向右移动多少度
left(degree)
#向左移动多少度
goto(x,y)
#将画笔移动到坐标为x,y的位置
speed(speed)
#画笔绘制的速度范围[0,10]整数
三、画笔控制命令
down()
画笔落下,移动时绘制图形
up()
画笔抬起,移动时不绘制图形
setheading(degree)
海龟朝向,degree代表角度
reset()
恢复所有设置
pensize(width)
画笔的宽度
pencolor(colorstring)
画笔的颜色
fillcolor(colorstring)
绘制图形的填充颜色
fill(True)
fill(False)
四、程序体验
1.奥运五环
代码:
#绘制奥运五环
import turtle
turtle.width(15) #画笔粗细
turtle.color(“blue”)
turtle.circle(50)
turtle.penup()
turtle.goto(120,0)
turtle.down()
turtle.color(“black”)
turtle.circle(50)
turtle.penup()
turtle.goto(240,0)
turtle.down()
turtle.color(“red”)
turtle.circle(50)
turtle.penup()
turtle.goto(60,-50)
turtle.down()
turtle.color(“yellow”)
turtle.circle(50)
turtle.penup()
turtle.goto(180,-50)
turtle.down()
turtle.color(“green”)
turtle.circle(50)
显示效果:
2.使用递归,可以绘制出非常复杂的图形。例如,下面的代码可以绘制一棵分型树:
from turtle import *
# 设置色彩模式是RGB:
colormode(255)
lt(90)
lv = 14
l = 120
s = 45
width(lv)
# 初始化RGB颜色:
r = 0
g = 0
b = 0
pencolor(r, g, b)
penup()
bk(l)
pendown()
fd(l)
def draw_tree(l, level):
global r, g, b
# save the current pen width
w = width()
# narrow the pen width
width(w * 3.0 / 4.0)
# set color:
r = r + 1
g = g + 2
b = b + 3
pencolor(r % 200, g % 200, b % 200)
l = 3.0 / 4.0 * l
lt(s)
fd(l)
if level < lv:
draw_tree(l, level + 1)
bk(l)
rt(2 * s)
fd(l)
if level < lv:
draw_tree(l, level + 1)
bk(l)
lt(s)
# restore the previous pen width
width(w)
speed(“fastest”)
draw_tree(l, 4)
done()
显示效果:执行上述程序需要花费一定的时间,最后的效果如下
原文:https://www.cnblogs.com/Fairy-02-11/p/12778477.html
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/154091.html原文链接:https://javaforall.cn
相关文章
- python pip源更换国内镜像,速度加快10倍
- 用Python写个魂斗罗
- python中griddata的外插值_利用griddata进行二维插值
- Python基础14-内置模块
- Python元祖详解
- python提取xml指定内容
- 慎用!Python 实现微信消息轰炸
- 造数据神器Faker,一个有趣又实用的Python第三方库
- Python模拟一个用户登录系统
- python 制作淘宝秒杀脚本
- 如何用python画一朵樱花_如何用python绘制粉色樱花
- Python与数据库之学员管理系统「建议收藏」
- python interpolate.interp1d_索引错误scipy.interpolate.interp1d「建议收藏」
- python三种基本数据类型有哪些_python中有哪些基本数据类型
- 视频识别车牌号(Python)
- Python-基础02-程序与用户交互
- Python爬取热搜数据之炫酷可视化[通俗易懂]
- python解释器找不到_python解释器路径
- Python抓取腾视频所有电影,不用钱就可以看会员电影
- Python 图_系列之基于<链接表>实现无向图最短路径搜索