使用Python画了一棵圣诞树的实例代码

作者:曾亲桂林 时间:2022-06-18 23:55:04 

分享给大家一篇文章,教你怎样用Python画了一棵圣诞树,快来学习。

使用Python画了一棵圣诞树的实例代码

如何用Python画一个圣诞树呢?

最简单:


height = 5

stars = 1
for i in range(height):
 print((' ' * (height - i)) + ('*' * stars))
 stars += 2
print((' ' * height) + '|')

效果:

使用Python画了一棵圣诞树的实例代码

哈哈哈哈,总有一种骗了大家的感觉。

其实本文是想介绍Turtle库来画圣诞树。


import turtle

screen = turtle.Screen()
screen.setup(375, 700)


circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.speed('fastest')
circle.up()

square = turtle.Turtle()
square.shape('square')
square.color('green')
square.speed('fastest')
square.up()

circle.goto(0, 280)
circle.stamp()

k = 0
for i in range(1, 13):
 y = 30 * i
 for j in range(i - k):
   x = 30 * j
   square.goto(x, -y + 280)
   square.stamp()
   square.goto(-x, -y + 280)
   square.stamp()

 if i % 4 == 0:
   x = 30 * (j + 1)
   circle.color('red')
   circle.goto(-x, -y + 280)
   circle.stamp()
   circle.goto(x, -y + 280)
   circle.stamp()
   k += 3

 if i % 4 == 3:
   x = 30 * (j + 1)
   circle.color('yellow')
   circle.goto(-x, -y + 280)
   circle.stamp()
   circle.goto(x, -y + 280)
   circle.stamp()

square.color('brown')
for i in range(13, 17):
 y = 30 * i
 for j in range(2):
   x = 30 * j
   square.goto(x, -y + 280)
   square.stamp()
   square.goto(-x, -y + 280)
   square.stamp()

效果:

使用Python画了一棵圣诞树的实例代码

方法二:


import turtle


# 定义圣诞树的绿叶函数
def tree(d, s):
 if d <= 0:
   return
 turtle.forward(s)
 tree(d - 1, s * .8)
 turtle.right(120)
 tree(d - 3, s * .5)
 turtle.right(120)
 tree(d - 3, s * .5)
 turtle.right(120)
 turtle.backward(s)


n = 100
""" 设置绘图速度
'fastest' : 0
'fast'  : 10
'normal' : 6
'slow'  : 3
'slowest' : 1
"""
turtle.speed('fastest') # 设置速度

turtle.left(90)
turtle.forward(3 * n)
turtle.color("orange", "yellow")
turtle.left(126)


# turtle.begin_fill()
for i in range(5):
 turtle.forward(n / 5)
 turtle.right(144)
 turtle.forward(n / 5)
 turtle.left(72)
 turtle.end_fill()
turtle.right(126)
turtle.color("dark green")
turtle.backward(n * 4.8)

# 执行函数
tree(15, n)
turtle.backward(n / 5)

效果:

使用Python画了一棵圣诞树的实例代码

来源:https://blog.csdn.net/bigzql/article/details/110124594

标签:Python,圣诞树
0
投稿

猜你喜欢

  • PHP模板引擎Smarty的缓存使用总结

    2023-11-15 09:55:12
  • oracle数据库导入导出命令使用方法

    2023-07-22 19:06:39
  • MySQL5创建存储过程实例

    2010-06-13 12:49:00
  • python中的list字符串元素排序

    2022-03-14 12:27:14
  • 图标设计常犯的10种错误

    2008-03-06 13:40:00
  • JavaScript for: i++ vs i–

    2010-06-24 21:42:00
  • 盖座漂亮的“楼”–浅谈网页设计中的构图

    2010-09-10 12:44:00
  • 如何在Frontpage中定义CSS样式

    2008-08-02 12:32:00
  • Javascript优化五大原则

    2007-10-30 13:49:00
  • YUI3设计中的激进和妥协

    2010-01-17 09:59:00
  • JS实现两周内自动登录功能

    2023-08-04 21:20:57
  • VS2013设置护眼背景颜色

    2023-06-28 12:59:02
  • pytorch 实现张量tensor,图片,CPU,GPU,数组等的转换

    2023-08-12 07:44:34
  • .NET Core、Xamarin、.NET Standard和.NET Framework四者之间的区别介绍

    2023-07-13 03:38:13
  • 永不熄灭的爱心图标——腾讯公益月捐计划 “QQ首席图标”诞生记

    2009-09-01 19:43:00
  • Django模板获取field的verbose_name实例

    2023-07-30 06:53:55
  • Go语言实现的web爬虫实例

    2023-07-21 02:35:57
  • SQL Server 2005恢复Master库

    2011-05-16 13:11:00
  • apache集成php5.6方法分享

    2023-09-06 09:03:54
  • 统计热门文章的算法

    2008-03-16 15:40:00
  • asp之家 网络编程 m.aspxhome.com