def placeFood(self, painter: QPainter): if self.foodPlaced is False: self.foodX = randrange(24) * 12 self.foodY = randrange(2, 24) * 12 if not [self.foodX, self.foodY] in self.snakeArray: self.foodPlaced = True painter.setBrush(QColor("#ffdd55")) painter.drawRect(self.foodX, self.foodY, 12, 12)
def icon_from_resources(self, path: Union[str, ColoredIcon]) -> QIcon: "Fetch icon from Qt resources, and invert if in night mode." if self.night_mode: cache = self._icon_cache_light else: cache = self._icon_cache_dark if isinstance(path, str): key = path else: key = f"{path.path}-{path.color}" icon = cache.get(key) if icon: return icon if isinstance(path, str): # default black/white icon = QIcon(path) if self.night_mode: img = icon.pixmap(self._icon_size, self._icon_size).toImage() img.invertPixels() icon = QIcon(QPixmap(img)) else: # specified colours icon = QIcon(path.path) pixmap = icon.pixmap(16) painter = QPainter(pixmap) painter.setCompositionMode(QPainter.CompositionMode_SourceIn) painter.fillRect(pixmap.rect(), QColor(path.current_color(self.night_mode))) painter.end() icon = QIcon(pixmap) return icon return cache.setdefault(path, icon)
def eventFilter(self, obj, evt): if not (evt.type() == QEvent.Paint and self.save_png): return super().eventFilter(obj, evt) filename, oldsize = self.save_png self.save_png = () size = self._page.contentsSize().toSize() image = QImage(size, QImage.Format_ARGB32) painter = QPainter(image) self.render(painter) painter.end() success = image.save(filename, "png") self.resize(oldsize) mw.progress.finish() if success: showInfo("Image saved to %s!" % os.path.abspath(filename)) else: showCritical("Failed to save the image.") return super().eventFilter(obj, evt)
def eventFilter(self, obj, evt): if not(evt.type() == QEvent.Paint and self.save_png): return super().eventFilter(obj, evt) filename, oldsize = self.save_png self.save_png = () size = self._page.contentsSize().toSize() image = QImage(size, QImage.Format_ARGB32) painter = QPainter(image) self.render(painter) painter.end() success = image.save(filename, "png") self.resize(oldsize) mw.progress.finish() if success: showInfo("Image saved to %s!" % os.path.abspath(filename)) else: showCritical("Failed to save the image.") return super().eventFilter(obj, evt)
def paintEvent(self, event: QPaintEvent): qp = QPainter() qp.begin(self) self.scoreBoard(qp) self.placeFood(qp) self.drawSnake(qp) self.scoreText(event, qp) if self.isOver: self.gameOver(event, qp) qp.end()
def gameOver(self, event: QPaintEvent, painter: QPainter): info = "" if self.score > self.highscore: self.lives += 1 self.highscore = self.score info = "\n\nNew high score! 1 life replenished." font_size = 10 if not is_mac else 12 painter.setPen(QColor(0, 34, 3)) painter.setFont(QFont("Decorative", font_size)) if self.lives > 0: msg = "GAME OVER{}\n\nPress space to play again".format(info) else: self.setCursor(Qt.CursorShape.PointingHandCursor) msg = ("GAME OVER\n\nYou're out of lives for today,\n" "but tomorrow is another day :)\n\n" "Tip: Get more lives by\nkeeping up with your reviews!\n\n" "Pro-Tip: Pledge your support on Patreon\n" "and get access to other secret\n" "features and add-ons :)" "\n\nClick here to go to\n" "patreon.com/glutanimate") painter.drawText(event.rect(), Qt.AlignmentFlag.AlignCenter, msg)
def drawSnake(self, painter: QPainter): painter.setPen(Qt.PenStyle.NoPen) painter.setBrush(QColor("#ffffff")) for i in self.snakeArray: painter.drawRect(i[0], i[1], 12, 12)
def scoreText(self, event: QPaintEvent, painter: QPainter): painter.setPen(QColor("#ffffff")) painter.setFont(QFont("Decorative", 10)) painter.drawText(4, 17, "LIVES: " + str(self.lives)) painter.drawText(120, 17, "SCORE: " + str(self.score)) painter.drawText(230, 17, "BEST: " + str(self.highscore))
def scoreBoard(self, painter: QPainter): painter.setPen(Qt.PenStyle.NoPen) painter.setBrush(QColor("#3e7a78")) painter.drawRect(0, 0, 300, 24)