add support for endscreen aswell

This commit is contained in:
Love 2024-01-29 10:46:27 +01:00
parent 62e7eb1e93
commit 18ce5670e4
3 changed files with 21 additions and 10 deletions

View File

@ -35,7 +35,7 @@ add_executable(Pong src/main.cpp
src/VisibleObjects/Score.h
src/TextScreen.h
src/defaultfont.h
src/StartScreen.h)
src/OptionScreen.h)
# Now link the libraries to the target
target_link_libraries(Pong ${SDL2_LIBRARIES} ${SDL2_GFX_LIBRARY} ${SDL2_TTF_LIBRARY})

View File

@ -10,7 +10,7 @@
#include "VisibleObjects/PlayerPaddle.h"
#include "VisibleObjects/Score.h"
#include "TextScreen.h"
#include "StartScreen.h"
#include "OptionScreen.h"
enum class GameState {
START_SCREEN, GAME, END_SCREEN
@ -21,8 +21,7 @@ private:
Ball *ball;
Score *score;
PlayerPaddle *leftPaddle, *rightPaddle;
StartScreen *startScreen;
TextScreen *endScreen;
OptionScreen *startScreen, *endScreen;
protected:
GameState gameState;
@ -39,8 +38,8 @@ public:
};
score = new Score(5, &this->screenSize, func);
ball = new Ball(&this->screenSize, leftPaddle, rightPaddle, score);
startScreen = new StartScreen(&this->screenSize, 4);
endScreen = new TextScreen("", &this->screenSize);
startScreen = new OptionScreen("Welcome to Pong!\nPress any key to get started...", &this->screenSize, 4);
endScreen = nullptr;
gameState = GameState::START_SCREEN;
}
@ -78,8 +77,10 @@ public:
switch (gameState) {
case GameState::START_SCREEN:
startScreen->update();
if (startScreen->isDone())
if (startScreen->isDone()) {
gameState = GameState::GAME;
delete startScreen; // We will never get back to this state
}
break;
case GameState::GAME:
ball->update();
@ -88,6 +89,16 @@ public:
score->update();
break;
case GameState::END_SCREEN:
if (endScreen == nullptr) {
std::stringstream ss;
ss << "Player " << " won" << "\nWould you like to play again?" << "\nIf so, press any button...";
endScreen = new OptionScreen(ss.str(), &screenSize, 4);
}
endScreen->update();
if (endScreen->isDone()) {
gameState = GameState::GAME;
delete endScreen; // The text will not be the same if we get back here. We might as well free it.
}
break;
}
}

View File

@ -16,7 +16,7 @@ int_least64_t getCurrentEpochTimeMillis() {
}
class StartScreen : public TextScreen {
class OptionScreen : public TextScreen {
private:
bool hasStartedCounting_ = false;
int_least64_t nextMsEpoch = 0;
@ -32,8 +32,8 @@ public:
}
public:
StartScreen(SDL_Point *screenSize, int seconds)
: TextScreen("Welcome to Pong!\nPress any key to get started...", screenSize), stepsToDo(seconds) {
OptionScreen(const std::string &text, SDL_Point *screenSize, int seconds)
: TextScreen(text, screenSize), stepsToDo(seconds) {
}
void update() override {