python: 蝴蝶跟随鼠标
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2024.3.6 python 3.11
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/3/28 14:00
# User : geovindu
# Product : PyCharm
# Project : Pysimple
# File : buttyerfly4.py
import turtle as tur
import random
import math
# 颜色
color1 = "#f6aa0c"
color2 = "#0099ff"
color3 = "#f83079"
color4 = "#72e7fb"
# ===================== 飞行参数 =====================
tx, ty = 0, 0
bx, by = 0, 0
frame = 0
stars = []
# ===================== 你原版完整蝴蝶(100% 对称) =====================
def go_to(pos:float):
"""
:param pos:
:return:
"""
tur.up()
tur.goto(pos[0], pos[1])
tur.down()
def rel_pos(x:float,y:float):
"""
:param x:
:param y:
:return:
"""
tur.up()
tur.goto(tur.pos()+(x,y))
tur.down()
def draw_oval(width:float, height:float, color:str):
"""
:param width:
:param height:
:param color:
:return:
"""
tur.fillcolor(color)
tur.begin_fill()
for _ in range(2):
tur.circle(height/2, 45)
tur.circle(width/2, 135)
tur.end_fill()
def draw_heart(size:float):
"""
:param size:
:return:
"""
tur.left(50)
tur.forward(size)
tur.circle(size * 0.5, 200)
tur.right(140)
tur.circle(size * 0.5, 200)
tur.forward(size+5)
tur.left(50)
def rightwings(top_pos:float, flap:float):
"""
:param top_pos:
:param flap:
:return:
"""
go_to(top_pos)
tur.seth(-65 + flap)
tur.circle(-200,35)
cur_pos = tur.pos()
tur.begin_fill()
tur.fillcolor(color1)
draw_heart(100)
tur.end_fill()
go_to((cur_pos[0],cur_pos[1] + 20))
tur.begin_fill()
tur.fillcolor(color2)
draw_heart(80)
tur.end_fill()
rel_pos(60,-70)
tur.seth(-45)
draw_oval(20,50,color3)
rel_pos(-25,30)
draw_oval(10, 30, color3)
rel_pos(-10, -15)
draw_oval(10,20,color4)
go_to(top_pos)
tur.seth(-65 + flap)
tur.circle(-200,20)
cur_pos = tur.pos()
tur.begin_fill()
tur.fillcolor(color1)
draw_heart(100)
tur.end_fill()
go_to(cur_pos)
tur.begin_fill()
tur.fillcolor(color2)
draw_heart(80)
tur.end_fill()
rel_pos(50,30)
tur.seth(10)
draw_oval(10,60,color3)
rel_pos(40,-5)
draw_oval(20,60,color4)
rel_pos(-10,-40)
draw_oval(20,40,color3)
tur.seth(-45)
rel_pos(0,-45)
draw_oval(10,80,color4)
rel_pos(-30,20)
draw_oval(10,30,color4)
rel_pos(-30,30)
draw_oval(25,30,color3)
def leftWings(top_pos:float, flap:float):
"""
:param top_pos:
:param flap:
:return:
"""
go_to(top_pos)
tur.seth(-110 - flap)
tur.circle(200,22)
cur_pos = tur.pos()
tur.seth(100)
tur.begin_fill()
tur.fillcolor(color1)
draw_heart(100)
tur.end_fill()
go_to((cur_pos[0], cur_pos[1] + 10))
tur.begin_fill()
tur.fillcolor(color2)
draw_heart(80)
tur.end_fill()
rel_pos(-80, -45)
tur.seth(-135)
draw_oval(20, 50, color3)
rel_pos(25,30)
draw_oval(10,30,color4)
rel_pos(30,10)
draw_oval(10,20,color3)
go_to(top_pos)
tur.seth(-110 - flap)
tur.circle(200,5)
cur_pos = tur.pos()
tur.seth(90)
tur.begin_fill()
tur.fillcolor(color1)
draw_heart(100)
tur.end_fill()
go_to((cur_pos[0], cur_pos[1] -15))
tur.begin_fill()
tur.fillcolor(color2)
draw_heart(80)
tur.end_fill()
rel_pos(-90,50)
tur.seth(110)
draw_oval(10,60,color4)
rel_pos(40,-5)
draw_oval(20,60,color4)
rel_pos(-10,-40)
draw_oval(20,40,color3)
tur.seth(-150)
rel_pos(-29,-30)
draw_oval(10,80,color4)
rel_pos(-30,20)
draw_oval(10,30, color3)
rel_pos(90,15)
draw_oval(10,30,color3)
def drawWings(top_pos:float, flap:float):
"""
:param top_pos:
:param flap:
:return:
"""
rightwings(top_pos, flap)
leftWings(top_pos, flap)
def drawButterfly(x:float, y:float, flap:float):
"""
:param x:
:param y:
:param flap:
:return:
"""
tur.penup()
tur.goto(x, y)
tur.pendown()
tur.seth(70)
tur.circle(200,45)
top_pos = tur.pos()
tur.seth(-110)
tur.circle(200,45)
go_to(top_pos)
tur.seth(0)
tur.begin_fill()
tur.fillcolor(color2)
tur.circle(10)
tur.end_fill()
tur.circle(10, 180)
tur.width(3)
cur_pos = tur.pos()
tur.seth(90)
tur.circle(50,60)
go_to(cur_pos)
tur.seth(90)
tur.circle(-50,60)
tur.width(2)
drawWings(top_pos, flap)
go_to((x, y))
tur.begin_fill()
tur.fillcolor(color1)
tur.seth(70)
tur.circle(200,45)
top_pos = tur.pos()
tur.seth(-110)
tur.circle(200,45)
tur.end_fill()
tur.begin_fill()
tur.fillcolor(color3)
tur.seth(70)
tur.circle(200,20)
tur.seth(180)
tur.forward(30)
tur.seth(-85)
tur.circle(200,20)
tur.end_fill()
tur.begin_fill()
tur.fillcolor(color1)
tur.seth(70)
tur.circle(200,15)
tur.seth(180)
tur.forward(27)
tur.seth(-81)
tur.circle(200,15)
tur.end_fill()
go_to((x, y))
tur.begin_fill()
tur.fillcolor(color4)
tur.seth(70)
tur.circle(200, 10)
tur.seth(180)
tur.forward(20)
tur.seth(-75)
tur.circle(200,10)
tur.end_fill()
# ===================== 鼠标点击跟随 =====================
def on_click(x:float, y:float):
"""
:param x:
:param y:
:return:
"""
global tx, ty
tx, ty = x, y
# ===================== 动画:飞 + 扇动 + 闪烁 =====================
def animate():
"""
:return:
"""
global frame, bx, by
frame += 1
tur.clear()
# 星光闪烁
for s in stars:
t, size, color, phase = s
phase += 0.15
s[3] = phase
r = size * (0.5 + abs(math.sin(phase)))
t.dot(max(1, r), color)
# 蝴蝶飞行
bx += (tx - bx) * 0.12
by += (ty - by) * 0.12
# 翅膀扇动
flap = math.sin(frame * 0.3) * 10
# 画完整蝴蝶
drawButterfly(bx, by, flap)
tur.update()
tur.ontimer(animate, 30)
def main():
# ===================== 窗口 =====================
tur.setup(1000, 700)
tur.title("🦋 漂亮蝴蝶 - 会飞 + 扇动翅膀 + 星光闪烁")
tur.bgcolor("#050a15")
tur.tracer(0)
tur.width(2)
tur.hideturtle()
# ===================== 星光(永久闪烁) =====================
for _ in range(200):
t = tur.Turtle()
t.hideturtle()
t.penup()
t.goto(random.randint(-480, 480), random.randint(-320, 320))
size = random.uniform(1.5, 4)
color = random.choice(["white", "#cceeff", "#e6ccff", "#fff9cc"])
stars.append([t, size, color, random.uniform(0, 6.28)])
# ===================== 主程序 =====================
if __name__ == '__main__':
main()
tur.onscreenclick(on_click)
# ===================== 启动 =====================
animate()
tur.done()
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号