aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authorroy <[email protected]>2026-07-08 04:51:35 +0300
committerroy <[email protected]>2026-07-08 04:51:35 +0300
commit5c749ef8005e79411a25fa838d6d645b10f26b45 (patch)
tree00429dbd9b6400e5487896fbfeee3bb467bb8b67 /main.cpp
parent35c90588b348cc0cf7ee7d130dd9eb21139a8d35 (diff)
Reorganized functions
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp252
1 files changed, 126 insertions, 126 deletions
diff --git a/main.cpp b/main.cpp
index 7e18d2b..790b659 100644
--- a/main.cpp
+++ b/main.cpp
@@ -78,22 +78,6 @@ class Snake {
return 0b0000;
}
- mSnakeUnit mPopSnake() {
- mSnakeUnit tr = mSnake.front();
- mCell val(tr.x, tr.y);
-
- mUnoccupy(val);
- mSnake.pop_front();
- return tr;
- }
-
- void mPushSnake(mSnakeUnit input) {
- mCell val(input.x, input.y);
-
- mOccupy(val);
- mSnake.push_back(input);
- }
-
void mOccupy(int x, int y) {
mCell val(x, y);
@@ -172,50 +156,63 @@ class Snake {
return ' ';
}
- void mHandleGameInput(tgu::GameKey input) {
- char PotentialSnakeDirection;
+ void mDie() {
+ std::cout << "Die about it";
+ tgu::ClearTerminal();
+ tgu::MoveCursor(0, 0);
+ exit(0);
+ }
- if (input == tgu::GameKey::W || input == tgu::GameKey::UPARROW)
- PotentialSnakeDirection = 'u';
- else if (input == tgu::GameKey::A || input == tgu::GameKey::LEFTARROW)
- PotentialSnakeDirection = 'l';
- else if (input == tgu::GameKey::S || input == tgu::GameKey::DOWNARROW)
- PotentialSnakeDirection = 'd';
- else if (input == tgu::GameKey::D ||
- input == tgu::GameKey::RIGHTARROW) {
- PotentialSnakeDirection = 'r';
- } else if (input == tgu::GameKey::ESCAPE || input == tgu::GameKey::P) {
- mPaused = true;
- return;
- } else if (input == tgu::GameKey::Q) {
- tgu::ClearTerminal();
- tgu::MoveCursor(0, 0);
- exit(0);
- } else
- return;
+ bool IsInSnake(int x, int y) {
+ for (mSnakeUnit &u : mSnake) {
+ if (u.x == x && u.y == y)
+ return true;
+ }
+ return false;
+ }
- if (PotentialSnakeDirection == mFlipDirection(mSnakeDirection))
- return;
- else
- mSnakeDirection = PotentialSnakeDirection;
+ void mInputLoop() {
+ tgu::GameKey key;
+ while (mGameNotOver) {
+ key = tgu::GetKeyPress();
+ if (mKeyQueue.size() <= 2) {
+ mKeyMutex.lock();
+ mKeyQueue.push(key);
+ mKeyMutex.unlock();
+ }
+ }
}
- void mHandlePausedInput(tgu::GameKey input) {
- if (input == tgu::GameKey::P || input == tgu::GameKey::ESCAPE)
- mPaused = false;
+ bool mHorizontalConnectors(mSnakeUnit a, mSnakeUnit b) {
+ std::string horizontals = "╔╗╚╝◁▷";
+ if (a.y != b.y)
+ return false;
- if (input == tgu::GameKey::Q)
- mGameNotOver = false;
+ if (horizontals.find(a.appearance) != std::string::npos &&
+ horizontals.find(b.appearance) != std::string::npos)
+ return true;
+
+ return false;
}
- void mHandleInput() {
- tgu::GameKey popped = mKeyQueue.front();
- mKeyQueue.pop();
+ void mInitializeApple() {
+ int iPos = rand() % mUnoccupied.size();
+ mCell apple(mUnoccupied[iPos].x, mUnoccupied[iPos].y);
- if (mPaused)
- mHandlePausedInput(popped);
- else
- mHandleGameInput(popped);
+ mOccupy(apple.x, apple.y);
+
+ mApple = {apple.x, apple.y, "■"};
+ }
+
+ void mAddApple() {
+ mApplesAte++;
+ int iPos = rand() % mUnoccupied.size();
+ mCell apple(mUnoccupied[iPos].x, mUnoccupied[iPos].y);
+
+ mUnoccupy(mApple.x, mApple.y);
+ mOccupy(apple.x, apple.y);
+
+ mApple = {mUnoccupied[iPos].x, mUnoccupied[iPos].y, mApple.appearance};
}
std::tuple<int, int> mGetDirectionOffset(char c) {
@@ -234,13 +231,6 @@ class Snake {
return std::tuple<int, int>(0, 0);
}
- void mDie() {
- std::cout << "Die about it";
- tgu::ClearTerminal();
- tgu::MoveCursor(0, 0);
- exit(0);
- }
-
bool mProcessApple(int &PotentialX, int &PotentialY) {
if (mApple.x == PotentialX && mApple.y == PotentialY) {
mAddApple();
@@ -250,21 +240,6 @@ class Snake {
}
}
- void mAdvanceSnake(int &PotentialX, int &PotentialY) {
- mPushSnake({PotentialX, PotentialY, HEAD_ARROWS.at(mSnakeDirection)});
- mSnake[mSnake.size() - 2].appearance = mGetTurningCharacter(
- mSnake[mSnake.size() - 3], mSnake[mSnake.size() - 2],
- mSnake[mSnake.size() - 1]);
- }
-
- bool IsInSnake(int x, int y) {
- for (mSnakeUnit &u : mSnake) {
- if (u.x == x && u.y == y)
- return true;
- }
- return false;
- }
-
bool mCheckDeathTick(int &PotentialX, int &PotentialY) {
if (PotentialX <= -1 || PotentialY <= -1 || PotentialX >= WIDTH ||
PotentialY >= HEIGHT || IsInSnake(PotentialX, PotentialY)) {
@@ -280,6 +255,29 @@ class Snake {
}
}
+ mSnakeUnit mPopSnake() {
+ mSnakeUnit tr = mSnake.front();
+ mCell val(tr.x, tr.y);
+
+ mUnoccupy(val);
+ mSnake.pop_front();
+ return tr;
+ }
+
+ void mPushSnake(mSnakeUnit input) {
+ mCell val(input.x, input.y);
+
+ mOccupy(val);
+ mSnake.push_back(input);
+ }
+
+ void mAdvanceSnake(int &PotentialX, int &PotentialY) {
+ mPushSnake({PotentialX, PotentialY, HEAD_ARROWS.at(mSnakeDirection)});
+ mSnake[mSnake.size() - 2].appearance = mGetTurningCharacter(
+ mSnake[mSnake.size() - 3], mSnake[mSnake.size() - 2],
+ mSnake[mSnake.size() - 1]);
+ }
+
void mSnakeStep() {
std::tuple<int, int> Offset = mGetDirectionOffset(mSnakeDirection);
@@ -304,6 +302,52 @@ class Snake {
}
}
+ void mHandleGameInput(tgu::GameKey input) {
+ char PotentialSnakeDirection;
+
+ if (input == tgu::GameKey::W || input == tgu::GameKey::UPARROW)
+ PotentialSnakeDirection = 'u';
+ else if (input == tgu::GameKey::A || input == tgu::GameKey::LEFTARROW)
+ PotentialSnakeDirection = 'l';
+ else if (input == tgu::GameKey::S || input == tgu::GameKey::DOWNARROW)
+ PotentialSnakeDirection = 'd';
+ else if (input == tgu::GameKey::D ||
+ input == tgu::GameKey::RIGHTARROW) {
+ PotentialSnakeDirection = 'r';
+ } else if (input == tgu::GameKey::ESCAPE || input == tgu::GameKey::P) {
+ mPaused = true;
+ return;
+ } else if (input == tgu::GameKey::Q) {
+ tgu::ClearTerminal();
+ tgu::MoveCursor(0, 0);
+ exit(0);
+ } else
+ return;
+
+ if (PotentialSnakeDirection == mFlipDirection(mSnakeDirection))
+ return;
+ else
+ mSnakeDirection = PotentialSnakeDirection;
+ }
+
+ void mHandlePausedInput(tgu::GameKey input) {
+ if (input == tgu::GameKey::P || input == tgu::GameKey::ESCAPE)
+ mPaused = false;
+
+ if (input == tgu::GameKey::Q)
+ mGameNotOver = false;
+ }
+
+ void mHandleInput() {
+ tgu::GameKey popped = mKeyQueue.front();
+ mKeyQueue.pop();
+
+ if (mPaused)
+ mHandlePausedInput(popped);
+ else
+ mHandleGameInput(popped);
+ }
+
void mGameTick() {
mKeyMutex.lock();
if (mKeyQueue.size() != 0) {
@@ -315,30 +359,6 @@ class Snake {
mSnakeStep();
}
- void mInputLoop() {
- tgu::GameKey key;
- while (mGameNotOver) {
- key = tgu::GetKeyPress();
- if (mKeyQueue.size() <= 2) {
- mKeyMutex.lock();
- mKeyQueue.push(key);
- mKeyMutex.unlock();
- }
- }
- }
-
- bool mHorizontalConnectors(mSnakeUnit a, mSnakeUnit b) {
- std::string horizontals = "╔╗╚╝◁▷";
- if (a.y != b.y)
- return false;
-
- if (horizontals.find(a.appearance) != std::string::npos &&
- horizontals.find(b.appearance) != std::string::npos)
- return true;
-
- return false;
- }
-
void mDrawSnake() {
tgu::TextRGB(0, 255, 0);
if (mLastDequeued.x != -1) {
@@ -376,26 +396,6 @@ class Snake {
std::cout << std::flush;
}
- void mInitializeApple() {
- int iPos = rand() % mUnoccupied.size();
- mCell apple(mUnoccupied[iPos].x, mUnoccupied[iPos].y);
-
- mOccupy(apple.x, apple.y);
-
- mApple = {apple.x, apple.y, "■"};
- }
-
- void mAddApple() {
- mApplesAte++;
- int iPos = rand() % mUnoccupied.size();
- mCell apple(mUnoccupied[iPos].x, mUnoccupied[iPos].y);
-
- mUnoccupy(mApple.x, mApple.y);
- mOccupy(apple.x, apple.y);
-
- mApple = {mUnoccupied[iPos].x, mUnoccupied[iPos].y, mApple.appearance};
- }
-
void mGameLoop() {
while (mGameNotOver) {
int CalculatedDelay = STARTING_DELAY - (mApplesAte * ACCELERATION);
@@ -417,6 +417,14 @@ class Snake {
int acceleration = 10;
};
+ void mFillUnoccupied() {
+ for (int i = 0; i < WIDTH; i++) {
+ for (int j = 0; j < HEIGHT; j++) {
+ mUnoccupied.push_back({i, j});
+ }
+ }
+ }
+
Snake(const GameSettings &settings)
: WIDTH(settings.width), HEIGHT(settings.height),
STARTING_DELAY(settings.startingdelay),
@@ -427,14 +435,6 @@ class Snake {
}
}
- void mFillUnoccupied() {
- for (int i = 0; i < WIDTH; i++) {
- for (int j = 0; j < HEIGHT; j++) {
- mUnoccupied.push_back({i, j});
- }
- }
- }
-
void StartGame() {
tgu::EnableRawMode();
tgu::MakeCursorInvisible();