QT實現(xiàn)簡單打地鼠游戲
本文實例為大家分享了QT實現(xiàn)簡單打地鼠游戲的具體代碼,供大家參考,具體內(nèi)容如下
開發(fā)工具:VS2017,qt5.9.8
開發(fā)語言:c++
實現(xiàn)功能:
有若干地鼠洞,每次出現(xiàn)一只地鼠,當(dāng)擊中地鼠后,分?jǐn)?shù)加1,地鼠更換位置。當(dāng)分?jǐn)?shù)大于20時,游戲結(jié)束。
實現(xiàn)思路:
1.先初始化一個頁面,放一只地鼠和若干個地鼠洞,為地鼠和地鼠洞添加槽函數(shù)。
2.當(dāng)點擊時就執(zhí)行相應(yīng)函數(shù)。判斷是否擊中,從而對其進行加分或者減分。
3.當(dāng)擊中地鼠后,應(yīng)該刷新頁面,讓地鼠換個位置出現(xiàn)。
4.重復(fù)2.3,直到分?jǐn)?shù)到達(dá)一定值或者其他結(jié)束條件后結(jié)束游戲。
用到的知識點:
1.qt按鈕組,以及按鈕組連接信號槽(代碼里地鼠是用按鈕實現(xiàn)的,也可以使用QLabel實現(xiàn),點擊時,可以用static_cast<QLabel *>(childAt(event->pos()));判斷點中的是不是地鼠)
2.QLabel設(shè)置圖片,字體,顏色,大小
3.QPushButton 設(shè)置圖片
4.給光標(biāo)換圖片
下面開始創(chuàng)建項目,代碼在最下面,也可以直接拉到下面看代碼
1.創(chuàng)建qt項目,等待項目創(chuàng)建完成,這里我的項目名是BeatMouse
2.接下來會有這個彈框,點next即可
3.繼續(xù)next,release那里勾不勾都可以,不影響
4.選擇QWidget,然后finish
5.靜靜等待項目創(chuàng)建完成就好啦! 然后刪除項目里.cpp,.h文件里用到的ui相關(guān)的東西,這里用不到。
6.添加圖片資源文件,在項目解決方案里有個 Resource Files 文件夾,打開里面應(yīng)該有一個自動創(chuàng)建好的.qrc文件,雙擊打開,點擊Add,選擇Add Files,即可添加資源進來,點擊添加好的某個資源,Resource URL就是資源的路徑,在項目里直接使用這個路徑,就可以用到這個資源。
最后的效果圖
初學(xué)代碼寫的有點亂,下面放上代碼
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è)置分?jǐn)?shù) private: int m_width; //獲取屏幕的寬高 默認(rèn)尺寸1920*1080 int m_height; int mouseItem; //地鼠序號 int m_score; QTime t; QRect m_screenGeometry; //屏幕尺寸 QLabel* m_background; //背景圖 QLabel* m_gameOver; //游戲結(jié)束后的遮罩 QLabel* m_gameOverText; //游戲結(jié)束后的提示文字 QPushButton* m_btnQuit; //右上角關(guān)閉按鈕 QButtonGroup* m_groupBtn; // 按鈕組 QVector<QPushButton*> m_Mouse; // 地鼠按鈕 QLCDNumber* m_lcdScore; //分?jǐn)?shù) void setBackground(); //設(shè)置背景 void setGameQuit(); //設(shè)置關(guān)閉按鈕 void initMouse(); //初始化地鼠洞 void beginGame(); //隨機選擇一個地鼠洞變成地鼠 void loadScore(); //初始化分?jǐn)?shù) void gameOver(); //游戲結(jié)束 出現(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("恭喜過關(guān)!")); 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,但是直接使用這個路徑播放不出來聲音,經(jīng)過查尋,把路徑換成這種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 設(shè)置一個計時器,每間隔1s就刷新一下時間的顯示。就能達(dá)到計時顯示的效果。定義一個QTime 記錄時長。 QTimer* m_timeUpdate; QTime m_tTotalTime; //總時長 //3.重玩游戲 重玩游戲就是一個把地鼠歸位,時間,分?jǐn)?shù)清零的動作。
圖片資源都是網(wǎng)圖,如果哪里寫的不好,歡迎指正!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言中隊列的結(jié)構(gòu)和函數(shù)接口的使用示例
隊列只允許一端進行插入數(shù)據(jù)操作,在另一端進行刪除數(shù)據(jù)操作的特殊線性表,隊列具有先進先出FIFO的性質(zhì);隊列可用數(shù)組和鏈表 的方法實現(xiàn),使用鏈表的結(jié)構(gòu)實現(xiàn)更優(yōu)一些,因為如果使用數(shù)組節(jié),出隊列時刪去首元素需要將整個數(shù)組前移,效率比較低2023-02-02C++示例分析內(nèi)聯(lián)函數(shù)與引用變量及函數(shù)重載的使用
為了消除函數(shù)調(diào)用的時空開銷,C++ 提供一種提高效率的方法,即在編譯時將函數(shù)調(diào)用處用函數(shù)體替換,類似于C語言中的宏展開。這種在函數(shù)調(diào)用處直接嵌入函數(shù)體的函數(shù)稱為內(nèi)聯(lián)函數(shù)(Inline Function),又稱內(nèi)嵌函數(shù)或者內(nèi)置函數(shù)2022-08-08