Browse Source

Tut 8

master
Georg Spar 2 years ago
parent
commit
3d40e05dba
  1. 3
      CMakeLists.txt
  2. 2
      CMakeLists.txt.user
  3. 12
      MyRect.cpp
  4. 4
      MyRect.h
  5. BIN
      bg.png
  6. 9
      bullet.cpp
  7. 6
      bullet.h
  8. 8
      enemy.cpp
  9. 6
      enemy.h
  10. BIN
      enemy.jpg
  11. BIN
      enemy.png
  12. BIN
      enemy.xcf
  13. 25
      game.cpp
  14. BIN
      missile.png
  15. BIN
      missile2.png
  16. BIN
      player.png
  17. BIN
      player.xcf
  18. 7
      res.qrc
  19. 6
      res.qrc.txt

3
CMakeLists.txt

@ -14,6 +14,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) find_package(Qt6 REQUIRED COMPONENTS Multimedia)
find_package(Qt6 REQUIRED COMPONENTS Gui)
set(PROJECT_SOURCES set(PROJECT_SOURCES
main.cpp main.cpp
@ -27,6 +28,7 @@ set(PROJECT_SOURCES
game.cpp game.cpp
score.h score.cpp score.h score.cpp
health.h health.cpp health.h health.cpp
res.qrc
) )
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) 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 Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(GameTut PRIVATE Qt6::Multimedia) target_link_libraries(GameTut PRIVATE Qt6::Multimedia)
target_link_libraries(GameTut PRIVATE Qt6::Gui)
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}

2
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-03T23:13:28. --> <!-- Written by QtCreator 8.0.1, 2022-10-05T17:01:12. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

12
MyRect.cpp

@ -7,16 +7,20 @@
#include "enemy.h" #include "enemy.h"
#include <QAudioOutput> #include <QAudioOutput>
#include <QMediaDevices> #include <QMediaDevices>
#include <QAudioDevice>
MyRect::MyRect(QGraphicsItem *parent): QGraphicsRectItem(parent){ MyRect::MyRect(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
bulletsound = new QMediaPlayer; bulletsound = new QMediaPlayer;
audioOutput = new QAudioOutput; audioOutput = new QAudioOutput;
//audioOutput->setDevice(QMediaDevices::defaultAudioOutput()); const QAudioDevice tempAudi = QAudioDevice(QMediaDevices::defaultAudioOutput());
audioOutput->setDevice(tempAudi);
bulletsound->setAudioOutput(audioOutput); 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")); bulletsound->setSource(QUrl("qrc:/sounds/laser-cannon-shot.wav"));
audioOutput->setVolume(50); audioOutput->setVolume(50);
setPixmap(QPixmap(":/images/player.png"));
} }
void MyRect::keyPressEvent(QKeyEvent *event) void MyRect::keyPressEvent(QKeyEvent *event)
@ -33,7 +37,7 @@ void MyRect::keyPressEvent(QKeyEvent *event)
else if(event->key() == Qt::Key_Space) { else if(event->key() == Qt::Key_Space) {
Bullet* bullet = new Bullet(); Bullet* bullet = new Bullet();
bullet->setPos(x(),y()); bullet->setPos(x()+45,y());
scene()->addItem(bullet); scene()->addItem(bullet);
//play bulletsound //play bulletsound

4
MyRect.h

@ -1,11 +1,11 @@
#ifndef MYRECT_H #ifndef MYRECT_H
#define MYRECT_H #define MYRECT_H
#include <QGraphicsRectItem> #include <QGraphicsPixmapItem>
#include <QObject> #include <QObject>
#include <QMediaPlayer> #include <QMediaPlayer>
class MyRect: public QObject, public QGraphicsRectItem { class MyRect: public QObject, public QGraphicsPixmapItem {
Q_OBJECT Q_OBJECT
public: public:
MyRect(QGraphicsItem *parent=0); MyRect(QGraphicsItem *parent=0);

BIN
bg.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

9
bullet.cpp

@ -7,9 +7,10 @@
extern Game* game; extern Game* game;
Bullet::Bullet()
{ Bullet::Bullet(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent){
setRect(0,0,10,50); setPixmap(QPixmap(":/images/missile2.png"));
show();
QTimer* timer = new QTimer(); QTimer* timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(move())); connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(50); timer->start(50);
@ -29,7 +30,7 @@ void Bullet::move(){
} }
} }
this->setPos(x(), y()-10); this->setPos(x(), y()-10);
if(pos().y() + rect().height() < 0) { if(pos().y() < 0) {
scene()->removeItem(this); scene()->removeItem(this);
delete this; delete this;
qDebug() << "bullet deleted"; qDebug() << "bullet deleted";

6
bullet.h

@ -1,14 +1,14 @@
#ifndef BULLET_H #ifndef BULLET_H
#define BULLET_H #define BULLET_H
#include <QGraphicsPixmapItem>
#include <QGraphicsRectItem> #include <QGraphicsRectItem>
#include <QObject> #include <QObject>
class Bullet : public QObject, public QGraphicsRectItem { class Bullet : public QObject, public QGraphicsPixmapItem {
Q_OBJECT Q_OBJECT
public: public:
Bullet(); Bullet(QGraphicsItem *parent=0);
public slots: public slots:
void move(); void move();
}; };

8
enemy.cpp

@ -7,11 +7,13 @@
extern Game* game; extern Game* game;
Enemy::Enemy() Enemy::Enemy(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent)
{ {
int random_number = rand() % 700; int random_number = rand() % 700;
setPos(random_number,0); setPos(random_number,0);
setRect(0,0,100,100); //setRect(0,0,100,100);
setPixmap(QPixmap(":/images/enemy.png"));
QTimer* timer = new QTimer(); QTimer* timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(move())); connect(timer, SIGNAL(timeout()), this, SLOT(move()));
timer->start(50); timer->start(50);
@ -25,7 +27,7 @@ void Enemy::move()
game->health->decrease(); game->health->decrease();
scene()->removeItem(this); scene()->removeItem(this);
delete this; delete this;
qDebug() << "bullet deleted"; //qDebug() << "bullet deleted";
} }
} }

6
enemy.h

@ -1,13 +1,13 @@
#ifndef ENEMY_H #ifndef ENEMY_H
#define ENEMY_H #define ENEMY_H
#include <QGraphicsRectItem> #include <QGraphicsPixmapItem>
#include <QObject> #include <QObject>
class Enemy : public QObject, public QGraphicsRectItem { class Enemy : public QObject, public QGraphicsPixmapItem {
Q_OBJECT Q_OBJECT
public: public:
Enemy(); Enemy(QGraphicsItem *parent=0);
public slots: public slots:
void move(); void move();
}; };

BIN
enemy.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
enemy.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
enemy.xcf

Binary file not shown.

25
game.cpp

@ -10,30 +10,37 @@
Game::Game(QWidget *parent) Game::Game(QWidget *parent)
: QGraphicsView{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->setFlag(QGraphicsItem::ItemIsFocusable);
player->setFocus(); player->setFocus();
QGraphicsScene* scene = new QGraphicsScene();
scene->addItem(player); scene->addItem(player);
score = new Score(); score = new Score();
scene->addItem(score); scene->addItem(score);
health = new Health(); health = new Health();
health->setPos(health->x(), health->y()+25); health->setPos(health->x(), health->y()+25);
scene->addItem(health); scene->addItem(health);
//scene->addWidget(this); //scene->addWidget(this);
this->setScene(scene);
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setFixedSize(800,600);
//view->show(); //view->show();
//view->setFixedSize(800,600); //view->setFixedSize(800,600);
scene->setSceneRect(0,0,800,600);
player->setPos(this->width()/2, this->height() - player->rect().height());
// spawn enemies // spawn enemies
QTimer* timer = new QTimer(); QTimer* timer = new QTimer();
QObject::connect(timer, SIGNAL(timeout()), player, SLOT(spawn())); QObject::connect(timer, SIGNAL(timeout()), player, SLOT(spawn()));
timer->start(2000); timer->start(2000);
show();
} }

BIN
missile.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 896 B

BIN
missile2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

BIN
player.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
player.xcf

Binary file not shown.

7
res.qrc

@ -2,4 +2,11 @@
<qresource prefix="/sounds"> <qresource prefix="/sounds">
<file>laser-cannon-shot.wav</file> <file>laser-cannon-shot.wav</file>
</qresource> </qresource>
<qresource prefix="/images">
<file>missile.png</file>
<file>player.png</file>
<file>bg.png</file>
<file>missile2.png</file>
<file>enemy.png</file>
</qresource>
</RCC> </RCC>

6
res.qrc.txt

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