温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用QT怎么编写一个打地鼠游戏

发布时间:2020-12-24 16:11:08 来源:亿速云 阅读:464 作者:Leah 栏目:开发技术

使用QT怎么编写一个打地鼠游戏?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

实现功能:

有若干地鼠洞,每次出现一只地鼠,当击中地鼠后,分数加1,地鼠更换位置。当分数大于20时,游戏结束。

实现思路:

 1.先初始化一个页面,放一只地鼠和若干个地鼠洞,为地鼠和地鼠洞添加槽函数。
 2.当点击时就执行相应函数。判断是否击中,从而对其进行加分或者减分。
 3.当击中地鼠后,应该刷新页面,让地鼠换个位置出现。
 4.重复2.3,直到分数到达一定值或者其他结束条件后结束游戏。

用到的知识点:

 1.qt按钮组,以及按钮组连接信号槽(代码里地鼠是用按钮实现的,也可以使用QLabel实现,点击时,可以用static_cast<QLabel *>(childAt(event->pos()));判断点中的是不是地鼠)
 2.QLabel设置图片,字体,颜色,大小
 3.QPushButton 设置图片
 4.给光标换图片

下面开始创建项目,代码在最下面,也可以直接拉到下面看代码

1.创建qt项目,等待项目创建完成,这里我的项目名是BeatMouse

使用QT怎么编写一个打地鼠游戏

2.接下来会有这个弹框,点next即可

使用QT怎么编写一个打地鼠游戏

3.继续next,release那里勾不勾都可以,不影响

使用QT怎么编写一个打地鼠游戏

4.选择QWidget,然后finish

使用QT怎么编写一个打地鼠游戏

5.静静等待项目创建完成就好啦! 然后删除项目里.cpp,.h文件里用到的ui相关的东西,这里用不到。

6.添加图片资源文件,在项目解决方案里有个 Resource Files 文件夹,打开里面应该有一个自动创建好的.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);  //设置分数

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;  //分数
 
 void setBackground();  //设置背景
 void setGameQuit();  //设置关闭按钮
 void initMouse();  //初始化地鼠洞
 void beginGame();  //随机选择一个地鼠洞变成地鼠
 void loadScore();  //初始化分数
 void gameOver();  //游戏结束 出现遮罩和提示文字
};

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();
}

简易版打地鼠,所以没有加计时功能,也没有加音乐,也没有加重玩游戏的功能,下面说一下实现这些功能的简单思路。

 //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可以获取音乐播放的状态,在OnState槽函数中,可以判断音乐的状态,进而进行你想要的操作。
 QObject::connect(&m_MediaObject, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(OnState(QMediaPlayer::State)));
  
  void BeatMouse::OnState(QMediaPlayer::State state) {
 switch (state) { //判断音乐的状态
 case QMediaPlayer::StoppedState: //停止
 
 case QMediaPlayer::PlayingState: //播放
 break;
 case QMediaPlayer::PausedState: //暂停
 break;
 default: break;
 }
}

//2.添加计时
使用QTimer 设置一个计时器,每间隔1s就刷新一下时间的显示。就能达到计时显示的效果。定义一个QTime 记录时长。
QTimer* m_timeUpdate;
QTime m_tTotalTime; //总时长

//3.重玩游戏
重玩游戏就是一个把地鼠归位,时间,分数清零的动作。

关于使用QT怎么编写一个打地鼠游戏问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

qt
AI