Python超火动态爱心程序

本文介绍了使用Python脚本制作exe程序的方法。首先需安装PyInstaller库,接着准备后缀为.ico的图片并放在PY文件位置,然后在终端进入文件目录执行特定命令,当命令行返回successfully指令,表明程序制作成功,打包的exe程序会放在dist目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

import random
import sys
from math import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

import turtle

turtle.setup (1200, 800)
turtle.delay (0)
turtle.goto (0, -200)
turtle.begin_poly ( )
turtle.setheading (150)
turtle.fd (70)
turtle.circle (70 * -3.745, 45)
turtle.circle (70 * -1.431, 165)
turtle.left (120)
turtle.circle (70 * -1.431, 165)
turtle.circle (70 * -3.745, 45)
turtle.fd (70)
turtle.end_poly ( )
angle_coord_list = turtle.get_poly ( )
turtle.bye ( )

new_coord_list = []

for order2, angle_coord in enumerate (angle_coord_list):
    if order2 + 1 < len (angle_coord_list):
        pointnum_x = int (abs (angle_coord_list[order2 + 1][0] - angle_coord_list[order2][0]) / 0.25)
        pointnum_y = int (abs (angle_coord_list[order2 + 1][1] - angle_coord_list[order2][1]) / 0.25)
        pointnum = max (pointnum_x, pointnum_y)
        step_x = ((angle_coord_list[order2 + 1][0] - angle_coord_list[order2][0]) / pointnum)
        step_y = ((angle_coord_list[order2 + 1][1] - angle_coord_list[order2][1]) / pointnum)
        for i in range (pointnum):
            new_coord = (round (angle_coord_list[order2][0] + (i + 1) * step_x, 2),
                         round (angle_coord_list[order2][1] + (i + 1) * step_y, 2),)
            if new_coord not in new_coord_list:
                new_coord_list.append (new_coord)


class QheartWindow (QMainWindow):

    def __init__(self):

        super (QheartWindow, self).__init__ (None)

        self.setWindowTitle ('我爱你')
        self.resize (QDesktopWidget ( ).screenGeometry ( ).width ( ),
                     QDesktopWidget ( ).screenGeometry ( ).height ( ))
        self.move (0, 0)
        self.setStyleSheet (" QMainWindow { background-color:#000000}")

        self.startTimer (50)
        self.readlist = 0
        self.largen = True
        self.cen_x = QDesktopWidget ( ).screenGeometry ( ).width ( ) / 2
        self.cen_y = QDesktopWidget ( ).screenGeometry ( ).height ( ) / 2 - 50
        self.cent = 100
        self.makecoord ( )

    def makecoord(self):
        self.coord_list1 = []
        self.all_coord_list1 = []
        expend_list = [int (9 * round (sqrt (10000 - (i * i)), 4)) + 200 for i in range (0, 105, 5)]
        for order, expend in enumerate (expend_list):
            offset = int ((len (expend_list) - sqrt ((len (expend_list) ** 2)
                                                     - ((order + 1) ** 2)) + order + 2) * 0.8)

            for new_coord in new_coord_list:
                if random.randint (1, 8) == 1:
                    size = random.randint (1, 4)
                    heart_x = (new_coord[0] * (sqrt (expend) * 0.024))
                    heart_y = (new_coord[1] * (sqrt (expend) * 0.026))
                    x = int ((heart_x) + self.width ( ) / 2)
                    y = int ((- heart_y) + self.height ( ) / 2)
                    draw_x = x + random.randint (- offset, offset)
                    draw_y = y + random.randint (- offset, offset)
                    colorint = random.randint (1, 7)
                    if colorint == 1:
                        color = QColor (19, 43, 77)
                    elif colorint == 2:
                        color = QColor (255, 181, 198)
                    elif colorint == 3:
                        color = QColor (161, 25, 45)
                    elif colorint == 4:
                        color = QColor (232, 51, 92)
                    elif colorint == 5:
                        color = QColor (255, 0, 0)
                    else:
                        color = QColor (255, 181, 198)

                    if (draw_x, draw_y, size, color) not in self.coord_list1:
                        self.coord_list1.append ((draw_x, draw_y, size, color))

        self.all_coord_list1.append (self.coord_list1)

        for su in range (1, 10):
            coord_temporary1 = []
            for coord in self.coord_list1:

                flexk = ((536 - 1.11111111111 * sqrt (((coord[0] - self.cen_x) ** 2) +
                                                      ((coord[1] - self.cen_y) ** 2))) * (0.00006) * su) - (
                                    su * 0.01 + 0.017)
                if flexk < 0:
                    flexk = 0
                new_x = self.cen_x - (1 + flexk) * (self.cen_x - coord[0])
                new_y = self.cen_y - (1 + flexk) * (self.cen_y - coord[1])
                coord_temporary1.append ((new_x, new_y, coord[2], coord[3]))

            self.all_coord_list1.append (coord_temporary1)

        self.coord_list2 = []
        self.all_coord_list2 = []
        expend_list = [int (round (sqrt (10000 - (1 * i)) + 100 - i, 4)) for i in range (0, 92, 5)]
        for order, expend in enumerate (expend_list):
            offset = int (len (expend_list) - sqrt ((len (expend_list) ** 2) - ((order + 1) ** 2)) + 2) + 10
            for new_coord in new_coord_list:
                if random.randint (1, 7) == 1:
                    size = random.randint (1, 3)
                    heart_x = new_coord[0] * (sqrt (expend) * 0.075)
                    heart_y = new_coord[1] * (sqrt (expend) * 0.078)
                    x = int ((heart_x) + self.width ( ) / 2)
                    y = int ((-heart_y) + self.height ( ) / 2)
                    # 偏移量
                    draw_x = x + random.randint (-offset, offset)
                    draw_y = y + random.randint (-offset, offset)
                    colorint = random.randint (1, 10)
                    if colorint == 1:
                        color = QColor (190, 43, 77)
                    elif colorint == 2:
                        color = QColor (255, 181, 198)
                    elif colorint == 3 or colorint == 5:
                        color = QColor (161, 25, 45)
                    elif colorint == 4:
                        color = QColor (232, 51, 92)
                    elif colorint == 7:
                        color = QColor (255, 0, 0)

                    else:
                        color = QColor (214, 79, 100)
                    if (draw_x, draw_y, size, color) not in self.coord_list2:
                        self.coord_list2.append ((draw_x, draw_y, size, color))

        for expendx in range (- self.cent, self.cent):
            for expendy in range (-self.cent, self.cent):
                if random.randint (1, 100) == 1:
                    size = random.randint (1, 3)
                    heart_x = expendx
                    heart_y = expendy
                    x = int ((heart_x) + self.width ( ) / 2)
                    y = int ((-heart_y) + self.height ( ) / 2 - 40)

                    offset = 20
                    draw_x = x + random.randint (- offset, offset)
                    draw_y = y + random.randint (- offset, offset)

                    colorint = random.randint (1, 10)
                    if colorint == 1:
                        color = QColor (190, 43, 77)
                    elif colorint == 2 or colorint == 6:
                        color = QColor (255, 181, 198)
                    elif colorint == 3 or colorint == 5:
                        color = QColor (161, 25, 45)
                    elif colorint == 4:
                        color = QColor (232, 51, 92)
                    elif colorint == 7:
                        color = QColor (255, 0, 0)
                    else:
                        color = QColor (214, 79, 100)
                    if (draw_x, draw_y, size, color) not in self.coord_list2:
                        self.coord_list2.append ((draw_x, draw_y, size, color))

    def paintEvent(self, event):
        self.painter = QPainter (self)
        self.painter.begin (self)

        if self.readlist >= 0:
            coord_temporary2 = []
            offset = (9 - self.readlist) * 6
            if offset > 0:
                for coord in self.coord_list2:
                    new_x = coord[0] + random.randint (- offset, offset)
                    new_y = coord[1] + random.randint (- offset, offset)
                    coord_temporary2.append ((new_x, new_y, coord[2], coord[3]))
            else:
                coord_temporary2 = self.coord_list2
            self.all_coord_list = self.all_coord_list1[self.readlist] + coord_temporary2

        for coord in self.all_coord_list:
            if coord[2] <= 3:
                self.pen = QPen ( )
                self.pen.setColor (coord[3])
                self.pen.setWidth (coord[2])
                self.painter.setPen (self.pen)
                self.painter.drawPoint (coord[0], coord[1], )
            else:
                self.painter.setBrush (coord[3])
                self.painter.drawEllipse (coord[0], coord[1], coord[2] - 1, coord[2] - 1)
        self.painter.end ( )

        if self.readlist == 9:
            self.largen = False
        elif self.readlist == 0:
            self.largen = True

        if self.largen == True:
            self.readlist += 1
        elif self.largen == False:
            self.readlist -= 1

    def timerEvent(self, event):
        self.update ( )


if __name__ == "__main__":
    app = QApplication (sys.argv)
    window = QheartWindow ( )
    window.show ( )
    sys.exit (app.exec_ ( ))

Python脚本制作exe程序


1、需要安装PyInstaller库pip insatllPyInstaller
2、找一张以 .ico为后缀名的图
片,将该图片放到要制作exe程序PY文件的位置
3、在终端 (cmd) 进入game.py文件的目录执行命令,                                                                  开始制作exe应用程序命令: pyinstaller -F -i game.ico game.py
4、命令行返回successfully指令,说明程序制作成功,打包完成的exe程序被放到dist目录中

提示:game.ico为自己本地的图标,后缀需要为.ico,想要打包的程序名字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值