This game was created in C language using Turbo C compiler for DOS. This is a great example of using VGA mode graphics in C language, and an ideal example for beginners who learn game programming in any programming language.
What is DotGame?
It is a two-player game, where a matrix a dots will be presented to the players. Each player has to draw a line between two dots; and a box will be formed if four adjacent dots are connected. Each box will be labelled with the number of the player who drawn the last line of the box. After all the dots in the matrix were connected, the winner will be the player with maximum number of boxes.
The following image shows the game screen.
Initializing VGA Graphics
The following code shows how to initialize the graphics mode in Turbo C. The current directory (where the C program resides) should contain the graphics engine file EGAVGA.BGI and the character definition files namely LITT.CHR, SCRI.CHR and BOLD.CHR.
/* get current directory */
char CURDIR[MAXPATH];
strcpy(CURDIR, "X:\\");
CURDIR[0] = 'A' + getdisk();
getcurdir(0, CURDIR+3);
/* initialize graphics */
int gd = VGA, gm = VGAHI;
initgraph(&gd, &gm, CURDIR);
Initializing Mouse
The following code initializes the mouse operations in C.
union REGS ii, oo;
ii.x.ax = 0;
int86(0x33, &ii, &oo);
if (oo.x.ax == 0)
{
printf("Failed to initiliaze Mouse.");
return;
}
The Game Loop
The following code shows the game loop. The loop will execute until the user presses the Escape key (keycode=1) in the keyboard. Inside the loop, the mouse press events are captured and the corresponding action is taken. For every mouse click, it checks whether the clicked position is around a dot (with a margin of 4 pixels). When the user presses the left-mouse-button, a horizontal line will be drawn from the clicked dot to the adjacent dot at the right. If the user presses the right-mouse-button, a vertical line will be drawn from the clicked dot to the adjacent dot at the bottom.
void game()
{
int key = 0, i, j, x, y;
int margin = 4; /* margin for clicking around the dots */
int LEFTMOUSE = 1, RIGHTMOUSE = 2;
while (key != 1)
{
while (!kbhit())
{
get_mouse_pos();
if (mousebutton == LEFTMOUSE)
{
for (i = 0, x = START_X; i < ROWS - 1; i++, x += BOX_WIDTH)
{
for (j = 0, y = START_Y; j < ROWS; j++, y += BOX_HEIGHT)
{
if (mousex >= x - margin && mousex <= x + margin && mousey >= y - margin && mousey <= y + margin)
{
if (matrix_h[j][i] != 1)
{
matrix_h[j][i] = 1;
line(x, y, x + BOX_WIDTH, y);
player_lines[PLAYER - 1]++;
}
}
}
}
}
if (mousebutton == RIGHTMOUSE)
{
for (i = 0, x = START_X; i < ROWS; i++, x += BOX_WIDTH)
{
for (j = 0, y = START_Y; j < ROWS - 1; j++, y += BOX_HEIGHT)
{
if (mousex >= x - margin && mousex <= x + margin && mousey >= y - margin && mousey <= y + margin)
{
if (matrix_v[j][i] != 1)
{
matrix_v[j][i] = 1;
line(x, y, x, y + BOX_HEIGHT);
player_lines[PLAYER - 1]++;
}
}
}
}
}
}
ii.h.ah = 0;
int86(22, &ii, &oo);
key = oo.h.ah;
}
}
Full source code (C language) of this game is available for download below. You need Turbo C Compiler for DOS version 3.0 to compile and run this code.
Downloads for this article
File |
Language |
Tools |
DotGame-Source 26.93 kb (357 downloads) |
C/C++ |
Turbo C++ 3.0 |