Sunday, 15 May 2016



C++ language Game |  Carrom Board  Game


Overview:


Firstly our game is designed in visual studio using c++-language and graphics are added by using Open GL. In the start of game the Carrom Board with all the coins at their arranged positions appear. The task of players/user is to pocket all the coins of their selected colors. The compiler tells the players during playing that which color’s turn is that by showing statement “White side’s Turn”.

The player has to strike the coins with the striker whose position, angle and striking rate is controlled by the player. The main aim is to finish the coins of required color as early as possible. The striking properties i-e angle position and etc. are selected by seeing the position of coin to whom we have to hit.

The game also provides an interesting option of selection of number of players i-e in the start it is asked that whether you two want to play or four. In case of two players the competition is to pocket the coins of their own color. When there are four players then two teams are generated and each team has to pocket their selected color’s coin. The Queen coin is higher in rank than all other and has to be followed by another coin during pocketing.



Preview of Game:

  

 Link:

 https://www.youtube.com/watch?v=GucxCXAjdB0&feature=youtu.be

 

In the preview the working window of game id shown in which the black and white circles represents the coins. The skin colored large circle represents the striker and the three lines with striker are movable and represents the direction of striking. The meshed circles are pockets at the corners and the light skin colored square box is the main playing area. The dialog at the right side is allocated for instructions and information. This dialog also has two selection buttons which are “Back” to go to main menu and to “Quit” button to end game.






Menu:


The main Menu of the game shows the buttons of various choices i-e.  2     Player, 4   Player, Rules  &   How to Play. You can select the operation of your choice by just clicking the statement shown on screen.





By clicking on the "How to Play" Button you see a screen describing the way of playing and "Rules" shows the rules of game:







Main Working Functions of Game:


First task is the arrangement of the coins on the carom Board. This arrangement comprises of the queen coin in the center and others arranged in the form of a circle outside it. This function is performed by the function “SetFlower” which is as under. 


void SetFlower(float angle)
{
       for (int i = 0; i < 20; i++)
       {
              Coin_Active[i] = true;
       }

       static float angle_local = 3;
       angle_local += angle;

       posX[1] = 350;
       posY[1] = 350;

       posX[11] = posX[1] + 28 * cosf(angle + PI / 3);
       posY[11] = posY[1] + 28 * sinf(angle + PI / 3);

       posX[12] = posX[1] + 28 * cosf(angle + PI);


       posY[12] = posY[1] + 28 * sinf(angle + PI);

       posX[13] = posX[1] + 28 * cosf(angle + 5 * PI / 3);
       posY[13] = posY[1] + 28 * sinf(angle + 5 * PI / 3);

       posX[14] = posX[1] + 56 * cosf(angle);
       posY[14] = posY[1] + 56 * sinf(angle);

       posX[15] = posX[1] + 56 * cosf(angle + PI / 3);
       posY[15] = posY[1] + 56 * sinf(angle + PI / 3);

       posX[16] = posX[1] + 56 * cosf(angle + 2 * PI / 3);
       posY[16] = posY[1] + 56 * sinf(angle + 2 * PI / 3);

       posX[17] = posX[1] + 56 * cosf(angle + PI);
       posY[17] = posY[1] + 56 * sinf(angle + PI);

       posX[18] = posX[1] + 56 * cosf(angle + 4 * PI / 3);
       posY[18] = posY[1] + 56 * sinf(angle + 4 * PI / 3);

       posX[19] = posX[1] + 56 * cosf(angle + 5 * PI / 3);
       posY[19] = posY[1] + 56 * sinf(angle + 5 * PI / 3);
//now black coins
       posX[2] = posX[1] + 28 * cosf(angle);
       posY[2] = posY[1] + 28 * sinf(angle);

       posX[3] = posX[1] + 28 * cosf(angle + 2 * PI / 3);
       posY[3] = posY[1] + 28 * sinf(angle + 2 * PI / 3);

       posX[4] = posX[1] + 28 * cosf(angle + 4 * PI / 3);
       posY[4] = posY[1] + 28 * sinf(angle + 4 * PI / 3);

       posX[5] = (posX[14] + posX[15]) / 2;
       posY[5] = (posY[14] + posY[15]) / 2;

       posX[6] = (posX[15] + posX[16]) / 2;
       posY[6] = (posY[15] + posY[16]) / 2;

       posX[7] = (posX[16] + posX[17]) / 2;
       posY[7] = (posY[16] + posY[17]) / 2;

       posX[8] = (posX[17] + posX[18]) / 2;
       posY[8] = (posY[17] + posY[18]) / 2;

       posX[9] = (posX[18] + posX[19]) / 2;
       posY[9] = (posY[18] + posY[19]) / 2;

       posX[10] = (posX[14] + posX[19]) / 2;
       posY[10] = (posY[14] + posY[19]) / 2;
}



The flower of the coins is shown in the picture below:






The main work in the game is the movement of the striker and coins under the force applied, friction and the gravitational force.



if (MenuSelect == 3)
       {
              if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) //Click
              {
                     Left_Click_Press = true;
              }
       }
       if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
       {
              if (MenuSelect == 3)
              {
                     Left_Click_Press = false;
                     Shoot_Striker();
                     printf("mouse clicked \n");
                     printf("\nmouseX: %d mouseY: %d, State: %d", mouseX, mouseY, state);
              }
       


The above function moves the striker when the left mouse button is released after pressing. Now the following code of a function will decide with how much force the striker will be fired.



void Shoot_Striker()
{
       if (Left_Click_Press_Duration != 0)
       {
              {
                     float Power = Left_Click_Press_Duration * 900;
                     float X = mouse_X - posX[0];
                     float Y = mouse_Y - posY[0];
                     float angle = atan2f(Y, X);
                     VX[0] = Power * cosf(angle);
                     VY[0] = Power * sinf(angle);
                     Moving = true;
                     Left_Click_Press_Duration = 0;
              }
       }
}



The following code will decide and check for the collision of coins with each other and it also checks for that the striker should not strike behind its position as it is a foul.




void Collision_Check()
{
       for (int i = 0; Moving ? i < 20 : i < 1; i++)
       if (Coin_Active[i])
       {
              Coin_Active[i] = !Pocket_Check(i);
              if (i == 0)
                     radius = 25;
              else
                     radius = 14;

              OutOfBounds_Check(i, radius);

              StrikerCollide = false;

              for (int j = i + 1; j < 20; j++)
              if (Coin_Active[j])
              {
                     float CollideDistance = 0;
                     float distance = Distance(i, j);
                     if (i == 0)
                           CollideDistance = radius + 14;
                     else
                           CollideDistance = radius * 2;
                     if (abs(posX[i] - posX[j]) <= CollideDistance && abs(posY[i] - posY[j]) <= CollideDistance)
                     if (distance <= CollideDistance)
                     {
                           if (Moving)
                           {

                                  if (i == 0)
                                  {
                                         if (Foul_BaseLine == false)
                                         {
                                                Foul_BaseLine = Foul_BaseLine_Check(j);
                                         }
                                         if (FirstCoinCollision == 0)
                                                FirstCoinCollision = j;
                                         if (j == 1)
                                                Queen_Touch = true;
                                         else if (j <= 10)
                                                Black_Touch = true;
                                         else if (j <= 19)
                                                White_Touch = true;
                                  }
                                  Perform_Collision(i, j, distance, CollideDistance);
                           }
                           else if (i == 0)
                           {
                                  StrikerCollide = true;
                           }
                     }
              }
       }
}