python绘制神器五角星+小黄人+樱花

 更新时间:2022年07月07日 17:04:12   作者:王小王_123​​​​​​​  
这篇文章主要介绍了python绘制神器五角星+小黄人+樱花,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下

前言

这期文章我就为大家介绍关于python的艺术美——画图神器

在数学中有一种美,叫几何美,我们又称为艺术美,用具有规律的线条,迭代出美丽的图片,这就是许多艺术家在创作是的灵感渠道。下面我们就为大家准备了一个简单的五角星绘制。

涉及第三库的导入,模块的导入,还有一些我想我现在也介绍不完,下面就让我们一起来领略美感吧!

绘制五角星

import turtle
def draw_recursive_pentargram(size):
'''
迭代绘制五角星
'''
count = 1
while count <= 5:
turtle.backward(size)
turtle.right(144)
count += 1
# 五角星绘制完成,更新参数
size += 10
if size <= 100:
draw_recursive_pentargram(size)
def main():
'''
主函数
'''
turtle.speed(0)
# turtle.penup()
turtle.forward(40)
# turtle.pendown()
turtle.pensize(2)
turtle.pencolor("red")
turtle.bgcolor('black')
size = 50
draw_recursive_pentargram(size)
turtle.exitonclick()
if __name__ == "__main__":
main()

是不是感觉挺神奇的,其实这个算法是最简单的。下面我就让大家来见识一下它的真正的本领,感性的魅力。
这个代码我是去向大师借来的哟,不是小王自己写的,嘻嘻嘻。

小黄人绘制案例

# 小黄人绘制案例************************************************************************
import turtle
t = turtle.Turtle()
wn = turtle.Screen()
turtle.colormode(255)
t.hideturtle()
t.speed(10)
t.penup()
t.pensize(4)
t.goto(100,0)
t.pendown()
t.left(90)
t.color((0,0,0),(255,255,0))
#身体绘制上色
t.begin_fill()
t.forward(200)
t.circle(100,180)
t.forward(200)
t.circle(100,180)
t.end_fill()
#右眼睛绘制上色
t.pensize(12)
t.penup()
t.goto(-100,200)
t.pendown()
t.right(100)
t.circle(500,23)

t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()

t.penup()
t.goto(15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()

t.penup()
t.goto(35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#左眼睛绘制上色
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(90)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()

t.penup()
t.goto(-15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()

t.penup()
t.goto(-35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()

#嘴绘制上色
t.penup()
t.goto(-20,100)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(20,180)
t.left(90)
t.forward(40)
t.end_fill()

#裤子绘制上色
t.penup()
t.goto(-100,0)
t.pendown()
t.seth(0)
t.color("black","blue")
t.begin_fill()
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(160)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.seth(270)
t.penup()
t.goto(-100,0)
t.circle(100,180)
t.end_fill()

#左裤子腰带
t.penup()
t.goto(-70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(45)
t.forward(15)
t.left(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.left(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(-70,30)
t.dot()

#右裤腰带
t.penup()
t.goto(70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(135)
t.forward(15)
t.right(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.right(40)
t.forward(50)
t.end_fill()

t.left(180)
t.goto(70,30)

t.dot()

#脚
t.penup()
t.goto(4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.left(90)
t.forward(40)
t.seth(20)
t.circle(10,180)
t.circle(400,2)
t.seth(90)
t.forward(20)
t.goto(4,-100)
t.end_fill()

t.penup()
t.goto(-4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.right(90)
t.forward(40)
t.seth(20)
t.circle(10,-225)
t.circle(400,-3)
t.seth(90)
t.forward(21)
t.goto(-4,-100)
t.end_fill()

#左手
t.penup()
t.goto(-100,50)
t.pendown()
t.seth(225)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.left(90)
t.forward(35)
t.seth(90)
t.forward(50)
t.end_fill()
#右手
t.penup()
t.goto(100,50)
t.pendown()
t.seth(315)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.right(90)
t.forward(36)
t.seth(90)
t.forward(50)
t.end_fill()

#
t.penup()
t.goto(0,-100)
t.pendown()
t.forward(30)

#
t.penup()
t.goto(0,-20)
t.pendown()
t.color("yellow")
t.begin_fill()
t.seth(45)
t.forward(20)
t.circle(10,180)
t.right(90)
t.circle(10,180)
t.forward(20)
t.end_fill()

#
t.penup()
t.color("black")
t.goto(-100,-20)
t.pendown()
t.circle(30,90)

t.penup()
t.goto(100,-20)
t.pendown()
t.circle(30,-90)
#头顶
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(135)
t.circle(100,40)
t.end_fill()

t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(45)
t.circle(100,40)
t.end_fill()
turtle.exitonclick()

看似复杂而又繁琐的代码,它其实是由规律的,不信你可以自己看看它的写法。

运行美图:

小黄人不错吧!!!

樱花案例

import turtle as T
import random
import time
# 画樱花的躯干(60,t)
def Tree(branch, t):
time.sleep(0.0005)
if branch > 3:
if 8 <= branch <= 12:
if random.randint(0, 2) == 0:
t.color('snow') # 白
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branch / 3)
elif branch < 8:
if random.randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branch / 2)
else:
t.color('sienna') # 赭(zhě)色
t.pensize(branch / 10) # 6
t.forward(branch)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
Tree(branch - 10 * b, t)
t.left(40 * a)
Tree(branch - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branch)
t.down()
# 掉落的花瓣
def Petal(m, t):
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('lightcoral') # 淡珊瑚色
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
# 绘图区域
t = T.Turtle()
# 画布大小
w = T.Screen()
t.hideturtle() # 隐藏画笔
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat') # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
# 画樱花的躯干
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()

喜欢的小伙伴可以去运行一下,再次声明一下,上面两个复杂算法,是其他的人写的。

到此这篇关于python绘制神器五角星+小黄人+樱花的文章就介绍到这了,更多相关python绘制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python代码模拟CPU工作原理

    Python代码模拟CPU工作原理

    Python代码来实现一个最简单的CPU。用代码模拟大的部件,使大家从原理上理解CPU工作。使它可编程,支持加减法运算、读写内存、无条件跳转、条件跳转的功能。
    2023-01-01
  • 带你学习Python如何实现回归树模型

    带你学习Python如何实现回归树模型

    这篇文章主要介绍了Python如何实现回归树模型,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • python数据可视化之初探 Seaborn

    python数据可视化之初探 Seaborn

    Seaborn 是一个基于 Matplotlib 的 Python 数据可视化库,它提供了更高级别的接口,使得创建美观的统计图形变得非常简单,在这篇文章中,我们将讨论 Seaborn 的基础使用方法,包括如何创建各种常见的统计图形
    2023-07-07
  • PyTorch中的nn.ConvTranspose2d模块详解

    PyTorch中的nn.ConvTranspose2d模块详解

    nn.ConvTranspose2d是PyTorch中用于实现二维转置卷积的模块,广泛应用于生成对抗网络(GANs)和卷积神经网络(CNNs)的解码器中。该模块通过参数如输入输出通道数、卷积核大小、步长、填充等,能控制输出尺寸和避免棋盘效应
    2024-09-09
  • 18个好用的Python技巧分享(建议收藏)

    18个好用的Python技巧分享(建议收藏)

    在这篇文章中,我们将讨论最常用的一些python技巧,这些技巧都是在日常工作中使用过的简单的Trick,小编觉得好东西就是要拿出来和大家一起分享哒
    2023-07-07
  • python3实现字符串的全排列的方法(无重复字符)

    python3实现字符串的全排列的方法(无重复字符)

    这篇文章主要介绍了python3实现字符串的全排列的方法(无重复字符),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • 利用python中集合的唯一性实现去重

    利用python中集合的唯一性实现去重

    集合,用{ }表示,集合中所有元素具有唯一性。这篇文章给大家介绍利用python中集合的唯一性实现去重,感兴趣的朋友一起看看吧
    2020-02-02
  • pytorch加载语音类自定义数据集的方法教程

    pytorch加载语音类自定义数据集的方法教程

    这篇文章主要给大家介绍了关于pytorch加载语音类自定义数据集的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • python正则表达式抓取成语网站

    python正则表达式抓取成语网站

    做NLPproject时需要一个成语库,我需要的是纯成语,网上找的都是有详细解释的。于是自己写了一个爬成语的python程序
    2013-11-11
  • 分享一个可以生成各种进制格式IP的小工具实例代码

    分享一个可以生成各种进制格式IP的小工具实例代码

    这篇文章主要给大家分享了一个可以生成各种进制格式IP的小工具,利用的语言是python实现的一个小工具,这个小工具对大家的日常使用与开发具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。
    2017-07-07

最新评论