Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / game

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

4.89/5 (5 votes)
7 Nov 2014CPOL3 min read 64.3K   6K  
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)