爱心表白小程序--Python

小编最近刷算法题刷得头疼,发一下库存水一下,上学期做的python小项目,移动爱心。小编第一个熟悉的语言其实是Python,最近才学的C/C++。自我感觉还是Python好玩,不想刷算法题了,呜呜呜呜。

先看效果:

彩色移动爱心

import turtle
import random
from math import sqrt

class Heart:

    def __init__(self, x, y, size,color):
        self.color_1=color
        self.size = size                  # 心形大小
        self.speed = size                 # 移动速度根据大小变化
                                          # 设置画笔的统一属性
        t = turtle.Turtle(visible=False, shape='circle')
                                           #画笔设置为不可见,形状为圆形
                                           #“arrow” 扁三角 “blank” 透明  “turtle”小乌龟  “circle”圆  “square” 正方形
                                           #“triangle” 正三角  “classic” 鼠标箭头
        t.shapesize(size, size)          #设置图形的大小
        color =(self.color_1)                #(1, 1- size/4, 1-size/4)   # 颜色修改为根据大小变化的粉色
                                           #颜色可以写“pink”形式,也支持RGB三元组
        t.pencolor(color)
        t.fillcolor(color)
        t.penup()
        # 克隆一个圆形,设置位置
        self.circle1 = t.clone()           #移动到该该点位置,一个粉色圆形
        self.circle1.goto(x-sqrt(size*size*160)/2, y)
        # 克隆第二个圆形,设置位置              #利用两个圆和一个旋转45度的正方形拼成一个心
        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)        #设置朝向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)
        #设置三个画笔的移动,取出之前的横坐标,减去输入的self.speed,也就是size,执行一次移动size个像素
    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()
        #当心移到屏幕外时,将其隐身后再次移到原来位置,之后显示,再次执行move方法即可



def xinxing(m):
    hearts = []
    colorlist =['pink','lavender','lightsteelblue',"orangered",'red',
             'lightgoldenrodyellow',"hotpink",'pink',"tomato","lightpink","red","lightcyan","gold"]
    for i in range(30):
        #循环25次,每一次添加一个心(随机的)
        color_2=random.choice(colorlist)
        heart = Heart(width / 2 + random.randint(1, width), random.randint(-height / 2, height / 2), random.random() * 3,color_2)
        #0到1的数
        hearts.append(heart)
    i=m
    while True:
        for heart in hearts:
            heart.move()
            if heart.square.xcor() < -width / 2:  # 如果爱心移动出屏幕左侧
                heart.moveTo(width / 2 + random.randint(1, width), random.randint(-height / 2, height / 2))  # 回到右侧随机位置
                i-=1
        if i<=0:
            break









if __name__ == '__main__':
    width, height = 1000, 600     #定义宽和高
    screen = turtle.Screen()     # 创建窗口对象
    screen.setup(width, height)  # 设置窗口的宽高
    screen.delay(0)              # 设置无延时绘画
    # screen.bgcolor('pink')       # 设置背景颜色为粉色
    turtle.bgpic(r'C:\Users\HP\Desktop\Python\404.png')
    su = turtle.Turtle()         #建立su实例(画笔)
    xinxing(1000)
    turtle.done()              #关闭

源码奉上,祝大家表白成功哦。我本将心向明月,奈何明月照沟渠。呜呜呜呜。

猜你喜欢

转载自blog.csdn.net/m0_73731708/article/details/129327798