使用python代码实现粉色爱心雨
时间: 2023-10-24 18:19:13 浏览: 148
很高兴回答你的问题!以下是用 Python 实现粉色爱心雨的代码:
```python
import random
import time
class HeartRain:
def __init__(self, width, height, heart_char='♥ '):
self.width = width
self.height = height
self.heart_char = heart_char
self.hearts = []
self.colors = ['\033[95m', '\033[91m', '\033[92m', '\033[93m', '\033[94m', '\033[95m', '\033[96m', '\033[97m']
def create_heart(self, x):
y = 0
heart = [x, y, random.randint(1, 4)]
self.hearts.append(heart)
def draw_hearts(self):
for heart in self.hearts:
color = random.choice(self.colors)
x, y, step = heart
if y < self.height:
print('\033[%d;%dH%s%s\033[0m' % (y, x, color, self.heart_char))
heart[1] += step
else:
self.hearts.remove(heart)
def run(self):
print('\033[2J')
while True:
self.create_heart(random.randint(0, self.width))
self.draw_hearts()
time.sleep(0.1)
if __name__ == '__main__':
rain = HeartRain(80, 24)
rain.run()
```
你可以把代码复制到你的 Python 环境中运行,然后就可以看到粉色爱心雨啦!
阅读全文