Browse Source

Tut 7

master
Georg Spar 2 years ago
parent
commit
86dc73e711
  1. 3
      CMakeLists.txt
  2. 9
      CMakeLists.txt.user
  3. 22
      MyRect.cpp
  4. 5
      MyRect.h
  5. 2
      game.cpp
  6. BIN
      laser-cannon-shot.wav
  7. 5
      res.qrc

3
CMakeLists.txt

@ -13,6 +13,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
find_package(Qt6 REQUIRED COMPONENTS Multimedia)
set(PROJECT_SOURCES set(PROJECT_SOURCES
main.cpp main.cpp
@ -52,7 +53,7 @@ else()
endif() endif()
target_link_libraries(GameTut PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) target_link_libraries(GameTut PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(GameTut PRIVATE Qt6::Multimedia)
set_target_properties(GameTut PROPERTIES set_target_properties(GameTut PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}

9
CMakeLists.txt.user

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 8.0.1, 2022-10-03T16:54:03. --> <!-- Written by QtCreator 8.0.1, 2022-10-03T23:13:28. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
@ -341,12 +341,15 @@
<valuelist type="QVariantList" key="CustomOutputParsers"/> <valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value> <value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/> <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">GameTut</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">CMakeProjectManager.CMakeRunConfiguration.GameTut</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">GameTut</value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value> <value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value> <value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value> <value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value> <value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">/home/sparg/programming/qt/build-GameTut-Desktop_Qt_6_4_0_GCC_64bit-Debug</value>
</valuemap> </valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value> <value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap> </valuemap>

22
MyRect.cpp

@ -5,6 +5,19 @@
#include "qgraphicsscene.h" #include "qgraphicsscene.h"
#include <QDebug> #include <QDebug>
#include "enemy.h" #include "enemy.h"
#include <QAudioOutput>
#include <QMediaDevices>
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) void MyRect::keyPressEvent(QKeyEvent *event)
{ {
@ -22,6 +35,15 @@ void MyRect::keyPressEvent(QKeyEvent *event)
Bullet* bullet = new Bullet(); Bullet* bullet = new Bullet();
bullet->setPos(x(),y()); bullet->setPos(x(),y());
scene()->addItem(bullet); scene()->addItem(bullet);
//play bulletsound
if (bulletsound->playbackState() == QMediaPlayer::PlayingState){
bulletsound->setPosition(0);
}
else if (bulletsound->playbackState() == QMediaPlayer::StoppedState){
bulletsound->play();
}
} }
} }

5
MyRect.h

@ -3,13 +3,18 @@
#include <QGraphicsRectItem> #include <QGraphicsRectItem>
#include <QObject> #include <QObject>
#include <QMediaPlayer>
class MyRect: public QObject, public QGraphicsRectItem { class MyRect: public QObject, public QGraphicsRectItem {
Q_OBJECT Q_OBJECT
public: public:
MyRect(QGraphicsItem *parent=0);
void keyPressEvent(QKeyEvent* event); void keyPressEvent(QKeyEvent* event);
public slots: public slots:
void spawn(); void spawn();
private:
QMediaPlayer* bulletsound;
QAudioOutput *audioOutput;
}; };
#endif // MYRECT_H #endif // MYRECT_H

2
game.cpp

@ -3,6 +3,8 @@
#include <QGraphicsScene> #include <QGraphicsScene>
#include "MyRect.h" #include "MyRect.h"
#include <QTimer> #include <QTimer>
#include <QMediaPlayer>
Game::Game(QWidget *parent) Game::Game(QWidget *parent)

BIN
laser-cannon-shot.wav

Binary file not shown.

5
res.qrc

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/sounds">
<file>laser-cannon-shot.wav</file>
</qresource>
</RCC>
Loading…
Cancel
Save