2.5回合倒計時
在對弈時,每個回合都要有時間的限定,一般都是用的圓形進度條和中間秒數顯示。
QT當中沒有圓形的進度條,所以需要自己設計。圓形設計進度條傳送門:圓形進度條設計
圓形進度條需要時間軸搭配使用。
①定義自定義進度條類和時間軸類,需添加對應頭文件。
ArcPaint * progressStepPrompt; QTimeLine * timeLine;
②創建兩個實例,並設置時間軸運行狀態。
設置幀范圍為15,則剛好是1秒鍾1幀,幀改變時,會有信號發射,自定義槽函數接收信號,則幀改變時,自動更新倒計時即可;
時間軸具有值改變信號(值范圍0.0~1.0),剛好定義槽函數接收顯示圓弧(0~360°);
時間軸具有時間運行完成信號,剛好定義槽函數接收判斷當前回合是否超時。
progressStepPrompt = new ArcPaint(this); timeLine = new QTimeLine(15000,this); // 時間軸總時間15秒 timeLine->setFrameRange(0, 15); // 幀范圍0~15 timeLine->setDirection(QTimeLine::Backward); // 時間軸向后走,默認向前 timeLine->setCurveShape(QTimeLine::LinearCurve); // 時間軸速度不變,默認先慢后快,最后慢
自定義槽函數,與信號連接如下:
// timeLine connect(timeLine, SIGNAL(frameChanged(int)), this, SLOT(updateFrame(int))); connect(timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(timeValueChanged(qreal))); connect(timeLine, SIGNAL(finished()), this, SLOT(timefinished()));
槽函數實現:

1 void FiveChess::updateFrame(int frame) 2 { 3 static int i = 0; 4 if (frame <= 5) 5 { 6 soundList->setCurrentIndex(2); 7 music->play(); 8 } 9 progressStepPrompt->setCenterText(frame); 10 // qDebug() << "fram change!" << i++; 11 } 12 13 void FiveChess::timeValueChanged(qreal value) 14 { 15 progressStepPrompt->setArcAngle(360 * value); 16 } 17 18 void FiveChess::timefinished() 19 { 20 CurrentRound cur = currentRound == player ? computer: player; 21 GameOverPrompt(cur); 22 progressStepPrompt->setVisible(false); 23 chessboard->setDropFlag(false); 24 gameOverFlag = true; 25 if(currentRound==player) { 26 ui->msgLabel->setText("超時失敗!"); 27 } else { 28 ui->msgLabel->setText("對方超時!"); 29 } 30 }
③回合未進行時,進度條禁能,不顯示,當回合到來時,根據當前回合,獲取玩家頭像位置坐標,然后使能和顯示,並開啟時間軸。圓圈會依時間進行圓弧倒退並倒計時顯示。更換回合時,來回顯示。
1 void FiveChess::pregressPrompt() 2 { 3 progressStepPrompt->setVisible(true); 4 if (currentRound == computer) { 5 timeLine->stop(); 6 progressStepPrompt->move(ui->computerLabel->pos()+QPoint(-5,6)); 7 timeLine->start(); 8 } else { 9 timeLine->stop(); 10 progressStepPrompt->move(ui->playerLabel->pos()+QPoint(-5,6)); 11 timeLine->start(); 12 } 13 }
2.6音效
①找幾個簡短MP3音效文件,添加進資源文件。
②定義媒體播放類和播放列表,除了添加頭文件外,還需要在工程文件(.pro)中添加模塊:QT += multimedia
1 QMediaPlayer * music; 2 QMediaPlaylist * soundList;
③初始化中創建媒體播放器和播放列表實例,在播放列表中添加音效曲目並設置為單曲一次模式,把播放列表給媒體播放器。
1 music = new QMediaPlayer; 2 soundList = new QMediaPlaylist; 3 soundList->addMedia(QMediaContent(QUrl("qrc:/sound/press.mp3"))); 4 soundList->addMedia(QMediaContent(QUrl("qrc:/sound/start.mp3"))); 5 soundList->addMedia(QMediaContent(QUrl("qrc:/sound/clock.mp3"))); 6 soundList->addMedia(QMediaContent(QUrl("qrc:/sound/win.mp3"))); 7 soundList->setPlaybackMode(QMediaPlaylist::CurrentItemOnce); 8 music->setPlaylist(soundList);
④在需要顯示音效的地方,修改當前播放的下標,進行播放。播放列表下標與添加順序對應,此處范圍為0~3
1 soundList->setCurrentIndex(0); 2 music->play();
⑤開始游戲時,提示開始音效;落子時,提示落子音效;倒計時最后5秒,提示落子音效;結束時,提示音效。