def onEntry(self, event): n = qrand() % self.m_stateCount + 1 while n == self.m_lastIndex: n = qrand() % self.m_stateCount + 1 self.m_lastIndex = n self.machine().postEvent(StateSwitchEvent(n))
def __init__(self): super(ColorItem, self).__init__() self.color = QColor(qrand() % 256, qrand() % 256, qrand() % 256) self.setToolTip( "QColor(%d, %d, %d)\nClick and drag this color onto the robot!" % (self.color.red(), self.color.green(), self.color.blue())) self.setCursor(Qt.OpenHandCursor)
def timerEvent(self): # Don't move too far away. lineToCenter = QLineF(QPointF(0, 0), self.mapFromScene(0, 0)) if lineToCenter.length() > 150: angleToCenter = math.acos(lineToCenter.dx() / lineToCenter.length()) if lineToCenter.dy() < 0: angleToCenter = Mouse.TwoPi - angleToCenter; angleToCenter = Mouse.normalizeAngle((Mouse.Pi - angleToCenter) + Mouse.Pi / 2) if angleToCenter < Mouse.Pi and angleToCenter > Mouse.Pi / 4: # Rotate left. self.angle += [-0.25, 0.25][self.angle < -Mouse.Pi / 2] elif angleToCenter >= Mouse.Pi and angleToCenter < (Mouse.Pi + Mouse.Pi / 2 + Mouse.Pi / 4): # Rotate right. self.angle += [-0.25, 0.25][self.angle < Mouse.Pi / 2] elif math.sin(self.angle) < 0: self.angle += 0.25 elif math.sin(self.angle) > 0: self.angle -= 0.25 # Try not to crash with any other mice. dangerMice = self.scene().items(QPolygonF([self.mapToScene(0, 0), self.mapToScene(-30, -50), self.mapToScene(30, -50)])) for item in dangerMice: if item is self: continue lineToMouse = QLineF(QPointF(0, 0), self.mapFromItem(item, 0, 0)) angleToMouse = math.acos(lineToMouse.dx() / lineToMouse.length()) if lineToMouse.dy() < 0: angleToMouse = Mouse.TwoPi - angleToMouse angleToMouse = Mouse.normalizeAngle((Mouse.Pi - angleToMouse) + Mouse.Pi / 2) if angleToMouse >= 0 and angleToMouse < Mouse.Pi / 2: # Rotate right. self.angle += 0.5 elif angleToMouse <= Mouse.TwoPi and angleToMouse > (Mouse.TwoPi - Mouse.Pi / 2): # Rotate left. self.angle -= 0.5 # Add some random movement. if len(dangerMice) > 1 and (qrand() % 10) == 0: if qrand() % 1: self.angle += (qrand() % 100) / 500.0 else: self.angle -= (qrand() % 100) / 500.0 self.speed += (-50 + qrand() % 100) / 100.0 dx = math.sin(self.angle) * 10 self.mouseEyeDirection = 0.0 if qAbs(dx / 5) < 1 else dx / 5 self.setRotation(self.rotation() + dx) self.setPos(self.mapToParent(0, -(3 + math.sin(self.speed) * 3)))
def __init__(self): super(ColorItem, self).__init__() self.color = QColor(qrand() % 256, qrand() % 256, qrand() % 256) self.setToolTip( "QColor(%d, %d, %d)\nClick and drag this color onto the robot!" % (self.color.red(), self.color.green(), self.color.blue()) ) self.setCursor(Qt.OpenHandCursor)
def confirmBtEvent(self, pressed): # print('pressed', pressed) if not pressed: value = 0 self.color.setRgb(value, value, value) else: r = qrand() % 256 g = qrand() % 256 b = qrand() % 256 self.color.setRgb(r, g, b) self.frame.setStyleSheet("QWidget { background-color: %s }" % self.color.name())
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) # 背景白色 painter.fillRect(event.rect(), QBrush(Qt.white)) # 绘制边缘虚线框 painter.setPen(Qt.DashLine) painter.setBrush(Qt.NoBrush) painter.drawRect(self.rect()) # 随机画条线 for _ in range(3): painter.setPen(QPen(QTCOLORLIST[qrand() % 5], 1, Qt.SolidLine)) painter.setBrush(Qt.NoBrush) painter.drawLine(QPoint(0, qrand() % self.height()), QPoint(self.width(), qrand() % self.height())) painter.drawLine(QPoint(qrand() % self.width(), 0), QPoint(qrand() % self.width(), self.height())) # 绘制噪点 painter.setPen(Qt.DotLine) painter.setBrush(Qt.NoBrush) for _ in range(self.width()): # 绘制噪点 painter.drawPoint(QPointF(qrand() % self.width(), qrand() % self.height())) # super(WidgetCode, self).paintEvent(event) # 绘制文字 # 绘制跳动文字 metrics = QFontMetrics(self.font()) x = (self.width() - metrics.width(self.text())) / 2 y = (self.height() + metrics.ascent() - metrics.descent()) / 2 for i, ch in enumerate(self.text()): index = (self.step + i) % 16 painter.setPen(TCOLORLIST[qrand() % 6]) painter.drawText(x, y - ((SINETABLE[index] * metrics.height()) / 400), ch) x += metrics.width(ch)
def mouseMoveEvent(self, event): if QLineF(QPointF(event.screenPos()), QPointF(event.buttonDownScreenPos(Qt.LeftButton))).length() < QApplication.startDragDistance(): return drag = QDrag(event.widget()) mime = QMimeData() drag.setMimeData(mime) ColorItem.n += 1 if ColorItem.n > 2 and qrand() % 3 == 0: image = QImage(':/images/head.png') mime.setImageData(image) drag.setPixmap(QPixmap.fromImage(image).scaled(30,40)) drag.setHotSpot(QPoint(15, 30)) else: mime.setColorData(self.color) mime.setText("#%02x%02x%02x" % (self.color.red(), self.color.green(), self.color.blue())) pixmap = QPixmap(34, 34) pixmap.fill(Qt.white) painter = QPainter(pixmap) painter.translate(15, 15) painter.setRenderHint(QPainter.Antialiasing) self.paint(painter, None, None) painter.end() pixmap.setMask(pixmap.createHeuristicMask()) drag.setPixmap(pixmap) drag.setHotSpot(QPoint(15, 20)) drag.exec_() self.setCursor(Qt.OpenHandCursor)
def timerEvent(self): #self.speed += (-50 + qrand() % 100) #self.angle = qrand() % 360 #dx = math.sin(self.angle) * 10 #self.setRotation(self.rotation() + dx) #self.setPos(self.mapToParent(0, -(3 + math.sin(self.speed) * 3))) #self.setOffset(QPointF((-50 + qrand() % 100),(-50 + qrand() % 100))) #print("x = " + str(self.offset().x()),"y = " + str(self.offset().y())) #print("speed = " + str(self.speed),"angle = " + str(self.angle)) pixmap_item.setPos( QPointF((-50 + qrand() % 100), (-50 + qrand() % 100)))
def createObjectName(self, title: str) -> str: s = title result = s while self.findTabPaneByName(result): i = qrand() result = "%s#%d" % (s, i) return result
def __init__(self): super(Mouse, self).__init__() self.angle = 0.0 self.speed = 0.0 self.mouseEyeDirection = 0.0 self.color = QColor(qrand() % 256, qrand() % 256, qrand() % 256) self.setRotation(qrand() % (360 * 16)) # In the C++ version of this example, this class is also derived from # QObject in order to receive timer events. PyQt does not support # deriving from more than one wrapped class so we just create an # explicit timer instead. self.timer = QTimer() self.timer.timeout.connect(self.timerEvent) self.timer.start(1000 / 33)
def __init__(self): super(Mouse, self).__init__() self.angle = 0.0 self.speed = 0.0 self.mouseEyeDirection = 0.0 self.color = QColor(qrand() % 256, qrand() % 256, qrand() % 256) self.setRotation(qrand() % (360 * 16)) # In the C++ version of this example, this class is also derived from # QObject in order to receive timer events. PyQtExamples does not support # deriving from more than one wrapped class so we just create an # explicit timer instead. self.timer = QTimer() self.timer.timeout.connect(self.timerEvent) self.timer.start(1000 / 33)
def __init__(self, rect, scene): super(myRectItem, self).__init__(rect) color = QColor(qrand() % 256, qrand() % 256, qrand() % 256) self.setToolTip( "QColor(%d, %d, %d)\nClick and drag this color onto the robot!" % (color.red(), color.green(), color.blue())) self.setCursor(Qt.OpenHandCursor) self.setAcceptedMouseButtons(Qt.LeftButton) self.setFlags(QtWidgets.QGraphicsItem.ItemIsMovable) self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable) # self.setFlag(QGraphicsItem.ItemSendsGeometryChanges) # self.setCacheMode(QGraphicsItem.DeviceCoordinateCache) # self.setZValue(0) self.setAcceptDrops(True) self.setSelected(True) scene.addItem(self) self.setPen(QPen(color, 2))
def keyPressEvent(self, event): key = event.key() if key == Qt.Key_Up: self.centerNode.moveBy(0, -20) elif key == Qt.Key_Down: self.centerNode.moveBy(0, 20) elif key == Qt.Key_Left: self.centerNode.moveBy(-20, 0) elif key == Qt.Key_Right: self.centerNode.moveBy(20, 0) elif key == Qt.Key_Plus: self.scaleView(1.2) elif key == Qt.Key_Minus: self.scaleView(1 / 1.2) elif key == Qt.Key_Space or key == Qt.Key_Enter: for item in self.scene().items(): if isinstance(item, Node): item.setPos(-150 + qrand() % 300, -150 + qrand() % 300) else: super(GraphWidget, self).keyPressEvent(event)
def paintEvent(self, event): self.main_width = self.width() self.main_height = self.height() painter = QPainter() painter.begin(self) painter.setPen(QPen(Qt.black, 1)) painter.setBrush(QBrush(self.colorIcon, Qt.SolidPattern)) # Painting start if self.doPaint: if self.doBar: max_val = self.mHisto.max() segment = int(self.main_width / len(self.mHisto.m_list)) rescale = self.main_height / max_val x = 0 for rect in self.mHisto.m_list: height = rect.m_amount * rescale y = self.main_height - height painter.drawRect(int(x), int(y), int(segment), int(height)) x += segment elif self.doPie: angle_scale = 360 / self.mHisto.total() start_angle = 0 segment = QRect(0, 0, 400, 400) for rect in self.mHisto.m_list: painter.setBrush( QColor(qrand() % 256, qrand() % 256, qrand() % 256)) centre = QPoint(int(self.width() / 2), int(self.width() / 2)) segment.moveCenter(centre) span_angle = (rect.m_amount * angle_scale) * 16 painter.drawPie(segment, int(start_angle), int(span_angle)) start_angle += span_angle # Painting end painter.end()
class Pokemon(QGraphicsPixmapItem): #boundary: pixmap_item = 0 adjust = 0.5 BoundingRect = QRectF(-20 - adjust, -22 - adjust, 40 + adjust, 83 + adjust) color = QColor(qrand() % 256, qrand() % 256, qrand() % 256) def __init__(self, t_pixmap_item): pixmap_item = t_pixmap_item #self.angle = 0.0 #self.speed = 0.0 self.timer = QTimer() self.timer.timeout.connect(self.timerEvent) self.timer.start(1000 / 33) #self.setRotation(0) def timerEvent(self): #self.speed += (-50 + qrand() % 100) #self.angle = qrand() % 360 #dx = math.sin(self.angle) * 10 #self.setRotation(self.rotation() + dx) #self.setPos(self.mapToParent(0, -(3 + math.sin(self.speed) * 3))) #self.setOffset(QPointF((-50 + qrand() % 100),(-50 + qrand() % 100))) #print("x = " + str(self.offset().x()),"y = " + str(self.offset().y())) #print("speed = " + str(self.speed),"angle = " + str(self.angle)) pixmap_item.setPos( QPointF((-50 + qrand() % 100), (-50 + qrand() % 100))) ''' def boundingRect(self): return self.BoundingRect ''' '''
def mouseMoveEvent(self, event): if ( QLineF( QPointF(event.screenPos()), QPointF(event.buttonDownScreenPos(Qt.LeftButton)), ).length() < QApplication.startDragDistance() ): return drag = QDrag(event.widget()) mime = QMimeData() drag.setMimeData(mime) ColorItem.n += 1 if ColorItem.n > 2 and qrand() % 3 == 0: root = QFileInfo(__file__).absolutePath() image = QImage(root + "/images/head.png") mime.setImageData(image) drag.setPixmap(QPixmap.fromImage(image).scaled(30, 40)) drag.setHotSpot(QPoint(15, 30)) else: mime.setColorData(self.color) mime.setText( "#%02x%02x%02x" % (self.color.red(), self.color.green(), self.color.blue()) ) pixmap = QPixmap(34, 34) pixmap.fill(Qt.white) painter = QPainter(pixmap) painter.translate(15, 15) painter.setRenderHint(QPainter.Antialiasing) self.paint(painter, None, None) painter.end() pixmap.setMask(pixmap.createHeuristicMask()) drag.setPixmap(pixmap) drag.setHotSpot(QPoint(15, 20)) drag.exec_() self.setCursor(Qt.OpenHandCursor)
def randomValue(): #a number between [ 0.0, 1.0 ] return (qrand() % 100000) / 100000.0
def randNo(low, high): qr = qrand() % ((high + 1) - low) + low; return qr
def create_obs(self): qsrand(QTime(0, 0, 0).secsTo(QTime.currentTime())) self.obs_pos = QPoint((2 + (qrand() % (self.cols - 4))), 0)
# Values. for i, item in enumerate(items): # Ellipse. ellipseState.assignProperty(item, 'pos', QPointF(math.cos((i / 63.0) * 6.28) * 250, math.sin((i / 63.0) * 6.28) * 250)) # Figure 8. figure8State.assignProperty(item, 'pos', QPointF(math.sin((i / 63.0) * 6.28) * 250, math.sin(((i * 2)/63.0) * 6.28) * 250)) # Random. randomState.assignProperty(item, 'pos', QPointF(-250 + qrand() % 500, -250 + qrand() % 500)) # Tiled. tiledState.assignProperty(item, 'pos', QPointF(((i % 8) - 4) * kineticPix.width() + kineticPix.width() / 2, ((i // 8) - 4) * kineticPix.height() + kineticPix.height() / 2)) # Centered. centeredState.assignProperty(item, 'pos', QPointF()) # Ui. view = View(scene) view.setWindowTitle("Animated Tiles") view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate) view.setBackgroundBrush(QBrush(bgPix)) view.setCacheMode(QGraphicsView.CacheBackground)
def eventTest(self, e): return super(LightningStrikesTransition, self).eventTest(e) and (qrand() % 50) == 0
moveButton.setPos(74, 40) centeredButton.setPos(-186, -102) scene.addItem(buttonParent) buttonParent.setPos(-73, -40) #positions groups of buttons buttonParent.setZValue(65) rootState = QState() moveState = QState(rootState) centeredState = QState(rootState) for i, item in enumerate(items): # move moveState.assignProperty( item, 'pos', QPointF(-500 + qrand() % 500, -400 + qrand() % 500)) #how big the effects are # centered centeredState.assignProperty(item, 'pos', QPointF()) # interface view = View(scene) view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate) view.setBackgroundBrush(QBrush(bgPix)) view.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) view.setWindowFlags(Qt.FramelessWindowHint) view.show() states = QStateMachine() states.addState(rootState) states.setInitialState(rootState)
def timerEvent(self): # Don't move too far away. lineToCenter = QLineF(QPointF(0, 0), self.mapFromScene(0, 0)) if lineToCenter.length() > 150: angleToCenter = math.acos(lineToCenter.dx() / lineToCenter.length()) if lineToCenter.dy() < 0: angleToCenter = Mouse.TwoPi - angleToCenter angleToCenter = Mouse.normalizeAngle((Mouse.Pi - angleToCenter) + Mouse.Pi / 2) if angleToCenter < Mouse.Pi and angleToCenter > Mouse.Pi / 4: # Rotate left. self.angle += [-0.25, 0.25][self.angle < -Mouse.Pi / 2] elif angleToCenter >= Mouse.Pi and angleToCenter < ( Mouse.Pi + Mouse.Pi / 2 + Mouse.Pi / 4): # Rotate right. self.angle += [-0.25, 0.25][self.angle < Mouse.Pi / 2] elif math.sin(self.angle) < 0: self.angle += 0.25 elif math.sin(self.angle) > 0: self.angle -= 0.25 # Try not to crash with any other mice. dangerMice = self.scene().items( QPolygonF([ self.mapToScene(0, 0), self.mapToScene(-30, -50), self.mapToScene(30, -50) ])) for item in dangerMice: if item is self: continue lineToMouse = QLineF(QPointF(0, 0), self.mapFromItem(item, 0, 0)) angleToMouse = math.acos(lineToMouse.dx() / lineToMouse.length()) if lineToMouse.dy() < 0: angleToMouse = Mouse.TwoPi - angleToMouse angleToMouse = Mouse.normalizeAngle((Mouse.Pi - angleToMouse) + Mouse.Pi / 2) if angleToMouse >= 0 and angleToMouse < Mouse.Pi / 2: # Rotate right. self.angle += 0.5 elif angleToMouse <= Mouse.TwoPi and angleToMouse > (Mouse.TwoPi - Mouse.Pi / 2): # Rotate left. self.angle -= 0.5 # Add some random movement. if len(dangerMice) > 1 and (qrand() % 10) == 0: if qrand() % 1: self.angle += (qrand() % 100) / 500.0 else: self.angle -= (qrand() % 100) / 500.0 self.speed += (-50 + qrand() % 100) / 100.0 dx = math.sin(self.angle) * 10 self.mouseEyeDirection = 0.0 if qAbs(dx / 5) < 1 else dx / 5 self.setRotation(self.rotation() + dx) self.setPos(self.mapToParent(0, -(3 + math.sin(self.speed) * 3)))
ellipseState.assignProperty( item, 'pos', QPointF( math.cos((i / 63.0) * 6.28) * 250, math.sin((i / 63.0) * 6.28) * 250)) # Figure 8. figure8State.assignProperty( item, 'pos', QPointF( math.sin((i / 63.0) * 6.28) * 250, math.sin(((i * 2) / 63.0) * 6.28) * 250)) # Random. randomState.assignProperty( item, 'pos', QPointF(-250 + qrand() % 500, -250 + qrand() % 500)) # Tiled. tiledState.assignProperty( item, 'pos', QPointF( ((i % 8) - 4) * kineticPix.width() + kineticPix.width() / 2, ((i // 8) - 4) * kineticPix.height() + kineticPix.height() / 2)) # Centered. centeredState.assignProperty(item, 'pos', QPointF()) # Ui. view = View(scene) view.setWindowTitle("Animated Tiles")
def createObjectName ( self, title ): result = title while result in self.spawned: i = qrand () result = "%s#%d" % (title, i) return result
def eventTest(self, e): return (super(LightningStrikesTransition, self).eventTest(e) and (qrand() % 50) == 0)
def onchange_text(): color = QColor(qrand() % 256, qrand() % 256, qrand() % 256) w.setStyleSheet('background: rgb({}, {}, {});'.format(color.red(), color.green(), color.blue()))
def __init__(self): super().__init__() self.setBackgroundBrush(QBrush(QPixmap(':/images/background.png'))) self.skill_label = self.addSimpleText(Config.UserName, Config.BigFont) self.skill_label.setPos(-400, -100) self.photos = list() group = QParallelAnimationGroup(self) for i in range(7): photo = Photo() self.photos.append(photo) self.addItem(photo) x = i * photo.boundingRect().width() + Config.Rect.x() y = Config.Rect.y() + 10 duration = 1500.0 * qrand() / RAND_MAX translation = QPropertyAnimation(photo, bytes("pos", 'utf-8')) translation.setEndValue(QPointF(x, y)) translation.setEasingCurve(QEasingCurve.OutBounce) translation.setDuration(duration) group.addAnimation(translation) self.photos[0].loadAvatar("generals/small/caocao.png") self.photos[1].loadAvatar("generals/small/liubei.png") self.photos[2].loadAvatar("generals/small/sunquan.png") self.photos[3].loadAvatar("generals/small/simayi.png") self.photos[4].loadAvatar("generals/small/guojia.png") self.photos[5].loadAvatar("generals/small/zhugeliang.png") self.photos[6].loadAvatar("generals/small/zhouyu.png") self.dashboard = Dashboard() self.dashboard.setGeneral(General("caocao", "wei", 4, True)) self.addItem(self.dashboard) start_pos = Config.Rect.topLeft() end_pos = QPointF( Config.Rect.x(), Config.Rect.bottom() - self.dashboard.boundingRect().height()) duration = 1500 translation = QPropertyAnimation(self.dashboard, bytes("pos", 'utf-8')) translation.setStartValue(start_pos) translation.setEndValue(end_pos) translation.setEasingCurve(QEasingCurve.OutBounce) translation.setDuration(duration) enlarge = QPropertyAnimation(self.dashboard, bytes("scale", 'utf-8')) enlarge.setStartValue(0.2) enlarge.setEndValue(1.0) enlarge.setEasingCurve(QEasingCurve.OutBounce) enlarge.setDuration(duration) group.addAnimation(translation) group.addAnimation(enlarge) group.start(QAbstractAnimation.DeleteWhenStopped) card1 = Card("savage_assault", Card.Spade, 1) card2 = Card("slash", Card.Club, 7) card3 = Card("jink", Card.Heart, 2) card4 = Card("peach", Card.Diamond, 10) card5 = Card("archery_attack", Card.Heart, 11) card6 = Card("crossbow", Card.Club, 12) self.dashboard.addCard(card1) self.dashboard.addCard(card2) self.dashboard.addCard(card3) self.dashboard.addCard(card4) self.dashboard.addCard(card5) self.dashboard.addCard(card6) card4.setEnabled(False)
centeredState = QState(rootState) # Values. for i, item in enumerate(items): # Ellipse. ellipseState.assignProperty( item, "pos", QPointF(math.cos((i / 63.0) * 6.28) * 250, math.sin((i / 63.0) * 6.28) * 250) ) # Figure 8. figure8State.assignProperty( item, "pos", QPointF(math.sin((i / 63.0) * 6.28) * 250, math.sin(((i * 2) / 63.0) * 6.28) * 250) ) # Random. randomState.assignProperty(item, "pos", QPointF(-250 + qrand() % 500, -250 + qrand() % 500)) # Tiled. tiledState.assignProperty( item, "pos", QPointF( ((i % 8) - 4) * kineticPix.width() + kineticPix.width() / 2, ((i // 8) - 4) * kineticPix.height() + kineticPix.height() / 2, ), ) # Centered. centeredState.assignProperty(item, "pos", QPointF()) # Ui.
def __init__(self): super(node,self).__init__() self.color = QColor(qrand() % 256, qrand() % 256, qrand() % 256) self.timer = QtCore.QBasicTimer()
math.sin((i / 63.0) * 6.28) * 250), ) # Figure 8. figure8State.assignProperty( item, "pos", QPointF( math.sin((i / 63.0) * 6.28) * 250, math.sin(((i * 2) / 63.0) * 6.28) * 250, ), ) # Random. randomState.assignProperty( item, "pos", QPointF(-250 + qrand() % 500, -250 + qrand() % 500)) # Tiled. tiledState.assignProperty( item, "pos", QPointF( ((i % 8) - 4) * kineticPix.width() + kineticPix.width() / 2, ((i // 8) - 4) * kineticPix.height() + kineticPix.height() / 2, ), ) # Centered. centeredState.assignProperty(item, "pos", QPointF()) # Ui.