基于Qt實(shí)現(xiàn)電子木魚小游戲
前言
今年最火爆的解壓小游戲電子木魚,現(xiàn)在許多軟件都上架了這個小程序。我在網(wǎng)上看了一下基本上都是用py和Java寫的,所以我用QT重新寫了一下,作為小白練手項(xiàng)目非常適合
一、界面展示

二、功能模塊
功能設(shè)計(jì)
鼠標(biāo)點(diǎn)擊和釋放事件,模擬敲打木魚動作
每一次的敲打木魚都會縮小和放大一次
并且在木魚上方顯示出"功德+1"字樣和播放一次敲打木魚的聲音
背景音樂一直播放
設(shè)置一個按鈕為自動敲擊木魚
設(shè)置一個按鈕為背景音樂的開關(guān)
1) 木魚縮放
我是使用的一個label來放圖片
縮小的原理是在現(xiàn)有木魚圖片大小上長和寬都同時縮小一個比例m
因?yàn)槭前凑罩行狞c(diǎn)不變的縮小
所以左上點(diǎn)pos的坐標(biāo)下降m/2
放大同理

// m = 10 圖簽放大,pos點(diǎn)上移.
// m = -10 圖簽縮小,pos點(diǎn)下移.
void Widget::MuYu(int m)
{
//獲取當(dāng)前l(fā)abel圖片寬
int currentWidth = ui->label->width();
//獲取當(dāng)前l(fā)abel圖片高
int currentHeight = ui->label->height();
//改變圖片大小
currentWidth += m;
currentHeight += m;
//在標(biāo)簽上重新設(shè)置圖片大小和圖片起始位置
ui->label->setGeometry(ui->label->pos().x()-m/2,ui->label->pos().y()-m/2,currentWidth, currentHeight);
}
2) 功德+1 顯示
用一個label設(shè)置文字 “功德+1”
這里文字出現(xiàn)的位置可以是隨機(jī)的也可以定點(diǎn)出現(xiàn)
隨機(jī)出現(xiàn)可以跟蹤鼠標(biāo)點(diǎn)擊的位置
定點(diǎn)出現(xiàn)要提前寫一個QPoint指定地點(diǎn) (示例這個方式)
每一次出現(xiàn)后先上移一定位置(會使用QT動畫函數(shù) QPropertyAnimation ),然后消失
void Widget::gongde()
{
ui->label_2->setText("功德+1");
//QPropertyAnimation *m_TopPropertyAnimation;
//綁定要移動的label對象
m_TopPropertyAnimation->setTargetObject(ui->label_2);
//設(shè)置按pos屬性移動
m_TopPropertyAnimation->setPropertyName("pos");
// set 動畫的起點(diǎn)、終點(diǎn)、持續(xù)時間
m_TopPropertyAnimation->setDuration(600);
m_TopPropertyAnimation->setStartValue(pos);
m_TopPropertyAnimation->setEndValue(pos+QPoint(0, -120));
// 啟動和結(jié)束
m_TopPropertyAnimation->start();
//這里加一個延時函數(shù)避免,避免動畫沒有結(jié)束直接清除文字
Delay(600);
//清除文字
ui->label_2->clear();
}3) 音樂
背景音樂BGM<<大悲咒>>直接功德加滿
void Widget::bgMusice()
{
//QMediaPlayer *bg_player;
qDebug()<<"dmz";
//BACKMUSICE 宏定義文件路徑
bg_player->setMedia(QUrl::fromLocalFile(BACKMUSICE));
bg_player->setVolume(10);
bg_player->play();
// 槽函數(shù) 監(jiān)聽QMediaPlayer::mediaStatusChanged信號 實(shí)現(xiàn)背景音樂循環(huán)播放
connect(bg_player, &QMediaPlayer::mediaStatusChanged,this,&Widget::initStatus);
}
void Widget::initStatus(QMediaPlayer::MediaStatus status)
{
if(status == QMediaPlayer::EndOfMedia)
{
bg_player->setPosition(0);
bg_player->play();
}
}敲擊木魚聲音
void Widget::MuYuMusice()
{
//QMediaPlayer *MuYu_player;
//設(shè)置要播放的媒體
//MUYUMUSICE宏定義文件路徑
MuYu_player->setMedia(QUrl::fromLocalFile(MUYUMUSICE));
//設(shè)置音量
MuYu_player->setVolume(50);
//播放
MuYu_player->play();
}
4) 自動
寫個槽函數(shù),定時器定時觸發(fā),可以綁定滑桿設(shè)置一個敲打頻率,同理可以調(diào)節(jié)背景音樂大小
void Widget::Auto()
{
qDebug()<<"Auto";
//圖片縮小
MuYu(-10);
//敲到木魚聲音
MuYuMusice();
//功德+1文字
gongde();
//圖片放大
MuYu(10);
}
5) 延時
void Widget::Delay(int delay_time)
{
QEventLoop loop;
QTimer::singleShot(delay_time,&loop,SLOT(quit()));
loop.exec();
}三、完整代碼
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPainter>
#include <QRect>
#include <QPropertyAnimation>
#include <QMediaPlayer>
#include <QTime>
#include <QTimer>
#include <QSystemTrayIcon>
#include <QLabel>
#include <QPainter>
#include <QRect>
#define WIDTH 480
#define HEIGH 640
#define MUYUMUSICE "C:\\Users\\Liu\\Desktop\\code\\QT\\muyu\\untitled\\musice\\muyu.mp3"
#define BACKMUSICE "C:\\Users\\Liu\\Desktop\\code\\QT\\muyu\\untitled\\musice\\bg.mp3"
#define ICON ":/img/muy.ico"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void mousePressEvent(QMouseEvent *event); //點(diǎn)擊
void mouseReleaseEvent(QMouseEvent *event); //釋放
void MuYu(int);
void gongde();
void MuYuMusice();
void bgMusice();
void Delay(int);
void tray();
void initStatus(QMediaPlayer::MediaStatus status); // 槽函數(shù) 監(jiān)聽QMediaPlayer::mediaStatusChanged信號
private slots:
void on_toolButton_2_clicked(bool checked);
void on_toolButton_clicked(bool checked);
void Auto();
private:
Ui::Widget *ui;
QMediaPlayer *MuYu_player;
QMediaPlayer *bg_player;
QPoint pos;
QPropertyAnimation *m_TopPropertyAnimation;
QTimer *timer;
QSystemTrayIcon *m_systemTray;
int conut=0;
};
#endif // WIDGET_Hwidget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QMouseEvent>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
setMouseTracking(true);
ui->setupUi(this);
bg_player = new QMediaPlayer;
MuYu_player = new QMediaPlayer;
this->setWindowTitle("電子木魚");
this->setFixedSize(WIDTH,HEIGH);
this->setWindowIcon(QIcon(ICON));
m_TopPropertyAnimation = new QPropertyAnimation(this);
pos=ui->label_2->pos();
timer = new QTimer;
connect(timer,SIGNAL(timeout()),this,SLOT(Auto()));
bgMusice();
tray();
}
Widget::~Widget()
{
delete ui;
}
void Widget::tray()
{
m_systemTray = new QSystemTrayIcon(this);
m_systemTray->setIcon(QIcon(ICON));
m_systemTray->setToolTip("SystemTray Program");
m_systemTray->show();
}
// m=10 圖簽放大 pos點(diǎn)上移
// m=-10 圖簽縮小 pos點(diǎn)下移
void Widget::MuYu(int m)
{
//獲取當(dāng)前l(fā)abel圖片寬
int currentWidth = ui->label->width();
//獲取當(dāng)前l(fā)abel圖片高
int currentHeight = ui->label->height();
//改變圖片大小
currentWidth += m;
currentHeight += m;
//在標(biāo)簽上重新設(shè)置圖片大小和圖片起始位置
ui->label->setGeometry(ui->label->pos().x()-m/2,ui->label->pos().y()-m/2,currentWidth, currentHeight);
}
void Widget::MuYuMusice()
{
//設(shè)置要播放的媒體
MuYu_player->setMedia(QUrl::fromLocalFile(MUYUMUSICE));
//設(shè)置音量
MuYu_player->setVolume(50);
//播放
MuYu_player->play();
}
void Widget::bgMusice()
{
qDebug()<<"dmz";
bg_player->setMedia(QUrl::fromLocalFile(BACKMUSICE));
bg_player->setVolume(10);
bg_player->play();
// 槽函數(shù) 監(jiān)聽QMediaPlayer::mediaStatusChanged信號 實(shí)現(xiàn)背景音樂循環(huán)播放
connect(bg_player, &QMediaPlayer::mediaStatusChanged,this,&Widget::initStatus);
}
void Widget::initStatus(QMediaPlayer::MediaStatus status)
{
if(status == QMediaPlayer::EndOfMedia)
{
bg_player->setPosition(0);
bg_player->play();
}
}
void Widget::gongde()
{
ui->label_2->setText("功德+1");
// bind
m_TopPropertyAnimation->setTargetObject(ui->label_2);
m_TopPropertyAnimation->setPropertyName("pos");
// set 動畫的起點(diǎn)、終點(diǎn)、持續(xù)時間
m_TopPropertyAnimation->setDuration(600);
m_TopPropertyAnimation->setStartValue(pos);
m_TopPropertyAnimation->setEndValue(pos+QPoint(0, -120));
// 啟動和結(jié)束
m_TopPropertyAnimation->start();
Delay(600);
ui->label_2->clear();
}
void Widget::mousePressEvent(QMouseEvent *event)
{
qDebug()<<"press";
MuYu(-10);
MuYuMusice();
gongde();
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
qDebug()<<"release";
MuYu(10);
}
void Widget::on_toolButton_clicked(bool checked)
{
if (checked) {
timer->start(500);
}
else {
timer->stop();
}
}
void Widget::on_toolButton_2_clicked(bool checked)
{
if(checked)
{
bg_player->stop();
}else {
bg_player->play();
}
}
void Widget::Auto()
{
qDebug()<<"Auto";
MuYu(-10);
MuYuMusice();
gongde();
MuYu(10);
}
//延時
void Widget::Delay(int delay_time)
{
QEventLoop loop;
QTimer::singleShot(delay_time,&loop,SLOT(quit()));
loop.exec();
}到此這篇關(guān)于基于Qt實(shí)現(xiàn)電子木魚小游戲的文章就介紹到這了,更多相關(guān)Qt電子木魚內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(36.驗(yàn)證數(shù)獨(dú))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(36.驗(yàn)證數(shù)獨(dú)),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語言rewind與fseek函數(shù)之隨機(jī)讀寫文件的用法詳解
這篇文章主要介紹了C語言rewind與fseek函數(shù)之隨機(jī)讀寫文件的用法詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
C++操作MySQL大量數(shù)據(jù)插入效率低下的解決方法
這篇文章主要介紹了C++操作MySQL大量數(shù)據(jù)插入效率低下的解決方法,需要的朋友可以參考下2014-07-07
詳解C語言結(jié)構(gòu)體中的char數(shù)組如何賦值
這篇文章主要給大家介紹了關(guān)于C語言結(jié)構(gòu)體中的char數(shù)組如何賦值的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-03-03
C語言中send()函數(shù)和sendto()函數(shù)的使用方法
這篇文章主要介紹了C語言中send()函數(shù)和sendto()函數(shù)的使用方法,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
Matlab實(shí)現(xiàn)二維散點(diǎn)主方向直方圖的繪制詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Matlab實(shí)現(xiàn)二維散點(diǎn)主方向直方圖的繪制,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定幫助,需要的可以參考一下2022-09-09
C++數(shù)據(jù)結(jié)構(gòu)之哈希表的實(shí)現(xiàn)
哈希表,即散列表,可以快速地存儲和查詢記錄。這篇文章主要為大家詳細(xì)介紹了C++數(shù)據(jù)結(jié)構(gòu)中哈希表的實(shí)現(xiàn),感興趣的小伙伴可以了解一下2023-03-03

