mirror of
https://github.com/lov3b/Pong.git
synced 2025-01-18 20:50:12 +01:00
add support for endscreen aswell
This commit is contained in:
parent
62e7eb1e93
commit
18ce5670e4
@ -35,7 +35,7 @@ add_executable(Pong src/main.cpp
|
|||||||
src/VisibleObjects/Score.h
|
src/VisibleObjects/Score.h
|
||||||
src/TextScreen.h
|
src/TextScreen.h
|
||||||
src/defaultfont.h
|
src/defaultfont.h
|
||||||
src/StartScreen.h)
|
src/OptionScreen.h)
|
||||||
|
|
||||||
# Now link the libraries to the target
|
# Now link the libraries to the target
|
||||||
target_link_libraries(Pong ${SDL2_LIBRARIES} ${SDL2_GFX_LIBRARY} ${SDL2_TTF_LIBRARY})
|
target_link_libraries(Pong ${SDL2_LIBRARIES} ${SDL2_GFX_LIBRARY} ${SDL2_TTF_LIBRARY})
|
||||||
|
23
src/Game.h
23
src/Game.h
@ -10,7 +10,7 @@
|
|||||||
#include "VisibleObjects/PlayerPaddle.h"
|
#include "VisibleObjects/PlayerPaddle.h"
|
||||||
#include "VisibleObjects/Score.h"
|
#include "VisibleObjects/Score.h"
|
||||||
#include "TextScreen.h"
|
#include "TextScreen.h"
|
||||||
#include "StartScreen.h"
|
#include "OptionScreen.h"
|
||||||
|
|
||||||
enum class GameState {
|
enum class GameState {
|
||||||
START_SCREEN, GAME, END_SCREEN
|
START_SCREEN, GAME, END_SCREEN
|
||||||
@ -21,8 +21,7 @@ private:
|
|||||||
Ball *ball;
|
Ball *ball;
|
||||||
Score *score;
|
Score *score;
|
||||||
PlayerPaddle *leftPaddle, *rightPaddle;
|
PlayerPaddle *leftPaddle, *rightPaddle;
|
||||||
StartScreen *startScreen;
|
OptionScreen *startScreen, *endScreen;
|
||||||
TextScreen *endScreen;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
GameState gameState;
|
GameState gameState;
|
||||||
@ -39,8 +38,8 @@ public:
|
|||||||
};
|
};
|
||||||
score = new Score(5, &this->screenSize, func);
|
score = new Score(5, &this->screenSize, func);
|
||||||
ball = new Ball(&this->screenSize, leftPaddle, rightPaddle, score);
|
ball = new Ball(&this->screenSize, leftPaddle, rightPaddle, score);
|
||||||
startScreen = new StartScreen(&this->screenSize, 4);
|
startScreen = new OptionScreen("Welcome to Pong!\nPress any key to get started...", &this->screenSize, 4);
|
||||||
endScreen = new TextScreen("", &this->screenSize);
|
endScreen = nullptr;
|
||||||
gameState = GameState::START_SCREEN;
|
gameState = GameState::START_SCREEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,8 +77,10 @@ public:
|
|||||||
switch (gameState) {
|
switch (gameState) {
|
||||||
case GameState::START_SCREEN:
|
case GameState::START_SCREEN:
|
||||||
startScreen->update();
|
startScreen->update();
|
||||||
if (startScreen->isDone())
|
if (startScreen->isDone()) {
|
||||||
gameState = GameState::GAME;
|
gameState = GameState::GAME;
|
||||||
|
delete startScreen; // We will never get back to this state
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case GameState::GAME:
|
case GameState::GAME:
|
||||||
ball->update();
|
ball->update();
|
||||||
@ -88,6 +89,16 @@ public:
|
|||||||
score->update();
|
score->update();
|
||||||
break;
|
break;
|
||||||
case GameState::END_SCREEN:
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ int_least64_t getCurrentEpochTimeMillis() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class StartScreen : public TextScreen {
|
class OptionScreen : public TextScreen {
|
||||||
private:
|
private:
|
||||||
bool hasStartedCounting_ = false;
|
bool hasStartedCounting_ = false;
|
||||||
int_least64_t nextMsEpoch = 0;
|
int_least64_t nextMsEpoch = 0;
|
||||||
@ -32,8 +32,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StartScreen(SDL_Point *screenSize, int seconds)
|
OptionScreen(const std::string &text, SDL_Point *screenSize, int seconds)
|
||||||
: TextScreen("Welcome to Pong!\nPress any key to get started...", screenSize), stepsToDo(seconds) {
|
: TextScreen(text, screenSize), stepsToDo(seconds) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void update() override {
|
void update() override {
|
Loading…
x
Reference in New Issue
Block a user