在有限区域内移动矩形

时间:2019-07-28 14:36:53

标签: python pyqt5

graphicsscene上有一个roundrect,我正在尝试移动,但是当它在屏幕任一侧的“末端”时,它会移出屏幕,但它不应移出屏幕外,它应该像被召唤的游戏一样移动“蛇仙子”

from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QGraphicsView, QGraphicsScene, QGraphicsItem
from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import QBrush, QColor
import sys


class GraphicsItem(QGraphicsItem):
    def __init__(self, parent):
        super().__init__()
        self.setFlag(QGraphicsItem.ItemIsFocusable)
        self.setFocus()
        self.screenSize = QDesktopWidget().screenGeometry(0)
        self.screenHeight = self.screenSize.height()
        self.screenWidth = self.screenSize.width()

    def boundingRect(self):
        return QRectF(0, 0, self.screenWidth, self.screenHeight)

    def paint(self, painter, option, widget):
        painter.setBrush(QBrush(Qt.magenta))
        painter.drawRoundedRect(10, 10, 70, 70, 5, 5)
        # painter.drawEllipse(10, 100, 70, 70)

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Right:
            self.moveBy(10, 0)
        elif event.key() == Qt.Key_Left:
            self.moveBy(-10, 0)
        elif event.key() == Qt.Key_Down:
            self.moveBy(0, 10)
        elif event.key() == Qt.Key_Up:
            self.moveBy(0, -10)
        self.update()



class GraphicsScene(QGraphicsScene):
    def __init__(self, parent):
        super().__init__(parent=parent)
        graphicsitem = GraphicsItem(self)
        self.addItem(graphicsitem)
        self.setBackgroundBrush(QColor(10, 155, 79))
        self.screenSize = QDesktopWidget().screenGeometry(0)
        self.screenHeight = self.screenSize.height()
        self.screenWidth = self.screenSize.width()


class GraphicsView(QGraphicsView):
    def __init__(self, parent):
        super().__init__(parent=parent)
        graphicsscene = GraphicsScene(self)
        self.screenSize = QDesktopWidget().screenGeometry(0)
        self.screenHeight = self.screenSize.height()
        self.screenWidth = self.screenSize.width()
        self.setGeometry(0, 0, self.screenWidth, self.screenHeight)
        self.setScene(graphicsscene)
        self.show()


class Widget(QWidget):
    def __init__(self):
        super().__init__()
        graphicsview = GraphicsView(self)
        self.screenSize = QDesktopWidget().screenGeometry(0)
        self.screenHeight = self.screenSize.height()
        self.screenWidth = self.screenSize.width()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("Graphics View")
        self.setGeometry(0, 0, self.screenWidth, self.screenHeight)
        self.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = Widget()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

尝试一下:


      octalVal = "57";
      String bin = Integer.toBinaryString(Integer.valueOf(octalVal));
      String paddedBin =
            "00000000000000000000000000000".substring(0, 32 - bin.length())
                  + bin;
      System.out.println(paddedBin);