跳动的爱心python 代码
时间: 2023-10-05 18:07:38 浏览: 70
以下是一个简单的跳动爱心的 Python 代码:
```python
import time
heart = '\033[91m♥\033[0m'
empty = ' '
heart_matrix = [
[empty, heart, empty, empty, heart, empty, empty, empty, heart, empty, empty, empty, heart, empty, empty],
[heart, empty, heart, empty, empty, heart, empty, heart, empty, heart, empty, empty, empty, heart, empty],
[heart, empty, empty, heart, empty, empty, heart, empty, empty, empty, heart, empty, empty, empty, heart],
[heart, empty, empty, empty, heart, empty, empty, empty, empty, empty, heart, empty, empty, empty, heart],
[empty, heart, empty, empty, empty, heart, empty, empty, empty, heart, empty, empty, heart, empty, empty],
[empty, empty, heart, empty, empty, empty, heart, empty, heart, empty, empty, heart, empty, empty, empty],
[empty, empty, empty, heart, empty, empty, empty, heart, empty, empty, heart, empty, empty, empty, heart],
[empty, empty, empty, empty, heart, empty, empty, empty, empty, heart, empty, empty, empty, heart, empty],
[empty, empty, empty, empty, empty, heart, empty, empty, heart, empty, empty, empty, heart, empty, empty],
[empty, empty, empty, empty, empty, empty, heart, heart, empty, empty, empty, heart, empty, empty, empty]
]
while True:
for i in range(len(heart_matrix)):
for j in range(len(heart_matrix[i])):
print(heart_matrix[i][j], end='')
print()
time.sleep(0.2)
heart_matrix[0], heart_matrix[-1] = heart_matrix[-1], heart_matrix[0]
for i in range(len(heart_matrix)):
heart_matrix[i][0], heart_matrix[i][-1] = heart_matrix[i][-1], heart_matrix[i][0]
```
这个代码使用 ANSI 转义序列来实现在命令行界面中输出彩色字符。它会打印一个跳动的爱心,并且每隔 0.2 秒调换一次爱心的位置,让它看起来像是在跳动。
阅读全文