Print a box, which can be moved freely in the plane and control a coordinate point in the box so that it cannot be moved outside the box.
Development Environment: ubuntu 12.04
Development tools: gcc compiler, vim Editor
Function:
Print a box that can be moved freely in the plane. You need to control a coordinate point in the box so that it cannot be moved out of the box.
Purpose:
It can be used for technical training.
# Include
# Include
# Include
# Include
Typedef struct object OBJ;
Struct object
{
Int px;
Int py;
Char c;
};
Int Y, X, Origin_X, Origin_Y; // x y indicates the border size. Origin_X and Oigin_Y indicate the origin coordinates.
Struct termios old_setting;
Void background_set (void );
Void game1 (void );
Void obj_printf1 (OBJ * p );
Void obj_printf2 (OBJ * p );
Int term_set ();
Int term_reset ();
Void main (void)
{
Int flag;
Printf ("Input the mode :");
Scanf ("% d", & flag );
Getchar ();
If (flag = 1)
Game1 ();
}
Void background_set (void)
{
// Static int L, W;
Printf ("input border size: \ n ");
Printf ("Horizontal X = ");
Scanf ("% d", & X );
Getchar ();
Printf ("vertical Y = ");
Scanf ("% d", & Y );
Getchar ();
Printf ("\ n ");
Printf ("coordinate of the origin of the input border: \ n ");
Printf ("Origin_X = ");
Scanf ("% d", & Origin_X );
Getchar ();
Printf ("\ nOrigin_Y = ");
Scanf ("% d", & Origin_Y );
Getchar ();
Printf ("\ n ");
}
Void game1 (void)
{
OBJ * ptr1;
Int c;
Background_set ();
OBJ obj = {Origin_X + 5, Origin_Y + 5 ,'*'};
Ptr1 = & obj;
System ("clear ");
Obj_printf2 (ptr1); // print box and set coordinate point
// Obj_printf1 (ptr1 );
Printf ("\ n ");
Term_set ();
For (;;)
{
C = getchar ();
// Getchar ();
// Move the set point to make sure it is not moved out of the box
If (c = 'W') & (ptr1-> py> 1 + Origin_Y) ptr1-> py --;
Else if (c ='s ') & (ptr1-> py <Y-2) ptr1-> py ++;
Else if (c = 'A') & (ptr1-> px> 1 + Origin_X) ptr1-> px --;
Else if (c = 'D') & (ptr1-> px <X-2) ptr1-> px ++;
System ("clear ");
Obj_printf2 (ptr1 );
// Obj_printf1 (ptr1 );
}
Term_reset ();
}
Void obj_printf2 (OBJ * p)
{
Int I, j, k;
For (I = 0; I
{
For (j = 0; j
{
If (I = Origin_Y & j> = Origin_X) | (I = Y-1 & (j> = Origin_X )) | (j = Origin_X & I> = Origin_Y) | (j = X-1 & I> = Origin_Y) // | (j = 10) | (j = X + 9 ))
Printf ("*");
Else if (I = p-> py) & (j = p-> px) printf ("*");
Else printf ("");
// Printf ("\ n ");
}
Printf ("\ n ");
// If (I = p-> px-1) printf ("*");
}
// For (I = 0; ipy; I ++)
// Printf ("");
}
Void obj_printf1 (OBJ * p)
{
Int I;
For (I = 0; ipy; I ++)
Printf ("\ n ");
For (I = 0; ipx; I ++)
Printf ("");
Printf ("% c", p-> c );
}
// Set the terminal input not to bring back the display, and each time you press the button to send feedback
Int term_set ()
{
Struct termios new_setting;
Tcgetattr (fileno (stdin), & old_setting );
New_setting = old_setting;
New_setting.c_lflag & = ~ ECHO;
New_setting.c_lflag & = ~ (ICANON | ECHO );
New_setting.c_cc [VMIN] = 1;
New_setting.c_cc [VTIME] = 0;
Return tcsetattr (fileno (stdin), TCSAFLUSH, & new_setting );
}
Int term_reset ()
{
Return tcsetattr (fileno (stdin), TCSANOW, & old_setting );
}
Original
Welcome
Qq: 920052390
Welcome to communicate with me
Sina Blog: http://blog.sina.com.cn/u/2049150530
Csdn blog: http://blog.csdn.net/u011749143
Blog: http://www.cnblogs.com/sky-heaven/
Sina Weibo: Zhang xiaohua -- sky