2d - How to exit a "game" in Qt when health is 0 -
i'm quite new qt, got start experimenting making 2d games. i've got extremely rough , simple game started, have issue. whenever health gets 0, game doesn't end. want know how end game , put exit command before making "game over" screen. code below, , can grasp, assume qapplication::quit() goes in game.cpp file. doing taking health integer health.cpp , health.h , putting in game.cpp. appreciated. here code feel answers lie in, if more info needed, ask.
game.h
#ifndef game_h #define game_h #include <qgraphicsview> #include <qwidget> #include <qgraphicsscene> #include "player.h" #include "score.h" #include "health.h" #include "level.h" #include "main.h" class game: public qgraphicsview{ public: game(qwidget * parent=0); qgraphicsscene * scene; player * player; score * score; health * health; level * level; main * close; int end(); }; #endif // game_h game.cpp
#include "game.h" #include <qtimer> #include <qgraphicstextitem> #include <qfont> #include "enemy.h" #include <qmediaplayer> #include <qbrush> #include <qimage> #include <qapplication> game::game(qwidget *parent){ // create scene scene = new qgraphicsscene(); scene->setscenerect(0,0,800,600); // make scene 800x600 instead of infinity infinity (default) setbackgroundbrush(qbrush(qimage(":/images/bg.png"))); // make newly created scene scene visualize (since game qgraphicsview widget, // can used visualize scenes) setscene(scene); sethorizontalscrollbarpolicy(qt::scrollbaralwaysoff); setverticalscrollbarpolicy(qt::scrollbaralwaysoff); setfixedsize(800,600); // create player player = new player(); player->setpos(400,500); // todo generalize in middle bottom of screen // make player focusable , set current focus player->setflag(qgraphicsitem::itemisfocusable); player->setfocus(); // add player scene scene->additem(player); // create score/health score = new score(); scene->additem(score); health = new health(); health->setpos(health->x(),health->y()+25); scene->additem(health); level = new level(); scene->additem(level);bull level->setpos(level->x(),level->y()+50); // spawn enemies qtimer * timer = new qtimer(); qobject::connect(timer,signal(timeout()),player,slot(spawn())); timer->start(2000); // play background music qmediaplayer * music = new qmediaplayer(); music->setmedia(qurl("qrc:/sounds/bgsound.mp3")); music->play(); show(); } int game::end(){ if (health == 0){ qapplication::quit(); } return 0; } health.h
#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 health.cpp
#include "health.h" #include <qfont> #include <qapplication> health::health(qgraphicsitem *parent): qgraphicstextitem(parent){ // initialize score 0 health = 3; // draw text setplaintext(qstring("health: ") + qstring::number(health)); // health: 3 setdefaulttextcolor(qt::red); setfont(qfont("times",16)); } void health::decrease(){ health--; setplaintext(qstring("health: ") + qstring::number(health)); // health: 2 } int health::gethealth(){ return health; } main.cpp
#include <qapplication> #include "game.h" #include "main.h" game * game; int main(int argc, char *argv[]){ qapplication a(argc, argv); game = new game(); game->show(); return a.exec(); }
your end() function never called.
the best way achieve want use qt's signal/slot mechanism. amkes easy connect event (signal) action (slot):
- add
q_objectmacrohealth,gameclasses , make sure compilation environment moc's 2 header files - declare in
healthsignalnameddead() - emit signal
health::decrease() - make
game::end()slot,void - connect
health::dead()game::end()
then, game::end() called health reaches zero.
class health: public qgraphicstextitem { q_object public: health(qgraphicsitem * parent=0); void decrease(); int gethealth(); signals: void dead(); private: int health; }; ... class game: public qgraphicsview{ q_object public: ... public slots: void end(); }; ... void health::decrease(){ health--; setplaintext(qstring("health: ") + qstring::number(health)); if ( health == 0 ) emit dead(); } ... game::game(qwidget *parent){ ... connect( health, signal(dead()), this, slot(end()) ); } ... void game::end(){ // no need test health anymore, signal emited when health 0 // stuff before exiting qapplication::quit(); } if end() calls qapplication::quit(), can remove , diectly connect signal qapplication::quit(), that:
connect( health, signal(dead()), qapp, slot(quit()) ); also note testing health == 0 in game::end(), health pointer, and, looking @ code, never 0 (you maybe meant write if ( health->gethealth() == 0 ).
Comments
Post a Comment