Georg Spar
2 years ago
10 changed files with 153 additions and 24 deletions
@ -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); |
||||||
|
|
||||||
|
} |
@ -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
|
@ -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; |
||||||
|
} |
||||||
|
|
@ -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
|
@ -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(); |
||||||
} |
} |
||||||
|
@ -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; |
||||||
|
} |
||||||
|
|
Loading…
Reference in new issue