您现在的位置是:首页 > 技术教程 正文

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

admin 阅读: 2024-03-26
后台-插件-广告管理-内容页头部广告(手机)

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转:网站。

人工智能学习网站icon-default.png?t=N7T8https://www.captainbed.cn/yan

一、实现效果:

        3692fec4d8a5412f9cdfa62be039e572.gif

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

 二、准备工作

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

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

  1. import requests
  2. from lxml import etree
  3. import csv

        如果出现模块报错

c124a1693bfc457ba1f2909ee9d299fc.png

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

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

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

  1. 清华大学
  2. https://pypi.tuna.tsinghua.edu.cn/simple
  3. 阿里云
  4. https://mirrors.aliyun.com/pypi/simple/
  5. 豆瓣
  6. https://pypi.douban.com/simple/
  7. 百度云
  8. https://mirror.baidu.com/pypi/simple/
  9. 中科大
  10. https://pypi.mirrors.ustc.edu.cn/simple/
  11. 华为云
  12. https://mirrors.huaweicloud.com/repository/pypi/simple/
  13. 腾讯云
  14. https://mirrors.cloud.tencent.com/pypi/simple/

firework 类

  1. class firework:
  2. def __init__(self, x, y, color):
  3. self.x = x
  4. self.y = y
  5. self.color = color
  6. self.radius = 1
  7. self.speed = random.uniform(0.5, 1.5)
  8. self.angle = math.radians(random.randint(0, 360))
  9. self.vx = self.speed * math.cos(self.angle)
  10. self.vy = self.speed * math.sin(self.angle)
  11. self.age = 0
  12. self.alive = True
  13. self.particles = []

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

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

  • color:当前烟花的颜色。

  • radius:当前烟花的半径。

  • speed:当前烟花的速度。

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

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

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

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

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

colorChange 函数

  1. def colorChange(color, age):
  2. r, g, b = color
  3. if age > 255:
  4. age = 255
  5. if age <= 85:
  6. return (r+age, g, b)
  7. elif age <= 170:
  8. return (r, g+age-85, b)
  9. else:
  10. return (r, g, b+age-170)

这个函数用于计算烟花的颜色,它接受两个参数:

  • color:当前烟花的颜色。

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

根据 age 的值,逐渐改变颜色的 R、G、B 分量来实现颜色的渐变效果。具体来说,如果 age 小于等于 85,则只改变红色分量,否则如果 age 小于等于 170,则同时改变红色和绿色分量,否则同时改变红色、绿色和蓝色分量。

appendFirework 函数

  1. def appendFirework():
  2. f = firework(random.randint(100, w-100), h, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
  3. fireworks.append(f)
  4. root.after(random.randint(100, 1000), appendFirework)

这个函数用于递归生成烟花对象,并在画布上显示烟花效果。具体来说,它做了以下几件事情:

  • 创建一个新的 firework 对象,随机指定其坐标、颜色、速度和角度等属性。

  • 将新的烟花对象添加到 fireworks 列表中。

  • 随机生成 100 到 1000 毫秒的时间,之后再次调用 appendFirework 函数,实现递归生成烟花对象。

heart_function 函数

  1. def heart_function(theta):
  2. x = 16 * math.sin(theta) ** 3
  3. y = 13 * math.cos(theta) - 5 * math.cos(2*theta) - 2 * math.cos(3*theta) - math.cos(4*theta)
  4. return (x, -y)

这个函数用于计算心形图案上的点坐标,它接受一个参数 theta,表示当前点所在的极角。具体来说,它使用一组极坐标方程来计算出心形图案上的点坐标,然后将其转换为笛卡尔坐标系下的坐标值并返回。

scatter_inside 函数

  1. def scatter_inside(p, speed):
  2. x, y = p.pos
  3. vx, vy = p.vel
  4. dist = math.hypot(x, y)
  5. if dist < 1:
  6. dist = 1
  7. dx = x / dist
  8. dy = y / dist
  9. force = (10 / (dist ** 2)) * speed
  10. dvx = force * dx
  11. dvy = force * dy
  12. p.vel = (vx+dvx, vy+dvy)

这个函数用于实现心形内部的扩散效果,它接受两个参数:

  • p:当前粒子对象。

  • speed:扩散速度。

首先根据当前粒子的位置计算出一个向心力,然后根据该力的大小和方向改变粒子的速度,从而实现向外扩散的效果。

shrink 函数

  1. def shrink(p, speed):
  2. x, y = p.pos
  3. vx, vy = p.vel
  4. dist = math.hypot(x, y)
  5. if dist < 1:
  6. dist = 1
  7. dx = x / dist
  8. dy = y / dist
  9. force = (-10 / (dist ** 2)) * speed
  10. dvx = force * dx
  11. dvy = force * dy
  12. p.vel = (vx+dvx, vy+dvy)

这个函数用于实现心形收缩效果,它接受两个参数:

  • p:当前粒子对象。

  • speed:收缩速度。

与 scatter_inside 函数类似,这个函数也是根据当前粒子的位置计算出一个向心力,然后根据该力的大小和方向改变粒子的速度,从而实现向内收缩的效果。

curve 函数

  1. def curve(t):
  2. if t < 1:
  3. return math.sin(t*math.pi/2)
  4. else:
  5. return math.sin((2-t)*math.pi/2) * 0.5 + 0.5

这个函数返回一个介于 0 和 4 之间的值,用于控制心形动画的曲线效果。具体来说,它接受一个参数 t,表示当前时间占总动画时间的比例,然后根据 t 的值返回一个介于 0 和 4 之间的值,用于控制心形动画的曲线效果。

Heart 类

  1. class Heart:
  2. def __init__(self):
  3. self.points = []
  4. self.colors = []
  5. self.particles = []
  6. self.speed = 5
  7. self.pos = (w/2, h/2)
  8. self.rotation = 0
  9. self.scale = 1
  10. self._create_heart()
  11. def _create_heart(self):
  12. for i in range(1000):
  13. theta = i / 1000 * math.pi * 2
  14. r = heart_function(theta)[0]
  15. x = r * math.cos(theta)
  16. y = r * math.sin(theta)
  17. self.points.append((x, y))
  18. self.colors.append((random.randint(128, 255), random.randint(0, 128), random.randint(0, 128)))
  19. def update(self):
  20. for p in self.particles:
  21. p.update()
  22. self.particles = [p for p in self.particles if p.alive]
  23. if random.random() < 0.3:
  24. x, y = self.pos
  25. dx = random.uniform(-1, 1) * self.speed
  26. dy = random.uniform(-1, 1) * self.speed
  27. p = Particle((x+dx, y+dy), (dx/4, dy/4))
  28. self.particles.append(p)
  29. self.rotation += 0.001
  30. self.scale = curve(self.rotation)
  31. def draw(self, canvas):
  32. cx, cy = self.pos
  33. for i, (x, y) in enumerate(self.points):
  34. r, g, b = self.colors[i]
  35. x *= self.scale
  36. y *= self.scale
  37. x, y = rotate(x, y, self.rotation)
  38. x += cx
  39. y += cy
  40. canvas.create_oval(x-1, y-1, x+1, y+1, fill="#%02x%02x%02x" % (r, g, b), width=0)

这个类用于生成爱心图案及其动态效果,它有以下属性:

  • points:存储心形图案上的所有点的坐标。

  • colors:存储心形图案上的所有点的颜色。

  • particles:存储所有心形收缩和扩散过程中生成的粒子。

  • speed:控制粒子运动速度的参数。

  • pos:控制心形图案位置的参数。

  • rotation:控制心形图案旋转角度的参数。

  • scale:控制心形图案缩放比例的参数。

其中,初始化函数 _create_heart 用于生成心形图案上的所有点和颜色,update 函数用于更新心形图案的动画效果,draw 函数用于在画布上绘制心形图案,并在每一帧更新心形的动态效果。

draw 函数

  1. def draw():
  2. global fireworks, hearts
  3. canvas.delete("all")
  4. for f in fireworks:
  5. if f.alive:
  6. f.draw(canvas)
  7. f.update()
  8. else:
  9. for p in f.particles:
  10. if random.random() < 0.5:
  11. hearts.append(Heart())
  12. fireworks.remove(f)
  13. for h in hearts:
  14. h.draw(canvas)
  15. h.update()
  16. root.after(25, draw)

这个函数用于在画布上绘制烟花和心形图案,并在每一帧更新它们的动画效果。具体来说,它做了以下几件事情:

  • 遍历所有烟花对象,如果烟花还存活,则在画布上显示它的效果并更新它的状态;否则将烟花爆炸后生成的粒子转化为心形对象,并将烟花从 fireworks 列表中移除。

  • 遍历所有心形对象,显示它们的效果并更新它们的状态。

  • 在 root 窗口上注册一个定时器,在 25 毫秒之后再次调用 draw 函数,实现连续播放动画的效果。

三、完整代码:

      

  1. import math
  2. import random
  3. import threading
  4. import time
  5. from math import sin, cos, pi, log
  6. from tkinter import *
  7. import re
  8. # 烟花相关设置
  9. Fireworks = []
  10. maxFireworks = 8
  11. CANVAS_WIDTH = 1080 # 画布的宽
  12. CANVAS_HEIGHT = 600 # 画布的高
  13. CANVAS_CENTER_X = CANVAS_WIDTH / 2 # 画布中心的X轴坐标
  14. CANVAS_CENTER_Y = CANVAS_HEIGHT / 2 # 画布中心的Y轴坐标
  15. IMAGE_ENLARGE = 12 # 放大比例
  16. HEART_COLOR = "pink" # 心的颜色
  17. # 烟花类
  18. class firework(object):
  19. def __init__(self, color, speed, width, height):
  20. self.radius = random.randint(2, 3) # 粒子半径为2~3像素
  21. self.color = color # 粒子颜色
  22. self.speed = speed # speed是1.5-3.5秒
  23. self.status = 0 # 在烟花未爆炸的情况下,status=0;爆炸后,status>=1;当status>100时,烟花的生命期终止
  24. self.nParticle = random.randint(80, 100) # 粒子数量
  25. self.center = [random.randint(0, width - 15), random.randint(0, height - 15)] # 烟花随机中心坐标
  26. self.oneParticle = [] # 原始粒子坐标(100%状态时)
  27. self.rotTheta = random.uniform(-1, 2 * math.pi) # 椭圆平面旋转角
  28. self.ellipsePara = [random.randint(30, 40), random.randint(20, 30)] # 椭圆参数方程:x=a*cos(theta),y=b*sin(theta)
  29. theta = 2 * math.pi / self.nParticle
  30. for i in range(self.nParticle):
  31. t = random.uniform(-1.0 / 16, 1.0 / 16) # 产生一个 [-1/16,1/16) 的随机数
  32. x, y = self.ellipsePara[0] * math.cos(theta * i + t), self.ellipsePara[1] * math.sin(theta * i + t) # 椭圆参数方程
  33. xx, yy = x * math.cos(self.rotTheta) - y * math.sin(self.rotTheta), y * math.cos(
  34. self.rotTheta) + x * math.sin(self.rotTheta) # 平面旋转方程
  35. self.oneParticle.append([xx, yy])
  36. self.curParticle = self.oneParticle[0:] # 当前粒子坐标
  37. self.thread = threading.Thread(target=self.extend) # 建立线程对象
  38. 完整代码,见文末

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

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

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索