0


基于QT的电子木鱼小游戏(C/C++)

文章目录


前言

今年最火爆的解压小游戏电子木鱼,现在许多软件都上架了这个小程序。我在网上看了一下基本上都是用py和Java写的,所以我用QT重新写了一下,作为小白练手项目非常适合

一、界面展示

在这里插入图片描述

VID_20221230_203659

二、功能模块

功能设计
鼠标点击和释放事件,模拟敲打木鱼动作
每一次的敲打木鱼都会缩小和放大一次
并且在木鱼上方显示出"功德+1"字样和播放一次敲打木鱼的声音
背景音乐一直播放
设置一个按钮为自动敲击木鱼
设置一个按钮为背景音乐的开关

1) 木鱼缩放

我是使用的一个label来放图片
缩小的原理是在现有木鱼图片大小上长和宽都同时缩小一个比例m
因为是按照中心点不变的缩小
所以左上点pos的坐标下降m/2
放大同理
在这里插入图片描述

  1. // m = 10 图签放大,pos点上移.// m = -10 图签缩小,pos点下移.voidWidget::MuYu(int m){//获取当前label图片宽int currentWidth = ui->label->width();//获取当前label图片高int currentHeight = ui->label->height();//改变图片大小
  2. currentWidth += m;
  3. currentHeight += m;//在标签上重新设置图片大小和图片起始位置
  4. ui->label->setGeometry(ui->label->pos().x()-m/2,ui->label->pos().y()-m/2,currentWidth, currentHeight);}

2) 功德+1 显示

用一个label设置文字 “功德+1”
这里文字出现的位置可以是随机的也可以定点出现
随机出现可以跟踪鼠标点击的位置
定点出现要提前写一个QPoint指定地点 (示例这个方式)

每一次出现后先上移一定位置(会使用QT动画函数 QPropertyAnimation ),然后消失

  1. voidWidget::gongde(){
  2. ui->label_2->setText("功德+1");//QPropertyAnimation *m_TopPropertyAnimation;//绑定要移动的label对象
  3. m_TopPropertyAnimation->setTargetObject(ui->label_2);//设置按pos属性移动
  4. m_TopPropertyAnimation->setPropertyName("pos");// set 动画的起点、终点、持续时间
  5. m_TopPropertyAnimation->setDuration(600);
  6. m_TopPropertyAnimation->setStartValue(pos);
  7. m_TopPropertyAnimation->setEndValue(pos+QPoint(0,-120));// 启动和结束
  8. m_TopPropertyAnimation->start();//这里加一个延时函数避免,避免动画没有结束直接清除文字Delay(600);//清除文字
  9. ui->label_2->clear();}

3) 音乐

背景音乐BGM<<大悲咒>>直接功德加满

  1. voidWidget::bgMusice(){//QMediaPlayer *bg_player;qDebug()<<"dmz";//BACKMUSICE 宏定义文件路径
  2. bg_player->setMedia(QUrl::fromLocalFile(BACKMUSICE));
  3. bg_player->setVolume(10);
  4. bg_player->play();// 槽函数 监听QMediaPlayer::mediaStatusChanged信号 实现背景音乐循环播放connect(bg_player,&QMediaPlayer::mediaStatusChanged,this,&Widget::initStatus);}voidWidget::initStatus(QMediaPlayer::MediaStatus status){if(status == QMediaPlayer::EndOfMedia){
  5. bg_player->setPosition(0);
  6. bg_player->play();}}

敲击木鱼声音

  1. voidWidget::MuYuMusice(){//QMediaPlayer *MuYu_player;//设置要播放的媒体//MUYUMUSICE宏定义文件路径
  2. MuYu_player->setMedia(QUrl::fromLocalFile(MUYUMUSICE));//设置音量
  3. MuYu_player->setVolume(50);//播放
  4. MuYu_player->play();}

4) 自动

写个槽函数,定时器定时触发,可以绑定滑杆设置一个敲打频率,同理可以调节背景音乐大小

  1. voidWidget::Auto(){qDebug()<<"Auto";//图片缩小MuYu(-10);//敲到木鱼声音MuYuMusice();//功德+1文字gongde();//图片放大MuYu(10);}

5) 延时

  1. voidWidget::Delay(int delay_time){
  2. QEventLoop loop;QTimer::singleShot(delay_time,&loop,SLOT(quit()));
  3. loop.exec();}

6) 完整代码

widget.h

  1. #ifndefWIDGET_H#defineWIDGET_H#include<QWidget>#include<QPainter>#include<QRect>#include<QPropertyAnimation>#include<QMediaPlayer>#include<QTime>#include<QTimer>#include<QSystemTrayIcon>#include<QLabel>#include<QPainter>#include<QRect>#defineWIDTH480#defineHEIGH640#defineMUYUMUSICE"C:\\Users\\Liu\\Desktop\\code\\QT\\muyu\\untitled\\musice\\muyu.mp3"#defineBACKMUSICE"C:\\Users\\Liu\\Desktop\\code\\QT\\muyu\\untitled\\musice\\bg.mp3"#defineICON":/img/muy.ico"
  2. QT_BEGIN_NAMESPACE
  3. namespace Ui {classWidget;}
  4. QT_END_NAMESPACE
  5. classWidget:publicQWidget{
  6. Q_OBJECT
  7. public:Widget(QWidget *parent =nullptr);~Widget();voidmousePressEvent(QMouseEvent *event);//点击voidmouseReleaseEvent(QMouseEvent *event);//释放voidMuYu(int);voidgongde();voidMuYuMusice();voidbgMusice();voidDelay(int);voidtray();voidinitStatus(QMediaPlayer::MediaStatus status);// 槽函数 监听QMediaPlayer::mediaStatusChanged信号private slots:voidon_toolButton_2_clicked(bool checked);voidon_toolButton_clicked(bool checked);voidAuto();private:
  8. Ui::Widget *ui;
  9. QMediaPlayer *MuYu_player;
  10. QMediaPlayer *bg_player;
  11. QPoint pos;
  12. QPropertyAnimation *m_TopPropertyAnimation;
  13. QTimer *timer;
  14. QSystemTrayIcon *m_systemTray;int conut=0;};#endif// WIDGET_H

widget.cpp

  1. #include"widget.h"#include"ui_widget.h"#include<QDebug>#include<QMouseEvent>Widget::Widget(QWidget *parent):QWidget(parent),ui(new Ui::Widget){setMouseTracking(true);
  2. ui->setupUi(this);
  3. bg_player =new QMediaPlayer;
  4. MuYu_player =new QMediaPlayer;this->setWindowTitle("电子木鱼");this->setFixedSize(WIDTH,HEIGH);this->setWindowIcon(QIcon(ICON));
  5. m_TopPropertyAnimation =newQPropertyAnimation(this);
  6. pos=ui->label_2->pos();
  7. timer =new QTimer;connect(timer,SIGNAL(timeout()),this,SLOT(Auto()));bgMusice();tray();}Widget::~Widget(){delete ui;}voidWidget::tray(){
  8. m_systemTray =newQSystemTrayIcon(this);
  9. m_systemTray->setIcon(QIcon(ICON));
  10. m_systemTray->setToolTip("SystemTray Program");
  11. m_systemTray->show();}// m=10 图签放大 pos点上移// m=-10 图签缩小 pos点下移voidWidget::MuYu(int m){//获取当前label图片宽int currentWidth = ui->label->width();//获取当前label图片高int currentHeight = ui->label->height();//改变图片大小
  12. currentWidth += m;
  13. currentHeight += m;//在标签上重新设置图片大小和图片起始位置
  14. ui->label->setGeometry(ui->label->pos().x()-m/2,ui->label->pos().y()-m/2,currentWidth, currentHeight);}voidWidget::MuYuMusice(){//设置要播放的媒体
  15. MuYu_player->setMedia(QUrl::fromLocalFile(MUYUMUSICE));//设置音量
  16. MuYu_player->setVolume(50);//播放
  17. MuYu_player->play();}voidWidget::bgMusice(){qDebug()<<"dmz";
  18. bg_player->setMedia(QUrl::fromLocalFile(BACKMUSICE));
  19. bg_player->setVolume(10);
  20. bg_player->play();// 槽函数 监听QMediaPlayer::mediaStatusChanged信号 实现背景音乐循环播放connect(bg_player,&QMediaPlayer::mediaStatusChanged,this,&Widget::initStatus);}voidWidget::initStatus(QMediaPlayer::MediaStatus status){if(status == QMediaPlayer::EndOfMedia){
  21. bg_player->setPosition(0);
  22. bg_player->play();}}voidWidget::gongde(){
  23. ui->label_2->setText("功德+1");// bind
  24. m_TopPropertyAnimation->setTargetObject(ui->label_2);
  25. m_TopPropertyAnimation->setPropertyName("pos");// set 动画的起点、终点、持续时间
  26. m_TopPropertyAnimation->setDuration(600);
  27. m_TopPropertyAnimation->setStartValue(pos);
  28. m_TopPropertyAnimation->setEndValue(pos+QPoint(0,-120));// 启动和结束
  29. m_TopPropertyAnimation->start();Delay(600);
  30. ui->label_2->clear();}voidWidget::mousePressEvent(QMouseEvent *event){qDebug()<<"press";MuYu(-10);MuYuMusice();gongde();}voidWidget::mouseReleaseEvent(QMouseEvent *event){qDebug()<<"release";MuYu(10);}voidWidget::on_toolButton_clicked(bool checked){if(checked){
  31. timer->start(500);}else{
  32. timer->stop();}}voidWidget::on_toolButton_2_clicked(bool checked){if(checked){
  33. bg_player->stop();}else{
  34. bg_player->play();}}voidWidget::Auto(){qDebug()<<"Auto";MuYu(-10);MuYuMusice();gongde();MuYu(10);}//延时voidWidget::Delay(int delay_time){
  35. QEventLoop loop;QTimer::singleShot(delay_time,&loop,SLOT(quit()));
  36. loop.exec();}

代码写的不好,很多地方还可以优化,欢迎补充!!!
要素材的可以私聊我

标签: qt c语言 c++

本文转载自: https://blog.csdn.net/qq_51163115/article/details/128499635
版权归原作者 今天你debug了嘛? 所有, 如有侵权,请联系我们删除。

“基于QT的电子木鱼小游戏(C/C++)”的评论:

还没有评论