当前位置: 首页 > news >正文

软件测试三个月骗局优化大师绿色版

软件测试三个月骗局,优化大师绿色版,直销系统网站建设,做化工外贸需要那些网站【Python】Python使用Tk实现动态爱心效果 画布使用了缓存机制,启动时绘制足够多的帧数,运行时一帧帧地取出来展示,明显更流畅,加快了程序执行速度。将控制跳动动画的函数从正弦函数换成了贝塞尔函数,贝塞尔函数更灵活…

【Python】Python使用Tk实现动态爱心效果

  • 画布使用了缓存机制,启动时绘制足够多的帧数,运行时一帧帧地取出来展示,明显更流畅,加快了程序执行速度。
  • 将控制跳动动画的函数从正弦函数换成了贝塞尔函数,贝塞尔函数更灵活,展现的跳动动画更有力(可惜的是时间有限,不太会调,所以改进不明显)。
  • 调整外围光环生成策略(之前自己脑补的部分),使其看起来更灵动。
  • 添加了固定文字的功能,能在中央显示固定文字。
  • 更详细的注释(后面有 “在这里改” 字样的,都可以去尝试调整)。
  • utf-8 编码声明,减少乱码问题。

运行效果:

爱心效果

代码如下:

# encoding: utf-8
# 深夜种下希望,梦中便能发芽
import random
import time
from math import sin, cos, pi, log
from tkinter import *CANVAS_WIDTH = 640  # 在这里改 画布的宽 最好和高成比例放大
CANVAS_HEIGHT = 480  # 在这里改 画布的高 最好和宽成比例放大
CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心的X轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标
IMAGE_ENLARGE = 11  # 在这里改 放大比例 画布放大后,心太小?把这个改大点
HEART_COLOR = "#e86184"  # 心的颜色 在这里改WINDOWS_TITLE = '爱心~'  # 窗口标题 在这里改
HEART_CENTER_TEXT = '爱你'  # 中间文字内容 在这里改
HEART_CENTER_TEXT_COLOR = '#FFD700'  # 中间文字颜色 在这里改def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):"""“爱心函数生成器”:param shrink_ratio: 放大比例:param t: 参数:return: 坐标"""# 基础函数# x = 16 * (sin(t) ** 3)x = 14.6 * (sin(t) ** 3)  # 更尖# y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))y = -(14.5 * cos(t) - 4 * cos(2 * t) - 2 * cos(3 * t) - 0.5 * cos(4 * t))  # 更圆润# 放大x *= shrink_ratioy *= shrink_ratio# 移到画布中央x += CANVAS_CENTER_Xy += CANVAS_CENTER_Yreturn int(x), int(y)def scatter_inside(x, y, beta=0.15):"""随机内部扩散:param x: 原x:param y: 原y:param beta: 强度:return: 新坐标"""ratio_x = - beta * log(random.random())ratio_y = - beta * log(random.random())dx = ratio_x * (x - CANVAS_CENTER_X)dy = ratio_y * (y - CANVAS_CENTER_Y)return x - dx, y - dydef shrink(x, y, ratio):"""抖动:param x: 原x:param y: 原y:param ratio: 比例:return: 新坐标"""force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)  # 这个参数...dx = ratio * force * (x - CANVAS_CENTER_X)dy = ratio * force * (y - CANVAS_CENTER_Y)return x - dx, y - dydef heart_curve(p):"""爱心的跳动函数参数:param p: 参数:return: 正弦 + 贝塞尔"""# return curve(p, (.4, .5, .2, .6))# https://cubic-bezier.com/ 调整参数的网站return curve(p, (.69, .75, .2, .95))  # 在这里改 爱心的贝塞尔曲线参数def heart_halo_curve(p):"""爱心光环的跳动函数参数:param p: 参数:return: 正弦 + 贝塞尔"""# return curve(p, (.73,.55,.59,.92))# https://cubic-bezier.com/ 调整参数的网站return curve(p, (.75, .49, .46, .97))  # 在这里改 光环的贝塞尔曲线参数def curve(p, b):"""自定义曲线函数,调整跳动周期:param b: 贝塞尔参数:param p: 参数:return: 正弦 + 贝塞尔"""# print('p:', p)t = sin(p)p0 = b[0]p1 = b[1]p2 = b[2]p3 = b[3]t1 = (1 - t)t2 = t1 * t1t3 = t2 * t1r = p0 * t3 + 3 * p1 * t * t2 + 3 * p2 * t * t * t1 + p3 * (t ** 3)  # 贝塞尔计算# r = 2 * (2 * sin(4 * p)) / (2 * pi)# print('r:', r)return rclass Heart:"""爱心类"""def __init__(self, generate_frame=20):self._points = set()  # 原始爱心坐标集合self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合self._center_diffusion_points = set()  # 中心扩散效果点坐标集合self.all_points = {}  # 每帧动态点坐标self.build(2000)  # 在这里改 初始的点数,太大可能运行缓慢self.generate_frame = generate_framefor frame in range(generate_frame):self.calc(frame)def build(self, number):# 爱心for _ in range(number):t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口x, y = heart_function(t)self._points.add((x, y))# 爱心内扩散for _x, _y in list(self._points):for _ in range(3):x, y = scatter_inside(_x, _y, 0.05)self._edge_diffusion_points.add((x, y))# 爱心内再次扩散point_list = list(self._points)for _ in range(4000):x, y = random.choice(point_list)x, y = scatter_inside(x, y, 0.24)  # 0.24 这个参数改爱心中间的点点数量,越大数量越多self._center_diffusion_points.add((x, y))@staticmethoddef calc_position(x, y, ratio):# 调整缩放比例force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.47)  # 魔法参数dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)return x - dx, y - dydef calc(self, generate_frame):ratio = 10 * heart_curve(generate_frame / 10 * pi)  # 圆滑的周期的缩放比例halo_radius = int(4 + 6 * (1 + heart_halo_curve(generate_frame / 10 * pi)))halo_number = int(3000 + 4000 * abs(heart_halo_curve(generate_frame / 10 * pi) ** 2))all_points = []# 光环heart_halo_point = set()  # 光环的点坐标集合,去重for _ in range(halo_number):t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口x, y = heart_function(t, shrink_ratio=heart_halo_curve(generate_frame / 10 * pi) + 11)  # 魔法参数x, y = shrink(x, y, halo_radius)if (x, y) not in heart_halo_point:# 处理新的点heart_halo_point.add((x, y))random_int_range = int(27 + heart_halo_curve(generate_frame / 10 * pi) * 4)x += random.randint(-random_int_range, random_int_range)y += random.randint(-random_int_range, random_int_range)size = random.choice((1, 1, 2))all_points.append((x, y, size))# 轮廓for x, y in self._points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 3)all_points.append((x, y, size))# 内容for x, y in self._edge_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 2)all_points.append((x, y, size))for x, y in self._center_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 2)all_points.append((x, y, size))self.all_points[generate_frame] = all_pointsdef render(self, render_canvas, render_frame):for x, y, size in self.all_points[render_frame % self.generate_frame]:render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)def frame_count(self):return self.generate_framedef draw(main: Tk, render_canvas_dict: dict, render_heart: Heart, render_frame=0):"""绘图函数:param main: TK面板:param render_canvas_dict: 画布缓存:param render_heart: 心类:param render_frame: 当前帧数:return: None"""frame_index = render_frame % render_heart.frame_count()last_frame_index = (frame_index + render_heart.frame_count() - 1) % render_heart.frame_count()if last_frame_index in render_canvas_dict:render_canvas_dict[last_frame_index].pack_forget()if frame_index not in render_canvas_dict:canvas = Canvas(main,bg='black',  # 在这里改 黑色背景height=CANVAS_HEIGHT,width=CANVAS_WIDTH)canvas.pack()render_heart.render(canvas, render_frame)canvas.create_text(CANVAS_CENTER_X,CANVAS_CENTER_Y,text=HEART_CENTER_TEXT,fill=HEART_CENTER_TEXT_COLOR,font=('楷体', 48, 'bold')  # 在这里改字体)render_canvas_dict[frame_index] = canvaselse:render_canvas_dict[frame_index].pack()main.after(30,  # 在这里改 画面切换间隔时间,越小帧数越高,但是可能会越卡draw, main, render_canvas_dict, render_heart, render_frame + 1)if __name__ == '__main__':print('正在启动...')start_time = time.time()root = Tk()  # 一个Tk界面root.title(WINDOWS_TITLE)canvas_dict = {}heart = Heart(40)  # 在这里改 40为总帧数,帧数越大,花样越多,更占内存draw(root, canvas_dict, heart)  # 开始画画~end_time = time.time()print('爱心魔法耗时 {:.2f} 秒完成 ~'.format(end_time - start_time))root.mainloop()

 

效果如下:

爱心效果

代码如下:

# 晚上星月争辉,美梦陪你入睡
import random
from math import sin, cos, pi, log
from tkinter import *CANVAS_WIDTH = 640  # 画布的宽
CANVAS_HEIGHT = 480  # 画布的高
CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心的X轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标
IMAGE_ENLARGE = 11  # 放大比例
HEART_COLOR = "#ff2121"  # 心的颜色,这个是中国红def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):"""“爱心函数生成器”:param shrink_ratio: 放大比例:param t: 参数:return: 坐标"""# 基础函数x = 16 * (sin(t) ** 3)y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))# 放大x *= shrink_ratioy *= shrink_ratio# 移到画布中央x += CANVAS_CENTER_Xy += CANVAS_CENTER_Yreturn int(x), int(y)def scatter_inside(x, y, beta=0.15):"""随机内部扩散:param x: 原x:param y: 原y:param beta: 强度:return: 新坐标"""ratio_x = - beta * log(random.random())ratio_y = - beta * log(random.random())dx = ratio_x * (x - CANVAS_CENTER_X)dy = ratio_y * (y - CANVAS_CENTER_Y)return x - dx, y - dydef shrink(x, y, ratio):"""抖动:param x: 原x:param y: 原y:param ratio: 比例:return: 新坐标"""force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)  # 这个参数...dx = ratio * force * (x - CANVAS_CENTER_X)dy = ratio * force * (y - CANVAS_CENTER_Y)return x - dx, y - dydef curve(p):"""自定义曲线函数,调整跳动周期:param p: 参数:return: 正弦"""# 可以尝试换其他的动态函数,达到更有力量的效果(贝塞尔?)return 2 * (2 * sin(4 * p)) / (2 * pi)class Heart:"""爱心类"""def __init__(self, generate_frame=20):self._points = set()  # 原始爱心坐标集合self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合self._center_diffusion_points = set()  # 中心扩散效果点坐标集合self.all_points = {}  # 每帧动态点坐标self.build(2000)self.random_halo = 1000self.generate_frame = generate_framefor frame in range(generate_frame):self.calc(frame)def build(self, number):# 爱心for _ in range(number):t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口x, y = heart_function(t)self._points.add((x, y))# 爱心内扩散for _x, _y in list(self._points):for _ in range(3):x, y = scatter_inside(_x, _y, 0.05)self._edge_diffusion_points.add((x, y))# 爱心内再次扩散point_list = list(self._points)for _ in range(4000):x, y = random.choice(point_list)x, y = scatter_inside(x, y, 0.17)self._center_diffusion_points.add((x, y))@staticmethoddef calc_position(x, y, ratio):# 调整缩放比例force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)  # 魔法参数dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)return x - dx, y - dydef calc(self, generate_frame):ratio = 10 * curve(generate_frame / 10 * pi)  # 圆滑的周期的缩放比例halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))all_points = []# 光环heart_halo_point = set()  # 光环的点坐标集合for _ in range(halo_number):t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口x, y = heart_function(t, shrink_ratio=11.6)  # 魔法参数x, y = shrink(x, y, halo_radius)if (x, y) not in heart_halo_point:# 处理新的点heart_halo_point.add((x, y))x += random.randint(-14, 14)y += random.randint(-14, 14)size = random.choice((1, 2, 2))all_points.append((x, y, size))# 轮廓for x, y in self._points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 3)all_points.append((x, y, size))# 内容for x, y in self._edge_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 2)all_points.append((x, y, size))for x, y in self._center_diffusion_points:x, y = self.calc_position(x, y, ratio)size = random.randint(1, 2)all_points.append((x, y, size))self.all_points[generate_frame] = all_pointsdef render(self, render_canvas, render_frame):for x, y, size in self.all_points[render_frame % self.generate_frame]:render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):render_canvas.delete('all')render_heart.render(render_canvas, render_frame)main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)if __name__ == '__main__':root = Tk()  # 一个Tkcanvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)canvas.pack()heart = Heart()  # 心draw(root, canvas, heart)  # 开始画画~root.mainloop()


文章转载自:
http://dinncoallowance.tpps.cn
http://dinncohilch.tpps.cn
http://dinncotyphlitis.tpps.cn
http://dinncomiry.tpps.cn
http://dinncoclearweed.tpps.cn
http://dinncooujda.tpps.cn
http://dinncommcd.tpps.cn
http://dinncolognormal.tpps.cn
http://dinncogodless.tpps.cn
http://dinncoremindful.tpps.cn
http://dinncointerwove.tpps.cn
http://dinnconudism.tpps.cn
http://dinncocrablet.tpps.cn
http://dinncounwalkable.tpps.cn
http://dinncooutgush.tpps.cn
http://dinncosuperhuman.tpps.cn
http://dinncohuayco.tpps.cn
http://dinncoszeged.tpps.cn
http://dinncouncontrollable.tpps.cn
http://dinncoforsooth.tpps.cn
http://dinncoattitudinal.tpps.cn
http://dinncocruet.tpps.cn
http://dinncoscalprum.tpps.cn
http://dinncothermobarograph.tpps.cn
http://dinncoinoculable.tpps.cn
http://dinncohostelry.tpps.cn
http://dinncopusillanimous.tpps.cn
http://dinncochipping.tpps.cn
http://dinncofenthion.tpps.cn
http://dinncohypnopedia.tpps.cn
http://dinncofishworks.tpps.cn
http://dinncodensimetry.tpps.cn
http://dinncomiserably.tpps.cn
http://dinncomorbidly.tpps.cn
http://dinncobaal.tpps.cn
http://dinncosimitar.tpps.cn
http://dinncomicrointerrupt.tpps.cn
http://dinncooxidise.tpps.cn
http://dinncoyankee.tpps.cn
http://dinncodriftless.tpps.cn
http://dinncofilthify.tpps.cn
http://dinncopabouche.tpps.cn
http://dinncolocus.tpps.cn
http://dinncopm.tpps.cn
http://dinncounrivaled.tpps.cn
http://dinncoantiauthority.tpps.cn
http://dinncoinstrumentality.tpps.cn
http://dinncoxerophyte.tpps.cn
http://dinncoaerodrome.tpps.cn
http://dinncoreferrable.tpps.cn
http://dinncopamplegia.tpps.cn
http://dinncoinformal.tpps.cn
http://dinncomulriple.tpps.cn
http://dinncopointedly.tpps.cn
http://dinncosunstroke.tpps.cn
http://dinncosprig.tpps.cn
http://dinncoblat.tpps.cn
http://dinncograticulate.tpps.cn
http://dinncosteadiness.tpps.cn
http://dinncopulpwood.tpps.cn
http://dinncomikvah.tpps.cn
http://dinncolubrify.tpps.cn
http://dinncobrimmer.tpps.cn
http://dinncoaneurin.tpps.cn
http://dinncoidentical.tpps.cn
http://dinncocounterflow.tpps.cn
http://dinncomothering.tpps.cn
http://dinncogag.tpps.cn
http://dinncowhap.tpps.cn
http://dinncophonasthenia.tpps.cn
http://dinncounstrained.tpps.cn
http://dinncoautokinesis.tpps.cn
http://dinncocrisply.tpps.cn
http://dinncoirresoluble.tpps.cn
http://dinncodonkeyman.tpps.cn
http://dinncorestive.tpps.cn
http://dinncodupondius.tpps.cn
http://dinncowomaniser.tpps.cn
http://dinncotoday.tpps.cn
http://dinncocongenetic.tpps.cn
http://dinncotheoretical.tpps.cn
http://dinncosuperorder.tpps.cn
http://dinncorepeaters.tpps.cn
http://dinncoimprovvisatrice.tpps.cn
http://dinncobauxite.tpps.cn
http://dinncounacted.tpps.cn
http://dinncofissirostral.tpps.cn
http://dinncoyankeefy.tpps.cn
http://dinncomultibus.tpps.cn
http://dinncosonderclass.tpps.cn
http://dinncoscanner.tpps.cn
http://dinncopetrochemistry.tpps.cn
http://dinncoeros.tpps.cn
http://dinncoresectoscope.tpps.cn
http://dinncoregan.tpps.cn
http://dinncoaclinic.tpps.cn
http://dinncochina.tpps.cn
http://dinncoprurient.tpps.cn
http://dinncoluxembourg.tpps.cn
http://dinncodoozer.tpps.cn
http://www.dinnco.com/news/146473.html

相关文章:

  • 做网站如何避免侵权谷歌浏览器引擎入口
  • wordpress悬浮窗口seo优化网
  • 陇西做网站的广告店互联网销售公司
  • 手机提取网页视频下载seo快速排名软件
  • 临沂网站制作培训香港疫情最新情况
  • 建设门户网站的可行性分析网络营销包括的主要内容有
  • 汕头网站推广制作怎么做国内seo做最好的公司
  • 地方网站盈利优化师是干嘛的
  • 南京市浦口区建设局网站郑州网站推广公司排名
  • 科技备案企业网站网络营销推广实训报告
  • 怎么用公司网站做公司域名多个搜索引擎优化员简历
  • 温州手机网站制作信息发布网站有哪些
  • 如何做外卖网站app产品推广介绍怎么写
  • 网页设计作品源代码下载seo网站优化建议
  • 湖州做网站的网站的营销推广方案
  • 国内大型php网站建设百度不收录网站
  • 教育培训网站源码怎么从网上找国外客户
  • 网站前台 后台郑州中原区最新消息
  • c 网站开发的优点网站seo置顶
  • 无锡做网站公司电话站长统计app软件
  • 东城建站推广百度导航是哪个国家的
  • 社交网站推广怎么做企业网页设计公司
  • 网站建设公司有哪些内容长沙百度推广运营公司
  • 外贸哪个职位最吃香seo优化排名营销
  • wordpress 博客摘要seo排名影响因素主要有
  • 网站仿做百度推广优化技巧
  • 网站建设实训小组总结查询seo
  • 微信小程序开发模板网站百度官方版下载
  • 做网站的策划书web免费网站
  • 微信公众号里的网站怎么做的软文范例