void TetrisBoard::paintEvent(QPaintEvent *event) { QFrame::paintEvent(event); QPainter painter(this); QRect rect = contentsRect(); int boardTop = rect.bottom() - BoardHeight*squareHeight(); //draw the board const BoardModel& board = gameModel.getBoard(); for (int i = 0; i < BoardHeight; ++i) { for (int j = 0; j < BoardWidth; ++j) { const TetronimoShape &shape = board.getShapeAt(j, BoardHeight - i - 1); if (shape != NoShape) drawSquare(painter, rect.left() + j * squareWidth(), boardTop + i * squareHeight(), shape); } } //draw the tetronimo on top of the board const Tetronimo &curPiece = gameModel.getCurrentPiece(); const int curX = tetris.getCurrentPieceX(); const int curY = tetris.getCurrentPieceY(); if (curPiece.shape() != NoShape) { for (int i = 0; i < 4; ++i) { int x = curX + curPiece.x(i); int y = curY - curPiece.y(i); drawSquare(painter, rect.left() + x * squareWidth(), boardTop + (BoardHeight - y - 1) * squareHeight(), curPiece.shape()); } } }
void TetrisBoard::drawSquare(QPainter &painter, int x, int y, TetronimoShape shape) { static const QRgb colorTable[8] = { 0x000000, 0xCC6666, 0x66CC66, 0x6666CC, 0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00 }; QColor color = colorTable[int(shape)]; painter.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2, color); painter.setPen(color.light()); painter.drawLine(x, y + squareHeight() - 1, x, y); painter.drawLine(x, y, x + squareWidth() - 1, y); painter.setPen(color.dark()); painter.drawLine(x + 1, y + squareHeight() - 1, x + squareWidth() - 1, y + squareHeight() - 1); painter.drawLine(x + squareWidth() - 1, y + squareHeight() - 1, x + squareWidth() - 1, y + 1); }
void TetrisBoard::showNextPiece() { if (!nextPieceLabel) return; const Tetronimo& nextPiece = gameModel.getNextPiece(); int dx = nextPiece.maxX() - nextPiece.minX() + 1; int dy = nextPiece.maxY() - nextPiece.minY() + 1; QPixmap pixmap(dx * squareWidth(), dy * squareHeight()); QPainter painter(&pixmap); painter.fillRect(pixmap.rect(), nextPieceLabel->palette().background()); for (int i = 0; i < 4; ++i) { int x = nextPiece.x(i) - nextPiece.minX(); int y = nextPiece.y(i) - nextPiece.minY(); drawSquare(painter, x * squareWidth(), y * squareHeight(), nextPiece.shape()); } nextPieceLabel->setPixmap(pixmap); }
//! [32] void QrfeKeyBoard::showNextPiece() { if (!nextPieceLabel) return; int dx = nextPiece.maxX() - nextPiece.minX() + 1; int dy = nextPiece.maxY() - nextPiece.minY() + 1; QPixmap pixmap(dx * squareWidth(), dy * squareHeight()); QPainter painter(&pixmap); painter.fillRect(pixmap.rect(), nextPieceLabel->palette().background()); for (int i = 0; i < 4; ++i) { int x = nextPiece.x(i) - nextPiece.minX(); int y = nextPiece.y(i) - nextPiece.minY(); drawSquare(painter, x * squareWidth(), y * squareHeight(), nextPiece.shape()); } nextPieceLabel->setPixmap(pixmap); //! [32] //! [33] }
//! [7] void TetrixBoard::paintEvent(QPaintEvent *event) { QFrame::paintEvent(event); QPainter painter(this); QRect rect = contentsRect(); //! [7] if (isPaused) { painter.drawText(rect, Qt::AlignCenter, tr("Pause")); return; } //! [8] int boardTop = rect.bottom() - BoardHeight*squareHeight(); for (int i = 0; i < BoardHeight; ++i) { for (int j = 0; j < BoardWidth; ++j) { TetrixShape shape = shapeAt(j, BoardHeight - i - 1); if (shape != NoShape) drawSquare(painter, rect.left() + j * squareWidth(), boardTop + i * squareHeight(), shape); } //! [8] //! [9] } //! [9] //! [10] if (curPiece.shape() != NoShape) { for (int i = 0; i < 4; ++i) { int x = curX + curPiece.x(i); int y = curY - curPiece.y(i); drawSquare(painter, rect.left() + x * squareWidth(), boardTop + (BoardHeight - y - 1) * squareHeight(), curPiece.shape()); } //! [10] //! [11] } //! [11] //! [12] }
void TetrixBoard::showNextPiece(int width, int height) { if (!nextPieceLabel()) return; QPixmap pixmap(width * squareWidth(), height * squareHeight()); QPainter painter(&pixmap); painter.fillRect(pixmap.rect(), nextPieceLabel()->palette().background()); emit paintNextPieceRequested(&painter); nextPieceLabel()->setPixmap(pixmap); }
void TetrixBoard::drawSquare(QPainter *painter, int x, int y, int shape) { static const QRgb colorTable[8] = { 0x000000, 0xCC6666, 0x66CC66, 0x6666CC, 0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00 }; x = x*squareWidth(); y = y*squareHeight(); QColor color = colorTable[shape]; painter->fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2, color); painter->setPen(color.light()); painter->drawLine(x, y + squareHeight() - 1, x, y); painter->drawLine(x, y, x + squareWidth() - 1, y); painter->setPen(color.dark()); painter->drawLine(x + 1, y + squareHeight() - 1, x + squareWidth() - 1, y + squareHeight() - 1); painter->drawLine(x + squareWidth() - 1, y + squareHeight() - 1, x + squareWidth() - 1, y + 1); }