Hello everyone,
I have made small computer game using QT in CLion.
At beginning I show main menu with four rect items created as buttons, I connect it to some slots f.e start() - slot which start game, showHelp() - showing help informations, showScores() - top scores and the last one is quit connected to close() slot.
Let me explain how my code works - I running for first time application then all items in the main menu working. When I click the start button it bring me to start method and all code from this method working as I wish.
If the player health is equal 0 or lower program showing gameOverWindow() and in this window have two buttons. One is playAgain and this button restarting game, next one moving to main menu - this menu which are displayed at start of my program.
The problem that arises is when I click play again button it successful restart game, but sometimes it crashing my application at showGameOverWindow or after when I back to mainMenuWindow with those error information:
pure virtual method called terminate called without an active exception
but not always, sometimes it's this exit code:
Process finished with exit code -1073741819 (0xC0000005)
When it doesn't crash after that cases program will always crash after clicking any button in mainMenu without play game button.
Any help would be appreciated
Source code:
Code which checking health is under or equal 0
//some useless code before
if (game->health->getHealth() <= 0){
int score = game->score->getScore();
game->scene->clear();
game->ShowGameOverWindow(score);
}
//some useless code before
if (game->health->getHealth() <= 0){
int score = game->score->getScore();
game->scene->clear();
game->ShowGameOverWindow(score);
}
To copy to clipboard, switch view to plain text mode
ShowGameOverWindow
void Game::ShowGameOverWindow(int score){
setBackgroundBrush
(QImage("../Sources/Pictures/gameover.png"));
float height = window()->height();
float width = window()->width();
drawPanel(0,0,860,600,Qt::black,0.35);
drawPanel(width/4+30,height/4,400,400,Qt::lightGray,0.75);
backMenuButton
= new Button
(QString("../Sources/Pictures/Menu/ok-inactive.png"),
QString("../Sources/Pictures/Menu/ok-active.png"));
int qxPos = width-320;
int qyPos = height-110;
backMenuButton->setPos(qxPos, qyPos);
connect(backMenuButton, SIGNAL(clicked()), this, SLOT(displayMainMenu()));
scene->addItem(backMenuButton);
playAgainButton
= new Button
(QString("../Sources/Pictures/Menu/again-inactive.png"),
QString("../Sources/Pictures/Menu/again-active.png"));
int pxPos = width/3-30;
int pyPos = height-110;
playAgainButton->setPos(pxPos, pyPos);
playAgainButton->setSize(200,51);
connect(playAgainButton,SIGNAL(clicked()), this, SLOT(start()));
scene->addItem(playAgainButton);
}
void Game::ShowGameOverWindow(int score){
setBackgroundBrush(QImage("../Sources/Pictures/gameover.png"));
float height = window()->height();
float width = window()->width();
drawPanel(0,0,860,600,Qt::black,0.35);
drawPanel(width/4+30,height/4,400,400,Qt::lightGray,0.75);
backMenuButton = new Button(QString("../Sources/Pictures/Menu/ok-inactive.png"),
QString("../Sources/Pictures/Menu/ok-active.png"));
int qxPos = width-320;
int qyPos = height-110;
backMenuButton->setPos(qxPos, qyPos);
connect(backMenuButton, SIGNAL(clicked()), this, SLOT(displayMainMenu()));
scene->addItem(backMenuButton);
playAgainButton = new Button(QString("../Sources/Pictures/Menu/again-inactive.png"),
QString("../Sources/Pictures/Menu/again-active.png"));
int pxPos = width/3-30;
int pyPos = height-110;
playAgainButton->setPos(pxPos, pyPos);
playAgainButton->setSize(200,51);
connect(playAgainButton,SIGNAL(clicked()), this, SLOT(start()));
scene->addItem(playAgainButton);
}
To copy to clipboard, switch view to plain text mode
start game slot
void Game::start() {
scene->clear();
scene->setSceneRect(0,0,800,600);
setFixedSize(800,600);
setBackgroundBrush
(QImage("../Sources/Pictures/gameBackground.png"));
player = new Player();
player->setFocus();
player->resetPos();
scene->addItem(player);
score = new Score();
score->resetScore();
score->setPos(score->x()+600, score->y()+9);
scene->addItem(score);
health = new Health();
health->resetHealth();
health->setPos(health->x()+380, health->y()+9);
scene->addItem(health);
if(!mainTimer->isActive()) {
connect(mainTimer, SIGNAL(timeout()), this, SLOT(mainLoop()));
mainTimer->start(0);
}
counting(1000);
auto * timerHurdle
= new QTimer();
QObject::connect(timerHurdle,
SIGNAL(timeout
()),player,
SLOT(spawnHurdle
()));
timerHurdle->start(2000);
auto * timerHeart
= new QTimer();
QObject::connect(timerHeart,
SIGNAL(timeout
()),player,
SLOT(spawnHeart
()));
timerHeart->start(10000);
}
void Game::start() {
scene->clear();
scene->setSceneRect(0,0,800,600);
setFixedSize(800,600);
setBackgroundBrush(QImage("../Sources/Pictures/gameBackground.png"));
player = new Player();
player->setFlag(QGraphicsItem::ItemIsFocusable);
player->setFocus();
player->resetPos();
scene->addItem(player);
score = new Score();
score->resetScore();
score->setPos(score->x()+600, score->y()+9);
scene->addItem(score);
health = new Health();
health->resetHealth();
health->setPos(health->x()+380, health->y()+9);
scene->addItem(health);
if(!mainTimer->isActive()) {
connect(mainTimer, SIGNAL(timeout()), this, SLOT(mainLoop()));
mainTimer->start(0);
}
counting(1000);
auto * timerHurdle = new QTimer();
QObject::connect(timerHurdle,SIGNAL(timeout()),player,SLOT(spawnHurdle()));
timerHurdle->start(2000);
auto * timerHeart = new QTimer();
QObject::connect(timerHeart,SIGNAL(timeout()),player,SLOT(spawnHeart()));
timerHeart->start(10000);
}
To copy to clipboard, switch view to plain text mode
Display main menu
void Game::displayMainMenu() {
scene->clear();
scene->setSceneRect(0,0,1030,768);
setFixedSize(1030,768);
setBackgroundBrush
(QImage("../Sources/Pictures/Menu/background.png"));
// buttons and properties
playButton
= new Button
(QString("../Sources/Pictures/Menu/start-inactive.png"),
QString("../Sources/Pictures/Menu/start-active.png"));
int bxPos = playButton->boundingRect().width()/8 + 3;
int byPos = 380;
playButton->setPos(bxPos, byPos);
connect(playButton,SIGNAL(clicked()), this, SLOT(start()));
scene->addItem(playButton);
scoreButton
= new Button
(QString("../Sources/Pictures/Menu/scores-inactive.png"),
QString("../Sources/Pictures/Menu/scores-active.png"));
int sxPos = scoreButton->boundingRect().width()/8 + 3;
int syPos = 450;
scoreButton->setPos(sxPos, syPos);
connect(scoreButton,SIGNAL(clicked()), this, SLOT(showScores()));
scene->addItem(scoreButton);
helpButton
= new Button
(QString("../Sources/Pictures/Menu/help-inactive.png"),
QString("../Sources/Pictures/Menu/help-active.png"));
int hxPos = helpButton->boundingRect().width()/8 + 3;
int hyPos = 520;
helpButton->setPos(hxPos, hyPos);
connect(helpButton,SIGNAL(clicked()), this, SLOT(showHelp()));
scene->addItem(helpButton);
quitButton
= new Button
(QString("../Sources/Pictures/Menu/quit-inactive.png"),
QString("../Sources/Pictures/Menu/quit-active.png"));
int qxPos = quitButton->boundingRect().width()/8 + 3;
int qyPos = 610;
quitButton->setPos(qxPos, qyPos);
connect(quitButton,SIGNAL(clicked()), this, SLOT(close()));
scene->addItem(quitButton);
backButton
= new Button
(QString("../Sources/Pictures/Menu/back-inactive.png"),
QString("../Sources/Pictures/Menu/back-active.png"));
int backxPos = scene->width()/2 - 40;
int backyPos = 610;
backButton->setPos(backxPos, backyPos);
connect(backButton,SIGNAL(clicked()), this, SLOT(displayMainMenu()));
scene->addItem(backButton);
parchmentImage
= new QLabel();
QPixmap img
("../Sources/Pictures/Menu/paper.png");
parchmentImage->setPixmap(img);
double x = img.width();
double y = img.height();
parchmentImage->setGeometry(300,250,x,y);
scene->addWidget(parchmentImage);
info = new TextInformation();
info->setPosition(300,270);
scene->addItem(info);
if(backButton->isVisible()) { scene->removeItem(backButton); }
if(info->isVisible()) { scene->removeItem(info); }
info->setProperties(Qt::black,"arial",16,300,270);
parchmentImage->setHidden(true);
playButton->setEnabled(true);
scoreButton->setEnabled(true);
helpButton->setEnabled(true);
quitButton->setEnabled(true);
}
void Game::displayMainMenu() {
scene->clear();
scene->setSceneRect(0,0,1030,768);
setFixedSize(1030,768);
setBackgroundBrush(QImage("../Sources/Pictures/Menu/background.png"));
// buttons and properties
playButton = new Button(QString("../Sources/Pictures/Menu/start-inactive.png"),
QString("../Sources/Pictures/Menu/start-active.png"));
int bxPos = playButton->boundingRect().width()/8 + 3;
int byPos = 380;
playButton->setPos(bxPos, byPos);
connect(playButton,SIGNAL(clicked()), this, SLOT(start()));
scene->addItem(playButton);
scoreButton = new Button(QString("../Sources/Pictures/Menu/scores-inactive.png"),
QString("../Sources/Pictures/Menu/scores-active.png"));
int sxPos = scoreButton->boundingRect().width()/8 + 3;
int syPos = 450;
scoreButton->setPos(sxPos, syPos);
connect(scoreButton,SIGNAL(clicked()), this, SLOT(showScores()));
scene->addItem(scoreButton);
helpButton = new Button(QString("../Sources/Pictures/Menu/help-inactive.png"),
QString("../Sources/Pictures/Menu/help-active.png"));
int hxPos = helpButton->boundingRect().width()/8 + 3;
int hyPos = 520;
helpButton->setPos(hxPos, hyPos);
connect(helpButton,SIGNAL(clicked()), this, SLOT(showHelp()));
scene->addItem(helpButton);
quitButton = new Button(QString("../Sources/Pictures/Menu/quit-inactive.png"),
QString("../Sources/Pictures/Menu/quit-active.png"));
int qxPos = quitButton->boundingRect().width()/8 + 3;
int qyPos = 610;
quitButton->setPos(qxPos, qyPos);
connect(quitButton,SIGNAL(clicked()), this, SLOT(close()));
scene->addItem(quitButton);
backButton = new Button(QString("../Sources/Pictures/Menu/back-inactive.png"),
QString("../Sources/Pictures/Menu/back-active.png"));
int backxPos = scene->width()/2 - 40;
int backyPos = 610;
backButton->setPos(backxPos, backyPos);
connect(backButton,SIGNAL(clicked()), this, SLOT(displayMainMenu()));
scene->addItem(backButton);
parchmentImage = new QLabel();
QPixmap img("../Sources/Pictures/Menu/paper.png");
parchmentImage->setPixmap(img);
double x = img.width();
double y = img.height();
parchmentImage->setGeometry(300,250,x,y);
scene->addWidget(parchmentImage);
info = new TextInformation();
info->setPosition(300,270);
scene->addItem(info);
if(backButton->isVisible()) { scene->removeItem(backButton); }
if(info->isVisible()) { scene->removeItem(info); }
info->setProperties(Qt::black,"arial",16,300,270);
parchmentImage->setHidden(true);
playButton->setEnabled(true);
scoreButton->setEnabled(true);
helpButton->setEnabled(true);
quitButton->setEnabled(true);
}
To copy to clipboard, switch view to plain text mode
If the problem is too hard to imagine I can make short video to show you how I really looks or even paste more source code.
Bookmarks