python 画圣诞树
时间: 2023-12-20 19:32:49 浏览: 87
以下是一个使用Python画圣诞树的例子:
```python
def draw_christmas_tree(height):
for i in range(height):
print(' ' * (height - i - 1) + '*' * (2 * i + 1))
print(' ' * (height - 1) + '|')
height = 5
draw_christmas_tree(height)
```
这段代码会输出一个高度为5的圣诞树,效果如下:
```
*
***
*****
*******
*********
|
```
相关问题
Python画圣诞树
以下是使用Python绘制圣诞树的示例代码:
```python
import turtle
# 设置画布和画笔
screen = turtle.Screen()
screen.bgcolor("black")
pen = turtle.Turtle()
pen.speed(0)
pen.color("green")
# 绘制圣诞树
def draw_tree():
pen.penup()
pen.goto(0, -200)
pen.pendown()
pen.setheading(90)
pen.forward(200)
draw_branch(100, 60)
def draw_branch(branch_length, angle):
if branch_length < 10:
pen.color("green")
else:
pen.color("brown")
if branch_length > 5:
pen.forward(branch_length)
pen.right(angle)
draw_branch(branch_length * 0.7, angle)
pen.left(angle * 2)
draw_branch(branch_length * 0.7, angle)
pen.right(angle)
pen.backward(branch_length)
draw_tree()
# 绘制雪花
def draw_snow():
pen.penup()
pen.goto(-248, -100)
pen.pendown()
pen.color("white")
pen.pensize(2)
for i in range(80):
pen.goto(randint(-248, 248), randint(-100, 248))
petal_number = 6
snow_size = int(randint(2, 10))
for j in range(petal_number):
pen.forward(snow_size)
pen.backward(snow_size)
pen.right(360 / petal_number)
draw_snow()
turtle.done()
```
python绘画圣诞树
以下是绘制圣诞树的Python代码示例:
```python
import turtle
import random
# 设置画笔
tree = turtle.Turtle()
tree.speed(0)
turtle.bgcolor("white")
tree.color("green")
# 绘制树干
def draw_trunk():
tree.color("brown")
tree.pensize(20)
tree.penup()
tree.goto(0, -200)
tree.pendown()
tree.goto(0, -100)
# 绘制树叶
def draw_triangle(length):
tree.color("green")
tree.begin_fill()
tree.forward(length)
tree.left(120)
tree.forward(length)
tree.left(120)
tree.forward(length)
tree.end_fill()
# 绘制树
def draw_tree(height):
tree.penup()
tree.goto(0, -100)
tree.pendown()
tree.left(90)
for i in range(height):
draw_triangle(20 * (i + 1))
tree.penup()
tree.right(120)
tree.forward(20)
tree.left(120)
tree.pendown()
# 绘制装饰物
def draw_decorations(height):
colors = ["red", "blue", "yellow", "purple", "orange"]
for i in range(height):
for j in range(i + 1):
tree.penup()
tree.goto(-20 * i + 40 * j, -100 + 20 * i)
tree.pendown()
tree.color(random.choice(colors))
tree.begin_fill()
tree.circle(10)
tree.end_fill()
# 绘制完整的圣诞树
def draw_christmas_tree(height, decorations):
draw_tree(height)
# Position the turtle for drawing decorations
tree.left(90)
tree.goto(0, height * 20)
tree.right(90)
draw_decorations(decorations)
# 调用函数绘制圣诞树
draw_trunk()
draw_christmas_tree(10, 50)
# 隐藏画笔
tree.hideturtle()
# 显示绘制结果
turtle.done()
```
阅读全文