Python描绘情意绵绵的爱心:18款动画代码源泉
2023-09-16 21:00:47
用 Python 绘制爱心,让爱意与代码完美交融
摘要
踏入 Python 绘制爱心的艺术领域,开启一场创意与真情的探索之旅。从跳动的心到旋转的爱心,从爱心雨到爱心花瓣,我们将使用 Python 绘图库,共同描绘出爱情的缤纷色彩。无论是作为礼物赠与挚爱,还是与朋友和家人分享,这些代码都能为你传递最诚挚的爱意。
跳动的爱心
让屏幕上跳动着一颗颗鲜红的爱心,仿佛在倾诉着热烈的情感。我们将使用 Turtle 绘图库来绘制爱心,并控制它们以不同的速度跳动。这段代码非常适合表达对某人的特别之爱。
import turtle
screen = turtle.Screen()
screen.setup(500, 500)
screen.title("跳动的爱心")
t = turtle.Turtle()
t.pensize(2)
t.color("red")
def heart():
t.begin_fill()
t.left(140)
t.forward(111.65)
t.circle(-50, 225)
t.left(120)
t.circle(-50, 225)
t.forward(111.65)
t.end_fill()
t.penup()
t.goto(0, -150)
t.pendown()
for i in range(36):
heart()
t.right(10)
screen.mainloop()
旋转的爱心
让一颗爱心在屏幕中央旋转,传递着爱的力量。我们将使用 Tkinter 绘图库来创建画布,并使用数学计算来控制爱心的旋转。这段代码适用于表达持久的爱或对某人的欣赏。
import tkinter as tk
import math
root = tk.Tk()
root.title("旋转的爱心")
root.geometry("500x500")
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack()
def draw_heart(x, y, size):
x1 = x - size
y1 = y - size
x2 = x + size
y2 = y + size
points = [x1, y1, x2, y1, x2, y2, x, y2+size, x1, y2]
canvas.create_polygon(points, fill="red", outline="red")
def rotate_heart():
for i in range(360):
canvas.delete("all")
draw_heart(250, 250, 100)
canvas.update()
canvas.after(10)
rotate_heart()
root.mainloop()
爱心雨
让屏幕上飘落着颗颗爱心,犹如一场浪漫的爱的雨滴。我们将再次使用 Turtle 绘图库来绘制爱心,并通过随机数来控制它们的位置。这段代码非常适合表达对某人的浓浓爱意或庆祝特别的时刻。
import turtle
import random
screen = turtle.Screen()
screen.setup(500, 500)
screen.title("爱心雨")
t = turtle.Turtle()
t.pensize(2)
t.color("red")
def heart():
t.begin_fill()
t.left(140)
t.forward(111.65)
t.circle(-50, 225)
t.left(120)
t.circle(-50, 225)
t.forward(111.65)
t.end_fill()
for i in range(100):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
t.penup()
t.goto(x, y)
t.pendown()
heart()
screen.mainloop()
爱心花瓣
让屏幕上绽放着朵朵爱心花瓣,诉说着爱的芬芳。我们将再次使用 Turtle 绘图库来绘制爱心花瓣,并通过数学计算来控制它们的位置和大小。这段代码非常适合表达对某人的欣赏或爱慕之情。
import turtle
import math
screen = turtle.Screen()
screen.setup(500, 500)
screen.title("爱心花瓣")
t = turtle.Turtle()
t.pensize(2)
t.color("red")
def heart_petal(size):
t.begin_fill()
t.left(140)
t.forward(size)
t.circle(-size/2, 225)
t.left(120)
t.circle(-size/2, 225)
t.forward(size)
t.end_fill()
for i in range(10):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
t.penup()
t.goto(x, y)
t.pendown()
heart_petal(50)
t.right(36)
screen.mainloop()
爱心路径
让屏幕上勾勒出一条条爱心路径,仿佛在诉说着一段段动人的爱情故事。我们将使用 Turtle 绘图库来绘制爱心路径,并通过随机数来控制它们的起始位置和方向。这段代码非常适合表达对某人的爱意或庆祝一段重要的旅程。
import turtle
import math
screen = turtle.Screen()
screen.setup(500, 500)
screen.title("爱心路径")
t = turtle.Turtle()
t.pensize(2)
t.color("red")
def heart_path():
t.begin_fill()
t.left(90)
for i in range(2):
t.forward(100)
t.left(120)
t.forward(100)
t.end_fill()
for i in range(20):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
t.penup()
t.goto(x, y)
t.pendown()
heart_path()
t.right(18)
screen.mainloop()
爱心星星
让屏幕上闪烁着颗颗爱心星星,点缀着爱的夜空。我们将使用 Turtle 绘图库来绘制爱心星星,并通过数学计算来控制它们的形状和位置。这段代码非常适合表达对某人的仰慕或爱慕之情。
import turtle
import math
screen = turtle.Screen()
screen.setup(500, 500)
screen.title("爱心星星")
t = turtle.Turtle()
t.pensize(2)
t.color("red")
def heart_star(size):
t.begin_fill()
for i in range(5):
t.forward(size)
t.right(144)
t.end_fill()
for i in range(20):
x = random.randint(-250, 250)
y = random.randint(-250, 250)
t.penup()
t.goto(x, y)
t.pendown()
heart_star(50)
t.right(18)
screen.mainloop()
爱心气球
让屏幕上飘扬着一个个爱心气球,承载着爱的祝福。我们将使用 Turtle 绘图库来绘制爱心气球,并通过随机数来控制它们的起始位置和速度。这段代码非常适合表达对某人的爱意或庆祝一个特别的日子。
import turtle
import math
screen = turtle.Screen()
screen.setup(500, 500)
screen.title("爱心气球")
t = turtle.Turtle()
t.pensize(2)
t.color("red")
def heart_balloon(size):
t.begin_fill()
t.left(90)
t.forward(size)
t.circle(-size/2, 180)
t.right(90)
t.forward(size)
t.circle(size/2, 180)
t.end_fill()
for i in range(10):
x = random.randint(-250, 250)
y = random.