使用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
投稿

猜你喜欢

  • 看看如何用Python绘制小米新版天价logo

    2023-04-09 06:01:10
  • MySql使用skip-name-resolve解决外网链接客户端过慢问题

    2024-01-26 16:07:35
  • Tensorflow Summary用法学习笔记

    2023-08-12 15:08:53
  • 学习ASP和编程的28个观点

    2008-06-27 12:57:00
  • 详解Python之Scrapy爬虫教程NBA球员数据存放到Mysql数据库

    2024-01-24 03:27:04
  • Python中的with...as用法介绍

    2023-03-30 11:17:19
  • asp 过滤尖括号内所有内容的正则代码

    2011-04-03 10:40:00
  • Python可视化模块altair的使用详解

    2023-10-03 09:00:28
  • 在python里使用await关键字来等另外一个协程的实例

    2021-03-01 19:10:16
  • jasypt 集成SpringBoot 数据库密码加密操作

    2024-01-26 03:34:48
  • 关于vue中根据用户权限动态添加路由的问题

    2024-05-05 09:25:11
  • python 对任意数据和曲线进行拟合并求出函数表达式的三种解决方案

    2021-10-02 12:40:16
  • python爬虫使用requests发送post请求示例详解

    2022-02-26 16:50:22
  • JavaScript编写棋盘覆盖代码详解

    2024-04-17 10:30:05
  • python 基于Appium控制多设备并行执行

    2022-12-11 12:00:06
  • 利用python实现可视化大屏

    2023-08-17 17:29:17
  • 如何配置关联Python 解释器 Anaconda的教程(图解)

    2021-06-13 15:27:30
  • MySQL 5.6 中 TIMESTAMP 的变化分析

    2024-01-22 04:30:14
  • python Matplotlib画图之调整字体大小的示例

    2023-03-27 09:43:30
  • Python利用plotly绘制正二十面体详解

    2021-07-25 13:14:27
  • asp之家 网络编程 m.aspxhome.com