C language game must: cursor positioning and color Settings

  • 2020-04-02 00:46:08
  • OfStack

For those of you who like to write games, this information is enough to allow you to play around with cursor positioning and color under WINDOWS and Linux. Enjoy it.

WINDOWS:
1. Cursor positioning function:


#include <windows.h>
#include <conio.h>

void gotoxy(HANDLE hOut, int x, int y)
{
      COORD pos;
      pos.X = x;             //abscissa
      pos.Y = y;            //ordinate
      SetConsoleCursorPosition(hOut, pos);
}
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//Defines the monitor handle variable
gotoxy(hOut,20,30);         //The cursor is positioned at the coordinate (20,30).

2. Color control:
2.1 function implementation


void Set_TextColor_Green (void)
{    
      HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
}

2.2 the system
System (" color 0 d "); // sets the text to pink
Note: there are 16 colors from 1.0 to 15.
                In 2.0d, 0 is the background color and D is the font color.
3. The black screen
System (" CLS ");

Second, the Linux:
In the Linux/Unix character interface, you can use some controls to locate the display position, control the color, clear the screen, and so on.
Printf (" \ [47, 033, 31 mhello world \ 033 [5 m ");
47 is the background color of the word, 31 is the color of the font, hello world is the string, \033[5m is the control code.
Color code:
QUOTE:
Word background color range: 40--49                                 Word color: 30--39
              40: black                                                   30: black
              41: red                                                   31: red
              42: green                                                   32: green
              43: yellow                                                   33: yellow
              44: blue                                                   34: blue
              45: purple                                                   35: purple
              46: dark green                                               36: dark green
              47: white                                               37: white
ANSI control code:
QUOTE:
  \033[0m] close all properties
  \033[1m set high brightness
  Underline \ '03 [4 m
  033 \ [5 m
  \ [033 7 m reverse video
  \ [033 8 m blanking
  033 \ [30 m   --   033 \ [37 m Setting the foreground
  40 m \ [033   --   47 m \ [033 Set the background color
  \033[nA cursor moved up n rows
  \03[nB cursor moved down n rows
  \033[nC cursor moved n rows to the right
  \033[nD cursor moved n rows to the left
  \033[y;xH] set cursor position
  2 j \ [033 Clear the screen
  \033[K] clear contents from cursor to end of line
  \033[s save cursor position
  \033[u to restore cursor position
  \033[?25l hide cursor
  \33[?25h display cursor


Related articles: