spritesheets in pyqt

Solutions on MaxInterview for spritesheets in pyqt by the best coders in the world

showing results for - "spritesheets in pyqt"
Angela
25 Jan 2019
1class PowerUp(QtGui.QGraphicsRectItem):
2    def __init__(self):
3        QtGui.QGraphicsRectItem.__init__(self)
4        self.images = ['data/images/objects/bonus_block/full-0.png',
5                       'data/images/objects/bonus_block/full-1.png',
6                       'data/images/objects/bonus_block/full-2.png',
7                       'data/images/objects/bonus_block/full-3.png',
8                       'data/images/objects/bonus_block/full-4.png']
9        self.image = self.images[0]
10        self.current = 0
11        self.position()
12        self.timer = QtCore.QTimer()
13        self.timer.timeout.connect(self.animation_step)
14        self.timer.start(175)
15        #random.choice([slide(), ghost()])
16
17    def position(self):
18        self.pos_x = random.randint(-300, 300)
19        self.pos_y = random.randint(-200, 200)
20
21    def boundingRect(self):
22        return QtCore.QRectF(0, 0, 32, 32)
23
24    def paint(self, painter, option, widget):
25        painter.setBrush(QtGui.QBrush(self.image))
26        painter.setPen(QtGui.QPen(QtCore.Qt.NoPen))
27        painter.drawRect(0, 0, 32, 32)
28        self.setPos(self.pos_x, self.pos_y)
29
30    def animation_step(self):
31        self.image = QtGui.QPixmap(self.images[self.current])
32        self.current += 1
33        if self.current == len(self.images):
34            self.current = 0
Elias
22 Jan 2019
1# -*- coding: utf-8 -*-
2import sys
3import time
4
5from PyQt4 import QtGui, QtCore
6
7
8class SpriteAnimation(object):
9    def __init__(self, image_path, sprite_width, sprite_height, label):
10        pixmap = QtGui.QPixmap(image_path)
11
12        width, height = pixmap.width(), pixmap.height()
13        self.pixmaps = []
14        for x in range(0, width, sprite_width):
15            for y in range(0, height, sprite_height):
16                self.pixmaps.append(pixmap.copy(x, y, 
17                                                sprite_width, sprite_height))
18        self._current_frame = 0
19        self.label = label
20
21    def play(self, interval=100):
22        self._timer = QtCore.QTimer(interval=interval,
23                                    timeout=self._animation_step)
24        self._timer.start()
25    def _animation_step(self):
26        self.label.setPixmap(self.pixmaps[self._current_frame])
27        self.label.update()
28        self._current_frame += 1
29        if self._current_frame >= len(self.pixmaps):
30            self._current_frame = 0
31
32
33class Window(QtGui.QWidget):
34    def __init__(self, parent=None):
35        QtGui.QWidget.__init__(self, parent)
36        self.resize(100, 100)
37        layout = QtGui.QVBoxLayout(self)
38        label = QtGui.QLabel()
39        layout.addWidget(label)
40        # http://content.makeyourflashgame.com/pbe/tutorials/star-green.png
41        self.animation = SpriteAnimation('star-green.png', 80, 80, label)
42        self.animation.play()
43
44
45if __name__ == '__main__':
46    app = QtGui.QApplication(sys.argv)
47    window = Window()
48    window.show()
49    sys.exit(app.exec_())
similar questions
queries leading to this page
spritesheets in pyqt