python跳动爱心代码高级
时间: 2023-11-30 13:43:09 浏览: 140
爱心源码-通过Python画一颗跳动的心
以下是Python实现跳动爱心的高级代码:
```python
import tkinter as tk
import math
class Love(tk.Tk):
def __init__(self):
super().__init__()
self.title('跳动的爱心')
self.geometry('800x600')
self.canvas = tk.Canvas(self, bg='white')
self.canvas.pack(fill=tk.BOTH, expand=True)
self.hearts = []
self.create_heart()
self.animate()
def create_heart(self):
for i in range(100):
x = 400 + 200 * math.sin(i / 10)
y = 300 + 200 * math.cos(i / 10) - 200 * math.cos(2 * i / 10)
self.hearts.append(self.canvas.create_text(x, y, text='❤', font=('Arial', 24), fill='red'))
def animate(self):
for i, heart in enumerate(self.hearts):
x, y = self.canvas.coords(heart)
x += 2 * math.sin(i / 10)
y -= 2
self.canvas.coords(heart, x, y)
self.after(50, self.animate)
if __name__ == '__main__':
love = Love()
love.mainloop()
```
阅读全文