使用QT怎么編寫一個打地鼠游戲-創(chuàng)新互聯(lián)

使用QT怎么編寫一個打地鼠游戲?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)建站專注于滄州企業(yè)網站建設,成都響應式網站建設公司,成都做商城網站。滄州網站建設公司,為滄州等地區(qū)提供建站服務。全流程按需開發(fā),專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務

實現(xiàn)功能:


有若干地鼠洞,每次出現(xiàn)一只地鼠,當擊中地鼠后,分數(shù)加1,地鼠更換位置。當分數(shù)大于20時,游戲結束。

實現(xiàn)思路:

 1.先初始化一個頁面,放一只地鼠和若干個地鼠洞,為地鼠和地鼠洞添加槽函數(shù)。
 2.當點擊時就執(zhí)行相應函數(shù)。判斷是否擊中,從而對其進行加分或者減分。
 3.當擊中地鼠后,應該刷新頁面,讓地鼠換個位置出現(xiàn)。
 4.重復2.3,直到分數(shù)到達一定值或者其他結束條件后結束游戲。


用到的知識點:

 1.qt按鈕組,以及按鈕組連接信號槽(代碼里地鼠是用按鈕實現(xiàn)的,也可以使用QLabel實現(xiàn),點擊時,可以用static_cast<QLabel *>(childAt(event->pos()));判斷點中的是不是地鼠)
 2.QLabel設置圖片,字體,顏色,大小
 3.QPushButton 設置圖片
 4.給光標換圖片

下面開始創(chuàng)建項目,代碼在最下面,也可以直接拉到下面看代碼

1.創(chuàng)建qt項目,等待項目創(chuàng)建完成,這里我的項目名是BeatMouse

使用QT怎么編寫一個打地鼠游戲

2.接下來會有這個彈框,點next即可

使用QT怎么編寫一個打地鼠游戲

3.繼續(xù)next,release那里勾不勾都可以,不影響

使用QT怎么編寫一個打地鼠游戲

4.選擇QWidget,然后finish

使用QT怎么編寫一個打地鼠游戲

5.靜靜等待項目創(chuàng)建完成就好啦! 然后刪除項目里.cpp,.h文件里用到的ui相關的東西,這里用不到。

6.添加圖片資源文件,在項目解決方案里有個 Resource Files 文件夾,打開里面應該有一個自動創(chuàng)建好的.qrc文件,雙擊打開,點擊Add,選擇Add Files,即可添加資源進來,點擊添加好的某個資源,Resource URL就是資源的路徑,在項目里直接使用這個路徑,就可以用到這個資源。

使用QT怎么編寫一個打地鼠游戲

最后的效果圖

使用QT怎么編寫一個打地鼠游戲

初學代碼寫的有點亂,下面放上代碼

BeatMouse.h

#pragma once
#include <QtWidgets>
#include<QTime>
class BeatMouse : public QMainWindow
{
  Q_OBJECT
signals:
 void quit();
public:
  BeatMouse(QWidget *parent = Q_NULLPTR);
public slots:
 void OnButtonClickMouse(int index); //連接按鈕組,判斷是哪個按鈕被點擊
 void setScore(int score);  //設置分數(shù)

private:
 int m_width; //獲取屏幕的寬高 默認尺寸1920*1080
 int m_height;
 int mouseItem; //地鼠序號
 int m_score;
 
 QTime t;
 QRect  m_screenGeometry; //屏幕尺寸
 QLabel*   m_background;   //背景圖
 QLabel*  m_gameOver; //游戲結束后的遮罩
 QLabel*  m_gameOverText; //游戲結束后的提示文字

 QPushButton* m_btnQuit;  //右上角關閉按鈕
 QButtonGroup*  m_groupBtn;   // 按鈕組 
 QVector<QPushButton*> m_Mouse; // 地鼠按鈕
 QLCDNumber* m_lcdScore;  //分數(shù)
 
 void setBackground();  //設置背景
 void setGameQuit();  //設置關閉按鈕
 void initMouse();  //初始化地鼠洞
 void beginGame();  //隨機選擇一個地鼠洞變成地鼠
 void loadScore();  //初始化分數(shù)
 void gameOver();  //游戲結束 出現(xiàn)遮罩和提示文字
};

BeatMouse.cpp

#include "BeatMouse.h"

BeatMouse::BeatMouse(QWidget *parent)
  : QMainWindow(parent)
{
 QDesktopWidget* pDesktop = QApplication::desktop(); //獲取桌面大小
 m_screenGeometry = pDesktop->screenGeometry();
 m_width = m_screenGeometry.width();
 m_height = m_screenGeometry.height();
 m_rateHeight = m_height / 1080.0;
 m_rateWidth = m_height / 1920.0;
 this->setGeometry(m_screenGeometry);
 
 setBackground();
 setGameQuit();
 initMouse();
 loadScore();
 beginGame();
}

void BeatMouse::setBackground() {
 QPixmap image(":/BeatMouse/qrc/bg.jpg");
 QPixmap img = image.scaled(m_screenGeometry.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

 m_background = new QLabel(this);
 m_background->setPixmap(img);
 m_background->setFixedSize(img.size());
 m_background->lower();

 QPixmap imgCursor(":/BeatMouse/qrc/chuizi.png");
 QCursor cursor(imgCursor);
 setCursor(cursor);
}
void BeatMouse::setGameQuit() {
 QPixmap image(":/BeatMouse/qrc/close.png");
 QPixmap img = image.scaled(80,80, Qt::KeepAspectRatio, Qt::SmoothTransformation);

 m_btnQuit = new QPushButton(this);
 m_btnQuit->setIcon(img);
 m_btnQuit->setIconSize(img.size());
 m_btnQuit->setFixedSize(img.size());
 m_btnQuit->setStyleSheet("QPushButton{border:0px;background-color:rgba(0,0,0,0);outline:none;}");
 m_btnQuit->move(m_width - 140, 50 );
 QObject::connect(m_btnQuit, SIGNAL(clicked()), this, SIGNAL(quit()));
}
void BeatMouse::initMouse() {
 m_score = 0;
 m_Mouse.clear();
 m_groupBtn = new QButtonGroup(this);
 QPushButton* m_pbtn;

 for (int i = 0; i < 10; i++) { //2*5
 m_pbtn = new QPushButton(this);
 QPixmap image(":/BeatMouse/qrc/dishu2.png");
 QPixmap img = image.scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation);
 m_pbtn->setStyleSheet("QPushButton{border:0px;background-color:rgba(0,0,0,0);outline:none;}");
 
 if (i > 4) { // 后4個放一起
  //QPixmap img = image.scaled(160, 160, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  m_pbtn->move(300+270*(i-5), 500);
 }
 else {
  m_pbtn->move(300+270*i, 700);
 }
 m_pbtn->setIcon(img);
 m_pbtn->setIconSize(img.size());
 m_pbtn->setFixedSize(img.size());
 m_Mouse.push_back(m_pbtn);
 m_groupBtn->addButton(m_Mouse[i], i);
 }
 QObject::connect(m_groupBtn, SIGNAL(buttonClicked(int)), this, SLOT(OnButtonClickMouse(int)));

}

void BeatMouse::OnButtonClickMouse(int index) {
 if (index == mouseItem) { //如果被打中的是地鼠
 m_score++;
 setScore(m_score);
 QPixmap image(":/BeatMouse/qrc/dishu2.png");
 QPixmap img = image.scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation);
 m_Mouse[mouseItem]->setIcon(img);
 beginGame();
 }
}
void BeatMouse::beginGame() {
 QPixmap image(":/BeatMouse/qrc/dishu1.png");
 QPixmap img = image.scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation);
 
 t = QTime::currentTime();
 qsrand(t.msec()*100 + t.second() * 1000);
 mouseItem = qrand() % 10;
 m_Mouse[mouseItem]->setIcon(img);
}

void BeatMouse::loadScore() {
 m_lcdScore = new QLCDNumber(this);
 m_lcdScore->setDigitCount(1);
 m_lcdScore->setSegmentStyle(QLCDNumber::Flat);
 m_lcdScore->setStyleSheet("QLCDNumber{color:rgb(146,64,146);border:none;}");
 m_lcdScore->display(0);
 //
 m_lcdScore->setGeometry(m_width/2-80, 20,150, 150);
}
void BeatMouse::setScore(int score) {
 if (score >= 100)
 {
 m_lcdScore->setDigitCount(3);
 }
 else if (score >= 10 && score < 100)
 {
 m_lcdScore->setDigitCount(2);
 }
 else
 {
 m_lcdScore->setDigitCount(1);
 }
 if (score > 20) {
 score = 20;
 gameOver();
 }
 m_lcdScore->display(score);
}
void BeatMouse::gameOver() {
 m_gameOver = new QLabel(this);
 m_gameOver->setGeometry(0, 200, m_width, m_height-200);
 m_gameOver->setStyleSheet("QLabel{background-color:rgba(0,0,0,20);}");
 //m_gameOver->raise();
 m_gameOver->show();   //顯示

 m_gameOverText=new QLabel(this);
 m_gameOverText->setText(QString::fromLocal8Bit("恭喜過關!"));
 m_gameOverText->show();   //顯示

 //顏色
 QPalette pal;
 pal.setColor(QPalette::WindowText, Qt::red);
 m_gameOverText->setPalette(pal);
 //字號
 QFont ft;
 ft.setPointSize(40);
 m_gameOverText->setFont(ft);
 m_gameOverText->setGeometry(m_width/2-100, m_height/2-100,400,200);
 m_gameOverText->raise();
}

簡易版打地鼠,所以沒有加計時功能,也沒有加音樂,也沒有加重玩游戲的功能,下面說一下實現(xiàn)這些功能的簡單思路。

 //1.添加音樂
 qt5里添加音樂需要增加MultiMedia模塊,在項目屬性里添加即可,并加上這個頭文件
 #include <QMediaPlayer>
 使用:
 QMediaPlayer m_MediaObject;
 m_MediaObject.setMedia(QUrl("qrc:qrc/sound/enterSound.mp3"));
 m_MediaObject.play();
 注: enterSound.mp3在qrc里的路徑是:/qrc/sound/enterSound.mp3,但是直接使用這個路徑播放不出來聲音,經過查尋,把路徑換成這種qrc:qrc/sound/enterSound.mp3就可以使用,可以兩種都試試。
 qt5可以獲取音樂播放的狀態(tài),在OnState槽函數(shù)中,可以判斷音樂的狀態(tài),進而進行你想要的操作。
 QObject::connect(&m_MediaObject, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(OnState(QMediaPlayer::State)));
  
  void BeatMouse::OnState(QMediaPlayer::State state) {
 switch (state) { //判斷音樂的狀態(tài)
 case QMediaPlayer::StoppedState: //停止
 
 case QMediaPlayer::PlayingState: //播放
 break;
 case QMediaPlayer::PausedState: //暫停
 break;
 default: break;
 }
}

//2.添加計時
使用QTimer 設置一個計時器,每間隔1s就刷新一下時間的顯示。就能達到計時顯示的效果。定義一個QTime 記錄時長。
QTimer* m_timeUpdate;
QTime m_tTotalTime; //總時長

//3.重玩游戲
重玩游戲就是一個把地鼠歸位,時間,分數(shù)清零的動作。

關于使用QT怎么編寫一個打地鼠游戲問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。

文章標題:使用QT怎么編寫一個打地鼠游戲-創(chuàng)新互聯(lián)
標題鏈接:http://muchs.cn/article30/dhihpo.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站營銷、響應式網站、網站內鏈、網站設計、外貿建站、自適應網站

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿網站制作