Click here to Skip to main content
15,867,756 members
Articles / Game Development
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.89/5 (5 votes)
7 Nov 2014CPOL3 min read 62.1K   6K   1   10
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.

Image 1

Image 2

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.

    Image 3

  • 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.

C++
 // 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.

C++
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:

C++
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.
Image 4

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.
Image 5

Game Board

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

Flow Chart

Image 7

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:

Image 8

Dice Value Display

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

Image 9

Score Display

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

Image 10

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.

Image 11

New and Exit Game

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

Image 12

Player Dice on Board

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

Image 13

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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseGood ! Pin
Pedro Buendia21-Oct-22 17:03
Pedro Buendia21-Oct-22 17:03 
QuestionI can't open this file in Android Pin
Member 1574309920-Aug-22 18:47
Member 1574309920-Aug-22 18:47 
AnswerRe: I can't open this file in Android Pin
OriginalGriff20-Aug-22 18:53
mveOriginalGriff20-Aug-22 18:53 
Questionprogram execution isn't happening Pin
Member 1333583429-Jul-17 17:38
Member 1333583429-Jul-17 17:38 
AnswerRe: program execution isn't happening Pin
Ullas_Krishnan31-May-18 23:39
Ullas_Krishnan31-May-18 23:39 
Questionthis code can't run on turbo C++ Pin
Member 132860131-Jul-17 1:55
Member 132860131-Jul-17 1:55 
AnswerRe: this code can't run on turbo C++ Pin
Ullas_Krishnan31-May-18 23:40
Ullas_Krishnan31-May-18 23:40 
Questionproduct perspective Pin
Member 119541754-Sep-15 6:08
Member 119541754-Sep-15 6:08 
AnswerRe: product perspective Pin
Ullas_Krishnan15-Oct-15 6:51
Ullas_Krishnan15-Oct-15 6:51 
AnswerRe: product perspective Pin
Ullas_Krishnan31-May-18 23:41
Ullas_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.