圣诞树代码html飘雪花
时间: 2023-12-16 20:28:33 浏览: 490
以下是Python编写的圣诞树代码,可以生成HTML文件,并且在HTML文件中飘雪花:
```python
import random
# 生成随机颜色
def random_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return f"rgb({r}, {g}, {b})"
# 生成HTML文件
def generate_html(height):
# 生成树干
trunk = ""
for i in range(height):
trunk += " " * (height - 1) + "*<br>"
# 生成树叶
leaves = ""
for i in range(height):
leaves += " " * (height - i - 1)
leaves += "*" * (2 * i + 1) + "<br>"
# 生成雪花
snowflakes = ""
for i in range(50):
snowflakes += f'<div class="snowflake" style="top: {random.randint(0, 100)}%; left: {random.randint(0, 100)}%; color: {random_color()};">❄</div>'
# 生成HTML文件
html = f'''
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Christmas Tree</title>
<style>
body {{
background-color: black;
}}
.tree {{
color: green;
font-size: 20px;
font-family: monospace;
white-space: pre;
}}
.snowflake {{
position: fixed;
font-size: 20px;
z-index: 9999;
user-select: none;
pointer-events: none;
animation: snowflake 5s linear infinite;
}}
@keyframes snowflake {{
0% {{
transform: translateY(-100%);
}}
100% {{
transform: translateY(100vh);
}}
}}
</style>
</head>
<body>
<div class="tree">
{trunk}{leaves}
</div>
{snowflakes}
</body>
</html>
'''
# 写入HTML文件
with open("christmas_tree.html", "w") as f:
f.write(html)
# 控制台输入树的高度
height = int(input("请输入树的高度:"))
# 生成HTML文件
generate_html(height)
```
你可以将上述代码复制到Python文件中并运行,然后在控制台输入树的高度,程序将会生成一个名为`christmas_tree.html`的HTML文件,其中包含了圣诞树和飘落的雪花效果。
阅读全文