# 代码由 离鲸鲸鲸 原创
# 有问题请联系微信号 innoin 沟通处理
from turtle import *
from math import sqrt
from random import random, randint
class Heart:
def __init__(self, x, y, size):
self.size = size # 心形大小
self.speed = size # 移动速度根据大小变化
# 设置画笔的统一属性
t = Turtle(visible=False, shape='circle')
t.shapesize(size, size)
color = (1, 1- size/4, 1-size/4) # 颜色修改为根据大小变化的粉色
t.pencolor(color)
t.fillcolor(color)
t.penup()
# 克隆一个圆形,设置位置
self.circle1 = t.clone()
self.circle1.goto(x-sqrt(size*size*160)/2, y)
# 克隆第二个圆形,设置位置
self.circle2 = t.clone()
self.circle2.goto(x+sqrt(size*size*160)/2, y)
# 克隆一个正方形,设置位置并旋转角度
self.square = t.clone()
self.square.shape("square")
self.square.setheading(45)
self.square.goto(x, y-sqrt(size * size * 160)/2)
# 显示图形
self.circle1.showturtle()
self.circle2.showturtle()
self.square.showturtle()
def move(self):
self.circle1.setx(self.circle1.xcor()-self.speed)
self.square.setx(self.square.xcor() - self.speed)
self.circle2.setx(self.circle2.xcor() - self.speed)
def moveTo(self, x, y):
# 隐藏形状后再移动防止看到移动轨迹
self.circle1.hideturtle()
self.circle2.hideturtle()
self.square.hideturtle()
# 移动到指定位置
self.circle1.goto(x - sqrt(self.size * self.size * 160) / 2, y)
self.circle2.goto(x + sqrt(self.size * self.size * 160) / 2, y)
self.square.goto(x, y - sqrt(self.size * self.size * 160) / 2)
# 恢复显示
self.circle1.showturtle()
self.circle2.showturtle()
self.square.showturtle()
width, height = 800, 600
screen = Screen() # 创建窗口对象
screen.setup(width, height) # 设置窗口的宽高
screen.delay(0) # 设置无延时绘画
screen.bgcolor('pink') # 设置背景颜色为粉色
hearts = []
for i in range(25):
heart = Heart(width/2 + randint(1, width), randint(-height/2,height/2), random()*3)
hearts.append(heart)
while True:
for heart in hearts:
heart.move()
if heart.square.xcor() < -width / 2: # 如果爱心移动出屏幕左侧
heart.moveTo(width / 2 + randint(1, width), randint(-height / 2, height / 2)) # 回到右侧随机位置

两只程序猿
- 粉丝: 386
最新资源
- 新一代电力通信网分层规划研究分析(1).docx
- 通信技术在PLC控制系统中的应用分析(1).docx
- 自考本科数据库系统原理串讲.doc
- 人工智能教育在中小学课程中的综合应用实践(1).pptx
- 电子商务推荐系统介绍(1).pptx
- 计算机网络信息安全防护措施(1).docx
- 刍议电气工程中自动化技术(1).docx
- 互联网+环境下的企业信息安全探讨(1).docx
- 电子商务公司简介(1).pptx
- 网站代理协议新(1).docx
- C语言面试笔试题(1).docx
- 纵横公路造价软件概预算完美讲课文档(1).ppt
- AutoCAD基础教程第15章.ppt
- Vue-js培训(1).ppt
- 金融投资实战软件平台解决方案(1).doc
- 工会财务软件业务知识(1)(1).ppt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
- 4
- 5
- 6
前往页