Browse Source

nach Tut 6

master
Georg Spar 2 years ago
parent
commit
69197fbd3c
  1. 5
      CMakeLists.txt
  2. 3
      bullet.cpp
  3. 7
      enemy.cpp
  4. 37
      game.cpp
  5. 24
      game.h
  6. 22
      health.cpp
  7. 15
      health.h
  8. 26
      main.cpp
  9. 22
      score.cpp
  10. 16
      score.h

5
CMakeLists.txt

@ -22,7 +22,10 @@ set(PROJECT_SOURCES
bullet.cpp bullet.cpp
enemy.h enemy.h
enemy.cpp enemy.cpp
game.h
game.cpp
score.h score.cpp
health.h health.cpp
) )
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)

3
bullet.cpp

@ -3,7 +3,9 @@
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QList> #include <QList>
#include "enemy.h" #include "enemy.h"
#include "game.h"
extern Game* game;
Bullet::Bullet() Bullet::Bullet()
{ {
@ -18,6 +20,7 @@ void Bullet::move(){
QList<QGraphicsItem *> colliding_items = collidingItems(); QList<QGraphicsItem *> colliding_items = collidingItems();
for (int i = 0, n = colliding_items.size(); i < n; i++) { for (int i = 0, n = colliding_items.size(); i < n; i++) {
if (typeid(*(colliding_items[i])) == typeid(Enemy)) { if (typeid(*(colliding_items[i])) == typeid(Enemy)) {
game->score->increase();
scene()->removeItem(colliding_items[i]); scene()->removeItem(colliding_items[i]);
scene()->removeItem(this); scene()->removeItem(this);
delete colliding_items[i]; delete colliding_items[i];

7
enemy.cpp

@ -2,6 +2,10 @@
#include <QTimer> #include <QTimer>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <stdlib.h> #include <stdlib.h>
#include "game.h"
#include "health.h"
extern Game* game;
Enemy::Enemy() Enemy::Enemy()
{ {
@ -17,7 +21,8 @@ Enemy::Enemy()
void Enemy::move() void Enemy::move()
{ {
this->setPos(x(), y()+5); this->setPos(x(), y()+5);
if(pos().y() + rect().height() < 0) { if(pos().y() > 600) {
game->health->decrease();
scene()->removeItem(this); scene()->removeItem(this);
delete this; delete this;
qDebug() << "bullet deleted"; qDebug() << "bullet deleted";

37
game.cpp

@ -0,0 +1,37 @@
#include "game.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include "MyRect.h"
#include <QTimer>
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);
}

24
game.h

@ -0,0 +1,24 @@
#ifndef GAME_H
#define GAME_H
#include <QGraphicsView>
#include <QWidget>
#include <QGraphicsScene>
#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

22
health.cpp

@ -0,0 +1,22 @@
#include "health.h"
#include <QFont>
#include <QGraphicsTextItem>
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;
}

15
health.h

@ -0,0 +1,15 @@
#ifndef HEALTH_H
#define HEALTH_H
#include <QGraphicsTextItem>
class Health: public QGraphicsTextItem {
public:
Health(QGraphicsItem* parent=0);
void decrease();
int getHealth();
private:
int health;
};
#endif // HEALTH_H

26
main.cpp

@ -1,31 +1,13 @@
#include <QApplication> #include <QApplication>
#include <QGraphicsView> #include "game.h"
#include <QGraphicsScene>
#include "MyRect.h"
#include <QTimer>
Game* game;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
MyRect* player = new MyRect(); game = new Game();
player->setRect(0,0,100,100); game->show();
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);
return a.exec(); return a.exec();
} }

22
score.cpp

@ -0,0 +1,22 @@
#include "score.h"
#include <QFont>
#include <QGraphicsTextItem>
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;
}

16
score.h

@ -0,0 +1,16 @@
#ifndef SCORE_H
#define SCORE_H
#include <QGraphicsTextItem>
class Score: public QGraphicsTextItem {
public:
Score(QGraphicsItem* parent=0);
void increase();
int getScore();
private:
int score;
};
#endif // SCORE_H
Loading…
Cancel
Save