Skip to content

Commit

Permalink
loose
Browse files Browse the repository at this point in the history
  • Loading branch information
Augustin-maker committed Apr 6, 2024
1 parent 768d88a commit 4548f2f
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ add_executable(SnakeQT ${RESOURCE_ADDED}
src/screens/snakewindow.cpp
src/screens/gamearea.cpp
src/screens/browsemapscreen.cpp
)
src/screens/endgamescreen.hpp src/screens/endgamescreen.cpp)

target_link_libraries(SnakeQT
Qt5::Core
Expand Down
1 change: 1 addition & 0 deletions src/jeu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ void Jeu::tick() {
} else {
// Game over
snake.clear(); // TODO: lose condition
game_over = -1; // perdu
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/jeu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Jeu {
private:
int score = 0;


public:
Jeu();

Expand Down Expand Up @@ -61,6 +62,7 @@ class Jeu {
void increaseScore(int point) {
score=score+point;
}
int game_over; // 0: not over, 1: win, -1: lose
};

#endif
20 changes: 20 additions & 0 deletions src/screens/endgamescreen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


#include "endgamescreen.hpp"

EndGameScreen::EndGameScreen(int score, QWidget *parent)
: QWidget(parent), scoreLabel(new QLabel(this)) {
// Layout vertical pour organiser les widgets
QVBoxLayout *layout = new QVBoxLayout(this);

// Étiquette de texte pour afficher le score
QLabel *scoreLabel = new QLabel("Score: " + QString::number(score), this);
scoreLabel->setFont(QFont("Arial", 24, QFont::Bold)); // Police en gras et taille 24
scoreLabel->setAlignment(Qt::AlignCenter); // Centrez le texte

// Ajouter l'étiquette de score au layout
layout->addWidget(scoreLabel);

// Définir le layout pour la fenêtre
setLayout(layout);
}
20 changes: 20 additions & 0 deletions src/screens/endgamescreen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


#ifndef SNAKEQT_ENDGAMESCREEN_HPP
#define SNAKEQT_ENDGAMESCREEN_HPP

#include <QWidget>
#include <QLabel>
#include <QVBoxLayout>

class EndGameScreen : public QWidget {
Q_OBJECT

public:
explicit EndGameScreen(int score, QWidget *parent = nullptr);

private:
QLabel *scoreLabel;
};

#endif //SNAKEQT_ENDGAMESCREEN_HPP
18 changes: 18 additions & 0 deletions src/screens/gamescreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ GameScreen::GameScreen(QWidget *parent, const QString &file_info): QWidget(paren
pauseLayout->setSpacing(0);
pauseLayout->addWidget(pauseLabel);
pauseLayout->addWidget(resumeLabel);


}

void GameScreen::resizeEvent(QResizeEvent* event) {
Expand Down Expand Up @@ -83,6 +85,10 @@ void GameScreen::updateScoreLabel() {
scoreLabel->setText("Score: " + QString::number(jeu.getScore()));
}

void GameScreen::endGame() {
emit gameOver(jeu.getScore());
}

/**
* Tick the game
*/
Expand All @@ -91,6 +97,18 @@ void GameScreen::handleTimer() {

updateScoreLabel();

if (jeu.game_over != 0) {
// Game over
if (jeu.game_over == 1) {
// Win
std::cout << "You win!" << std::endl;
} else {
// Lose
std::cout << "You lose!" << std::endl;
emit gameOver(jeu.getScore());
}

}


update();
Expand Down
5 changes: 5 additions & 0 deletions src/screens/gamescreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ class GameScreen final : public QWidget {
private:
QLabel *scoreLabel;
void updateScoreLabel();
void endGame();

signals:
void gameOver(int score);

};
#endif //GAMESCREEN_HPP
20 changes: 20 additions & 0 deletions src/screens/snakewindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ SnakeWindow::SnakeWindow(QWidget *pParent, Qt::WindowFlags flags)
stackedWidget->addWidget(gameScreen);
stackedWidget->setCurrentWidget(gameScreen);
}


});

const auto editorMenu = new QMenu(tr("&Editor"), this);
Expand Down Expand Up @@ -84,13 +86,19 @@ SnakeWindow::SnakeWindow(QWidget *pParent, Qt::WindowFlags flags)

// Set the menu bar
setMenuBar(menuBar);



}

void SnakeWindow::handleStartGameClicked() {
// Initialize and switch to the game screen when the "Start Game" button is clicked
gameScreen = new GameScreen(this);
stackedWidget->addWidget(gameScreen);
stackedWidget->setCurrentWidget(gameScreen);

connect(gameScreen,&GameScreen::gameOver, this, &SnakeWindow::handleGameOver);
setCentralWidget(gameScreen);
}

void SnakeWindow::handleBrowseMapClicked() {
Expand All @@ -106,10 +114,22 @@ void SnakeWindow::handleBrowseMapClicked() {
gameScreen = new GameScreen(this, fileName);
stackedWidget->addWidget(gameScreen);
stackedWidget->setCurrentWidget(gameScreen);

});
}

void SnakeWindow::handleExitClicked() {
// Exit the application when the "Exit" button is clicked
QApplication::quit();
}

void SnakeWindow::handleGameOver(int score) {
// Switch to the end game screen when the game is over
delete gameScreen;
gameScreen = nullptr;

endGameScreen = new EndGameScreen(score, this);
setCentralWidget(endGameScreen);

update();
}
7 changes: 7 additions & 0 deletions src/screens/snakewindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "editorscreen.hpp"
#include "mainmenu.hpp"
#include "gamescreen.hpp"
#include "endgamescreen.hpp"

class SnakeWindow final : public QMainWindow {
Q_OBJECT
Expand All @@ -19,6 +20,7 @@ class SnakeWindow final : public QMainWindow {
BrowseMapScreen *browseMapScreen{};
EditorScreen *editorScreen{};


public:
explicit SnakeWindow(QWidget *pParent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());

Expand All @@ -27,6 +29,8 @@ public slots:

void handleBrowseMapClicked();

void handleGameOver(int score);

static void handleExitClicked();

void toggleFullScreen() {
Expand All @@ -36,6 +40,9 @@ public slots:
showFullScreen();
}
}
private :
EndGameScreen *endGameScreen;

};

#endif

0 comments on commit 4548f2f

Please sign in to comment.