Exemple #1
0
class GrafScene(QGraphicsScene):
    def __init__(self, parent):
        super().__init__(parent)
        self.view = QGraphicsView(self)
        self.image = QImage()

    def initUI(self):
        self.setItemIndexMethod(QGraphicsScene.BspTreeIndex)
        self.setBackgroundBrush(QColor(255, 0, 0))
        # pokemon = QGraphicsPixmapItem(QPixmap("assets/gengar.png"))
        bgimg = QPixmap("assets/whosthatpokemon.jpg")
        # print(bgimg.width(), bgimg.height())
        self.addPixmap(QPixmap("assets/whosthatpokemon.jpg"))

        # img = image.load("assets/gengar.png")
        self.pokemon = QGraphicsPixmapItem()
        self.setNewPokemon("Gengar")

        self.answer = QGraphicsPixmapItem()

        # pokemon.pixmap(
        self.addItem(self.pokemon)
        self.addParticles(150)
        self.view.setFixedSize(761, 431)
        self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        # self.scene.setSceneRect(5, 5, self.width()-10, 300)
        self.view.setBackgroundBrush(QColor(
            0, 255, 0))  #QImage("assets/whosthatpokemon.png"))
        # self.view.setCacheMode(QGraphicsView.CacheBackground)
        self.view.setRenderHint(QPainter.Antialiasing)
        # self.scene.views

    def addParticles(self, count: int):
        gen = QRandomGenerator()
        for i in range(count):
            # print(random(self.scene.width()))
            part = Particle(QPointF(gen.bounded(self.width()),\
                gen.bounded(self.height())))
            part.setVisible(False)
            self.addItem(part)

    def setNewPokemon(self, name):
        self.image.load("assets/" + name.lower() + ".png")
        for i in range(self.image.width()):
            for j in range(self.image.height()):
                color = self.image.pixelColor(i, j)
                if color.alpha() != 0:
                    color.setAlpha(5)
                    self.image.setPixel(i, j, color.rgba())
        self.pokemon.setPixmap(QPixmap().fromImage(self.image))
        pokepix = self.pokemon.pixmap()
        self.pokemon.setPos(220 - self.image.width() / 2,
                            220 - self.image.height() / 2)

    def setAnswer(self, name):
        self.image.load("assets/" + name.lower() + ".png")
        self.pokemon.setPixmap(QPixmap().fromImage(self.image))
        pokepix = self.pokemon.pixmap()
        self.pokemon.setPos(220 - self.image.width() / 2,
                            220 - self.image.height() / 2)

    def upAlpha(self, particle: QGraphicsItem):
        if self.pokemon.collidesWithItem(particle):
            # particle.setVisible(False)
            pos = QPointF(
                particle.pos().x() - 220 + self.image.width() / 2,
                particle.pos().y() - 220 + self.image.height() /
                2)  # self.pokemon.mapToItem(self.pokemon, particle.pos())
            border = 2
            if pos.x() > border and pos.x() < self.image.width()-border and\
                pos.y() > border and pos.y() < self.image.height()-border:
                for i in range(-border, border):
                    for j in range(-border, border):
                        color = self.image.pixelColor(pos.x() + i, pos.y() + j)
                        if color.alpha() > 0 and color.alpha() < 245:
                            color.setAlpha(color.alpha() + 10)
                            self.image.setPixel(pos.x() + i,
                                                pos.y() + j, color.rgba())
                            self.pokemon.setPixmap(QPixmap().fromImage(
                                self.image))
                            self.pokemon.show()