python爱心代码完整
时间: 2023-12-08 18:38:30 浏览: 70
以下是Python实现动态漂浮爱心的完整代码:
```python
import os
import time
import random as ra
class Heart():
# 每个爱心(爱心类)
def __init__(self):
self.r = ra.randint(10,15) # 爱心的半径
self.x = ra.randint(-1000,1000) # 爱心的横坐标
self.y = ra.randint(-800,800) # 爱心的纵坐标
self.color = [ra.randint(0,255) for i in range(3)] # 爱心的颜色
def draw(self):
# 画爱心
os.system('cls') # 清屏
print('\033[1;31m') # 设置字体颜色为红色
for i in range(-self.r,self.r+1):
for j in range(-self.r,self.r+1):
if (i**2+j**2)**0.5 <= self.r:
print('♥',end=' ')
else:
print(' ',end=' ')
print()
print('\033[0m') # 恢复字体颜色
def move(self):
# 爱心移动
self.x += ra.randint(-5,5)
self.y += ra.randint(-5,5)
def change_color(self):
# 改变爱心颜色
self.color = [ra.randint(0,255) for i in range(3)]
while True:
heart = Heart() # 创建一个新的爱心
while True:
heart.draw() # 画爱心
heart.move() # 移动爱心
heart.change_color() # 改变爱心颜色
time.sleep(0.1) # 暂停0.1秒
```
阅读全文