画一个跳动的爱心代码 python
时间: 2023-12-14 16:34:04 浏览: 111
以下是一个画跳动的爱心的Python代码:
```python
import math
import os
import time
class Heart(object):
def __init__(self):
self._points = set() # 原始爱心坐标集合
self._edge_diffusion_points = set() # 边缘扩散效果点坐标集合
self._center_diffusion_points = set() # 中心扩散效果点坐标集合
self.all_points = {} # 每帧动态生成的所有点坐标集合
def _generate_heart_points(self, x_offset, y_offset):
for x in range(-14, 15):
for y in range(-14, 15):
if (x * 0.04) ** 2 + (y * 0.04) ** 2 - 1 <= 0:
self._points.add((x + x_offset, y + y_offset))
def _generate_diffusion_points(self, points_set, diffusion_set):
for point in points_set:
x, y = point
for i in range(-1, 2):
for j in range(-1, 2):
if i == 0 and j == 0:
continue
diffusion_set.add((x + i, y + j))
def _clear_screen(self):
os.system('cls' if os.name == 'nt' else 'clear')
def _print_heart(self):
self._clear_screen()
for y in range(-15, 15):
line = ''
for x in range(-30, 30):
if (x, y) in self._points:
line += '❤️'
elif (x, y) in self._edge_diffusion_points:
line += '*'
elif (x, y) in self._center_diffusion_points:
line += '+'
else:
line += ' '
print(line)
time.sleep(0.1)
def run(self):
x_offset = 0
y_offset = -10
while True:
self._points.clear()
self._edge_diffusion_points.clear()
self._center_diffusion_points.clear()
self._generate_heart_points(x_offset, y_offset)
self._generate_diffusion_points(self._points, self._edge_diffusion_points)
self._generate_diffusion_points(self._edge_diffusion_points, self._center_diffusion_points)
self.all_points = self._points | self._edge_diffusion_points | self._center_diffusion_points
self._print_heart()
x_offset += 1
y_offset += 1
if __name__ == '__main__':
heart = Heart()
heart.run()
```
阅读全文