Essential for C games: Implementation of cursor positioning and color settings

Source: Internet
Author: User

You are blessed to be friends who like to write games. This article is enough to let you play with the mouse position and color in WINDOWS and Linux, enjoy it.

I. WINDOWS:
1. cursor positioning function:

Copy codeThe Code is as follows: # include <windows. h>
# Include <conio. h>
***************************** ***/
Void gotoxy (HANDLE hOut, int x, int y)
{
COORD pos;
Pos. X = x; // X coordinate
Pos. Y = y; // Y coordinate
SetConsoleCursorPosition (hOut, pos );
}
HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE); // defines the display HANDLE variable
Gotoxy (hOut, 20, 30); // The cursor is positioned in the coordinate (20, 30)

2. Color Control:
2.1 function implementationCopy codeThe Code is as follows: ***************************** ***************/
Void Set_TextColor_Green (void)
{
HANDLE Handle = GetStdHandle (STD_OUTPUT_HANDLE );
SetConsoleTextAttribute (Handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN );
}

2.2. system
System ("color 0D"); // set the text to pink
Note: 1.0-15 is 16 colors.
0 in 2.0D is the background color, and D is the font color.
3. Clear Screen
System ("cls ");

Ii. Linux:
On the linux/unix character interface, you can use some controllers to locate the display position, control the color, and clear the screen.
Printf ("\ 033 [47; 31 mhello world \ 033 [5 m ");
47 is the background color, 31 is the font color, and hello world is the string. \ 033 [5 m is the control code.
Color Code:
QUOTE:
Color Range: 40--49 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 [0 m close all attributes
\ 033 [1 m set high brightness
\ 03 [4 m underline
\ 033 [5 m flashing
\ 033 [7 m reverse display
\ 033 [8 m blanking
\ 033 [30 m -- \ 033 [37 mSet foreground color
\ 033 [40 m -- \ 033 [47 mSet background color
\ 033 [move the nA cursor up n rows
\ 03 [move the nB cursor down n rows
\ 033 [the nC cursor shifts n rows right
\ 033 [nD cursor shifts n rows left
\ 033 [y; xH: Set the cursor position
\ 033 [2JClear Screen
\ 033 [K clear content from cursor to end of line
\ 033 [s Save the cursor position
\ 033 [u restore cursor position
\ 033 [? 25l hide the cursor
\ 33 [? 25 h show cursor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.