Skip to content

Commit

Permalink
feat: add single-player support
Browse files Browse the repository at this point in the history
- include an option for single-player support in the menu, with two players set as the default game mode
- enhance code readability by passing the global game state as an argument
  • Loading branch information
hyPnOtICDo0g committed May 20, 2023
1 parent 95e4204 commit 984934f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 37 deletions.
12 changes: 7 additions & 5 deletions include/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <GL/glut.h>

#include "carrom.h"

// display callback function used to redraw the scene
void display(void);

Expand All @@ -16,18 +18,18 @@ void trigger(int value);
void carromBoard(void);

// display the current positions of the carrom coins
void carromCoins(void);
void carromCoins(struct GameState *gameState);

// displaying the direction in which the striker must be struck
void strikerDirection(void);
void strikerDirection(struct GameState *gameState);

// display the power reader that shows the force applied to the striker
void powerReader(void);
void powerReader(struct GameState *gameState);

// display the scores obtained by either team during their individual turns
void scoreBoard(void);
void scoreBoard(struct BoardStatus *boardStatus);

// display the team and player number while it is their turn
void teamTurnPanel(void);
void teamTurnPanel(struct BoardStatus *boardStatus);

#endif
5 changes: 3 additions & 2 deletions include/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#define SUBMENU_NEW 1
#define MENU_RESTART 2
#define MENU_QUIT 3
#define MENU_SUB_TWO_PLAYER_GAME 4
#define MENU_SUB_FOUR_PLAYER_GAME 5
#define MENU_SUB_SINGLE_PLAYER_GAME 4
#define MENU_SUB_TWO_PLAYER_GAME 5
#define MENU_SUB_FOUR_PLAYER_GAME 6

// menu callback function for handling the user's selection from the menu
void menu(int option);
Expand Down
2 changes: 1 addition & 1 deletion include/physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define RADIANS_PER_DEGREE 57.29577951308232087679

// calculate the force at which the striker has to be struck
void hitStriker(void);
void hitStriker(struct GameState *gameState);

// simulate the physical interactions between coins during a collision
void coinCollision(int i, int j, struct Coin *coins);
Expand Down
40 changes: 20 additions & 20 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

void display(void) {
carromBoard();
carromCoins();
strikerDirection();
powerReader();
scoreBoard();
teamTurnPanel();
carromCoins(state);
strikerDirection(state);
powerReader(state);
scoreBoard(start);
teamTurnPanel(start);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
Expand Down Expand Up @@ -112,8 +112,8 @@ void carromBoard(void) {
}
}

void carromCoins(void) {
int i; struct Coin *coins = state->coins;
void carromCoins(struct GameState *gameState) {
int i; struct Coin *coins = gameState->coins;
// place coins in their initial positions
for(i = 0; i < (MAX_COIN_COUNT - 2) / 2; i++) {
// if the coin is in the pot, color it black
Expand Down Expand Up @@ -161,11 +161,11 @@ void carromCoins(void) {
}
}

void strikerDirection(void) {
float theta = state->theta;
struct Coin *coins = state->coins;
void strikerDirection(struct GameState *gameState) {
float theta = gameState->theta;
struct Coin *coins = gameState->coins;

if(state->strikerState == 1) {
if(gameState->strikerState == 1) {
float strikerX = BOARD_SCALING_FACTOR * coins[MAX_COIN_COUNT - 1].center.x;
float strikerY = BOARD_SCALING_FACTOR * coins[MAX_COIN_COUNT - 1].center.y;

Expand All @@ -180,8 +180,8 @@ void strikerDirection(void) {
}
}

void powerReader(void) {
int currentPower = state->currentPower;
void powerReader(struct GameState *gameState) {
int currentPower = gameState->currentPower;
// 25% power
if(currentPower == POWER_READER_VARIATION) {
glColor3f(POWER_READER_ON_25);
Expand Down Expand Up @@ -215,7 +215,7 @@ void powerReader(void) {
glEnd();
}

void scoreBoard(void) {
void scoreBoard(struct BoardStatus *boardStatus) {
int score;
float x = -1.03, y = 0;

Expand All @@ -228,11 +228,11 @@ void scoreBoard(void) {
glVertex2f(-1, 0.33);
glEnd();

if(start->currentTeam == 1) {
score = start->pointsTeamOne;
if(boardStatus->currentTeam == 1) {
score = boardStatus->pointsTeamOne;
}
else {
score = start->pointsTeamTwo;
score = boardStatus->pointsTeamTwo;
}

if(score == 0) {
Expand All @@ -250,7 +250,7 @@ void scoreBoard(void) {
drawStringAtPos(-1.385, -0.3, (const unsigned char *) "SCORE");
}

void teamTurnPanel(void) {
void teamTurnPanel(struct BoardStatus *boardStatus) {
float x = 1.69, y = 0.77, digitX = 1.47, digitY = 0.5;

// render a background for the player number display
Expand All @@ -261,7 +261,7 @@ void teamTurnPanel(void) {
glVertex2f(x - 0.65, y - 0.59);
glVertex2f(x - 0.65, y);
glEnd();
drawDigit(start->turn + 1, digitX, digitY);
drawDigit(boardStatus->turn + 1, digitX, digitY);
drawStringAtPos(1.31, 0.2, (const unsigned char *) "PLAYER");

// render a background for the team number display
Expand All @@ -272,6 +272,6 @@ void teamTurnPanel(void) {
glVertex2f(x - 0.65, -(y - 0.53));
glVertex2f(x - 0.65, -(y + 0.06));
glEnd();
drawDigit(start->currentTeam, digitX, -digitY);
drawDigit(boardStatus->currentTeam, digitX, -digitY);
drawStringAtPos(1.33, -0.8, (const unsigned char *) "TEAM");
}
4 changes: 2 additions & 2 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void keyboard(unsigned char key, int x, int y) {
break;

case SPACE:
hitStriker();
hitStriker(state);
break;

}
Expand All @@ -43,7 +43,7 @@ void joystick(unsigned int buttonMask, int x, int y, int z) {
shiftStrikerRight(state, start);
}
if(buttonMask & GLUT_JOYSTICK_BUTTON_B) {
hitStriker();
hitStriker(state);
}
if(buttonMask & GLUT_JOYSTICK_BUTTON_D) {
increaseStrikerPower(state);
Expand Down
10 changes: 9 additions & 1 deletion src/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

void menu(int option) {
switch(option) {
case MENU_SUB_SINGLE_PLAYER_GAME:
// restart the game only if it is not a single-player game
if(state->numberOfPlayers > 0) {
restartGame(1);
}
break;

case MENU_SUB_TWO_PLAYER_GAME:
// restart the game only if it is not a two-player game
if(state->numberOfPlayers > 1) {
if(state->numberOfPlayers > 1 || state->numberOfPlayers == 0) {
restartGame(2);
}
break;
Expand All @@ -33,6 +40,7 @@ void menu(int option) {

void createGameMenu(void) {
int subMenuId = glutCreateMenu(menu);
glutAddMenuEntry("Single Player", MENU_SUB_SINGLE_PLAYER_GAME);
glutAddMenuEntry("Two Players", MENU_SUB_TWO_PLAYER_GAME);
glutAddMenuEntry("Four Players", MENU_SUB_FOUR_PLAYER_GAME);
// create the main menu and attach the submenu
Expand Down
10 changes: 5 additions & 5 deletions src/physics.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#include "context.h"
#include "physics.h"

void hitStriker(void) {
int currentPower = state->currentPower;
float theta = state->theta;
struct Coin *coins = state->coins;
void hitStriker(struct GameState *gameState) {
int currentPower = gameState->currentPower;
float theta = gameState->theta;
struct Coin *coins = gameState->coins;
// update striker state to zero as it is in action
state->strikerState = 0;
gameState->strikerState = 0;
coins[MAX_COIN_COUNT - 1].velocity.x = currentPower * cos((theta / 360.0) * 2 * M_PI);
coins[MAX_COIN_COUNT - 1].velocity.y = currentPower * sin((theta / 360.0) * 2 * M_PI);
}
Expand Down
5 changes: 4 additions & 1 deletion src/rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ void finalize(struct GameState *gameState, struct BoardStatus *start, bool strik
- the player has not scored any points
*/
if(strikerState == false) {
if(numberOfPlayers < 3) {
if(numberOfPlayers == 0) {
turn = 0;
}
else if(numberOfPlayers < 3) {
turn = (turn + 2) % 4;
start->currentTeam = (int) (turn / 2) + 1;
}
Expand Down

0 comments on commit 984934f

Please sign in to comment.