Python turtle艺术画赏析-演道网

我一直在玩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()

直线脉冲效果图如下:

Python turtle艺术画赏析

示例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()

Python turtle艺术画赏析

Python turtle艺术画赏析

示例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退出。

效果如下:

Python turtle艺术画赏析

Python turtle艺术画赏析相关完整代码可从以下信息的得到下载:

——————————————分割线——————————————

全部文件在Linux公社1号FTP服务器

FTP地址:ftp://ftp1.linuxidc.com

用户名:ftp1.linuxidc.com

密码:www.linuxidc.com

在 2019年LinuxIDC.com/4月/Python turtle艺术画赏析/

下载方法见 http://www.linuxidc.com/Linux/2013-10/91140.htm

——————————————分割线—————————————— 

更多Python相关信息见Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.as

转载自演道,想查看更及时的互联网产品技术热点文章请点击http://go2live.cn