一个简单的qt动画(关键点 animation.setParent(self))

qt动画卡了有段时间,这中间主要要animation.setParent(self)才能正常运行

import sys
from PyQt5.QtCore import Qt, QTimer, QPropertyAnimation, QRect,QEasingCurve
from PyQt5.QtGui import QFont, QPainter
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel

class GameWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        self.score = 0
        self.score_label = QLabel(self)
        self.score_label.setFont(QFont('Arial', 20))
        self.score_label.setStyleSheet('color: red')
        self.score_label.move(10, 10)
        self.score_label.setFixedSize(300,100)
        self.update_score()
        
        self.score_effect_label = QLabel(self)
        self.score_effect_label.setFont(QFont('Arial', 30))
        self.score_effect_label.setStyleSheet('color: yellow')
        # self.score_effect_label.setAlignment(Qt.AlignCenter)
        self.score_effect_label.hide()
        self.score_effect_timer = QTimer()
        self.score_effect_timer.timeout.connect(self.hide_score_effect)
        
    def initUI(self):
        self.setGeometry(100, 100, 1300, 1200)
        self.setWindowTitle('Game')
        
    def update_score(self):
        self.score_label.setText(f'Score: {self.score}')
        
    def show_score_effect(self, score_effect):
        self.score_effect_label.setText(score_effect)
        self.score_effect_label.setGeometry(0, self.height() - 100, self.width(), 100)
        self.score_effect_label.show()
        
        animation = QPropertyAnimation(self.score_effect_label, b"geometry")
        animation.setParent(self)
        animation.setDuration(2000)  # 动画持续时间(单位:毫秒)
        animation.setStartValue(QRect(0, self.height() - 100, self.width(), 100))
        animation.setEndValue(QRect(0, self.height() - 500, self.width(), 100))
        animation.setEasingCurve(QEasingCurve.Linear)  # 动画速度曲线
        animation.start()
        
        self.score_effect_timer.start(2000)  # Score effect duration
    def hide_score_effect(self):
        self.score_effect_label.hide()
        self.score_effect_timer.stop()
        
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Space:
            self.score += 10
            self.update_score()
            self.show_score_effect('+10')
            
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = GameWindow()
    window.show()
    sys.exit(app.exec_())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值