Click here to Skip to main content
14,265,748 members

Snake & Ladder Game - C Graphics (TurboC++)

Rate this:
4.88 (4 votes)
Please Sign up or sign in to vote.
4.88 (4 votes)
7 Nov 2014CPOL
This is only for students who are developing mini projects with Turbo C / C++ Compiler. This example may help you to get an idea in submitting your mini projects with stunning user interface experience with Mouse click events.

Snake & Ladder Game in C (TurboC++ Compiler)

This game was developed for students who are developing mini project with the help of TurboC / C++ compiler. In my previous tips, I have explained how to create a splash screen with TurboC compiler. So those who are downloading this program should go through the previous tip explained here -> Splash Screen.

Game Mode

  • Single Mode: In this mode: "Player 1" -> User and "Player 2" -> Computer
  • Double Mode: Two players can play with the dice

Game Rules & Functions

  • Throwing Dice: This is done by pressing the left button on "Player 1". The counter is stopped when you release the button and the counter value is displayed on the Dice.

  • If player gets number six after throwing the dice, then the respective player is eligible for one more throw.

User Interface & Mouse Click Events

For user interface design ideas, please go through the previous tip explained here -> Splash Screen. User Interface was designed with the help of basic setfillstyle, floodfill, rectangle and bar functions. In this tip / tutorial, I will explain how to use mouse events and handle them nicely.

Graphics mode is initialized and then mouse is activated by calling initmouse() function. We are using ax register to store the values and then raising interrupts for activating mouse. After calling this function, we need to call show showmouseptr(). We need to call this function if we done any change in interface. Mouse pointer can be hidden by using hidemouseptr() function. This function will be frequently used if we are drawing with Mouse Pointer. We can also get the mouse position (xy) cordinates by using or calling this function getmousepos(). The first parameter in this getmousepos function is the button click option. The button pointer is assigned to 1 if "Left" button clicked and 2 if "Right" button is clicked.

 // Mouse functions
        union REGS i,o;
// Initialize mouse
        void initmouse()
        {
            i.x.ax=0;
            int86(0x33,&i,&o);
        }

// Show Mouse pointer
        void showmouseptr()
        {
            i.x.ax=1;
            int86(0x33,&i,&o);
        }
// Hide Mouse pointer
        void hidemouseptr()
        {
            i.x.ax=2;
            int86(0x33,&i,&o);
        }

Get Mouse Position Button 1-> Left and Button 2-> Right.

void getmousepos(int *button,int *x,int *y)
{
    i.x.ax=3;
    int86(0x33,&i,&o);

    *button=o.x.bx;
    *x=o.x.cx;
    *y=o.x.dx;
}

Restricting Mouse pointer in respective closed window:

void restrictmouseptr(int x1,int y1,int x2,int y2)
{
    i.x.ax=7;
    i.x.cx=x1;
    i.x.dx=x2;
    int86(0x33,&i,&o);
    i.x.ax=8;
    i.x.cx=y1;
    i.x.dx=y2;
    int86(0x33,&i,&o);
}

Shadow Effect

Shadows effect was created by using outtext. To get the effect, it's needed to print the same text twice, one in grey color (shadow) and the original text on top by moving left.

Splash and Snake Picture

Splash screen was created with Rectangle function. The snake picture was drawn with LineTo function and the color was done with Fill Patterns.

Game Board

Game board was designed with Lineto, Floodfill and Outtext function. Snake was drawn with continuous ellipse function with fill pattern style.

Flow Chart

User Interface Help

The below picture gives you an idea about the interface design and user option in the game board.

Game Mode

From the below option, user can change the mode:

Dice Value Display

Dice value after throwing will be displayed on the below orange dice:

Score Display

Each player score is displayed in the below button box. The value was updated for each throw:

Player Dice

Player dice was displayed on the left side of the player button. Button on the right side was used for throwing the dice (long press). You have to press the left button and release the button for recording the dice thrown value.

New and Exit Game

These two buttons were used to create a new game or exit the game.

Player Dice on Board

Player dice was shown in "Red" or "Yellow" square inside each Tile.

Source Code Module Details

Functions and modules are commented inside the source file. So please download the code and run the program.

www.codemx.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
Questionprogram execution isn't happening Pin
Member 1333583429-Jul-17 17:38
memberMember 1333583429-Jul-17 17:38 
AnswerRe: program execution isn't happening Pin
Ullas_Krishnan31-May-18 23:39
memberUllas_Krishnan31-May-18 23:39 
Questionthis code can't run on turbo C++ Pin
Member 132860131-Jul-17 1:55
memberMember 132860131-Jul-17 1:55 
AnswerRe: this code can't run on turbo C++ Pin
Ullas_Krishnan31-May-18 23:40
memberUllas_Krishnan31-May-18 23:40 
Questionproduct perspective Pin
Member 119541754-Sep-15 6:08
memberMember 119541754-Sep-15 6:08 
AnswerRe: product perspective Pin
Ullas_Krishnan15-Oct-15 6:51
memberUllas_Krishnan15-Oct-15 6:51 
AnswerRe: product perspective Pin
Ullas_Krishnan31-May-18 23:41
memberUllas_Krishnan31-May-18 23:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 7 Nov 2014

Stats

45.6K views
5K downloads
1 bookmarked