diff options
Diffstat (limited to 'main.cpp')
| -rw-r--r-- | main.cpp | 43 |
1 files changed, 26 insertions, 17 deletions
@@ -38,7 +38,7 @@ class Snake { const int GRACE_FRAMES = 3; const int BOARD_OFFSET_X = 2; const int BOARD_OFFSET_Y = 1; - const int WIDTH, HEIGHT, STARTING_DELAY, MAX_SPEED; + const int WIDTH, HEIGHT, STARTING_DELAY, MIN_DELAY; const double ACCELERATION = 0; std::mutex mKeyMutex; @@ -160,6 +160,7 @@ class Snake { std::cout << "Die about it"; tgu::ClearTerminal(); tgu::MoveCursor(0, 0); + tgu::MakeCursorVisible(); exit(0); } @@ -207,10 +208,10 @@ class Snake { void mAddApple() { mApplesAte++; int iPos = rand() % mUnoccupied.size(); - mCell apple(mUnoccupied[iPos].x, mUnoccupied[iPos].y); + mCell NewApple(mUnoccupied[iPos].x, mUnoccupied[iPos].y); mUnoccupy(mApple.x, mApple.y); - mOccupy(apple.x, apple.y); + mOccupy(NewApple.x, NewApple.y); mApple = {mUnoccupied[iPos].x, mUnoccupied[iPos].y, mApple.appearance}; } @@ -305,14 +306,18 @@ class Snake { void mHandleGameInput(tgu::GameKey input) { char PotentialSnakeDirection; - if (input == tgu::GameKey::W || input == tgu::GameKey::UPARROW) + if (input == tgu::GameKey::W || input == tgu::GameKey::UPARROW || + input == tgu::GameKey::K) PotentialSnakeDirection = 'u'; - else if (input == tgu::GameKey::A || input == tgu::GameKey::LEFTARROW) + else if (input == tgu::GameKey::A || input == tgu::GameKey::LEFTARROW || + input == tgu::GameKey::H) PotentialSnakeDirection = 'l'; - else if (input == tgu::GameKey::S || input == tgu::GameKey::DOWNARROW) + else if (input == tgu::GameKey::S || input == tgu::GameKey::DOWNARROW || + input == tgu::GameKey::J) PotentialSnakeDirection = 'd'; else if (input == tgu::GameKey::D || - input == tgu::GameKey::RIGHTARROW) { + input == tgu::GameKey::RIGHTARROW || + input == tgu::GameKey::L) { PotentialSnakeDirection = 'r'; } else if (input == tgu::GameKey::ESCAPE || input == tgu::GameKey::P) { mPaused = true; @@ -320,6 +325,7 @@ class Snake { } else if (input == tgu::GameKey::Q) { tgu::ClearTerminal(); tgu::MoveCursor(0, 0); + tgu::MakeCursorVisible(); exit(0); } else return; @@ -400,7 +406,7 @@ class Snake { while (mGameNotOver) { int CalculatedDelay = STARTING_DELAY - (mApplesAte * ACCELERATION); int TickDelay = - CalculatedDelay >= MAX_SPEED ? CalculatedDelay : MAX_SPEED; + CalculatedDelay >= MIN_DELAY ? CalculatedDelay : MIN_DELAY; std::this_thread::sleep_for(std::chrono::milliseconds(TickDelay)); mGameTick(); @@ -413,7 +419,7 @@ class Snake { int width = 14; int height = 14; int startingdelay = 400; - int maxspeed = 200; + int minimumdelay = 200; int acceleration = 10; }; @@ -428,7 +434,8 @@ class Snake { Snake(const GameSettings &settings) : WIDTH(settings.width), HEIGHT(settings.height), STARTING_DELAY(settings.startingdelay), - ACCELERATION(settings.acceleration), MAX_SPEED(settings.maxspeed) { + ACCELERATION(settings.acceleration), + MIN_DELAY(settings.minimumdelay) { if (WIDTH < 5 || HEIGHT < 5) { std::cout << "Minimum board size of 5x5 not met."; return; @@ -476,9 +483,6 @@ class Snake { try { std::getline(std::cin, sTmp); iTmp = std::stoi(sTmp); - if (iTmp <= val) - iTmp = val; - } catch (const std::exception &e) { iTmp = val; } @@ -489,7 +493,7 @@ class Snake { int width = 14; int height = 14; int startingdelay = 400; - int maxspeed = 200; + int mindelay = 200; int acceleration = 10; std::string prompt; @@ -498,7 +502,7 @@ class Snake { SinglePrompt("Board Width: ", width); SinglePrompt("Board Height: ", height); SinglePrompt("Starting delay between steps (ms): ", startingdelay); - SinglePrompt("Max speed: ", maxspeed); + SinglePrompt("Minimum delay: ", mindelay); SinglePrompt("Acceleration: ", acceleration); tgu::LeaveGameBuffer(); @@ -506,7 +510,7 @@ class Snake { GameSettings settings = {.width = width, .height = height, .startingdelay = startingdelay, - .maxspeed = maxspeed, + .minimumdelay = mindelay, .acceleration = acceleration}; return settings; @@ -514,6 +518,11 @@ class Snake { }; int main() { - Snake hi(Snake::SettingsPrompt()); + Snake::GameSettings s = Snake::SettingsPrompt(); + std::cout << "Accel: " << s.acceleration << ", Width: " << s.width + << ", Height:" << s.height + << ", Starting delay:" << s.startingdelay << ", Min delay" + << s.minimumdelay << ", Accel:" << s.acceleration; + Snake hi(s); hi.StartGame(); } |
