diff --git a/include/error.hpp b/include/error.hpp index dc5349a..bfaa6bb 100644 --- a/include/error.hpp +++ b/include/error.hpp @@ -20,10 +20,3 @@ class TTF_InitError : public std::exception{ public: const char *what(); }; - -/* -class SoundInitError : public std::exception{ - public: - char *what(); -} -*/ \ No newline at end of file diff --git a/include/game.hpp b/include/game.hpp index e05ef93..bec3403 100644 --- a/include/game.hpp +++ b/include/game.hpp @@ -22,6 +22,9 @@ class Game{ Food **foods = nullptr; Wall *wall = nullptr; cMain *parent = nullptr; + int penaltyPerCentForGameQuitOnMultiPlayer = 10; + BodyColor *culpritSnake = nullptr; + void setDir(Dir currentDir, Dir *dirs); void setDir(Dir currentDir, Dir *dirs, bool *snake_moved); Dir *resetDirs(); diff --git a/include/main.hpp b/include/main.hpp index 558b867..d959cb1 100644 --- a/include/main.hpp +++ b/include/main.hpp @@ -2,22 +2,13 @@ //marcos #define SDL_MAIN_HANDLED -#define VERSION "v4.1" +#define VERSION "v5.0" #define texWidth 16 #define texHeight 16 #define numFoodItemsSinglePlayer 2 -#define numFoodItemsMultiPlayer 2 +#define numFoodItemsMultiPlayer 3 #define maxNumChainedMovesAllowed 3 -//for debugging purposes only -#if defined(DEBUG_LINUX) -#define realP "/home/slpirate/Commons/programming/C++/snake/" -#elif defined(DEBUG_WIN) -#define realP "F:\\programming\\c++\\snake\\" -#else -#define realP "" -#endif - //for debugging purposes #include #if defined (__linux__) diff --git a/include/wxwids.hpp b/include/wxwids.hpp index 850ffe6..92f5dc6 100644 --- a/include/wxwids.hpp +++ b/include/wxwids.hpp @@ -14,6 +14,7 @@ class cMain : public wxFrame{ void pause(); void quit(); + void quit(double penaltyPerCentForGameQuitOnMultiPlayer, BodyColor *snake); private: wxButton* btn = nullptr; diff --git a/src/error.cpp b/src/error.cpp index 41df56f..67582ca 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -2,18 +2,12 @@ const char *SDL_InitError::what(){ return "Failed to initialize SDL"; - // errMsg = ; - // return errMsg.c_str(); } const char *ImageInitError::what(){ return "Failed to initialize SDL_image"; - // errMsg = "Failed to initialize SDL_image"; - // return errMsg.c_str(); } const char *TTF_InitError::what(){ return "Failed to initialize SDL_ttf"; - // errMsg = "Failed to initialize SDL_ttf"; - // return errMsg.c_str(); } \ No newline at end of file diff --git a/src/fonts.cpp b/src/fonts.cpp index f191d47..74ac4e9 100644 --- a/src/fonts.cpp +++ b/src/fonts.cpp @@ -4,7 +4,7 @@ Fonts::Fonts(Window *gameWin, std::string title, BodyColor color, int x, int y){ this->gameWin = gameWin; this->color = color; - font = TTF_OpenFont(realP "res/fonts/LiberationSans-Bold.ttf", 72); + font = TTF_OpenFont("res/fonts/LiberationSans-Bold.ttf", 72); // if (font == nullptr){ // std::cout << "could not load fonts " << SDL_GetError() << std::endl; // } diff --git a/src/food.cpp b/src/food.cpp index eadaded..c7d7a39 100644 --- a/src/food.cpp +++ b/src/food.cpp @@ -3,7 +3,7 @@ Food::Food(Window *gameWin, GraphicItem ***arr){ this->arr = arr; this->gameWin = gameWin; - texture = gameWin->loadTexture(realP "res/gfx/egg.png"); + texture = gameWin->loadTexture("res/gfx/egg.png"); srand(time(0)); diff --git a/src/game.cpp b/src/game.cpp index d5c86b7..569d482 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -25,7 +25,7 @@ Game::Game(cMain *parent){ void Game::start(){ gameWin = new Window("Snake", winHeight + 44, winWidth); - background = gameWin->loadTexture(realP "res/gfx/grass-pattern.jpg"); // res/gfx/grass-pattern.jpg + background = gameWin->loadTexture("res/gfx/grass-pattern.jpg"); // res/gfx/grass-pattern.jpg foods = new Food*[numFoodItemsSinglePlayer]; // arr[ROWS()][COLS()] @@ -140,7 +140,7 @@ void Game::start(){ void Game::startMultiplayer(){ gameWin = new Window("Snake", winHeight + 44, winWidth); - background = gameWin->loadTexture(realP "res/gfx/grass-pattern.jpg"); // res/gfx/grass-pattern.jpg + background = gameWin->loadTexture("res/gfx/grass-pattern.jpg"); // res/gfx/grass-pattern.jpg foods = new Food*[numFoodItemsMultiPlayer]; // arr[ROWS()][COLS()] @@ -161,7 +161,7 @@ void Game::startMultiplayer(){ //implementing snake & food snake1 = new Snake(gameWin, arr, parent->difficulty/*1000/parent->difficulty->GetValue()*/, RED); snake2 = new Snake(gameWin, arr, parent->difficulty/*1000/parent->difficulty->GetValue()*/, BLUE); - snake->setInitialDir(left); + snake2->setInitialDir(left); for (int i = 0; i < numFoodItemsMultiPlayer; i++){ foods[i] = new Food(gameWin, arr); } @@ -227,13 +227,13 @@ void Game::startMultiplayer(){ //move the snake if (isKeyPressed){ if (!isPaused){ - if(snake1_moved){ + if(*snake1_moved){ gameRunning = snake1->move(dir1); *snake1_moved = false; dir1 = resetDirs(); } - if(snake2_moved){ + if(*snake2_moved){ gameRunning = snake2->move(dir2); *snake2_moved = false; dir2 = resetDirs(); @@ -243,11 +243,13 @@ void Game::startMultiplayer(){ } else{ if(!snake1->move()){ - snake1->applyPenalty(); + snake1->applyPenalty(penaltyPerCentForGameQuitOnMultiPlayer); + culpritSnake = new BodyColor(snake1->color); gameRunning = false; } if(!snake2->move()){ - snake2->applyPenalty(); + snake2->applyPenalty(penaltyPerCentForGameQuitOnMultiPlayer); + culpritSnake = new BodyColor(snake2->color); gameRunning = false; } } @@ -279,7 +281,7 @@ void Game::startMultiplayer(){ std::this_thread::sleep_for(std::chrono::seconds(2)); } - parent->quit(); + parent->quit(penaltyPerCentForGameQuitOnMultiPlayer, culpritSnake); for (int i = 0; i < numFoodItemsMultiPlayer; i++){ delete (foods[i]); foods[i] = nullptr; diff --git a/src/main.cpp b/src/main.cpp index ce0d80e..da75433 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,21 +98,43 @@ void cMain::startGame(){ game->start(); } } + +// for singleplayer mode void cMain::quit(){ - if(!multiplayer){ - outLabel = "Game Over\nYour Score: "; - outLabel << game->snake->getScore(); - btn->SetLabel("Restart Singleplayer"); - } - else{ - winWidth = 496; - winHeight = 496; - outLabel = "Game Over\nRED's Score: "; - outLabel << game->snake1->getScore(); - outLabel << "\nBLUE's Score: "; - outLabel << game->snake2->getScore(); - btn2->SetLabel("Restart Multiplayer\n(Chaotic Mode)"); + outLabel = "Game Over\nYour Score: "; + outLabel << game->snake->getScore(); + btn->SetLabel("Restart Singleplayer"); + + out->SetLabel(outLabel); + btn1->Hide(); + btn->Show(); + btn2->Show(); + this->Show(); + game = nullptr; + t = nullptr; +} +// for multiplayer mode +void cMain::quit(double penaltyPerCentForGameQuitOnMultiPlayer, BodyColor *snake){ + winWidth = 496; + winHeight = 496; + outLabel = "Game Over\nRED's Score: "; + outLabel << game->snake1->getScore(); + outLabel << "\nBLUE's Score: "; + outLabel << game->snake2->getScore(); + std::string snake_color; + switch(*snake){ + case RED: + snake_color = "RED"; + break; + case BLUE: + snake_color = "BLUE"; + break; } + outLabel << "\n" << penaltyPerCentForGameQuitOnMultiPlayer << "% penalty applied for " << snake_color; + delete(snake); + snake = nullptr; + btn2->SetLabel("Restart Multiplayer\n(Chaotic Mode)"); + out->SetLabel(outLabel); btn1->Hide(); btn->Show(); diff --git a/src/snake.cpp b/src/snake.cpp index 5d67a16..e05b701 100644 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -25,7 +25,7 @@ Snake::Snake(Window *gameWin, GraphicItem ***arr, wxSlider *difficulty, BodyColo const char *filePath; if (color == RED){ - filePath = realP "res/gfx/red.png"; + filePath = "res/gfx/red.png"; texture = gameWin->loadTexture(filePath); // res/gfx/body.png snake[0] = new GraphicItem(texture, (int) ROWS()/2, (int) COLS()/2, SNAKE); snake[1] = new GraphicItem(texture, (int) ROWS()/2 + 1, (int) COLS()/2, SNAKE); @@ -36,7 +36,7 @@ Snake::Snake(Window *gameWin, GraphicItem ***arr, wxSlider *difficulty, BodyColo } else if(color == BLUE){ dir = left; - filePath = realP "res/gfx/blue.png"; + filePath = "res/gfx/blue.png"; texture = gameWin->loadTexture(filePath); // res/gfx/body.png snake[0] = new GraphicItem(texture, (int) ROWS()/2, (int) COLS()/2 + 1, SNAKE); snake[1] = new GraphicItem(texture, (int) ROWS()/2 + 1, (int) COLS()/2 + 1, SNAKE); @@ -184,8 +184,3 @@ int Snake::getScore(){ void Snake::applyPenalty(double penaltyPerCent){ score -= (int) (score * penaltyPerCent / 100.0); } - -// apply a penalty of 10% to the score -void Snake::applyPenalty(){ - applyPenalty(10); -} diff --git a/src/wall.cpp b/src/wall.cpp index 37f1e6f..3c262e2 100644 --- a/src/wall.cpp +++ b/src/wall.cpp @@ -5,7 +5,7 @@ Wall::Wall(Window *gameWin, GraphicItem ***arr){ this->gameWin = gameWin; this->arr = arr; - texture = gameWin->loadTexture(realP "res/gfx/brick_wall23.png"); //res/gfx/brick_wall23.png + texture = gameWin->loadTexture("res/gfx/brick_wall23.png"); //res/gfx/brick_wall23.png for (int i = 0; i < COLS(); i++){ arr[0][i] = new GraphicItem(texture, 0, i, WALL);