curPiece = nextPiece; // 预先随机设置好一下块方块 nextPiece.setRandomShape(); showNextPiece();
// 设置其初始下落的位置,在游戏区顶部中央 curX = BoardWidth / 2 + 1;
curY = BoardHeight-1 + curPiece.minY(); // 判断其是否还能移动,如果不能,则停止游戏 if (!tryMove(curPiece, curX, curY)) { curPiece.setShape(NoShape);
// painter.drawText(rect,tr(\游戏结束\ timer.stop(); isStarted = false; } }
// 展示下一个方块
void TetrixBoard::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);// 最后加载它 }
// 判断是否还能移动
bool TetrixBoard::tryMove(const TetrixPiece &newPiece, int newX, int newY) {
for (int i = 0; i < 4; ++i) { int x = newX + newPiece.x(i); int y = newY - newPiece.y(i);
if (x < 0 || x >= BoardWidth || y < 0 || y >= BoardHeight) return false;
if (shapeAt(x,y) != NoShape) // 判断当前位置是否有其他方块 return false; }
curPiece = newPiece; curX = newX; curY = newY; update(); return true; }
// int squareWidth() { return contentsRect().width() / BoardWidth; } // int squareHeight() { return contentsRect().height() / BoardHeight; } void TetrixBoard::drawSquare(QPainter &painter, int x,int y,TetrixShape shape) {
// google色彩
static const QRgb colorTable[8] = {
0x000000, 0x1851ce, 0xc61800, 0xefba00, 0x1851ce, 0x1ba823, 0xc61800, 0x606060 };
QColor color = colorTable[int(shape)]; // 填充单元网格的颜色
// void fillRect(int x, int y, int width, int height, const QColor & color) painter.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2,
color); painter.setPen(color.light()); // 左上角边框颜色
// void drawLine(int x1, int y1, int x2, int y2) 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); }
Tetrixpiece.h头文件代码:
// 俄罗斯方块类:方块形状/旋转情况 #ifndef TETRIXPIECE_H #define TETRIXPIECE_H
// 定义一个枚举类型(0-7):无形状、Z字形、S字形、直线形,T字形,田字形形、L字形,翻转L字形
enum TetrixShape { NoShape, ZShape,SShape,LineShape,TShape,SquareShape, LShape,MirroredLShape }; class TetrixPiece { public:
TetrixPiece() { setShape(NoShape); } // inline构造函数:初始设置成NoShape void setRandomShape(); // 设置随机规则
void setShape(TetrixShape shape); // 构造方块形状的数据结构 // 通过inline公有函数成员shape()访问私有数据成员pieceShape TetrixShape shape() const { return pieceShape; } int x(int index) const { return coords[index][0]; } int y(int index) const { return coords[index][1]; } int minX() const; int maxX() const;
int minY() const; int maxY() const;
TetrixPiece rotatedLeft() const; // 左转 private:
void setX(int index, int x) { coords[index][0] = x; } void setY(int index, int y) { coords[index][1] = y; } TetrixShape pieceShape; // 枚举类型创建一个该枚举类型对象 int coords[4][2]; };
#endif // TETRIXPIECE_H
Tetrixpiece.cpp程序代码: #include
// 伪随机函数:利用当前系统时间增加随机性 void TetrixPiece::setRandomShape() {
//QTime time = QTime::currentTime();
//qsrand( time.msec() + time.second()*1000 );// 重设随机种子:当前时间为随机变量
setShape(TetrixShape(qrand()%7 + 1)); // 共六种方块形状 }
void TetrixPiece::setShape(TetrixShape shape) {
// 按TetrixShape枚举顺序构建:
// 除NoShape外,每个形状由4个小方块组成,这里每行的四个坐标即4个小方块的坐标,其中横向为X,纵向为Y
// ZShape SShape LineShape TShape SquareShape LShape MirroredLShape
// -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2
// -1 * -1 * -1 * -1 -1 -1 * * -1 * * // 0 * * 0 * * 0 * 0 * * * 0 * * 0 * 0 * // 1 * 1 * 1 * 1 * 1 * * 1 * 1 * // 2 2 2 * 2 2 2 2 static const int coordsTable[8][4][2] = {
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },// NoShape { { 0, -1 }, { 0, 0 }, { -1, 0 }, { -1, 1 } }, { { 0, -1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } }, { { 0, -1 }, { 0, 0 }, { 0, 1 }, { 0, 2 } }, { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 } }, { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }, { { -1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } }, { { 1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } } };
for (int i=0; i<4; i++) { for (int j=0; j<2; ++j)
coords[i][j] = coordsTable[shape][i][j]; }
pieceShape = shape; }
// 获取最小X坐标值,QtGlobal::qMin int TetrixPiece::minX() const {
int min = coords[0][0]; for (int i = 1; i < 4; ++i) min = qMin(min, coords[i][0]); return min; }
// 获取最大X坐标值,QtGlobal::qMax int TetrixPiece::maxX() const {
int max = coords[0][0]; for (int i = 1; i < 4; ++i) max = qMax(max, coords[i][0]);