Skip to content

Commit

Permalink
score
Browse files Browse the repository at this point in the history
  • Loading branch information
Augustin-maker committed Apr 5, 2024
1 parent e8b6fff commit ca02d38
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/jeu.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "jeu.hpp"
#include <iostream>

using namespace std;

Expand Down Expand Up @@ -85,6 +86,9 @@ void Jeu::tick() {
snake.push_front(posTest); // Add the new head

if (posTest == applePos) {

increaseScore(1);
std::cout << "Snake eats the apple!" << std::endl;
// The snake eats the apple, place a new apple
std::uniform_int_distribution<> distr(0, map.getWidth() - 1);
int attempts = 0;
Expand Down
10 changes: 10 additions & 0 deletions src/jeu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Jeu {
std::mt19937 gen;
bool pause = false;

private:
int score = 0;

public:
Jeu();

Expand Down Expand Up @@ -51,6 +54,13 @@ class Jeu {
bool isPaused() const {
return pause;
}

int getScore() const {
return score;
}
void increaseScore(int point) {
score=score+point;
}
};

#endif
2 changes: 2 additions & 0 deletions src/screens/gamearea.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifndef GAMEAREA_HPP
#define GAMEAREA_HPP


#include <QtCore/QtCore>
#include <QtWidgets/QtWidgets>

#include "../map.hpp"
#include "../constants.hpp"
#include <iostream>

class GameArea final : public QWidget {
Q_OBJECT
Expand Down
14 changes: 12 additions & 2 deletions src/screens/gamescreen.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "gamescreen.hpp"

#include <iostream>

/**
* Construct main game window
Expand All @@ -12,7 +12,7 @@ GameScreen::GameScreen(QWidget *parent, const QString &file_info): QWidget(paren

auto *layout = new QVBoxLayout;
auto *mapNameLabel = new QLabel(jeu.getMap().getName() + " by " + jeu.getMap().getAuthor(), this);
auto *scoreLabel = new QLabel("Score: 0", this);
scoreLabel = new QLabel("Score: 0", this);

layout->addWidget(mapNameLabel);
layout->addWidget(gameArea);
Expand Down Expand Up @@ -75,13 +75,23 @@ void GameScreen::keyPressEvent(QKeyEvent *event) {
}
}


update();
}

void GameScreen::updateScoreLabel() {
scoreLabel->setText("Score: " + QString::number(jeu.getScore()));
}

/**
* Tick the game
*/
void GameScreen::handleTimer() {
jeu.tick();

updateScoreLabel();



update();
}
5 changes: 5 additions & 0 deletions src/screens/gamescreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class GameScreen final : public QWidget {

void handleTimer();


protected:
void keyPressEvent(QKeyEvent *) override;

private:
QLabel *scoreLabel;
void updateScoreLabel();
};
#endif //GAMESCREEN_HPP

0 comments on commit ca02d38

Please sign in to comment.