Dalej coś nie działa ;) Posłucham twonek i wkleje kody plików:
Main.cpp
Game *game;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
game = new Game();
return a.exec();
}
HealthPpayer.h
class HealthPlayer : public QGraphicsTextItem
{
Q_OBJECT
public:
explicit HealthPlayer();
int getHealth() const;
void decrease(int i);
signals:
void GameOver();
public slots:
private:
int m_Health;
};
Usunąłem dziedziczenie public QObject ponieważ QGraphicsTextItem dziedziczy już po tej klasie.
HelathPlayer.cpp
extern Game *game;
HealthPlayer::HealthPlayer()
{
m_Health = 100;
setPlainText(QString("Player: "+QString::number(m_Health)));
setDefaultTextColor(Qt::red);
connect(game->B_health, SIGNAL(GameOver()),game->player,SLOT(game_over()));
}
int HealthPlayer::getHealth() const
{
return m_Health;
}
void HealthPlayer::decrease(int i)
{
m_Health-=i;
if(m_Health<=0)
{
m_Health=0;
emit GameOver();
}
setPlainText(QString("Player: "+QString::number(m_Health)));
}
MyRec.h
class MyRec : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
explicit MyRec(QObject *parent = 0);
signals:
public slots:
void start_shooting();
void game_over();
// QGraphicsItem interface
protected:
void keyPressEvent(QKeyEvent *event) override;
private:
};
MyRec.cpp
MyRec::MyRec(QObject *parent) : QObject(parent)
{
setPixmap(QPixmap(":/graphics/rsz_bury_logo.png"));
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()),this,SLOT(start_shooting()));
timer->start(400); ;
}
void MyRec::keyPressEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key_Right)
{
if(pos().x()+pixmap().width()<1000)
{
setPos(x()+20,y());
}
}
else if (event->key()==Qt::Key_Left)
{
if(pos().x()>0)
{
setPos(x()-20,y());
}
}
else if(event->key()==Qt::Key_Up)
{
if(pos().y()>100)
{
setPos(x(),y()-20);
}
}
else if(event->key()==Qt::Key_Down)
{
if(pos().y()+pixmap().height()<600)
setPos(x(),y()+20);
}
}
void MyRec::start_shooting()
{
Bullet *bullet = new Bullet();
bullet->setPos(x()+50,y());
scene()->addItem(bullet);
}
void MyRec::game_over()
{
scene()->removeItem(this);
delete this;
}