This commit is contained in:
Love 2024-01-29 12:30:00 +01:00
parent b002cb62e5
commit 2a29f01fc2
5 changed files with 30 additions and 60 deletions

View File

@ -19,29 +19,24 @@ enum class GameState {
class Game : public SdlWrapper {
private:
Ball *ball;
Score *score;
PlayerPaddle *leftPaddle, *rightPaddle;
OptionScreen *startScreen, *endScreen;
Ball *ball;
protected:
GameState gameState;
public:
explicit Game(SDL_Point screenSize) : SdlWrapper("Pong", screenSize, 60) {
leftPaddle = new PlayerPaddle(&this->screenSize, Side::LEFT);
rightPaddle = new PlayerPaddle(&this->screenSize, Side::RIGHT);
auto func = [this](Side side) {
const char *player = side == Side::LEFT ? "one" : "two";
std::cout << "Player " << player << " won" << std::endl;
this->running = false;
};
score = new Score(&this->screenSize, 5, func);
ball = new Ball(&this->screenSize, leftPaddle, rightPaddle, score);
startScreen = new OptionScreen("Welcome to Pong!\nPress any key to get started...", &this->screenSize, 4);
endScreen = nullptr;
gameState = GameState::START_SCREEN;
explicit Game(SDL_Point screenSize) : SdlWrapper("Pong", screenSize, 60),
leftPaddle(new PlayerPaddle(&this->screenSize, Side::LEFT)),
rightPaddle(new PlayerPaddle(&this->screenSize, Side::RIGHT)),
score(new Score(&this->screenSize, 5)),
ball(new Ball(&this->screenSize, leftPaddle, rightPaddle, score)),
startScreen(
new OptionScreen("Welcome to Pong!\nPress any key to get started...",
&this->screenSize, 4)), endScreen(nullptr),
gameState(GameState::START_SCREEN) {
}
~Game() override {
@ -56,7 +51,6 @@ public:
SDL_SetRenderDrawColor(renderer, 128, 0, 128, 0);
SDL_RenderClear(renderer);
switch (gameState) {
case GameState::START_SCREEN:
startScreen->draw(renderer);
@ -93,8 +87,9 @@ public:
if (score->sideWon().has_value()) {
const char *player = score->sideWon().value() == Side::LEFT ? "left" : "right";
std::stringstream ss;
ss << "The " << player << " player won with " << std::to_string(score->leftScore) << " - " << std::to_string(score->rightScore)
<< "\nWould you like to play again?" << "\nIf so, press any button...";
ss << "The " << player << " player won with " << std::to_string(score->leftScore) << " - "
<< std::to_string(score->rightScore) << "\nWould you like to play again?"
<< "\nIf so, press any button...";
score->resetScore();
endScreen = new OptionScreen(ss.str(), &screenSize, 4);
gameState = GameState::END_SCREEN;

View File

@ -14,18 +14,17 @@
class Ball {
private:
static const uint8_t RADIUS = 15;
const SDL_Point *screen;
const SDL_Point *const screen;
Sint16 x, y;
Vec2d *vec2d;
static const uint32_t color = 0xCD5C5CFF;
const PlayerPaddle *leftPaddle, *rightPaddle;
Score *score;
const PlayerPaddle *const leftPaddle, *const rightPaddle;
Score *const score;
public:
explicit Ball(const SDL_Point *screen, const PlayerPaddle *leftPaddle, const PlayerPaddle *rightPaddle,
Score *score) :
score(score), screen(screen), leftPaddle(leftPaddle), rightPaddle(rightPaddle), x(screen->x / 2),
y(screen->y / 2), vec2d(new Vec2d(6)) {
Score *score) : score(score), screen(screen), leftPaddle(leftPaddle), rightPaddle(rightPaddle),
x(screen->x / 2), y(screen->y / 2), vec2d(new Vec2d(6)) {
}
void resetPosition() {
@ -76,15 +75,11 @@ private:
[[nodiscard]] std::optional<Side> collidedPaddle() const {
// Right paddle
if (x + RADIUS >= rightPaddle->x &&
y >= rightPaddle->y &&
y <= rightPaddle->y + rightPaddle->h) {
if (x + RADIUS >= rightPaddle->x && y >= rightPaddle->y && y <= rightPaddle->y + rightPaddle->h) {
return Side::RIGHT;
}
// Left paddle
if (x - RADIUS <= leftPaddle->x + leftPaddle->w &&
y >= leftPaddle->y &&
y <= leftPaddle->y + leftPaddle->h) {
if (x - RADIUS <= leftPaddle->x + leftPaddle->w && y >= leftPaddle->y && y <= leftPaddle->y + leftPaddle->h) {
return Side::LEFT;
}
return std::nullopt;

View File

@ -12,18 +12,15 @@ class PlayerPaddle : public SDL_Rect {
private:
static const int MOVE_PER_TICK = 5;
const SDL_Point *screen;
bool movingUp, movingDown;
bool movingUp = false, movingDown = false;
uint8_t color[4]{};
public:
PlayerPaddle(const SDL_Point *screen, const Side side) : SDL_Rect() {
PlayerPaddle(const SDL_Point *screen, const Side side) : SDL_Rect() , screen(screen){
w = 20;
h = 80;
x = side == Side::LEFT ? 0 : screen->x - w;
y = (screen->y - h) / 2;
movingUp = false;
movingDown = false;
this->screen = screen;
color[0] = 255;
color[1] = 234;

View File

@ -18,7 +18,6 @@
class Score : public TextScreen {
private:
const uint8_t MAX_SCORE;
const std::function<void(Side)> whenWon;
std::optional<Side> sideWon_;
public:
@ -29,8 +28,8 @@ public:
}
public:
explicit Score(SDL_Point *screenSize, uint8_t max_score, const std::function<void(Side)> &whenWon) : MAX_SCORE(
max_score), whenWon(whenWon), leftScore(0), rightScore(0), TextScreen("", screenSize, std::make_optional(
explicit Score(SDL_Point *screenSize, uint8_t max_score) : MAX_SCORE(max_score), leftScore(0), rightScore(0),
TextScreen("", screenSize, std::make_optional(
SDL_Point{screenSize->x / 2 - 50, 10})) {
}

View File

@ -29,7 +29,6 @@ private:
const SDL_Color shadowColor = {243, 156, 18, 100};
const int shadowOffset = 3;
protected:
std::vector<std::string> lines;
bool hasUpdated;
@ -46,7 +45,7 @@ public:
std::cerr << "Font path is not set for this platform (null)" << std::endl;
exit(-1);
}
this->font = TTF_OpenFont(defaultFontPath, 42);
font = TTF_OpenFont(defaultFontPath, 42);
if (font == nullptr) {
std::cerr << "Failed to load font: " << TTF_GetError() << std::endl;
exit(-1);
@ -86,7 +85,8 @@ private:
public:
~TextScreen() {
if (font)
// TTF_CLoseFont & SDL_FreeSurface are null-safe
TTF_CloseFont(font);
for (auto *surface: surfaces)
SDL_FreeSurface(surface);
@ -118,22 +118,6 @@ public:
initPositions(replaceText);
}
void replaceCharAtIndex(char c, int line, int index) {
if (lines.size() <= line) {
if (lines[line].length() <= index) {
std::cerr << "string lines is of length " << lines.size() << ", but line index is " << index
<< std::endl;
return;
}
}
if (lines[line].length() <= index) {
std::cerr << "text string is of length " << lines[line].length() << ", but index is " << index << std::endl;
return;
}
hasUpdated = false;
lines[line][index] = c;
}
virtual void update() {
if (hasUpdated)
return;
@ -163,7 +147,7 @@ public:
}
private:
static std::vector<std::string> splitString(const std::string &string, const char delim) {
static std::vector<std::string> splitString(const std::string &string, const char &delim) {
int size = 0;
for (char c: string)
if (c == delim) size++;