【python】python爱心代码【附源码】

慈云数据 2024-05-11 技术支持 68 0

欢迎来到英杰社区icon-default.png?t=N7T8https://bbs.csdn.net/topics/617804998

一、实现效果:

        3692fec4d8a5412f9cdfa62be039e572.gif

 二、准备工作

(1)、导入必要的模块:

       代码首先导入了需要使用的模块:requests、lxml和csv。

import requests
from lxml import etree
import csv

        如果出现模块报错

c124a1693bfc457ba1f2909ee9d299fc.png

        进入控制台输入:建议使用国内镜像源

pip install 模块名称 -i https://mirrors.aliyun.com/pypi/simple

         我大致罗列了以下几种国内镜像源:

清华大学
https://pypi.tuna.tsinghua.edu.cn/simple
阿里云
https://mirrors.aliyun.com/pypi/simple/
豆瓣
https://pypi.douban.com/simple/ 
百度云
https://mirror.baidu.com/pypi/simple/
中科大
https://pypi.mirrors.ustc.edu.cn/simple/
华为云
https://mirrors.huaweicloud.com/repository/pypi/simple/
腾讯云
https://mirrors.cloud.tencent.com/pypi/simple/

firework 类

class firework:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.radius = 1
        self.speed = random.uniform(0.5, 1.5)
        self.angle = math.radians(random.randint(0, 360))
        self.vx = self.speed * math.cos(self.angle)
        self.vy = self.speed * math.sin(self.angle)
        self.age = 0
        self.alive = True
        self.particles = []

这个类表示了一个烟花对象,它有以下属性:

  • x 和 y:当前烟花的坐标。

  • color:当前烟花的颜色。

  • radius:当前烟花的半径。

  • speed:当前烟花的速度。

  • angle:当前烟花的运动角度。

  • vx 和 vy:当前烟花的速度在 x 和 y 方向上的分量。

  • age:当前烟花已经存在的时间。

  • alive:当前烟花是否还存活。

  • particles:当前烟花爆炸后生成的粒子列表。

    colorChange 函数

    def colorChange(color, age):
        r, g, b = color
        if age > 255:
            age = 255
        if age 100时,烟花的生命期终止
            self.nParticle = random.randint(80, 100)  # 粒子数量
            self.center = [random.randint(0, width - 15), random.randint(0, height - 15)]  # 烟花随机中心坐标
            self.oneParticle = []  # 原始粒子坐标(100%状态时)
            self.rotTheta = random.uniform(-1, 2 * math.pi)  # 椭圆平面旋转角
            self.ellipsePara = [random.randint(30, 40), random.randint(20, 30)]  # 椭圆参数方程:x=a*cos(theta),y=b*sin(theta)
            theta = 2 * math.pi / self.nParticle
            for i in range(self.nParticle):
                t = random.uniform(-1.0 / 16, 1.0 / 16)  # 产生一个 [-1/16,1/16) 的随机数
                x, y = self.ellipsePara[0] * math.cos(theta * i + t), self.ellipsePara[1] * math.sin(theta * i + t)  # 椭圆参数方程
                xx, yy = x * math.cos(self.rotTheta) - y * math.sin(self.rotTheta), y * math.cos(
                    self.rotTheta) + x * math.sin(self.rotTheta)  # 平面旋转方程
                self.oneParticle.append([xx, yy])
            self.curParticle = self.oneParticle[0:]  # 当前粒子坐标
            self.thread = threading.Thread(target=self.extend)  # 建立线程对象
    完整代码,见文末

     资料获取,更多粉丝福利,关注下方公众号:“英杰代码编程”获取

           回复"python爱心代码",“爱心代码”,“python爱心” 均可获取完整代码

微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon