原标题:圣诞节!教你用Python画棵圣诞树
| 作者:糖糖甜甜
| 来源:经管人学数据分析(ID:DAT-2017)
| 欢迎转载,请注明出处。
如何用python画一个圣诞树呢?
最简单:
1height = 5
2
3stars = 1
4fori inrange(height):
5print((' '* (height - i)) + ('*'* stars))
6stars += 2
7print((' '* height) + '|')
效果:
哈哈哈哈,总有一种骗了大家的感觉。
其实本文是想介绍turtle库来画圣诞树。
方法一:
1importturtle
2screen = ()
(800,600)
4circle = ()
('circle')
('red')
('fastest')
()
9square = ()
('square')
('green')
('fastest')
()
(0,280)
()
16k = 0
17fori inrange(1, 17):
18y = 30*i
19forj inrange(i-k):
20x = 30*j
(x,-y+280)
()
(-x,-y+280)
()
25ifi % 4== 0:
26x = 30*(j+1)
('red')
(-x,-y+280)
()
(x,-y+280)
()
32k += 2
33ifi % 4== 3:
34x = 30*(j+1)
('yellow')
(-x,-y+280)
()
(x,-y+280)
()
('brown')
41fori inrange(17,20):
42y = 30*i
43forj inrange(3):
44x = 30*j
(x,-y+280)
()
(-x,-y+280)
()
()
效果:
方法二:
1fromturtle import*
2importrandom
3importtime
4
5n = 80.0
6
7speed("fastest")
8screensize(bg='seashell')
9left(90)
10forward(3*n)
11color("orange", "yellow")
12begin_fill()
13left(126)
14
15fori inrange(5):
16forward(n/5)
17right(144)
18forward(n/5)
19left(72)
20end_fill()
21right(126)
22
23color("dark green")
24backward(n*4.8)
25deftree(d, s):
26ifd <= 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
38fori inrange(200):
39a = 200- 400* ()
40b = 10- 20* ()
41up()
42forward(b)
43left(90)
44forward(a)
45down()
(0, 1) == 0:
47color('tomato')
48else:
49color('wheat')
50circle(2)
51up()
52backward(a)
53right(90)
54backward(b)
(60)
效果:
感兴趣的话就赶紧尝试一下吧!
祝大家圣诞节快乐!!!
责任编辑: