我一直在玩Python的turtle图形 - 这是一种有趣的学习方式,通过一些实验和思考,你可以想出画出各种有趣的图像。
示例1:
第一张图片的代码非常简单,绘制一束直线光线,使用嵌套循环使其变小,并且在每条射线的末尾涂鸦。
import turtle
import random
turtle.tracer(0, 0)
mi = turtle.Screen()
mi.colormode(255)
turtle.bgcolor("black")
alex = turtle.Turtle()
alex.speed(10)
alex.goto(0,0)
alex.pensize(0)
alex.ht()
for i in range(500):
alex.color(random.randrange(256),random.randrange(256),random.randrange(256))
alex.goto(round(random.gauss(0,150),0),round(random.gauss(0,150),0))
x = alex.xcor()
y = alex.ycor()
for j in range(25):
z = round(random.gauss(0,5), 0)
a = round(random.gauss(0,5), 0)
alex.color(random.randrange(256),random.randrange(256),random.randrange(256))
alex.pensize(0)
alex.goto(x + z, y + a)
alex.goto(z,a)
turtle.update()
mi.exitonclick()
直线脉冲效果图如下:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555396869724" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
如果你在学习Python的过程当中有遇见任何问题,可以加入我的python交流学习qq群:683380553,多多交流问题,互帮互助,群里有不错的学习教程和开发工具。学习python有任何问题(学习方法,学习效率,如何就业),可以随时来咨询我,如果你准备学习大数据,也欢迎加入大数据学习交流qq群683380553,每天与大家分享学习资源哦。
示例2:
画三组同心弯曲的光线,从黑暗中浮现,生成随机RGB值,sat range 0-1, val range = 0-1。
import random
import turtle
def colorGen(sat = 1, val = 1):
rand1 = round(random.randrange(256)*val)
rand2 = round(random.randrange(256)*val)
sat2 = round((1-sat)*255)
if rand1 >= rand2:
rand1 = round(255*val)
else:
rand2 = round(255*val)
color = [[sat2, rand1, rand2], [rand1, sat2, rand2], [sat2, rand2, rand1],
[rand1, rand2, sat2], [rand2, sat2, rand1], [rand2, rand1, sat2]]
return tuple(color[random.randrange(6)])
def timeTunnel(repeats = 1,linuxmi = 10, stepVar = 1):
for i in range(repeats):
alex.goto(0,0)
alex.seth(random.uniform(0,360)) # 设定标题
h1 = alex.heading() # 设定标题
alex.color(colorGen(val=0))
for j in range(10):
alex.down()
alex.forward(abs(round(random.gauss(10, stepVar),0)))
alex.seth(h1 + random.gauss(0,linuxmi))
x = alex.xcor()
y = alex.ycor()
alex.color(colorGen(val = j/10))
f2 = alex.heading()
for k in range(3):
alex.down()
alex.seth(f2 + random.gauss(0, linuxmi))
h3 = alex.heading()
for u2 in range(10):
alex.color(colorGen(val = u2/10))
alex.seth(h3 + random.gauss(0,linuxmi))
alex.forward(abs(round(random.gauss(10, stepVar), 0)))
m = alex.xcor()
n = alex.ycor()
h4 = alex.heading()
for l in range(2):
alex.color(colorGen())
alex.down()
alex.seth(abs(h4 + random.gauss(0,linuxmi)))
h5 = alex.heading()
for l2 in range(10):
alex.color(colorGen(val = l2/10))
alex.seth(h5 + random.gauss(0,linuxmi))
alex.forward(abs(round(random.gauss(10,stepVar),0)))
alex.up()
alex.goto(x, y)
alex.up()
turtle.tracer(0, 0)
wn = turtle.Screen()
wn.colormode(255)
turtle.bgcolor("black")
alex = turtle.Turtle()
alex.speed(10)
alex.pensize(0)
alex.ht()
timeTunnel(300)
turtle.update()
wn.exitonclick()
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555396869733" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image> <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555396869736" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
示例3:
代码如下:
from turtle import * # 导入模块turtle
speed(0) # 将绘图速度设置为0,这是最快的
pencolor('red') # 设置钢笔/线条的颜色为红色
bgcolor('black') # 设置背景/画布的颜色为黑色
x = 0 # 创建一个值为0的变量x
up() # 举起笔,这样就不会画线了
d()表示向前移动,bk() 示向后移动
rt()或lt()表示向右倾斜一定角度
rt(45)
fd(90)
rt(135)
down() # 放下笔,让乌龟画画
while x < 120: # 当x的值小于120时,
连续这样做:
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
rt(11.1111)
x = x+1 # #在x的值上加1,
所以每次循环后它都接近120
exitonclick() # 当您单击时,turtle退出。
效果如下:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555396869743" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
网友评论