p python爱心代码跳动
时间: 2023-12-25 18:30:10 浏览: 103
以下是一个Python爱心代码跳动的例子:
```python
import random
from math import sin, cos, pi, log
from tkinter import *
# 创建窗口
window = Tk()
window.title("跳动的爱心")
window.geometry("800x600")
# 创建画布
canvas = Canvas(window, width=800, height=600, bg="black")
canvas.pack()
# 定义爱心的参数
heart_color = ["red", "pink", "purple", "blue", "green", "yellow", "orange"]
heart_size = [10, 20, 30, 40, 50]
heart_speed = [1, 2, 3, 4, 5]
# 创建爱心函数
def create_heart():
x = random.randint(50, 750)
y = random.randint(50, 550)
size = random.choice(heart_size)
color = random.choice(heart_color)
speed = random.choice(heart_speed)
return [x, y, size, color, speed]
# 绘制爱心函数
def draw_heart():
canvas.delete("all")
for heart in hearts:
x, y, size, color, speed = heart
canvas.create_oval(x, y, x+size, y+size, fill=color)
heart[1] += speed
if heart[1] > 600:
hearts.remove(heart)
window.after(50, draw_heart)
# 创建爱心列表
hearts = []
# 添加爱心到列表
for i in range(10):
hearts.append(create_heart())
# 调用绘制爱心函数
draw_heart()
# 运行窗口
window.mainloop()
```
阅读全文