diff --git a/CMakeLists.txt b/CMakeLists.txt index 72002fe..341b150 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) +find_package(Qt6 REQUIRED COMPONENTS Multimedia) set(PROJECT_SOURCES main.cpp @@ -52,7 +53,7 @@ else() endif() target_link_libraries(GameTut PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) - +target_link_libraries(GameTut PRIVATE Qt6::Multimedia) set_target_properties(GameTut PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user index 450734a..658d4dd 100644 --- a/CMakeLists.txt.user +++ b/CMakeLists.txt.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -341,12 +341,15 @@ 2 - ProjectExplorer.CustomExecutableRunConfiguration - + GameTut + CMakeProjectManager.CMakeRunConfiguration.GameTut + GameTut false true + true false true + /home/sparg/programming/qt/build-GameTut-Desktop_Qt_6_4_0_GCC_64bit-Debug 1 diff --git a/MyRect.cpp b/MyRect.cpp index 9e82163..ac833e6 100644 --- a/MyRect.cpp +++ b/MyRect.cpp @@ -5,6 +5,19 @@ #include "qgraphicsscene.h" #include #include "enemy.h" +#include +#include + +MyRect::MyRect(QGraphicsItem *parent): QGraphicsRectItem(parent){ + bulletsound = new QMediaPlayer; + audioOutput = new QAudioOutput; + //audioOutput->setDevice(QMediaDevices::defaultAudioOutput()); + bulletsound->setAudioOutput(audioOutput); + + //connect(bulletsound, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); + bulletsound->setSource(QUrl("qrc:/sounds/laser-cannon-shot.wav")); + audioOutput->setVolume(50); +} void MyRect::keyPressEvent(QKeyEvent *event) { @@ -22,6 +35,15 @@ void MyRect::keyPressEvent(QKeyEvent *event) Bullet* bullet = new Bullet(); bullet->setPos(x(),y()); scene()->addItem(bullet); + + //play bulletsound + if (bulletsound->playbackState() == QMediaPlayer::PlayingState){ + bulletsound->setPosition(0); + } + else if (bulletsound->playbackState() == QMediaPlayer::StoppedState){ + bulletsound->play(); + } + } } diff --git a/MyRect.h b/MyRect.h index 351e0cf..7694bac 100644 --- a/MyRect.h +++ b/MyRect.h @@ -3,13 +3,18 @@ #include #include +#include class MyRect: public QObject, public QGraphicsRectItem { Q_OBJECT public: + MyRect(QGraphicsItem *parent=0); void keyPressEvent(QKeyEvent* event); public slots: void spawn(); +private: + QMediaPlayer* bulletsound; + QAudioOutput *audioOutput; }; #endif // MYRECT_H diff --git a/game.cpp b/game.cpp index 206e1a2..fdb108b 100644 --- a/game.cpp +++ b/game.cpp @@ -3,6 +3,8 @@ #include #include "MyRect.h" #include +#include + Game::Game(QWidget *parent) diff --git a/laser-cannon-shot.wav b/laser-cannon-shot.wav new file mode 100644 index 0000000..579c1f3 Binary files /dev/null and b/laser-cannon-shot.wav differ diff --git a/res.qrc b/res.qrc new file mode 100644 index 0000000..c513d7d --- /dev/null +++ b/res.qrc @@ -0,0 +1,5 @@ + + + laser-cannon-shot.wav + +