class StackedWidgetSlideOverAnimator(AbstractAnimator): m_direction: AnimationDirection m_coveredWidget: QWidget m_decorator: StackedWidgetSlideOverDecorator m_animation: QPropertyAnimation def __init__(self, _container, _widgetForSlide): super(StackedWidgetSlideOverAnimator, self).__init__(_container) self.m_direction = AnimationDirection.FromLeftToRight self.m_coveredWidget = _container.currentWidget() self.m_decorator = StackedWidgetSlideOverDecorator( _container, _widgetForGrab=_widgetForSlide) self.m_animation = QPropertyAnimation(self.m_decorator, b"pos") _container.installEventFilter(self) self.m_animation.setDuration(400) self.m_decorator.hide() def finished(): self.setAnimatedStopped() if self.isAnimatedForward(): _container.setCurrentWidget(_widgetForSlide) self.m_decorator.hide() self.m_animation.finished.connect(finished) def updateCoveredWidget(self): self.m_coveredWidget = self.stackedWidget().currentWidget() def setAnimationDirection(self, _direction): if self.m_direction != _direction: self.m_direction = _direction def animationDuration(self): return self.m_animation.duration() def animateForward(self): self.slideOverIn() def slideOverIn(self): if self.isAnimated() and self.isAnimatedForward(): return self.setAnimatedForward() self.m_decorator.grabWidget() startPos = QPoint() finalPos = QPoint() if self.m_direction == AnimationDirection.FromLeftToRight: startPos.setX(-1 * self.stackedWidget().width()) if self.m_direction == AnimationDirection.FromRightToLeft: startPos.setX(self.stackedWidget().width()) if self.m_direction == AnimationDirection.FromTopToBottom: startPos.setY(-1 * self.stackedWidget().height()) if self.m_direction == AnimationDirection.FromBottomToTop: startPos.setY(self.stackedWidget().height()) self.m_decorator.show() self.m_decorator.raise_() if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.InOutExpo) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(startPos) self.m_animation.setEndValue(finalPos) self.m_animation.start() def animateBackward(self): self.slideOverOut() def slideOverOut(self): if self.isAnimated() and self.isAnimatedBackward(): return self.setAnimatedBackward() self.m_decorator.grabWidget() startPos = QPoint() finalPos = QPoint() if self.m_direction == AnimationDirection.FromLeftToRight: finalPos.setX(-1 * self.stackedWidget().width()) if self.m_direction == AnimationDirection.FromRightToLeft: finalPos.setX(self.stackedWidget().width()) if self.m_direction == AnimationDirection.FromTopToBottom: finalPos.setY(-1 * self.stackedWidget().height()) if self.m_direction == AnimationDirection.FromBottomToTop: finalPos.setY(self.stackedWidget().height()) if isinstance(self.stackedWidget(), type(self)): self.stackedWidget().setCurrentWidget(self.m_coveredWidget) self.m_decorator.show() self.m_decorator.raise_() if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.InOutExpo) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(startPos) self.m_animation.setEndValue(finalPos) self.m_animation.start() def eventFilter(self, _object, _event): if _object == self.stackedWidget() and _event.type( ) == QEvent.Resize and self.m_decorator.isVisible(): self.m_decorator.grabWidget() return QWidget.eventFilter(self, _object, _event) def stackedWidget(self): return self.parent()
class MyForm(QDialog): def __init__(self): super(MyForm, self).__init__() self.ui = uic.loadUi('Game.ui', self) self.ui.frame.setStyleSheet('background-image:url("Lane.png")') self.image = [ "Car-Blue.png", "Car-Green.png", "Car-Oranges.png", "Car-Pink.png" ] self.setWindowTitle('Car Rush') self.setWindowIcon(QIcon('Supercar.png')) self.ui.pushButton_2.setEnabled(False) self.ui.pushButton_4.setEnabled(True) self.ui.pushButton_3.setEnabled(False) self.ui.pushButton_5.setEnabled(False) self.ui.pushButton_2.clicked.connect(self.Go) self.ui.pushButton_3.clicked.connect(self.stop) self.ui.pushButton_4.clicked.connect(self.Go) self.ui.pushButton_5.clicked.connect(self.resume) self.point = 0 self.x = 190 self.y = 400 self.settime2 = 6000 self.speed2 = 6000 self.settime = 5000 self.speed = 5000 def Go(self): print("Go") MF3 = MyForm3() MF3.show() self.resetpoint() self.setPos() # self.Disable() # Xe1 self.timexe1 = QtCore.QTimer() self.timexe1.start(self.settime) self.timexe1.timeout.connect(self.create) # Xe2 self.timexe2 = QtCore.QTimer() self.timexe2.start(self.settime2) self.timexe2.timeout.connect(self.create2) # point self.time_point = QtCore.QTimer() self.time_point.start(self.speed) self.time_point.timeout.connect(self.calculatePoint) #point 2 self.time_point2 = QtCore.QTimer() self.time_point2.start(self.speed2) self.time_point2.timeout.connect(self.calculatePoint2) # checkx self.timeCheck = QtCore.QTimer() self.timeCheck.start(100) self.timeCheck.timeout.connect(self.check) # time Disable self.timeDisable = QtCore.QTimer() self.timeDisable.start(4000) self.timeDisable.timeout.connect(self.Enabled) def create(self): image = random.choice(self.image) self.labelXe1.setPixmap(QPixmap(image)) self.lstR = [30, 190, 360] self.a = random.choice(self.lstR) self.CN = QPropertyAnimation(self.ui.labelXe1, b"geometry") self.CN.setDuration(self.speed) self.CN.setStartValue(QRect(self.a, -50, 100, 150)) self.CN.setEndValue(QRect(self.a, 800, 100, 150)) self.CN.start() def create2(self): self.lstR2 = [30, 190, 360] r2 = random.choice(self.lstR2) if r2 != self.a: self.labelXe2.setPixmap(QPixmap(random.choice(self.image))) self.CN2 = QPropertyAnimation(self.ui.labelXe2, b"geometry") self.CN2.setDuration(self.speed) self.CN2.setStartValue(QRect(r2, -50, 100, 150)) self.CN2.setEndValue(QRect(r2, 800, 100, 150)) self.CN2.start() else: self.lstR2.remove(self.a) r2 = random.choice(self.lstR2) self.labelXe2.setPixmap(QPixmap(random.choice(self.image))) self.CN2 = QPropertyAnimation(self.ui.labelXe2, b"geometry") self.CN2.setDuration(self.speed) self.CN2.setStartValue(QRect(r2, -50, 100, 150)) self.CN2.setEndValue(QRect(r2, 800, 100, 150)) self.CN2.start() def Enabled(self): print("Enabled") self.ui.pushButton_2.setEnabled(False) self.ui.pushButton_3.setEnabled(True) self.ui.pushButton_4.setEnabled(False) self.ui.pushButton_5.setEnabled(True) self.timeDisable.stop() def Disable(self): print("Disable") self.ui.pushButton_2.setEnabled(False) self.ui.pushButton_3.setEnabled(False) self.ui.pushButton_4.setEnabled(False) self.ui.pushButton_5.setEnabled(False) def setPos(self): self.ui.labelXe1.setGeometry(180, -200, 100, 150) self.ui.label_10.setGeometry(190, 400, 100, 150) self.x = 190 self.y = 400 def check(self): # tọa độ xe 1 w_car = self.ui.labelXe1.width() h_car = self.ui.labelXe1.height() X_car = self.ui.labelXe1.x() Y_car = self.ui.labelXe1.y() # tọa độ xe 2 w_car2 = self.ui.labelXe2.width() h_car2 = self.ui.labelXe2.height() X_car2 = self.ui.labelXe2.x() Y_car2 = self.ui.labelXe2.y() # chiều cao, rộng xe đỏ w_card = self.ui.label_10.width() h_card = self.ui.label_10.height() # print("Xe 1 ", X_car, Y_car, "|","Xe 2 ", X_car2, Y_car2,"|", self.x, self.y) if ((self.x <= (X_car + w_car) and self.x >= X_car) or (self.x + w_card >= X_car and self.x + w_card <= X_car + w_car)) \ and ((h_car + Y_car >= self.y) and (h_card + Y_car <= self.y + h_card) or ((Y_car >= self.y) and (Y_car <= self.y + w_card))): self.time_point.stop() self.time_point2.stop() self.timeCheck.stop() self.timexe1.stop() self.timexe2.stop() self.CN.stop() self.CN2.stop() self.ui.labelXe1.move(-200, -200) self.ui.labelXe2.move(-200, -200) self.Disable() self.a = None print("Game Over") self.MF2 = MyForm2() self.MF2.ui.pushButton.clicked.connect(self.PlayAgain) self.MF2.ui.label.setText('SCORE: ' + str(self.point)) self.MF2.show() if ((self.x <= (X_car2 + w_car2) and self.x >= X_car2) or (self.x + w_card >= X_car2 and self.x + w_card <= X_car2 + w_car2)) \ and ((h_car2 + Y_car2 >= self.y) and (h_card + Y_car2 <= self.y + h_card) or ((Y_car2 >= self.y) and (Y_car2 <= self.y + w_card))): self.time_point.stop() self.time_point2.stop() self.timexe1.stop() self.timexe2.stop() self.timeCheck.stop() self.CN.stop() self.CN2.stop() self.ui.labelXe1.move(-200, -200) self.ui.labelXe2.move(-200, -200) self.Disable() self.a = None print("Game Over") self.MF2 = MyForm2() self.MF2.ui.pushButton.clicked.connect(self.PlayAgain) self.MF2.ui.label.setText('SCORE: ' + str(self.point)) self.MF2.show() def PlayAgain(self): self.Go() self.MF2.close() def move(self): self.anim = QPropertyAnimation(self.ui.label_10, b"geometry") self.anim.setDuration(-10) self.anim.setStartValue(QRect(self.x, self.y, 100, 150)) self.anim.setEndValue(QRect(self.x, self.y, 100, 150)) self.anim.start() def stop(self): self.timexe1.stop() self.timexe2.stop() self.time_point.stop() self.time_point2.stop() self.timeCheck.stop() self.CN.pause() self.CN2.pause() print("End") def resume(self): self.timexe1.start() self.timexe2.start() self.time_point.start() self.time_point2.start() self.timeCheck.start() self.CN.resume() self.CN2.resume() def calculatePoint(self): if self.ui.labelXe1.y() > 700: self.point += 10 self.ui.label.setText(str(self.point)) print("Calculate Point 1") if self.point == 20: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) print(self.point) if self.point == 50: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) if self.point == 70: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) if self.point == 100: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) if self.point == 150: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) if self.point == 200: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) def calculatePoint2(self): if self.ui.labelXe2.y() > 700: self.point += 10 print(self.point) self.ui.label.setText(str(self.point)) print("Calculate Point 2") def resetpoint(self): self.point = 0 self.ui.label.setText(str(0)) def keyPressEvent(self, e): if e.key() == QtCore.Qt.Key_W or e.key() == QtCore.Qt.Key_Up: if self.y > 250: self.y -= 30 self.x += 0 elif e.key() == QtCore.Qt.Key_S or e.key() == QtCore.Qt.Key_Down: if self.y < 420: self.y += 30 # self.x += 0 elif e.key() == QtCore.Qt.Key_A or e.key() == QtCore.Qt.Key_Right: if self.x > 10: self.y += 0 self.x -= 30 elif e.key() == QtCore.Qt.Key_D or e.key() == QtCore.Qt.Key_Left: if self.x < 350: self.y += 0 self.x += 30 self.move()
class CircleFillAnimator(AbstractAnimator): m_decorator: CircleFillDecorator m_animation: QPropertyAnimation m_hideAfterFinish = True def __init__(self, _widgetForFill): super(CircleFillAnimator, self).__init__(_widgetForFill) self.m_decorator = CircleFillDecorator(_widgetForFill) self.m_animation = QPropertyAnimation(self.m_decorator, b"_radius") _widgetForFill.installEventFilter(self) self.m_animation.setDuration(500) self.m_decorator.setAttribute(Qt.WA_TransparentForMouseEvents) self.m_decorator.hide() def finished(): self.setAnimatedStopped() if self.m_hideAfterFinish: self.hideDecorator() self.m_animation.finished.connect(finished) def setStartPoint(self, _point): self.m_decorator.setStartPoint(_point) def setFillColor(self, _color): self.m_decorator.setFillColor(_color) def setHideAfterFinish(self, _hide): self.m_hideAfterFinish = _hide def animationDuration(self): return self.m_animation.duration() def animateForward(self): self.fillIn() def fillIn(self): if self.isAnimated() and self.isAnimatedForward(): return self.setAnimatedForward() startRadius = 0 finalRadius = math.sqrt( self.widgetForFill().height() * self.widgetForFill().height() + self.widgetForFill().width() * self.widgetForFill().width()) self.m_decorator.resize(self.widgetForFill().size()) self.m_decorator.move(0, 0) self.m_decorator.show() self.m_decorator.raise_() if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.OutQuart) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(startRadius) self.m_animation.setEndValue(finalRadius) self.m_animation.start() def animateBackward(self): self.fillOut() def fillOut(self): if self.isAnimated() and self.isAnimatedBackward(): return self.setAnimatedBackward() startRadius = math.sqrt( self.widgetForFill().height() * self.widgetForFill().height() + self.widgetForFill().width() * self.widgetForFill().width()) finalRadius = 0 self.m_decorator.resize(self.widgetForFill().size()) self.m_decorator.move(0, 0) self.m_decorator.show() self.m_decorator.raise_() if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.OutQuart) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(startRadius) self.m_animation.setEndValue(finalRadius) self.m_animation.start() def hideDecorator(self): hideEffect = self.m_decorator.graphicsEffect() if hideEffect == None: hideEffect = QGraphicsOpacityEffect(self.m_decorator) self.m_decorator.setGraphicsEffect(hideEffect) hideEffect.setOpacity(1) hideAnimation = QPropertyAnimation(hideEffect, b"opacity", self.m_decorator) hideAnimation.setDuration(400) hideAnimation.setStartValue(1) hideAnimation.setEndValue(0) def finished(): self.m_decorator.hide() hideEffect.setOpacity(1) hideAnimation.finished.connect(finished) hideAnimation.start(QAbstractAnimation.DeleteWhenStopped) def widgetForFill(self): return self.parent()
class StackedWidgetFadeInAnimator(AbstractAnimator): m_decorator: StackedWidgetFadeInDecorator m_animation: QPropertyAnimation def __init__(self, _container, _fadeWidget): super(StackedWidgetFadeInAnimator, self).__init__(_container) self.m_decorator = StackedWidgetFadeInDecorator(_container, _fadeWidget=_fadeWidget) self.m_animation = QPropertyAnimation(self.m_decorator, b"opacity") _container.installEventFilter(self) self.m_animation.setDuration(200) self.m_decorator.hide() def finished(): self.setAnimatedStopped() if self.m_animation.direction() == QPropertyAnimation.Forward: _container.setCurrentWidget(_fadeWidget) self.m_decorator.hide() self.m_animation.finished.connect(finished) def animationDuration(self): return self.m_animation.duration() def animateForward(self): self.fadeIn() def fadeIn(self): if self.isAnimated() and self.isAnimatedForward(): return self.setAnimatedForward() self.m_decorator.grabContainer() self.m_decorator.grabFadeWidget() startOpacity = 0 finalOpacity = 1 self.m_decorator.setOpacity(startOpacity) self.m_decorator.move(0, 0) self.m_decorator.show() self.m_decorator.raise_() if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.InQuad) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(startOpacity) self.m_animation.setEndValue(finalOpacity) self.m_animation.start() def eventFilter(self, _object, _event): if _object == self.fadeWidget() and _event.type() == QEvent.Resize and self.m_decorator.isVisible(): self.m_decorator.grabContainer() self.m_decorator.grabFadeWidget() return QWidget.eventFilter(self, _object, _event) def fadeWidget(self): return self.parent()
class ScatterDataModifier(QObject): shadowQualityChanged = pyqtSignal() def __init__(self, scatter): super(ScatterDataModifier, self).__init__() self.m_graph = scatter self.m_inputHandler = CustomInputHandler() self.m_graph.activeTheme().setType(Q3DTheme.ThemeDigia) self.m_graph.setShadowQuality(QAbstract3DGraph.ShadowQualityMedium) self.m_graph.scene().activeCamera().setCameraPreset( Q3DCamera.CameraPresetFront) self.m_graph.setAxisX(QValue3DAxis()) self.m_graph.setAxisY(QValue3DAxis()) self.m_graph.setAxisZ(QValue3DAxis()) self.m_graph.axisX().setRange(-10.0, 10.0) self.m_graph.axisY().setRange(-5.0, 5.0) self.m_graph.axisZ().setRange(-5.0, 5.0) series = QScatter3DSeries() series.setItemLabelFormat("@xLabel, @yLabel, @zLabel") series.setMesh(QAbstract3DSeries.MeshCube) series.setItemSize(0.15) self.m_graph.addSeries(series) self.m_animationCameraX = QPropertyAnimation( self.m_graph.scene().activeCamera(), 'xRotation') self.m_animationCameraX.setDuration(20000) self.m_animationCameraX.setStartValue(0.0) self.m_animationCameraX.setEndValue(360.0) self.m_animationCameraX.setLoopCount(-1) upAnimation = QPropertyAnimation(self.m_graph.scene().activeCamera(), 'yRotation') upAnimation.setDuration(9000) upAnimation.setStartValue(5.0) upAnimation.setEndValue(45.0) downAnimation = QPropertyAnimation(self.m_graph.scene().activeCamera(), 'yRotation') downAnimation.setDuration(9000) downAnimation.setStartValue(45.0) downAnimation.setEndValue(5.0) self.m_animationCameraY = QSequentialAnimationGroup() self.m_animationCameraY.setLoopCount(-1) self.m_animationCameraY.addAnimation(upAnimation) self.m_animationCameraY.addAnimation(downAnimation) self.m_animationCameraX.start() self.m_animationCameraY.start() self.m_graph.setActiveInputHandler(self.m_inputHandler) self.m_selectionTimer = QTimer() self.m_selectionTimer.setInterval(10) self.m_selectionTimer.timeout.connect(self.triggerSelection) self.m_selectionTimer.start() def start(self): self.addData() def addData(self): dataArray = [] stream = QTextStream() dataFile = QFile(QFileInfo(__file__).absolutePath() + '/data.txt') if dataFile.open(QIODevice.ReadOnly | QIODevice.Text): stream.setDevice(dataFile) while not stream.atEnd(): line = stream.readLine() if line.startswith('#'): continue strList = line.split(',') # Each line has three data items: xPos, yPos and zPos values. if len(strList) < 3: continue position = QVector3D(float(strList[0]), float(strList[1]), float(strList[2])) dataArray.append(QScatterDataItem(position)) self.m_graph.seriesList()[0].dataProxy().resetArray(dataArray) def toggleCameraAnimation(self): if self.m_animationCameraX.state() != QAbstractAnimation.Paused: self.m_animationCameraX.pause() self.m_animationCameraY.pause() else: self.m_animationCameraX.resume() self.m_animationCameraY.resume() def triggerSelection(self): self.m_graph.scene().setSelectionQueryPosition( self.m_inputHandler.inputPosition()) def shadowQualityUpdatedByVisual(self, sq): self.shadowQualityChanged.emit(int(sq)) def changeShadowQuality(self, quality): self.m_graph.setShadowQuality(QAbstract3DGraph.ShadowQuality(quality))
class ExpandAnimator(AbstractAnimator): m_decorator: ExpandDecorator m_animation: QPropertyAnimation m_expandRect: QRect def __init__(self, _widgetForFill): super(ExpandAnimator, self).__init__(_widgetForFill) self.m_decorator = ExpandDecorator(_widgetForFill) self.m_animation = QPropertyAnimation(self.m_decorator, b"_expandRect") _widgetForFill.installEventFilter(self) self.m_animation.setDuration(400) self.m_decorator.hide() def finished(): self.setAnimatedStopped() if self.isAnimatedBackward(): self.m_decorator.hide() self.m_animation.finished.connect(finished) def setExpandRect(self, _rect): self.m_expandRect = _rect self.m_decorator.setExpandRect(_rect) def setFillColor(self, _color): self.m_decorator.setFillColor(_color) def animationDuration(self): return self.m_animation.duration() def animateForward(self): self.expandIn() def expandIn(self): if self.isAnimated() and self.isAnimatedForward(): return self.setAnimatedForward() startExpandRect = self.m_expandRect finalExpandRect = self.widgetForFill().rect() self.m_decorator.resize(self.widgetForFill().size()) self.m_decorator.move(0, 0) self.m_decorator.grabExpandRect() self.m_decorator.show() self.m_decorator.raise_() if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.InOutQuart) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(startExpandRect) self.m_animation.setEndValue(finalExpandRect) self.m_animation.start() def animateBackward(self): self.expandOut() def expandOut(self): if self.isAnimated() and self.isAnimatedBackward(): return self.setAnimatedBackward() startExpandRect = self.widgetForFill().rect() finalExpandRect = self.m_expandRect self.m_decorator.resize(self.widgetForFill().size()) self.m_decorator.move(0, 0) self.m_decorator.hide() self.m_decorator.grabExpandRect() self.m_decorator.show() self.m_decorator.raise_() if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.InOutQuart) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(startExpandRect) self.m_animation.setEndValue(finalExpandRect) self.m_animation.start() def eventFilter(self, _object, _event): if _object == self.widgetForFill() and _event.type( ) == QEvent.Resize and self.m_decorator.isVisible(): self.m_decorator.resize(self.widgetForFill().size()) self.m_animation.setEndValue(self.widgetForFill().rect()) return QObject.eventFilter(self, _object, _event) def widgetForFill(self): return self.parent()
class MyForm(QDialog): def __init__(self): super(MyForm, self).__init__() self.ui = uic.loadUi('file_ui/Game.ui', self) self.ui.frame.setStyleSheet('background-image:url("image/Lane.png")') self.label_10.setPixmap(QPixmap('image/Super-car.png')) self.image = [ "image/Car-Blue.png", "image/Car-Green.png", "image/Car-Oranges.png", "image/Car-Pink.png" ] self.setWindowTitle('Car Rush') self.setWindowIcon(QIcon('image/Supercar.png')) self.ui.pushButton_4.setEnabled(True) self.ui.pushButton_3.setEnabled(False) self.ui.pushButton_5.setEnabled(False) self.ui.pushButton_3.clicked.connect(self.stop) self.ui.pushButton_4.clicked.connect(self.Go) self.ui.pushButton_5.clicked.connect(self.resume) # xoá dữ liệu file.txt # self.ctx = open('text1.txt', 'w') # self.ctx.write('') self.best_point = 0 self.bestscore() self.step = 30 self.x = 190 self.y = 400 self.settime2 = 6000 self.speed2 = 6000 self.settime = 5000 self.speed = 5000 self.xea = 0 self.lstR2 = [30, 190, 360] def bestscore(self): bp = open('text1.txt', 'r') for i in bp.readlines(): if int(i) > int(self.best_point): self.best_point = i self.ui.label_7.setText('BEST SCORE: ' + str(self.best_point)) def Go(self): self.p = vlc.MediaPlayer("music/Rush.mp3") self.p.play() self.p.audio_set_volume(50) print("Go") MF3 = MyForm3() MF3.show() self.resetpoint() self.setPos() # Xe1 self.timexe1 = QtCore.QTimer() self.timexe1.start(self.settime) self.timexe1.timeout.connect(self.create) # Xe2 self.timexe2 = QtCore.QTimer() self.timexe2.start(self.settime2) self.timexe2.timeout.connect(self.create2) # point self.time_point = QtCore.QTimer() self.time_point.start(self.speed) self.time_point.timeout.connect(self.calculatePoint) #point 2 self.time_point2 = QtCore.QTimer() self.time_point2.start(self.speed2) self.time_point2.timeout.connect(self.calculatePoint2) # check self.timeCheck = QtCore.QTimer() self.timeCheck.start(100) self.timeCheck.timeout.connect(self.check) # time Disable self.timeDisable = QtCore.QTimer() self.timeDisable.start(4000) self.timeDisable.timeout.connect(self.Enabled) def create(self): self.lstR2 = [30, 190, 360] image = random.choice(self.image) self.labelXe1.setPixmap(QPixmap(image)) self.xea = random.choice(self.lstR2) self.CN = QPropertyAnimation(self.ui.labelXe1, b"geometry") self.CN.setDuration(self.speed) self.CN.setStartValue(QRect(self.xea, -50, 100, 150)) self.CN.setEndValue(QRect(self.xea, 800, 100, 150)) self.CN.start() def create2(self): r2 = random.choice(self.lstR2) print(self.xea) if r2 != self.xea: print("run r2 " + str(r2)) self.labelXe2.setPixmap(QPixmap(random.choice(self.image))) self.CN2 = QPropertyAnimation(self.ui.labelXe2, b"geometry") self.CN2.setDuration(self.speed) self.CN2.setStartValue(QRect(r2, -50, 100, 150)) self.CN2.setEndValue(QRect(r2, 800, 100, 150)) self.CN2.start() else: self.lstR2.remove(self.xea) print(self.lstR2) r_again = random.choice(self.lstR2) print("run r_again " + str(r_again)) self.labelXe2.setPixmap(QPixmap(random.choice(self.image))) self.CN2 = QPropertyAnimation(self.ui.labelXe2, b"geometry") self.CN2.setDuration(self.speed) self.CN2.setStartValue(QRect(r_again, -50, 100, 150)) self.CN2.setEndValue(QRect(r_again, 800, 100, 150)) self.CN2.start() def Enabled(self): print("Enabled") self.ui.pushButton_3.setEnabled(True) self.ui.pushButton_4.setEnabled(False) self.ui.pushButton_5.setEnabled(True) self.timeDisable.stop() def Disable(self): print("Disable") self.ui.pushButton_3.setEnabled(False) self.ui.pushButton_4.setEnabled(False) self.ui.pushButton_5.setEnabled(False) def setPos(self): self.ui.labelXe1.setGeometry(180, -200, 100, 150) self.ui.label_10.setGeometry(190, 400, 100, 150) self.x = 190 self.y = 400 def check(self): self.a = None # tọa độ xe 1 w_car = self.ui.labelXe1.width() h_car = self.ui.labelXe1.height() X_car = self.ui.labelXe1.x() Y_car = self.ui.labelXe1.y() # tọa độ xe 2 w_car2 = self.ui.labelXe2.width() h_car2 = self.ui.labelXe2.height() X_car2 = self.ui.labelXe2.x() Y_car2 = self.ui.labelXe2.y() # chiều cao, rộng xe đỏ w_card = self.ui.label_10.width() h_card = self.ui.label_10.height() # print("Xe 1 ", X_car, Y_car, "|","Xe 2 ", X_car2, Y_car2,"|", self.x, self.y) if ((self.x <= (X_car + w_car) and self.x >= X_car) or (self.x + w_card >= X_car and self.x + w_card <= X_car + w_car)) \ and ((h_car + Y_car >= self.y) and (h_card + Y_car <= self.y + h_card) or ((Y_car >= self.y) and (Y_car <= self.y + w_card))): self.time_point.stop() self.time_point2.stop() self.timeCheck.stop() self.timexe1.stop() self.timexe2.stop() self.CN.stop() self.CN2.stop() self.p.stop() self.ui.labelXe1.move(-200, -200) self.ui.labelXe2.move(-200, -200) #Âm thanh đụng xe self.crash = vlc.MediaPlayer("music/BOOM.mp3") self.crash.play() self.crash = vlc.MediaPlayer("music/HEAVENLY.mp3") self.crash.play() self.Disable() print("Game Over") # append the point to text1.txt a = open('text1.txt', 'a') a.write(str(self.point)) a.write('\n') a.close() self.MF2 = MyForm2() self.MF2.ui.pushButton.clicked.connect(self.PlayAgain) # kiểm tra điểm a = open('text1.txt', 'r') for i in a.readlines(): if int(self.best_point) < int(i): self.best_point = i if int(self.best_point) <= int(self.point): self.MF2.ui.label.setText('BEST SCORE: ' + str(self.point)) else: self.MF2.ui.label.setText('SCORE: ' + str(self.point)) self.MF2.show() if ((self.x <= (X_car2 + w_car2) and self.x >= X_car2) or (self.x + w_card >= X_car2 and self.x + w_card <= X_car2 + w_car2)) \ and ((h_car2 + Y_car2 >= self.y) and (h_card + Y_car2 <= self.y + h_card) or ((Y_car2 >= self.y) and (Y_car2 <= self.y + w_card))): self.time_point.stop() self.time_point2.stop() self.timexe1.stop() self.timexe2.stop() self.timeCheck.stop() self.CN.stop() self.CN2.stop() self.p.stop() self.ui.labelXe1.move(-200, -200) self.ui.labelXe2.move(-200, -200) # Âm thanh đụng xe self.crash = vlc.MediaPlayer("music/BOOM.mp3") self.crash.play() self.crash = vlc.MediaPlayer("music/HEAVENLY.mp3") self.crash.play() self.Disable() print("Game Over") # append the point to text1.txt a = open('text1.txt', 'a') a.write(str(self.point)) a.write('\n') a.close() self.MF2 = MyForm2() self.MF2.ui.pushButton.clicked.connect(self.PlayAgain) # kiểm tra điểm a = open('text1.txt', 'r') for i in a.readlines(): if int(self.best_point) < int(i): self.best_point = i if int(self.best_point) <= int(self.point): self.MF2.ui.label.setText('BEST SCORE: ' + str(self.point)) else: self.MF2.ui.label.setText('SCORE: ' + str(self.point)) self.MF2.show() def PlayAgain(self): self.a = None self.crash.stop() self.settime2 = 7000 self.speed2 = 7000 self.settime = 5000 self.speed = 5000 self.Go() self.MF2.close() self.bestscore() def move(self): self.anim = QPropertyAnimation(self.ui.label_10, b"geometry") self.anim.setDuration(-10) self.anim.setStartValue(QRect(self.x, self.y, 100, 150)) self.anim.setEndValue(QRect(self.x, self.y, 100, 150)) self.anim.start() def stop(self): self.timexe1.stop() self.timexe2.stop() self.time_point.stop() self.time_point2.stop() self.timeCheck.stop() self.CN.pause() self.CN2.pause() self.ui.pushButton_3.setEnabled(False) self.step = 0 self.p.pause() print("Stop") def resume(self): self.timexe1.start() self.timexe2.start() self.time_point.start() self.time_point2.start() self.timeCheck.start() self.CN.resume() self.CN2.resume() self.ui.pushButton_3.setEnabled(True) self.ui.pushButton_5.setEnabled(True) self.step = 30 self.p.play() print("Resume") def calculatePoint(self): if self.ui.labelXe1.y() > 700: self.point += 10 self.ui.label.setText(str(self.point)) print("Calculate Point 1") if self.point == 20: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) print(self.point) if self.point == 50: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) if self.point == 70: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) if self.point == 100: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) if self.point == 150: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) if self.point == 200: self.settime = self.settime - 500 self.speed = self.speed - 500 print(self.settime, self.speed) def calculatePoint2(self): if self.ui.labelXe2.y() > 700: self.point += 10 print(self.point) self.ui.label.setText(str(self.point)) print("Calculate Point 2") if self.point == 30: self.settime2 = self.settime2 - 500 self.speed2 = self.speed2 - 500 print(self.settime2, self.speed2) print(self.point) if self.point == 60: self.settime2 = self.settime2 - 500 self.speed2 = self.speed2 - 500 print(self.settime2, self.speed2) if self.point == 80: self.settime2 = self.settime2 - 500 self.speed2 = self.speed2 - 500 print(self.settime2, self.speed2) if self.point == 110: self.settime2 = self.settime2 - 500 self.speed2 = self.speed2 - 500 print(self.settime2, self.speed2) if self.point == 160: self.settime2 = self.settime2 - 500 self.speed2 = self.speed2 - 500 print(self.settime2, self.speed2) if self.point == 210: self.settime2 = self.settime2 - 500 self.speed2 = self.speed2 - 500 print(self.settime2, self.speed2) def resetpoint(self): self.point = 0 self.ui.label.setText(str(0)) def keyPressEvent(self, e): if e.key() == QtCore.Qt.Key_W or e.key() == QtCore.Qt.Key_Up: if self.y > 250: self.y -= self.step self.x += 0 elif e.key() == QtCore.Qt.Key_S or e.key() == QtCore.Qt.Key_Down: if self.y < 420: self.y += self.step # self.x += 0 elif e.key() == QtCore.Qt.Key_A or e.key() == QtCore.Qt.Key_Right: if self.x > 10: self.y += 0 self.x -= self.step elif e.key() == QtCore.Qt.Key_D or e.key() == QtCore.Qt.Key_Left: if self.x < 350: self.y += 0 self.x += self.step self.move() def stopall(self): sys.exit()
class SlideAnimator(AbstractAnimator): m_direction:AnimationDirection m_isFixBackground:bool m_isFixStartSize:bool m_startMinSize = QSize() m_startMaxSize = QSize() m_startSize = QSize() m_decorator: SlideForegroundDecorator m_animation: QPropertyAnimation def __init__(self, _widgetForSlide): super(SlideAnimator, self).__init__(_widgetForSlide) self.m_direction = AnimationDirection.FromLeftToRight self.m_isFixBackground = True self.m_isFixStartSize = False self.m_decorator = SlideForegroundDecorator(_widgetForSlide) self.m_animation = QPropertyAnimation(self.m_decorator, b"maximumWidth") _widgetForSlide.installEventFilter(self) self.m_animation.setDuration(300) self.m_decorator.hide() def valueChanged(_value): if self.isWidth(): self.widgetForSlide().setMaximumWidth(_value) else: self.widgetForSlide().setMaximumHeight(_value) self.m_animation.valueChanged.connect(valueChanged) def finished(): self.setAnimatedStopped() self.m_decorator.hide() self.m_animation.finished.connect(finished) def setAnimationDirection(self, _direction): if self.m_direction != _direction: self.m_direction = _direction self.m_animation.setPropertyName(b"maximumWidth" if self.isWidth() else b"maximumHeight") def setFixBackground(self, _fix): if self.m_isFixBackground != _fix: self.m_isFixBackground = _fix def setFixStartSize(self, _fix): if self.m_isFixStartSize != _fix: self.m_isFixStartSize = _fix def animationDuration(self): return self.m_animation.duration() def animateForward(self): self.slideIn() def slideIn(self): if not self.m_startMinSize.isValid(): self.m_startMinSize = self.widgetForSlide().minimumSize() if not self.m_startMaxSize.isValid(): self.m_startMaxSize = self.widgetForSlide().maximumSize() if not self.m_startSize.isValid(): self.m_startSize = self.widgetForSlide().sizeHint() if self.isAnimated() and self.isAnimatedForward(): return self.setAnimatedForward() if self.isWidth(): self.widgetForSlide().setMaximumWidth(0) else: self.widgetForSlide().setMaximumHeight(0) self.widgetForSlide().show() currentSize = self.widgetForSlide().size() finalSize = QSize(currentSize.width(), currentSize.height()) self.fixSize(self.m_startSize, finalSize) self.widgetForSlide().hide() self.fixSizeOfWidgetForSlide(finalSize) self.m_decorator.grabParent(finalSize) self.fixSizeOfWidgetForSlide(currentSize) self.widgetForSlide().show() if self.m_isFixBackground: self.m_decorator.move(0, 0) self.m_decorator.show() self.m_decorator.raise_() if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.OutQuart) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(self.widgetForSlide().width() if self.isWidth() else self.widgetForSlide().height()) self.m_animation.setEndValue(finalSize.width() if self.isWidth() else finalSize.height()) self.m_animation.start() def animateBackward(self): self.slideOut() def slideOut(self): if not self.m_startMinSize.isValid(): self.m_startMinSize = self.widgetForSlide().minimumSize() if not self.m_startMaxSize.isValid(): self.m_startMaxSize = self.widgetForSlide().maximumSize() if not self.m_startSize.isValid() or not self.m_isFixStartSize: self.m_startSize = self.widgetForSlide().size() if self.isAnimated() and self.isAnimatedBackward(): return self.setAnimatedBackward() finalSize = self.widgetForSlide().size() if self.isWidth(): finalSize.setWidth(0) else: finalSize.setHeight(0) self.m_decorator.grabParent(self.widgetForSlide().size()) if self.m_isFixBackground: self.m_decorator.move(0, 0) self.m_decorator.show() self.m_decorator.raise_() if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.InQuart) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(self.widgetForSlide().width() if self.isWidth() else self.widgetForSlide().height()) self.m_animation.setEndValue(finalSize.width() if self.isWidth() else finalSize.height()) self.m_animation.start() def eventFilter(self, _object, _event): if _object == self.widgetForSlide() and _event.type() == QEvent.Resize and self.m_decorator.isVisible(): if self.m_direction == AnimationDirection.FromLeftToRight: self.m_decorator.move(self.widgetForSlide().width() - self.m_decorator.width(), 0) if self.m_direction == AnimationDirection.FromTopToBottom: self.m_decorator.move(0, self.widgetForSlide().height() - self.m_decorator.height()) return QObject.eventFilter(self, _object, _event) def isWidth(self): return self.m_direction == AnimationDirection.FromLeftToRight or self.m_direction == AnimationDirection.FromRightToLeft def fixSize(self, _sourceSize, _targetSize): if self.isWidth(): _targetSize.setWidth(_sourceSize.width()) else: _targetSize.setHeight(_sourceSize.height()) def fixSizeOfWidgetForSlide(self, _sourceSize): if self.isWidth(): self.widgetForSlide().setFixedWidth(_sourceSize.width()) else: self.widgetForSlide().setFixedHeight(_sourceSize.height()) def widgetForSlide(self): return self.parent()
class SideSlideAnimator(AbstractAnimator): m_side: ApplicationSide m_decorateBackground: bool m_decorator: SideSlideDecorator m_animation: QPropertyAnimation def __init__(self, _widgetForSlide): super(SideSlideAnimator, self).__init__(_widgetForSlide) self.m_side = ApplicationSide() self.m_decorateBackground = True self.m_decorator = SideSlideDecorator(_widgetForSlide.parentWidget()) self.m_animation = QPropertyAnimation(self.m_decorator, b"_slidePos") _widgetForSlide.parentWidget().installEventFilter(self) self.m_animation.setEasingCurve(QEasingCurve.InQuad) self.m_animation.setDuration(260) self.m_decorator.hide() def finished(): self.setAnimatedStopped() if self.isAnimatedForward(): self.widgetForSlide().move(self.m_decorator.slidePos()) else: self.m_decorator.hide() self.m_animation.finished.connect(finished) self.m_decorator.clicked.connect(self.slideOut) def setApplicationSide(self, _side): if self.m_side != _side: self.m_side = _side def setDecorateBackground(self, _decorate): if self.m_decorateBackground != _decorate: self.m_decorateBackground = _decorate def animationDuration(self): return self.m_animation.duration() def animateForward(self): self.slideIn() def slideIn(self): if self.isAnimated() and self.isAnimatedForward(): return self.setAnimatedForward() self.widgetForSlide().lower() self.widgetForSlide().move(-self.widgetForSlide().width(), -self.widgetForSlide().height()) self.widgetForSlide().show() self.widgetForSlide().resize(self.widgetForSlide().sizeHint()) _topWidget = self.widgetForSlide() topWidget = self.widgetForSlide() while _topWidget: topWidget = _topWidget _topWidget = topWidget.parentWidget() finalSize = self.widgetForSlide().size() startPosition = QPoint() finalPosition = QPoint() if self.m_side == ApplicationSide.LeftSide: finalSize.setHeight(topWidget.height()) startPosition = QPoint(-finalSize.width(), 0) finalPosition = QPoint(0, 0) if self.m_side == ApplicationSide.TopSide: finalSize.setWidth(topWidget.width()) startPosition = QPoint(0, -finalSize.height()) finalPosition = QPoint(0, 0) if self.m_side == ApplicationSide.RightSide: finalSize.setHeight(topWidget.height()) startPosition = QPoint(topWidget.width(), 0) finalPosition = QPoint(topWidget.width() - finalSize.width(), 0) if self.m_side == ApplicationSide.BottomSide: finalSize.setWidth(topWidget.width()) startPosition = QPoint(0, topWidget.height()) finalPosition = QPoint(0, topWidget.height() - finalSize.height()) if self.m_decorateBackground: self.m_decorator.setParent(topWidget) self.m_decorator.move(0, 0) self.m_decorator.grabParent() self.m_decorator.show() self.m_decorator.raise_() self.widgetForSlide().move(startPosition) self.widgetForSlide().resize(finalSize) self.widgetForSlide().raise_() self.m_decorator.grabSlideWidget(self.widgetForSlide()) if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.OutQuart) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(startPosition) self.m_animation.setEndValue(finalPosition) self.m_animation.start() if self.m_decorateBackground: DARKER = True self.m_decorator.decorate(DARKER) def animateBackward(self): self.slideOut() def slideOut(self): if self.isAnimated() and self.isAnimatedBackward(): return self.setAnimatedBackward() if self.widgetForSlide().isVisible(): _topWidget = self.widgetForSlide() topWidget = self.widgetForSlide() while _topWidget: topWidget = _topWidget _topWidget = topWidget.parentWidget() self.widgetForSlide().hide() finalSize = self.widgetForSlide().size() startPosition = self.widgetForSlide().pos() finalPosition = QPoint() if self.m_side == ApplicationSide.LeftSide: startPosition = QPoint(0, 0) finalPosition = QPoint(-finalSize.width(), 0) if self.m_side == ApplicationSide.TopSide: startPosition = QPoint(0, 0) finalPosition = QPoint(0, -finalSize.height()) if self.m_side == ApplicationSide.RightSide: startPosition = QPoint(topWidget.width() - finalSize.width(), 0) finalPosition = QPoint(topWidget.width(), 0) if self.m_side == ApplicationSide.BottomSide: startPosition = QPoint(0, topWidget.height() - finalSize.height()) finalPosition = QPoint(0, topWidget.height()) if self.m_animation.state() == QPropertyAnimation.Running: self.m_animation.pause() self.m_animation.setDirection(QPropertyAnimation.Backward) self.m_animation.resume() else: self.m_animation.setEasingCurve(QEasingCurve.InQuart) self.m_animation.setDirection(QPropertyAnimation.Forward) self.m_animation.setStartValue(startPosition) self.m_animation.setEndValue(finalPosition) self.m_animation.start() if self.m_decorateBackground: LIGHTER = False self.m_decorator.decorate(LIGHTER) def eventFilter(self, _object, _event): if _object == self.widgetForSlide().parentWidget() and _event.type( ) == QEvent.Resize: widgetForSlideParent = self.widgetForSlide().parentWidget() if self.m_side == ApplicationSide.RightSide: self.widgetForSlide().move( widgetForSlideParent.width() - self.widgetForSlide().width(), 0) if self.m_side == ApplicationSide.LeftSide: self.widgetForSlide().resize(self.widgetForSlide().width(), widgetForSlideParent.height()) if self.m_side == ApplicationSide.BottomSide: self.widgetForSlide().move( 0, widgetForSlideParent.height() - self.widgetForSlide().height()) if self.m_side == ApplicationSide.TopSide: self.widgetForSlide().resize(widgetForSlideParent.width(), self.widgetForSlide().height()) self.m_decorator.grabSlideWidget(self.widgetForSlide()) return QObject.eventFilter(self, _object, _event) def widgetForSlide(self): return self.parent()