Qt小遊戲開發:掃雷
阿新 • • 發佈:2019-02-11
根據遊戲的狀態和遊戲模型中各方塊的情況、計時器的情況,進行鍼對性的重繪,實現遊戲介面更新。void MainGameWindow::paintEvent(QPaintEvent *event) { QPainter painter(this); QPixmap bmpBlocks(":/res/blocks.bmp"); QPixmap bmpFaces(":/res/faces.bmp"); QPixmap bmpFrame(":/res/frame.bmp"); QPixmap bmpNumber(":/res/timenumber.bmp"); // 繪製笑臉 switch(game->gameState) { case OVER: painter.drawPixmap((game->mCol * blockSize + offsetX * 2) / 2 - 12, spaceY / 2, bmpFaces, 0 * 24, 0, 24, 24); // 24是笑臉的邊長,錨點在左上,因為工具欄佔了些,所以看起來不再中間 break; case PLAYING: painter.drawPixmap((game->mCol * blockSize + offsetX * 2) / 2 - 12, spaceY / 2, bmpFaces, 1 * 24, 0, 24, 24); break; case WIN: painter.drawPixmap((game->mCol * blockSize + offsetX * 2) / 2 - 12, spaceY / 2, bmpFaces, 2 * 24, 0, 24, 24); break; default: painter.drawPixmap((game->mCol * blockSize + offsetX * 2) / 2 - 12, spaceY / 2, bmpFaces, 1 * 24, 0, 24, 24); break; } // 繪製剩餘雷數 int n = game->curMineNumber; int posX = (game->mCol * blockSize + offsetX * 2) / 2 - 50; // 最後一位數字的橫座標 if(n <= 0) // 如果雷數為0或者減到0以下,單獨繪製 { painter.drawPixmap(posX, spaceY / 2, bmpNumber, n * 20, 0, 20, 28); // 20是數字的寬,28是高 } while(n > 0) // 如果是多位數 { painter.drawPixmap(posX - 20, spaceY / 2, bmpNumber, n % 10 * 20, 0, 20, 28); // 每次從後面繪製一位 n /= 10; posX -= 20; } // 繪製雷區 for(int i = 0; i < game->mRow; i++) { for(int j = 0; j < game->mCol; j++) { switch(game->gameMap[i][j].curState) { // 根據不同的方塊狀態繪製,算出在bmp中的偏移量 case UN_DIG: painter.drawPixmap(j * blockSize + offsetX, i * blockSize + offsetY + spaceY , bmpBlocks, blockSize * 10, 0, blockSize, blockSize); break; case DIGGED: painter.drawPixmap(j * blockSize + offsetX, i * blockSize + offsetY + spaceY, bmpBlocks, blockSize * game->gameMap[i][j].valueFlag, 0, blockSize, blockSize); break; case MARKED: painter.drawPixmap(j * blockSize + offsetX, i * blockSize + offsetY + spaceY, bmpBlocks, blockSize * 11, 0, blockSize, blockSize); break; case BOMB: painter.drawPixmap(j * blockSize + offsetX, i * blockSize + offsetY + spaceY, bmpBlocks, blockSize * 9, 0, blockSize, blockSize); break; case WRONG_BOMB: if(game->gameState == PLAYING || game->gameState == FAULT) { // 如果還在遊戲中就顯示旗子 painter.drawPixmap(j * blockSize + offsetX, i * blockSize + offsetY + spaceY, bmpBlocks, blockSize * 11, 0, blockSize, blockSize); } else if(game->gameState == OVER) { // 如果遊戲已經結束,就顯示標錯了 painter.drawPixmap(j * blockSize + offsetX, i * blockSize + offsetY + spaceY, bmpBlocks, blockSize * 12, 0, blockSize, blockSize); } break; default: break; } } } // 處理遊戲狀態 handleGameState(game); }