CodePudding user response:
Fyi:# include & lt; Conio. H>
# include & lt; Windows. H>
Void ConPrintAt (int x, int y, char * CharBuffer, int len)
{
DWORD count;
COORD COORD={x, y};
HANDLE hStdOut=GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition (hStdOut, coord);
The WriteConsole (hStdOut, CharBuffer, len, & amp; The count, NULL);
}
Void HideTheCursor ()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut=GetStdHandle (STD_OUTPUT_HANDLE);
If (GetConsoleCursorInfo (hStdOut, & amp; CciCursor))
{
CciCursor. BVisible=FALSE;
SetConsoleCursorInfo (hStdOut, & amp; CciCursor);
}
}
Void ShowTheCursor ()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut=GetStdHandle (STD_OUTPUT_HANDLE);
If (GetConsoleCursorInfo (hStdOut, & amp; CciCursor))
{
CciCursor. BVisible=TRUE;
SetConsoleCursorInfo (hStdOut, & amp; CciCursor);
}
}
Void GetWH (int * w, int * h) {
HANDLE hStdOut=GetStdHandle (STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
If (GetConsoleScreenBufferInfo (hStdOut, & amp; Csbi)) {
* w=csbi. SrWindow. Right;
* h=csbi. SrWindow. Bottom;
} else {
* w=80;
* h=25;
}
}
Void ClearConsole ()
{
//Get the handle to the current output buffer...
HANDLE hStdOut=GetStdHandle (STD_OUTPUT_HANDLE);
//This is 2 reset the carat/cursor to the top left.
COORD COORD={0, 0};
//A return value... Indicating how many chars were written
//not 2 but we need to capture this since it will be
//written anyway (passing NULL causes an access violation).
DWORD count;
//This is a structure containing all of the console info
//it is 2 here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
If (GetConsoleScreenBufferInfo (hStdOut, & amp; Csbi))
{
//This fills the buffer with a given character (in This case 32=space).
FillConsoleOutputCharacter (hStdOut, (TCHAR) 32, csbi dwSize. X * csbi dwSize. J Y, coord, & amp; The count);
FillConsoleOutputAttribute (hStdOut, csbi. WAttributes, csbi. DwSize. X * csbi dwSize. J Y, coord, & amp; The count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition (hStdOut, coord);
}
}
Int main () {
Unsigned short k;
Int x, y, w, h;
char d;
SetConsoleOutputCP (437);
ClearConsole ();
GetWH (& amp; W, & amp; H);
X=w/2; Y=h/2;
HideTheCursor ();
ConPrintAt (x, y, "O", 1);
D='o'.
While (1) {
Sleep (50);
If (kbhit ()) {
K=getch ();
If==k (27) break;//press the Esc key to exit the
If (0==k | | 0 xe0-0xfc==k) k |=getch () & lt; <8;//non-character keys
The switch (k) {
Case 0 x48e0: case 0 x04800://
D='u';
If (y> 0 {
ConPrintAt (x, y, "", 1);
Y -;
ConPrintAt (x, y, "O", 1);
}
break;
Case 0 x50e0: under the case 0 x05000://
D='d';
If (yConPrintAt (x, y, "", 1);
Y++;
ConPrintAt (x, y, "O", 1);
}
break;
Case 0 x4be0: case 0 x04b00://port
D='l'.
If (x> 0 {
ConPrintAt (x, y, "", 1);
X -;
ConPrintAt (x, y, "O", 1);
}
break;
Case 0 x4de0: case 0 x04d00://right
D='r'.
If (xConPrintAt (x, y, "", 1);
x++;
ConPrintAt (x, y, "O", 1);
}
break;
}
//cprintf (" % 4 x pressed. \ r \ n ", k);
} else {
The switch (d) {
Case 'u' :
If (y> 0 {
ConPrintAt (x, y, "", 1);
Y -;
ConPrintAt (x, y, "O", 1);
}
break;
Case 'd' :
If (yConPrintAt (x, y, "", 1);
Y++;
ConPrintAt (x, y, "O", 1);
}
break;
Case 'l' :
If (x> 0 {
ConPrintAt (x, y, "", 1);
X -;
ConPrintAt (x, y, "O", 1);
}
break;
Case "r" :
If (xConPrintAt (x, y, "", 1);
x++;
ConPrintAt (x, y, "O", 1);
}
break;
}
}
}
ClearConsole ();
ShowTheCursor ();
return 0;
}
CodePudding user response:
Thank you, miss zhao is one of sleep (50) function, this function seems to not achieve the timing, I have also tried settmer this API, met in single-threaded code getch () would have been key is not the time to press or not press down key down effect directlyCodePudding user response:
See the effect first running my code