Skip to content

Commit

Permalink
fixed multiplayer bug and added penalty mechanism for multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
SL-Pirate committed Apr 20, 2023
1 parent 0a5b204 commit b121630
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 55 deletions.
7 changes: 0 additions & 7 deletions include/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,3 @@ class TTF_InitError : public std::exception{
public:
const char *what();
};

/*
class SoundInitError : public std::exception{
public:
char *what();
}
*/
3 changes: 3 additions & 0 deletions include/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 2 additions & 11 deletions include/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>
#if defined (__linux__)
Expand Down
1 change: 1 addition & 0 deletions include/wxwids.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class cMain : public wxFrame{

void pause();
void quit();
void quit(double penaltyPerCentForGameQuitOnMultiPlayer, BodyColor *snake);

private:
wxButton* btn = nullptr;
Expand Down
6 changes: 0 additions & 6 deletions src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion src/fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
// }
Expand Down
2 changes: 1 addition & 1 deletion src/food.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
18 changes: 10 additions & 8 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down Expand Up @@ -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()]
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down
48 changes: 35 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 2 additions & 7 deletions src/snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/wall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b121630

Please sign in to comment.