python实现粉色爱心代码
时间: 2023-11-16 18:07:48 浏览: 105
以下是Python实现粉色爱心的代码:
```python
from tkinter import *
from math import sin, cos
# 设置画布大小和中心点
CANVAS_WIDTH = 600
CANVAS_HEIGHT = 600
CANVAS_CENTER_X = CANVAS_WIDTH / 2
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2
# 设置收缩率和放大倍数
IMAGE_ENLARGE = 20
SHRINK_RATIO = 0.02
# 定义爱心函数
def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
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)
# 绘制爱心
def draw_heart(root, canvas):
# 设置画笔颜色和宽度
canvas.config(bg='pink')
canvas.create_line(heart_function(0), heart_function(0), fill='red', width=2)
for t in range(1, 628):
canvas.create_line(heart_function(t * SHRINK_RATIO), heart_function(t), fill='red', width=2)
# 显示画布
canvas.pack()
root.mainloop()
if __name__ == '__main__':
root = Tk()
canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
draw_heart(root, canvas)
```
阅读全文