Fix of the game over now work

Next: make the turret work
This commit is contained in:
2024-04-30 13:53:12 +02:00
parent 7ec936a7a9
commit 9469bc99cd
7 changed files with 70 additions and 49 deletions

View File

@@ -8,7 +8,8 @@
#include <iostream>
#include <QDebug>
Game::Game(){
Game::Game(Menu* menu) : menu(menu)
{
// Set the user gold and wave number to 0
userGold = 0;
waveNumber = 0;
@@ -180,13 +181,14 @@ void Game::gameOver() {
totalWeight = 0;
targetWeight = 0;
// Show the menu
menu->showMenuGO();
// Delete the game object
deleteLater();
auto* gameOver = new Gameover(this);
connect(gameOver, &Gameover::restartGameSignal, this, &Game::start);
gameOver->setFixedSize(200, 100);
gameOver->show();
}
Game::~Game() {
qDebug() << "Game object deleted";
void Game::resetGame() {
// Recreate the player
player = new Player(1, 0, 10, 10, 1, "../ressources/player.png", 0, 0, gameMap, *this);
gameMap.addItem(player->getGraphics());
}

View File

@@ -13,6 +13,7 @@
#include "Player.h"
#include "Enemy.h"
#include "Menu.h"
#include "Gameover.h"
class Player;
@@ -24,7 +25,7 @@ class Game : public QGraphicsView
{
Q_OBJECT
public:
Game();
Game(Menu* menu);
void start();
Map gameMap;
void updateDisplay();
@@ -35,7 +36,7 @@ public:
void removeEnemy(Enemy* enemy);
Player* player;
void gameOver();
~Game() override;
void resetGame();
private:
QTimer gameTimer;

View File

@@ -4,32 +4,33 @@
#include "Gameover.h"
Gameover::Gameover(QWidget *parent) : QGraphicsScene(parent) {
gameOverLabel = new QGraphicsTextItem("Game Over");
gameOverLabel->setPos(640, 360);
gameOverLabel->setDefaultTextColor(Qt::red);
addItem(gameOverLabel);
Gameover::Gameover(Game* game, QWidget *parent) : QWidget(parent), game(game) {
restartButton = new QPushButton("Restart", this);
quitButton = new QPushButton("Quit", this);
auto* layout = new QVBoxLayout();
auto* restartButton = new QPushButton("Restart", parent);
connect(restartButton, &QPushButton::clicked, this, &Gameover::restartGame);
layout->addWidget(restartButton);
restartButton->setFixedSize(100, 50);
quitButton->setFixedSize(100, 50);
auto* returnButton = new QPushButton("Return to menu", parent);
connect(returnButton, &QPushButton::clicked, this, &Gameover::returnToMenu);
layout->addWidget(returnButton);
connect(restartButton, &QPushButton::clicked, this, &Gameover::onRestartButtonClicked);
connect(quitButton, &QPushButton::clicked, this, &Gameover::onQuitButtonClicked);
auto* widget = new QWidget();
widget->setLayout(layout);
auto *buttonLayout = new QVBoxLayout();
buttonLayout->addWidget(restartButton);
buttonLayout->addWidget(quitButton);
auto* proxy = addWidget(widget);
proxy->setPos(640, 400);
auto* mainLayout = new QGridLayout(this);
mainLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 0, 0);
mainLayout->addLayout(buttonLayout, 1, 1);
mainLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 2, 2);
setLayout(mainLayout);
}
void Gameover::restartGame() {
void Gameover::onRestartButtonClicked() {
game->resetGame();
emit restartGameSignal();
this->close();
}
void Gameover::returnToMenu() {
emit returnToMenuSignal();
void Gameover::onQuitButtonClicked() {
QApplication::quit();
}

View File

@@ -4,29 +4,32 @@
#ifndef POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_GAMEOVER_H
#define POULPES_DE_L_ESPACE_LA_DERNIERE_LIGNE_DE_DEFENSE_GAMEOVER_H
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QGraphicsProxyWidget>
#include <QApplication>
#include <QGridLayout>
#include <QSpacerItem>
#include "Game.h"
class Gameover : public QGraphicsScene
{
class Game;
class Gameover : public QWidget {
Q_OBJECT
public:
Gameover(QWidget* parent = nullptr);
private:
QGraphicsTextItem* gameOverLabel;
explicit Gameover(Game* game, QWidget *parent = nullptr);
private slots:
void restartGame();
void returnToMenu();
void onRestartButtonClicked();
static void onQuitButtonClicked();
private:
Game* game;
QPushButton* restartButton;
QPushButton* quitButton;
signals:
void restartGameSignal();
void returnToMenuSignal();
};

View File

@@ -6,7 +6,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
Menu* menu = new Menu(this);
this->setCentralWidget(menu);
this->setWindowTitle("Poulpes de l'espace: La dernière ligne de défense");
this->resize(1280, 720);
this->resize(1300, 740);
helpMenu = menuBar()->addMenu(tr("&Help"));
QAction* actionHelp = new QAction(tr("&About"), this);

View File

@@ -42,7 +42,7 @@ void Menu::onPlayButtonClicked() {
view->setFixedSize(1280, 720);
view->show();
// Create a new game
game = new Game();
game = new Game(this);
game->start();
// Show the game
@@ -86,16 +86,30 @@ void Menu::showMenu() {
view->hide();
}
this->setVisible(true);
this->raise();
if(this != nullptr) {
qDebug() << this;
this->raise();
}
playButton->show();
rulesButton->show();
leaderboardButton->show();
quitButton->show();
}
void Menu::showMenuGO() {
this->setVisible(true);
this->raise();
void Menu::showMenuAfterGame() {
// Hide the game view
if(view){
view->hide();
}
/*
// Make the menu visible
if(this != nullptr) {
this->setVisible(true);
}
*/
// Show the menu buttons
playButton->show();
rulesButton->show();
leaderboardButton->show();

View File

@@ -23,7 +23,7 @@ class Menu : public QWidget {
Q_OBJECT
public:
Menu(QWidget *parent = nullptr);
void showMenuGO();
void showMenuAfterGame();
public slots:
void showMenu();