def setTiles(self): #background brush = QBrush( ) #QBrush(画刷)是一个基本的图形对象,用于填充如矩形,椭圆形,多边形等形状,QBrush有三种类型,预定义,过渡,和纹理图案 pix = QPixmap(os.getcwd() + "/robotImages/tile.png") brush.setTexture(pix) brush.setStyle(24) self.setBackgroundBrush(brush) #wall:left、right、top、bottom都是QGraphicsRectItem类型 #left left = QGraphicsRectItem() pix = QPixmap(os.getcwd() + "/robotImages/tileVert.png") #获取贴图 left.setRect(QRectF(0, 0, pix.width(), self.height)) #尺寸:宽是图片宽度 ,高度是战场高度 brush.setTexture(pix) #设置贴图函数 brush.setStyle( 24 ) #参数24指平铺格式(参数为枚举类型)详情见 https://doc.qt.io/qt-5/qt.html#BrushStyle-enum left.setBrush(brush) left.name = 'left' self.addItem(left) #right right = QGraphicsRectItem() right.setRect(self.width - pix.width(), 0, pix.width(), self.height) #尺寸:宽是图片的宽度、高是战场的高 right.setBrush(brush) right.name = 'right' self.addItem(right) #top top = QGraphicsRectItem() pix = QPixmap(os.getcwd() + "/robotImages/tileHori.png") top.setRect(QRectF(0, 0, self.width, pix.height())) brush.setTexture(pix) brush.setStyle(24) top.setBrush(brush) top.name = 'top' self.addItem(top) #bottom bottom = QGraphicsRectItem() bottom.setRect(0, self.height - pix.height(), self.width, pix.height()) bottom.setBrush(brush) bottom.name = 'bottom' self.addItem(bottom)