【问题标题】:PyQT4 and tiles animationPyQT4 和瓷砖动画
【发布时间】:2012-12-25 03:08:20
【问题描述】:

我刚刚开始学习 PyQT4。

如何定义一个类,其实例将是动画图像?我有几个 png 文件,所以我希望该对象在一个循环中在某个时刻渲染一个这样的文件,而在另一个时刻渲染另一个文件。我应该使用 QMovie,还是 Qt Animation Framework,还是什么?谢谢!

【问题讨论】:

    标签: python animation pyqt4 tiles


    【解决方案1】:

    这个piece of code 应该可以帮助你。你应该使用QMovie

    【讨论】:

    • 谢谢!实际上,我不想使用 gif。无论如何,我会更深入地了解 QMovie。
    【解决方案2】:

    实际上,我在http://python.su/forum/topic/15679/?page=1#post-94136找到了更适合我的东西

    # -*- coding: utf-8 -*-
    import sys
    import time
    
    from PyQt4 import QtGui, QtCore
    
    
    class SpriteAnimation(object):
        def __init__(self, image_path, sprite_width, sprite_height, label):
            pixmap = QtGui.QPixmap(image_path)
    
            width, height = pixmap.width(), pixmap.height()
            self.pixmaps = []
            for x in range(0, width, sprite_width):
                for y in range(0, height, sprite_height):
                    self.pixmaps.append(pixmap.copy(x, y, 
                                                    sprite_width, sprite_height))
            self._current_frame = 0
            self.label = label
    
        def play(self, interval=100):
            self._timer = QtCore.QTimer(interval=interval,
                                        timeout=self._animation_step)
            self._timer.start()
        def _animation_step(self):
            self.label.setPixmap(self.pixmaps[self._current_frame])
            self.label.update()
            self._current_frame += 1
            if self._current_frame >= len(self.pixmaps):
                self._current_frame = 0
    
    
    class Window(QtGui.QWidget):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            self.resize(100, 100)
            layout = QtGui.QVBoxLayout(self)
            label = QtGui.QLabel()
            layout.addWidget(label)
            # http://content.makeyourflashgame.com/pbe/tutorials/star-green.png
            self.animation = SpriteAnimation('star-green.png', 80, 80, label)
            self.animation.play()
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(app.exec_())
    

    我以以下方式使用了这个想法:

    class PowerUp(QtGui.QGraphicsRectItem):
        def __init__(self):
            QtGui.QGraphicsRectItem.__init__(self)
            self.images = ['data/images/objects/bonus_block/full-0.png',
                           'data/images/objects/bonus_block/full-1.png',
                           'data/images/objects/bonus_block/full-2.png',
                           'data/images/objects/bonus_block/full-3.png',
                           'data/images/objects/bonus_block/full-4.png']
            self.image = self.images[0]
            self.current = 0
            self.position()
            self.timer = QtCore.QTimer()
            self.timer.timeout.connect(self.animation_step)
            self.timer.start(175)
            #random.choice([slide(), ghost()])
    
        def position(self):
            self.pos_x = random.randint(-300, 300)
            self.pos_y = random.randint(-200, 200)
    
        def boundingRect(self):
            return QtCore.QRectF(0, 0, 32, 32)
    
        def paint(self, painter, option, widget):
            painter.setBrush(QtGui.QBrush(self.image))
            painter.setPen(QtGui.QPen(QtCore.Qt.NoPen))
            painter.drawRect(0, 0, 32, 32)
            self.setPos(self.pos_x, self.pos_y)
    
        def animation_step(self):
            self.image = QtGui.QPixmap(self.images[self.current])
            self.current += 1
            if self.current == len(self.images):
                self.current = 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多