欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

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

實(shí)現(xiàn)功能:


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

實(shí)現(xiàn)思路:

 1.先初始化一個(gè)頁面,放一只地鼠和若干個(gè)地鼠洞,為地鼠和地鼠洞添加槽函數(shù)。
 2.當(dāng)點(diǎn)擊時(shí)就執(zhí)行相應(yīng)函數(shù)。判斷是否擊中,從而對其進(jìn)行加分或者減分。
 3.當(dāng)擊中地鼠后,應(yīng)該刷新頁面,讓地鼠換個(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)換圖片

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

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

使用QT怎么編寫一個(gè)打地鼠游戲

2.接下來會(huì)有這個(gè)彈框,點(diǎn)next即可

使用QT怎么編寫一個(gè)打地鼠游戲

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

使用QT怎么編寫一個(gè)打地鼠游戲

4.選擇QWidget,然后finish

使用QT怎么編寫一個(gè)打地鼠游戲

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

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

使用QT怎么編寫一個(gè)打地鼠游戲

最后的效果圖

使用QT怎么編寫一個(gè)打地鼠游戲

初學(xué)代碼寫的有點(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ā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();
}

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

 //1.添加音樂
 qt5里添加音樂需要增加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è)路徑播放不出來聲音,經(jīng)過查尋,把路徑換成這種qrc:qrc/sound/enterSound.mp3就可以使用,可以兩種都試試。
 qt5可以獲取音樂播放的狀態(tài),在OnState槽函數(shù)中,可以判斷音樂的狀態(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) { //判斷音樂的狀態(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í)長。
QTimer* m_timeUpdate;
QTime m_tTotalTime; //總時(shí)長

//3.重玩游戲
重玩游戲就是一個(gè)把地鼠歸位,時(shí)間,分?jǐn)?shù)清零的動(dòng)作。

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

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

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、響應(yīng)式網(wǎng)站網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計(jì)、外貿(mào)建站自適應(yīng)網(wǎng)站

廣告

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

外貿(mào)網(wǎng)站制作