圣诞树动态python
时间: 2023-12-15 18:06:19 浏览: 119
圣诞树源码(python)
以下是两个Python版本的圣诞树动态效果的代码和演示:
1. 粉色圣诞树动态效果
代码地址:https://github.com/ReadSmall/christmasTree/blob/main/Python%E7%89%88/ChristmasTree05_Pink.py
演示:
```python
# 导入必要的库
import time
# 定义变量
tree = [' * ', ' *** ', ' ***** ', '*******', ' *** ', ' *** ', ' *** ']
lights = ['🎄', '❤️', '💛', '💚', '💙', '💜']
light_index = 0
# 循环输出圣诞树和灯光效果
while True:
# 输出圣诞树
for i in tree:
print(i.center(50))
time.sleep(0.2)
# 输出灯光效果
for i in range(7):
tree[i] = tree[i].replace(' ', lights[light_index])
print(tree[i].center(50))
time.sleep(0.2)
tree[i] = tree[i].replace(lights[light_index], ' ')
# 更新灯光颜色
light_index = (light_index + 1) % len(lights)
```
2. 绿色圣诞树动态效果
代码地址:https://github.com/ReadSmall/christmasTree/blob/main/Python%E7%89%88/ChristmasTree04_Green.py
演示:
```python
# 导入必要的库
import time
# 定义变量
tree = [' * ', ' *** ', ' ***** ', '*******', ' *** ', ' *** ', ' *** ']
lights = ['🎄', '❤️', '💛', '💚', '💙', '💜']
light_index = 0
# 循环输出圣诞树和灯光效果
while True:
# 输出圣诞树
for i in tree:
print(i.center(50))
time.sleep(0.2)
# 输出灯光效果
for i in range(7):
tree[i] = tree[i].replace(' ', lights[light_index])
print(tree[i].center(50))
time.sleep(0.2)
tree[i] = tree[i].replace(lights[light_index], ' ')
# 更新灯光颜色
light_index = (light_index + 1) % len(lights)
```
阅读全文