defdrawSquare(self, painter, x, y, shape):
colorTable =[0x000000,0xCC6666,0x66CC66,0x6666CC,0xCCCC66,0xCC66CC,0x66CCCC,0xDAAA00]
color = QColor(colorTable[shape])
painter.fillRect(x +1, y +1, self.squareWidth()-2,
self.squareHeight()-2, color)
painter.setPen(color.lighter())
painter.drawLine(x, y + self.squareHeight()-1, x, y)
painter.drawLine(x, y, x + self.squareWidth()-1, y)
painter.setPen(color.darker())
painter.drawLine(x +1, y + self.squareHeight()-1,
x + self.squareWidth()-1, y + self.squareHeight()-1)
painter.drawLine(x + self.squareWidth()-1,
y + self.squareHeight()-1, x + self.squareWidth()-1, y +1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
效果图如下
➢
按键事件
defkeyPressEvent(self, event):ifnot self.isStarted or self.curPiece.shape()== ShapeForm.NoShape:super(MainBoard, self).keyPressEvent(event)return
key = event.key()if key == Qt.Key_P:
self.pause()returnif self.isPaused:returnelif key == Qt.Key_Left:
self.tryMove(self.curPiece, self.curX -1, self.curY)elif key == Qt.Key_Right:
self.tryMove(self.curPiece, self.curX +1, self.curY)elif key == Qt.Key_Down:
self.tryMove(self.curPiece.rotateRight(), self.curX,
self.curY)elif key == Qt.Key_Up:
self.tryMove(self.curPiece.rotateLeft(), self.curX, self.curY)elif key == Qt.Key_Space:
self.dropDown()elif key == Qt.Key_D:
self.oneLineDown()else:super(MainBoard, self).keyPressEvent(event)deftryMove(self, newPiece, newX, newY):for i inrange(4):
x = newX + newPiece.x(i)
y = newY - newPiece.y(i)if x <0or x >= MainBoard.BoardWidth or y <0or y >= MainBoard.BoardHeight:returnFalseif self.shapeAt(x, y)!= ShapeForm.NoShape:returnFalse
self.curPiece = newPiece
self.curX = newX
self.curY = newY
self.update()returnTrue