In: Computer Science
Based on the above Question:
Solution :
We are going to write code
/* 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.
I have tried
my best to resolve it.
If you have any Queries please comment here.
If you like my answer Please Upvote / Like it.
Thank you