如何用Python画一个圣诞树呢?
【圣诞节!教你用 Python 画棵圣诞树】最简单:
1height = 5
2
3stars = 1
4for i in range(height):
5print((' ' * (height - i)) + ('*' * stars))
6stars += 2
7print((' ' * height) + '|')
效果:
文章图片
哈哈哈哈,总有一种骗了大家的感觉。
其实本文是想介绍Turtle库来画圣诞树。
方法一:
1import turtle
2screen = turtle.Screen()
3screen.setup(800,600)
4circle = turtle.Turtle()
5circle.shape('circle')
6circle.color('red')
7circle.speed('fastest')
8circle.up()
9square = turtle.Turtle()
10square.shape('square')
11square.color('green')
12square.speed('fastest')
13square.up()
14circle.goto(0,280)
15circle.stamp()
16k = 0
17for i in range(1, 17):
18y = 30*i
19for j in range(i-k):
20x = 30*j
21square.goto(x,-y+280)
22square.stamp()
23square.goto(-x,-y+280)
24square.stamp()
25if i % 4 == 0:
26x = 30*(j+1)
27circle.color('red')
28circle.goto(-x,-y+280)
29circle.stamp()
30circle.goto(x,-y+280)
31circle.stamp()
32k += 2
33if i % 4 == 3:
34x = 30*(j+1)
35circle.color('yellow')
36circle.goto(-x,-y+280)
37circle.stamp()
38circle.goto(x,-y+280)
39circle.stamp()
40square.color('brown')
41for i in range(17,20):
42y = 30*i
43for j in range(3):
44x = 30*j
45square.goto(x,-y+280)
46square.stamp()
47square.goto(-x,-y+280)
48square.stamp()
49turtle.exitonclick()
效果:
文章图片
方法二:
1from turtle import *
2import random
3import time
4
5n = 80.0
6
7speed("fastest")
8screensize(bg='seashell')
9left(90)
10forward(3*n)
11color("orange", "yellow")
12begin_fill()
13left(126)
14
15for i in range(5):
16forward(n/5)
17right(144)
18forward(n/5)
19left(72)
20end_fill()
21right(126)
22
23color("dark green")
24backward(n*4.8)
25def tree(d, s):
26if d <= 0: return
27forward(s)
28tree(d-1, s*.8)
29right(120)
30tree(d-3, s*.5)
31right(120)
32tree(d-3, s*.5)
33right(120)
34backward(s)
35tree(15, n)
36backward(n/2)
37
38for i in range(200):
39a = 200 - 400 * random.random()
40b = 10 - 20 * random.random()
41up()
42forward(b)
43left(90)
44forward(a)
45down()
46if random.randint(0, 1) == 0:
47color('tomato')
48else:
49color('wheat')
50circle(2)
51up()
52backward(a)
53right(90)
54backward(b)
55time.sleep(60)
效果:
文章图片
感兴趣的话就赶紧尝试一下吧!
推荐阅读
- 推荐系统论文进阶|CTR预估 论文精读(十一)--Deep Interest Evolution Network(DIEN)
- Python专栏|数据分析的常规流程
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置
- Python绘制小红花
- Pytorch学习|sklearn-SVM 模型保存、交叉验证与网格搜索
- OpenCV|OpenCV-Python实战(18)——深度学习简介与入门示例
- python|8. 文件系统——文件的删除、移动、复制过程以及链接文件
- 爬虫|若想拿下爬虫大单,怎能不会逆向爬虫,价值过万的逆向爬虫教程限时分享
- 分布式|《Python3网络爬虫开发实战(第二版)》内容介绍
- java|微软认真聆听了开源 .NET 开发社区的炮轰( 通过CLI 支持 Hot Reload 功能)