From 69197fbd3ce5a7d95c9ec31243a2123ba990b2df Mon Sep 17 00:00:00 2001 From: Georg Spar Date: Mon, 3 Oct 2022 23:12:34 +0200 Subject: [PATCH] nach Tut 6 --- CMakeLists.txt | 5 ++++- bullet.cpp | 3 +++ enemy.cpp | 7 ++++++- game.cpp | 37 +++++++++++++++++++++++++++++++++++++ game.h | 24 ++++++++++++++++++++++++ health.cpp | 22 ++++++++++++++++++++++ health.h | 15 +++++++++++++++ main.cpp | 26 ++++---------------------- score.cpp | 22 ++++++++++++++++++++++ score.h | 16 ++++++++++++++++ 10 files changed, 153 insertions(+), 24 deletions(-) create mode 100644 game.cpp create mode 100644 game.h create mode 100644 health.cpp create mode 100644 health.h create mode 100644 score.cpp create mode 100644 score.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a74bc8e..72002fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,10 @@ set(PROJECT_SOURCES bullet.cpp enemy.h enemy.cpp - + game.h + game.cpp + score.h score.cpp + health.h health.cpp ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) diff --git a/bullet.cpp b/bullet.cpp index 47f4e6a..06b2e9c 100644 --- a/bullet.cpp +++ b/bullet.cpp @@ -3,7 +3,9 @@ #include #include #include "enemy.h" +#include "game.h" +extern Game* game; Bullet::Bullet() { @@ -18,6 +20,7 @@ void Bullet::move(){ QList colliding_items = collidingItems(); for (int i = 0, n = colliding_items.size(); i < n; i++) { if (typeid(*(colliding_items[i])) == typeid(Enemy)) { + game->score->increase(); scene()->removeItem(colliding_items[i]); scene()->removeItem(this); delete colliding_items[i]; diff --git a/enemy.cpp b/enemy.cpp index f0dd7d1..efc02e1 100644 --- a/enemy.cpp +++ b/enemy.cpp @@ -2,6 +2,10 @@ #include #include #include +#include "game.h" +#include "health.h" + +extern Game* game; Enemy::Enemy() { @@ -17,7 +21,8 @@ Enemy::Enemy() void Enemy::move() { this->setPos(x(), y()+5); - if(pos().y() + rect().height() < 0) { + if(pos().y() > 600) { + game->health->decrease(); scene()->removeItem(this); delete this; qDebug() << "bullet deleted"; diff --git a/game.cpp b/game.cpp new file mode 100644 index 0000000..206e1a2 --- /dev/null +++ b/game.cpp @@ -0,0 +1,37 @@ +#include "game.h" +#include +#include +#include "MyRect.h" +#include + + +Game::Game(QWidget *parent) + : QGraphicsView{parent} +{ + MyRect* player = new MyRect(); + player->setRect(0,0,100,100); + 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); + +} diff --git a/game.h b/game.h new file mode 100644 index 0000000..8862d48 --- /dev/null +++ b/game.h @@ -0,0 +1,24 @@ +#ifndef GAME_H +#define GAME_H + +#include +#include +#include +#include "MyRect.h" +#include "score.h" +#include "health.h" + +class Game : public QGraphicsView +{ +public: + Game(QWidget *parent = nullptr); + QGraphicsScene* scene; + MyRect* player; + Score* score; + Health* health; + +signals: + +}; + +#endif // GAME_H diff --git a/health.cpp b/health.cpp new file mode 100644 index 0000000..0391899 --- /dev/null +++ b/health.cpp @@ -0,0 +1,22 @@ +#include "health.h" +#include +#include + + +Health::Health(QGraphicsItem *parent): QGraphicsTextItem(parent){ + health = 3; + + setPlainText(QString("Health: ") + QString::number(health)); + setDefaultTextColor(Qt::red); + setFont(QFont("times",16)); +} + +void Health::decrease(){ + health--; + setPlainText(QString("Health: ") + QString::number(health)); +} + +int Health::getHealth(){ + return health; +} + diff --git a/health.h b/health.h new file mode 100644 index 0000000..e66747c --- /dev/null +++ b/health.h @@ -0,0 +1,15 @@ +#ifndef HEALTH_H +#define HEALTH_H +#include + +class Health: public QGraphicsTextItem { +public: + Health(QGraphicsItem* parent=0); + void decrease(); + int getHealth(); +private: + int health; + +}; + +#endif // HEALTH_H diff --git a/main.cpp b/main.cpp index 8c84e0e..0a8c446 100644 --- a/main.cpp +++ b/main.cpp @@ -1,31 +1,13 @@ #include -#include -#include -#include "MyRect.h" -#include +#include "game.h" +Game* game; int main(int argc, char *argv[]) { QApplication a(argc, argv); - MyRect* player = new MyRect(); - player->setRect(0,0,100,100); - player->setFlag(QGraphicsItem::ItemIsFocusable); - player->setFocus(); - QGraphicsScene* scene = new QGraphicsScene(); - scene->addItem(player); - - QGraphicsView* view = new QGraphicsView(scene); - - view->show(); - view->setFixedSize(800,600); - scene->setSceneRect(0,0,800,600); - player->setPos(view->width()/2, view->height() - player->rect().height()); - - // spawn enemies - QTimer* timer = new QTimer(); - QObject::connect(timer, SIGNAL(timeout()), player, SLOT(spawn())); - timer->start(2000); + game = new Game(); + game->show(); return a.exec(); } diff --git a/score.cpp b/score.cpp new file mode 100644 index 0000000..a752d01 --- /dev/null +++ b/score.cpp @@ -0,0 +1,22 @@ +#include "score.h" +#include +#include + + +Score::Score(QGraphicsItem *parent): QGraphicsTextItem(parent){ + score = 0; + + setPlainText(QString("Score: ") + QString::number(score)); + setDefaultTextColor(Qt::blue); + setFont(QFont("times",16)); +} + +void Score::increase(){ + score++; + setPlainText(QString("Score: ") + QString::number(score)); +} + +int Score::getScore(){ + return score; +} + diff --git a/score.h b/score.h new file mode 100644 index 0000000..590cafa --- /dev/null +++ b/score.h @@ -0,0 +1,16 @@ +#ifndef SCORE_H +#define SCORE_H + +#include + +class Score: public QGraphicsTextItem { +public: + Score(QGraphicsItem* parent=0); + void increase(); + int getScore(); +private: + int score; + +}; + +#endif // SCORE_H