網站首頁 編程語言 正文
核心代碼:
from random import random from time import time from PyQt5.QtCore import QPropertyAnimation, QObject, pyqtProperty, QEasingCurve,\ Qt, QRectF, pyqtSignal from PyQt5.QtGui import QColor, QPainterPath, QPainter from PyQt5.QtWidgets import QWidget __Author__ = """By: Irony QQ: 892768447 Email: 892768447@qq.com""" __Copyright__ = 'Copyright (c) 2018 Irony' __Version__ = 1.0 try: import pointtool # @UnusedImport @UnresolvedImport getDistance = pointtool.getDistance findClose = pointtool.findClose except: import math def getDistance(p1, p2): return math.pow(p1.x - p2.x, 2) + math.pow(p1.y - p2.y, 2) def findClose(points): plen = len(points) for i in range(plen): closest = [None, None, None, None, None] p1 = points[i] for j in range(plen): p2 = points[j] dte1 = getDistance(p1, p2) if p1 != p2: placed = False for k in range(5): if not placed: if not closest[k]: closest[k] = p2 placed = True for k in range(5): if not placed: if dte1 < getDistance(p1, closest[k]): closest[k] = p2 placed = True p1.closest = closest class Target: def __init__(self, x, y): self.x = x self.y = y class Point(QObject): valueChanged = pyqtSignal() def __init__(self, x, ox, y, oy, *args, **kwargs): super(Point, self).__init__(*args, **kwargs) self.__x = x self._x = x self.originX = ox self._y = y self.__y = y self.originY = oy # 5個閉合點 self.closest = [0, 0, 0, 0, 0] # 圓半徑 self.radius = 2 + random() * 2 # 連線顏色 self.lineColor = QColor(156, 217, 249) # 圓顏色 self.circleColor = QColor(156, 217, 249) def initAnimation(self): # 屬性動畫 if not hasattr(self, 'xanimation'): self.xanimation = QPropertyAnimation( self, b'x', self, valueChanged=self.valueChanged.emit, easingCurve=QEasingCurve.InOutSine) self.yanimation = QPropertyAnimation( self, b'y', self, valueChanged=self.valueChanged.emit, easingCurve=QEasingCurve.InOutSine, finished=self.updateAnimation) self.updateAnimation() def updateAnimation(self): self.xanimation.stop() self.yanimation.stop() duration = (1 + random()) * 1000 self.xanimation.setDuration(duration) self.yanimation.setDuration(duration) self.xanimation.setStartValue(self.__x) self.xanimation.setEndValue(self.originX - 50 + random() * 100) self.yanimation.setStartValue(self.__y) self.yanimation.setEndValue(self.originY - 50 + random() * 100) self.xanimation.start() self.yanimation.start() @pyqtProperty(float) def x(self): return self._x @x.setter def x(self, x): self._x = x @pyqtProperty(float) def y(self): return self._y @y.setter def y(self, y): self._y = y class Window(QWidget): def __init__(self, *args, **kwargs): super(Window, self).__init__(*args, **kwargs) self.setMouseTracking(True) self.resize(800, 600) self.points = [] self.target = Target(self.width() / 2, self.height() / 2) self.initPoints() def paintEvent(self, event): super(Window, self).paintEvent(event) painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.fillRect(self.rect(), Qt.black) self.animate(painter) painter.end() def mouseMoveEvent(self, event): super(Window, self).mouseMoveEvent(event) # 鼠標移動時更新xy坐標 self.target.x = event.x() self.target.y = event.y() self.update() def initPoints(self): t = time() self.points.clear() # 創建點 stepX = self.width() / 20 stepY = self.height() / 20 for x in range(0, self.width(), int(stepX)): for y in range(0, self.height(), int(stepY)): ox = x + random() * stepX oy = y + random() * stepY point = Point(ox, ox, oy, oy) point.valueChanged.connect(self.update) self.points.append(point) print(time() - t) t = time() # 每個點尋找5個閉合點 findClose(self.points) print(time() - t) def animate(self, painter): for p in self.points: # 檢測點的范圍 value = abs(getDistance(self.target, p)) if value < 4000: # 其實就是修改顏色透明度 p.lineColor.setAlphaF(0.3) p.circleColor.setAlphaF(0.6) elif value < 20000: p.lineColor.setAlphaF(0.1) p.circleColor.setAlphaF(0.3) elif value < 40000: p.lineColor.setAlphaF(0.02) p.circleColor.setAlphaF(0.1) else: p.lineColor.setAlphaF(0) p.circleColor.setAlphaF(0) # 畫線條 if p.lineColor.alpha(): for pc in p.closest: if not pc: continue path = QPainterPath() path.moveTo(p.x, p.y) path.lineTo(pc.x, pc.y) painter.save() painter.setPen(p.lineColor) painter.drawPath(path) painter.restore() # 畫圓 painter.save() painter.setPen(Qt.NoPen) painter.setBrush(p.circleColor) painter.drawRoundedRect(QRectF( p.x - p.radius, p.y - p.radius, 2 * p.radius, 2 * p.radius), p.radius, p.radius) painter.restore() # 開啟動畫 p.initAnimation() if __name__ == '__main__': import sys import cgitb sys.excepthook = cgitb.enable(1, None, 5, '') from PyQt5.QtWidgets import QApplication app = QApplication(sys.argv) w = Window() w.show() sys.exit(app.exec_())
運行結果如下:
原文鏈接:https://blog.csdn.net/Vertira/article/details/123555880
相關推薦
- 2022-07-26 使用SpringBoot?+?Redis?實現接口限流的方式_Redis
- 2022-02-04 Win10 無法保存對hosts權限所作的更改 拒絕訪問
- 2022-03-07 C++使用TinyXML解析XML_C 語言
- 2022-07-23 SQL?Server刪除表中的重復數據_MsSql
- 2022-10-03 利用正則表達式校驗金額最多保留兩位小數實例代碼_正則表達式
- 2022-06-02 redis?sentinel監控高可用集群實現的配置步驟_Redis
- 2022-07-13 查看nginx連接數
- 2022-03-15 BeanCreationException或NoSuchBeanDefinitionExceptio
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支