Skip to content

Commit

Permalink
Merge pull request #9 from MCWertGaming/pause2
Browse files Browse the repository at this point in the history
pause menu
  • Loading branch information
Alexgamer470 authored Nov 16, 2020
2 parents 0e86848 + f3d82b8 commit d57d9c1
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)
project(snek
VERSION 1.0.2
VERSION 1.1.0
DESCRIPTION "SNEK - A simple snake clone, playable in the commandline with some unique game modes."
HOMEPAGE_URL "https://github.com/MCWertGaming/snek"
LANGUAGES CXX)
Expand Down
8 changes: 8 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ create game menu
add second player
magenta apple vanishes after time
yellowApple spawns at 2% vanishes quickly too
revive oppDirection
shouldn't be drawField() enough

// pause
save time from timer -> pause
other colors (resume, quit)?
distances (resume, quit)
rename resume into continue?

// coding optimisations
move from ncurses to a better implementation
Expand Down
126 changes: 111 additions & 15 deletions src/snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
#include "information.hpp"

// input parsing values
#define noInput 0
#define inputUp 1
#define inputDown 2
#define inputLeft 3
#define inputRight 4
#define noInput 0
#define inputPause 5
#define inputQuit -1
// pause parsing values
#define pauseLeft 0
#define pauseRight 1
// last direction parsing values
#define notMovedYet 0
#define lastDirUp 1
Expand Down Expand Up @@ -128,6 +132,10 @@ void snake::snake::drawSnake()

mvaddch(snakePos[1][0],snakePos[0][0], 'O');

if (drawWholeSnek)
for (unsigned short int i = 1; i < snakeLength; i++)
mvaddch(snakePos[1][i],snakePos[0][i], 'o');

if (snakeLength > 1)
mvaddch(snakePos[1][1],snakePos[0][1], 'o');

Expand Down Expand Up @@ -274,7 +282,7 @@ void snake::snake::animateWatermark()

if (consoleSupportsColors)
attron(COLOR_PAIR(redText));
mvprintw(3,screen[0] + 2, "by MCWertGaming");
mvprintw(3,screen[0] + 2, "by BlackVyperStudios");
refresh();
timer.reset();
while (!timer.done());
Expand All @@ -295,7 +303,7 @@ void snake::snake::drawWatermark()
mvprintw(2,screen[0] + 2, "Version: %d.%d.%d", SNAKE_VERSION_MAJOR, SNAKE_VERSION_MINOR, SNAKE_VERSION_PATCH);
if (consoleSupportsColors)
attron(COLOR_PAIR(redText));
mvprintw(3,screen[0] + 2, "by MCWertGaming");
mvprintw(3,screen[0] + 2, "by BlackVyperStudios");
if (consoleSupportsColors)
attroff(COLOR_PAIR(blueBackground));
}
Expand Down Expand Up @@ -369,12 +377,8 @@ bool snake::snake::illegalPosition(const unsigned short int *locationX, const un
if (illegalApple)
snakeLengthCopy++;
for (unsigned short int i = 0; i < snakeLengthCopy; i++)
{
if (snakePos[0][i] == *locationX && snakePos[1][i] == *locationY)
{
return true;
}
}
if (illegalApple && apple[redApple][0] == *locationX && apple[redApple][1] == *locationY ||
illegalApple && apple[magentaApple][0] == *locationX && apple[magentaApple][0] == *locationY)
return true;
Expand Down Expand Up @@ -402,12 +406,12 @@ unsigned short int snake::snake::update()
{
/* pre-move tasks */
getInput();
// check, if the player newer moved to skip the other tasks
if (lastDir == notMovedYet && input == noInput)
return 0;
// check, if the user wants to exit the game
else if (input == inputQuit)
if (input == inputQuit)
return 1;
// check, if the player newer moved to skip the other tasks
else if (lastDir == notMovedYet && input == noInput || input == inputPause)
return 0;
// calculates the new snake position for illegalPosition() and updateSnake()
calcNewSnakePos();
// check, if the new snake destination is illegal (if that's true, the player looses)
Expand All @@ -421,6 +425,96 @@ unsigned short int snake::snake::update()
return 0;
}

void snake::snake::drawPause()
{
short int ch;
pause = true;
// stores the pause input, please use the definitions
unsigned short int pauseInput;

if (consoleSupportsColors)
attron(COLOR_PAIR(cyanText));
if (screen[0] % 2 == 0)
mvprintw(5,screen[0] / 2 - 2, "PAUSE");
else
mvprintw(5,screen[0] / 2 - 2, "PAUSE.");

if (consoleSupportsColors)
attron(COLOR_PAIR(greenText));
mvprintw(7, 3, "resume");
if (consoleSupportsColors)
attron(COLOR_PAIR(whiteText));
mvprintw(7,screen[0] - 7, "quit");

while (pause)
{
ch = getch();
if (ch != ERR)
{
switch (ch)
{
case KEY_RIGHT:
case 'd':
pauseInput = pauseRight;
if (consoleSupportsColors)
attron(COLOR_PAIR(whiteText));
mvprintw(7, 3, "resume");
if (consoleSupportsColors)
attron(COLOR_PAIR(greenText));
mvprintw(7,screen[0] - 7, "quit");
if (consoleSupportsColors)
attron(COLOR_PAIR(whiteText));
break;
case KEY_LEFT:
case 'a':
pauseInput = pauseLeft;
if (consoleSupportsColors)
attron(COLOR_PAIR(greenText));
mvprintw(7, 3, "resume");
if (consoleSupportsColors)
attron(COLOR_PAIR(whiteText));
mvprintw(7,screen[0] - 7, "quit");
default:
break;
}

if (pauseInput == pauseRight && ch == 10)
input = inputQuit;
else if (pauseInput == pauseLeft && ch == 10)
{
if (consoleSupportsColors)
attroff(COLOR_PAIR(whiteText));
if (screen[0] % 2 == 0)
mvprintw(5,screen[0] / 2 - 2, " ");
else
mvprintw(5,screen[0] / 2 - 2, " ");
mvprintw(7, 3, " ");
mvprintw(7,screen[0] - 7, " ");
}

if (ch == 10) // ENTER key
{
if (consoleSupportsColors)
attroff(COLOR_PAIR(whiteText));
if (screen[0] % 2 == 0)
mvprintw(5,screen[0] / 2 - 2, " ");
else
mvprintw(5,screen[0] / 2 - 2, " ");
mvprintw(7, 3, " ");
mvprintw(7,screen[0] - 7, " ");
pause = false;
}
}
}

drawWholeSnek = true;
drawSnake();
drawWholeSnek = false;
drawApple(redApple);
if (magentaAppleExist)
drawApple(magentaApple);
}

/* game mechanics */
void snake::snake::getInput()
{
Expand Down Expand Up @@ -454,15 +548,17 @@ void snake::snake::getInput()
input = inputRight;
break;
case 27: // ESC key
case 'q':
input = inputQuit;
break;
case 'q':
drawPause();
break;
default:
input = noInput;
}
}
// skip timer, if it's the first move
if (lastDir == notMovedYet)
// skip timer, if it's the very first move or the last move before pause (=> instant pause)
if (lastDir == notMovedYet || input == inputPause)
break;
}
}
Expand Down Expand Up @@ -539,7 +635,7 @@ void snake::snake::updateSnakePos()
void snake::snake::increaseSnakeSpeed()
{
if ((snakeLength -1) % 5 == 0)
snakeSpeed = maxSpeed - (snakeSpeedFactor * snakeLength);
snakeSpeed = maxSpeed - (unsigned short int)(snakeSpeedFactor * snakeLength);
}
snake::snake::~snake()
{
Expand Down
8 changes: 6 additions & 2 deletions src/snake.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ namespace snake
unsigned short int newSnakePos[2]{0};
// snakes length
unsigned short int snakeLength = 1;
bool pause = false;
// for checking if the whole snake should be drawn
bool drawWholeSnek = false;
// stores the apple positions apple[<appleDefinition>][<X/Y>], please use the apple definition
unsigned short int apple[2][2]{0};
// indicates, if the magenta apple exists currently
// indicates if the magenta apple exists currently
bool magentaAppleExist = false;
// score
unsigned short int score = 0;
Expand Down Expand Up @@ -45,6 +48,7 @@ namespace snake

/* drawing */
void drawScore();
void drawPause();
void drawField();
void animateField();
void drawWatermark();
Expand Down Expand Up @@ -77,7 +81,7 @@ namespace snake
void increaseSnakeSpeed();
public:
// constructor for default game options
explicit snake(bool, bool, unsigned short int, unsigned short int);
snake(bool, bool, unsigned short int, unsigned short int);
// constructor for manual game options
snake(bool, bool, unsigned short int, unsigned short int, unsigned short int, unsigned short int,
bool);
Expand Down
3 changes: 2 additions & 1 deletion src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void utils::initColorMode()
init_pair(redText, COLOR_RED, COLOR_BLACK);
init_pair(whiteText, COLOR_WHITE, COLOR_BLACK);
init_pair(magentaText, COLOR_MAGENTA, COLOR_BLACK);
init_pair(yellowText,COLOR_YELLOW,COLOR_BLACK);
init_pair(yellowText, COLOR_YELLOW, COLOR_BLACK);
init_pair(cyanText, COLOR_CYAN, COLOR_BLACK);
}
}
1 change: 1 addition & 0 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define whiteText 4
#define magentaText 5
#define yellowText 6
#define cyanText 7

namespace utils
{
Expand Down

0 comments on commit d57d9c1

Please sign in to comment.