diff --git a/CMakeLists.txt b/CMakeLists.txt index 341b150..a07a7a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,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) +find_package(Qt6 REQUIRED COMPONENTS Gui) set(PROJECT_SOURCES main.cpp @@ -27,6 +28,7 @@ set(PROJECT_SOURCES game.cpp score.h score.cpp health.h health.cpp + res.qrc ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) @@ -54,6 +56,7 @@ endif() target_link_libraries(GameTut PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) target_link_libraries(GameTut PRIVATE Qt6::Multimedia) +target_link_libraries(GameTut PRIVATE Qt6::Gui) 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 658d4dd..5df4796 100644 --- a/CMakeLists.txt.user +++ b/CMakeLists.txt.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/MyRect.cpp b/MyRect.cpp index ac833e6..26f9c40 100644 --- a/MyRect.cpp +++ b/MyRect.cpp @@ -7,16 +7,20 @@ #include "enemy.h" #include #include +#include -MyRect::MyRect(QGraphicsItem *parent): QGraphicsRectItem(parent){ +MyRect::MyRect(QGraphicsItem *parent): QGraphicsPixmapItem(parent){ bulletsound = new QMediaPlayer; audioOutput = new QAudioOutput; - //audioOutput->setDevice(QMediaDevices::defaultAudioOutput()); + const QAudioDevice tempAudi = QAudioDevice(QMediaDevices::defaultAudioOutput()); + audioOutput->setDevice(tempAudi); bulletsound->setAudioOutput(audioOutput); - //connect(bulletsound, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); + //connect(bulletsound, SIGNAL(positionChanged(qint64)), audioOutput, SLOT(positionChanged(qint64))); bulletsound->setSource(QUrl("qrc:/sounds/laser-cannon-shot.wav")); audioOutput->setVolume(50); + + setPixmap(QPixmap(":/images/player.png")); } void MyRect::keyPressEvent(QKeyEvent *event) @@ -33,7 +37,7 @@ void MyRect::keyPressEvent(QKeyEvent *event) else if(event->key() == Qt::Key_Space) { Bullet* bullet = new Bullet(); - bullet->setPos(x(),y()); + bullet->setPos(x()+45,y()); scene()->addItem(bullet); //play bulletsound diff --git a/MyRect.h b/MyRect.h index 7694bac..7bee474 100644 --- a/MyRect.h +++ b/MyRect.h @@ -1,11 +1,11 @@ #ifndef MYRECT_H #define MYRECT_H -#include +#include #include #include -class MyRect: public QObject, public QGraphicsRectItem { +class MyRect: public QObject, public QGraphicsPixmapItem { Q_OBJECT public: MyRect(QGraphicsItem *parent=0); diff --git a/bg.png b/bg.png new file mode 100644 index 0000000..0201d06 Binary files /dev/null and b/bg.png differ diff --git a/bullet.cpp b/bullet.cpp index 06b2e9c..642165a 100644 --- a/bullet.cpp +++ b/bullet.cpp @@ -7,9 +7,10 @@ extern Game* game; -Bullet::Bullet() -{ - setRect(0,0,10,50); + +Bullet::Bullet(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent){ + setPixmap(QPixmap(":/images/missile2.png")); + show(); QTimer* timer = new QTimer(); connect(timer, SIGNAL(timeout()), this, SLOT(move())); timer->start(50); @@ -29,7 +30,7 @@ void Bullet::move(){ } } this->setPos(x(), y()-10); - if(pos().y() + rect().height() < 0) { + if(pos().y() < 0) { scene()->removeItem(this); delete this; qDebug() << "bullet deleted"; diff --git a/bullet.h b/bullet.h index 646c6e4..abfd7c0 100644 --- a/bullet.h +++ b/bullet.h @@ -1,14 +1,14 @@ #ifndef BULLET_H #define BULLET_H - +#include #include #include -class Bullet : public QObject, public QGraphicsRectItem { +class Bullet : public QObject, public QGraphicsPixmapItem { Q_OBJECT public: - Bullet(); + Bullet(QGraphicsItem *parent=0); public slots: void move(); }; diff --git a/enemy.cpp b/enemy.cpp index efc02e1..8ca2030 100644 --- a/enemy.cpp +++ b/enemy.cpp @@ -7,11 +7,13 @@ extern Game* game; -Enemy::Enemy() +Enemy::Enemy(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent) { int random_number = rand() % 700; setPos(random_number,0); - setRect(0,0,100,100); + //setRect(0,0,100,100); + setPixmap(QPixmap(":/images/enemy.png")); + QTimer* timer = new QTimer(); connect(timer, SIGNAL(timeout()), this, SLOT(move())); timer->start(50); @@ -25,7 +27,7 @@ void Enemy::move() game->health->decrease(); scene()->removeItem(this); delete this; - qDebug() << "bullet deleted"; + //qDebug() << "bullet deleted"; } } diff --git a/enemy.h b/enemy.h index 66f6e68..f6cdd1f 100644 --- a/enemy.h +++ b/enemy.h @@ -1,13 +1,13 @@ #ifndef ENEMY_H #define ENEMY_H -#include +#include #include -class Enemy : public QObject, public QGraphicsRectItem { +class Enemy : public QObject, public QGraphicsPixmapItem { Q_OBJECT public: - Enemy(); + Enemy(QGraphicsItem *parent=0); public slots: void move(); }; diff --git a/enemy.jpg b/enemy.jpg new file mode 100644 index 0000000..e395dfd Binary files /dev/null and b/enemy.jpg differ diff --git a/enemy.png b/enemy.png new file mode 100644 index 0000000..44c1cdf Binary files /dev/null and b/enemy.png differ diff --git a/enemy.xcf b/enemy.xcf new file mode 100644 index 0000000..fb256be Binary files /dev/null and b/enemy.xcf differ diff --git a/game.cpp b/game.cpp index fdb108b..544e283 100644 --- a/game.cpp +++ b/game.cpp @@ -10,30 +10,37 @@ Game::Game(QWidget *parent) : QGraphicsView{parent} { - MyRect* player = new MyRect(); - player->setRect(0,0,100,100); + + scene = new QGraphicsScene(); + scene->setSceneRect(0,0,800,600); + setBackgroundBrush(QBrush(QImage(":/images/bg.png"))); + setScene(scene); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setFixedSize(800,600); + + player = new MyRect(); + player->setPos(400,500); player->setFlag(QGraphicsItem::ItemIsFocusable); player->setFocus(); - QGraphicsScene* scene = new QGraphicsScene(); scene->addItem(player); + score = new Score(); scene->addItem(score); health = new Health(); health->setPos(health->x(), health->y()+25); scene->addItem(health); //scene->addWidget(this); - this->setScene(scene); - this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - this->setFixedSize(800,600); + + //view->show(); //view->setFixedSize(800,600); - scene->setSceneRect(0,0,800,600); - player->setPos(this->width()/2, this->height() - player->rect().height()); // spawn enemies QTimer* timer = new QTimer(); QObject::connect(timer, SIGNAL(timeout()), player, SLOT(spawn())); timer->start(2000); + show(); + } diff --git a/missile.png b/missile.png new file mode 100644 index 0000000..c429e95 Binary files /dev/null and b/missile.png differ diff --git a/missile2.png b/missile2.png new file mode 100644 index 0000000..48dcede Binary files /dev/null and b/missile2.png differ diff --git a/player.png b/player.png new file mode 100644 index 0000000..67be871 Binary files /dev/null and b/player.png differ diff --git a/player.xcf b/player.xcf new file mode 100644 index 0000000..91fd47a Binary files /dev/null and b/player.xcf differ diff --git a/res.qrc b/res.qrc index c513d7d..93622b7 100644 --- a/res.qrc +++ b/res.qrc @@ -2,4 +2,11 @@ laser-cannon-shot.wav + + missile.png + player.png + bg.png + missile2.png + enemy.png + diff --git a/res.qrc.txt b/res.qrc.txt new file mode 100644 index 0000000..4116320 --- /dev/null +++ b/res.qrc.txt @@ -0,0 +1,6 @@ + + + laser-cannon-shot.wav + + +