用python写一个樱花落下的代码
时间: 2023-12-17 18:09:28 浏览: 117
import os
import random
import time
# 雪花字符集
sakura = ["❀", "✿", "❁", "✾", "❃", "❋", "❀", "✿", "❁", "✾", "❃", "❋", "❀", "✿", "❁", "✾", "❃", "❋"]
# 获取终端的宽度和高度
terminal_width = os.get_terminal_size().columns
terminal_height = os.get_terminal_size().lines
# 樱花的数量
sakura_count = 20
# 定义一个樱花类
class Sakura:
def __init__(self):
self.column = random.randint(0, terminal_width-1) # 列数
self.row = random.randint(-terminal_height, 0) # 行数
self.speed = random.randint(1, 3) # 下落速度
def move(self):
self.row += self.speed
def show(self):
if 0 <= self.row < terminal_height:
print("\033[{};{}H{}".format(self.row, self.column, random.choice(sakura)), end="")
def clear(self):
if 0 <= self.row < terminal_height:
print("\033[{};{}H{}".format(self.row, self.column, " "), end="")
# 初始化樱花列表
sakura_list = []
for i in range(sakura_count):
sakura_list.append(Sakura())
# 开始落花
while True:
for sakura in sakura_list:
sakura.clear()
sakura.move()
sakura.show()
time.sleep(0.1) # 刷新间隔
阅读全文