QT實(shí)現(xiàn)簡(jiǎn)單打地鼠游戲
本文實(shí)例為大家分享了QT實(shí)現(xiàn)簡(jiǎn)單打地鼠游戲的具體代碼,供大家參考,具體內(nèi)容如下
開(kāi)發(fā)工具:VS2017,qt5.9.8
開(kāi)發(fā)語(yǔ)言:c++
實(shí)現(xiàn)功能:
有若干地鼠洞,每次出現(xiàn)一只地鼠,當(dāng)擊中地鼠后,分?jǐn)?shù)加1,地鼠更換位置。當(dāng)分?jǐn)?shù)大于20時(shí),游戲結(jié)束。
實(shí)現(xiàn)思路:
1.先初始化一個(gè)頁(yè)面,放一只地鼠和若干個(gè)地鼠洞,為地鼠和地鼠洞添加槽函數(shù)。
2.當(dāng)點(diǎn)擊時(shí)就執(zhí)行相應(yīng)函數(shù)。判斷是否擊中,從而對(duì)其進(jìn)行加分或者減分。
3.當(dāng)擊中地鼠后,應(yīng)該刷新頁(yè)面,讓地鼠換個(gè)位置出現(xiàn)。
4.重復(fù)2.3,直到分?jǐn)?shù)到達(dá)一定值或者其他結(jié)束條件后結(jié)束游戲。
用到的知識(shí)點(diǎn):
1.qt按鈕組,以及按鈕組連接信號(hào)槽(代碼里地鼠是用按鈕實(shí)現(xiàn)的,也可以使用QLabel實(shí)現(xiàn),點(diǎn)擊時(shí),可以用static_cast<QLabel *>(childAt(event->pos()));判斷點(diǎn)中的是不是地鼠)
2.QLabel設(shè)置圖片,字體,顏色,大小
3.QPushButton 設(shè)置圖片
4.給光標(biāo)換圖片
下面開(kāi)始創(chuàng)建項(xiàng)目,代碼在最下面,也可以直接拉到下面看代碼
1.創(chuàng)建qt項(xiàng)目,等待項(xiàng)目創(chuàng)建完成,這里我的項(xiàng)目名是BeatMouse
2.接下來(lái)會(huì)有這個(gè)彈框,點(diǎn)next即可
3.繼續(xù)next,release那里勾不勾都可以,不影響
4.選擇QWidget,然后finish
5.靜靜等待項(xiàng)目創(chuàng)建完成就好啦! 然后刪除項(xiàng)目里.cpp,.h文件里用到的ui相關(guān)的東西,這里用不到。
6.添加圖片資源文件,在項(xiàng)目解決方案里有個(gè) Resource Files 文件夾,打開(kāi)里面應(yīng)該有一個(gè)自動(dòng)創(chuàng)建好的.qrc文件,雙擊打開(kāi),點(diǎn)擊Add,選擇Add Files,即可添加資源進(jìn)來(lái),點(diǎn)擊添加好的某個(gè)資源,Resource URL就是資源的路徑,在項(xiàng)目里直接使用這個(gè)路徑,就可以用到這個(gè)資源。
最后的效果圖
初學(xué)代碼寫(xiě)的有點(diǎn)亂,下面放上代碼
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); //連接按鈕組,判斷是哪個(gè)按鈕被點(diǎn)擊 void setScore(int score); //設(shè)置分?jǐn)?shù) private: int m_width; //獲取屏幕的寬高 默認(rèn)尺寸1920*1080 int m_height; int mouseItem; //地鼠序號(hào) 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(); //隨機(jī)選擇一個(gè)地鼠洞變成地鼠 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個(gè)放一起 //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ò)關(guān)!")); m_gameOverText->show(); //顯示 //顏色 QPalette pal; pal.setColor(QPalette::WindowText, Qt::red); m_gameOverText->setPalette(pal); //字號(hào) 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(); }
簡(jiǎn)易版打地鼠,所以沒(méi)有加計(jì)時(shí)功能,也沒(méi)有加音樂(lè),也沒(méi)有加重玩游戲的功能,下面說(shuō)一下實(shí)現(xiàn)這些功能的簡(jiǎn)單思路。
//1.添加音樂(lè) qt5里添加音樂(lè)需要增加MultiMedia模塊,在項(xiàng)目屬性里添加即可,并加上這個(gè)頭文件 #include <QMediaPlayer> 使用: QMediaPlayer m_MediaObject; m_MediaObject.setMedia(QUrl("qrc:qrc/sound/enterSound.mp3")); m_MediaObject.play(); 注: enterSound.mp3在qrc里的路徑是:/qrc/sound/enterSound.mp3,但是直接使用這個(gè)路徑播放不出來(lái)聲音,經(jīng)過(guò)查尋,把路徑換成這種qrc:qrc/sound/enterSound.mp3就可以使用,可以兩種都試試。 qt5可以獲取音樂(lè)播放的狀態(tài),在OnState槽函數(shù)中,可以判斷音樂(lè)的狀態(tài),進(jìn)而進(jìn)行你想要的操作。 QObject::connect(&m_MediaObject, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(OnState(QMediaPlayer::State))); void BeatMouse::OnState(QMediaPlayer::State state) { switch (state) { //判斷音樂(lè)的狀態(tài) case QMediaPlayer::StoppedState: //停止 case QMediaPlayer::PlayingState: //播放 break; case QMediaPlayer::PausedState: //暫停 break; default: break; } } //2.添加計(jì)時(shí) 使用QTimer 設(shè)置一個(gè)計(jì)時(shí)器,每間隔1s就刷新一下時(shí)間的顯示。就能達(dá)到計(jì)時(shí)顯示的效果。定義一個(gè)QTime 記錄時(shí)長(zhǎng)。 QTimer* m_timeUpdate; QTime m_tTotalTime; //總時(shí)長(zhǎng) //3.重玩游戲 重玩游戲就是一個(gè)把地鼠歸位,時(shí)間,分?jǐn)?shù)清零的動(dòng)作。
圖片資源都是網(wǎng)圖,如果哪里寫(xiě)的不好,歡迎指正!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
聊聊C語(yǔ)言中sizeof運(yùn)算符的一個(gè)陷阱
在C語(yǔ)言中,sizeof()是一個(gè)判斷數(shù)據(jù)類型或者表達(dá)式長(zhǎng)度的運(yùn)算符,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中sizeof運(yùn)算符的一個(gè)陷阱的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11C語(yǔ)言實(shí)現(xiàn)自動(dòng)存取款機(jī)模擬系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)自動(dòng)存取款機(jī)模擬系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05使用C語(yǔ)言計(jì)算長(zhǎng)方體的表面積和體積
這篇文章主要給大家介紹了關(guān)于如何使用C語(yǔ)言計(jì)算長(zhǎng)方體的表面積和體積的相關(guān)資料,在C語(yǔ)言中,我們可以使用乘法運(yùn)算符(*)來(lái)進(jìn)行乘法運(yùn)算,并將結(jié)果保存在一個(gè)變量中,需要的朋友可以參考下2023-10-10C語(yǔ)言中隊(duì)列的結(jié)構(gòu)和函數(shù)接口的使用示例
隊(duì)列只允許一端進(jìn)行插入數(shù)據(jù)操作,在另一端進(jìn)行刪除數(shù)據(jù)操作的特殊線性表,隊(duì)列具有先進(jìn)先出FIFO的性質(zhì);隊(duì)列可用數(shù)組和鏈表 的方法實(shí)現(xiàn),使用鏈表的結(jié)構(gòu)實(shí)現(xiàn)更優(yōu)一些,因?yàn)槿绻褂脭?shù)組節(jié),出隊(duì)列時(shí)刪去首元素需要將整個(gè)數(shù)組前移,效率比較低2023-02-02c語(yǔ)言通過(guò)opencv實(shí)現(xiàn)輪廓處理與切割
這篇文章主要介紹了c語(yǔ)言通過(guò)opencv實(shí)現(xiàn)輪廓處理與切割,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01C++示例分析內(nèi)聯(lián)函數(shù)與引用變量及函數(shù)重載的使用
為了消除函數(shù)調(diào)用的時(shí)空開(kāi)銷(xiāo),C++ 提供一種提高效率的方法,即在編譯時(shí)將函數(shù)調(diào)用處用函數(shù)體替換,類似于C語(yǔ)言中的宏展開(kāi)。這種在函數(shù)調(diào)用處直接嵌入函數(shù)體的函數(shù)稱為內(nèi)聯(lián)函數(shù)(Inline Function),又稱內(nèi)嵌函數(shù)或者內(nèi)置函數(shù)2022-08-08