python李询同款爱心代码
时间: 2023-12-01 10:43:49 浏览: 115
以下是李询同款爱心代码:
```python
import random
from math import sin, cos, pi, log
from tkinter import *
# 画布大小
CANVAS_WIDTH = 600
CANVAS_HEIGHT = 600
# 画布中心
CANVAS_CENTER_X = CANVAS_WIDTH // 2
CANVAS_CENTER_Y = CANVAS_HEIGHT // 2
# 放大比例
IMAGE_ENLARGE = 20
def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
"""
“爱心函数生成器”
:param shrink_ratio: 放大比例
:param t: 参数
:return: 坐标
"""
# 基础函数
x = 16 * (sin(t) ** 3)
y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
# 放大
x *= shrink_ratio
y *= shrink_ratio
# 移到画布中央
x += CANVAS_CENTER_X
y += CANVAS_CENTER_Y
return int(x), int(y)
# 创建画布
root = Tk()
root.title("李询同款爱心")
canvas = Canvas(root, width=CANVAS_WIDTH, height=CANVAS_HEIGHT, bg="white")
canvas.pack()
# 画爱心
t = 0
while True:
# 获取坐标
x, y = heart_function(t)
# 随机生成颜色
color = "#"
for i in range(6):
color += random.choice("0123456789abcdef")
# 画点
canvas.create_oval(x, y, x + 1, y + 1, fill=color)
# 更新参数
t += 0.01
# 退出条件
if t > 2 * pi:
break
# 进入消息循环
root.mainloop()
```
阅读全文