Language C - Position my Printf in X and Y on the screen.

In summary, the conversation discusses using functions to position a printf statement on the screen, specifically when a 2D array called "labyrinth Map" is also being printed. The code provided uses the functions "gotoxy", "wherex", and "wherey" to determine and set the cursor position for the printf statement. The suggestion of using ANSI codes is also mentioned as a possible solution.
  • #1
Max0007
66
1

Homework Statement


I work on Microsoft Visual studio 2010 on Win7.

Ok so let me explain the problem here, The functions works very good to position my printf in X and Y on my screen ONLY when my 2D Array labyrinth Map is NOT printed on the screen.

But once I printf my Map on the screen I cannot position the printf in X and Y.

This is the current code I use to position my printf and only works when my Map is not printed on the screen.

gotoxy(posx,5); clreoscr();
textcolor(15);
posx = printf("\nSpeed of Character: %d / %d",speed, m1);
textcolor(15);
posx = printf("\nSpeed of enemie: %d / %d", speed_e, m2);This is the functions I have to use to give my printf a position on my screen.
----------------------------------------------------------------------------------------------------------------------------
void gotoxy(int x,int y) //positions text cursor at (x, y) screen position
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; //variable declarations
HANDLE hConsoleOut;

hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);

csbiInfo.dwCursorPosition.X = x; //cursor position X coordinate
csbiInfo.dwCursorPosition.Y = y; //cursor position Y coordinate
SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition); //set cursor at the given screen coordinate
}/*-------------------------wherex()------------------------------------*/

int wherex() //returns current text cursor (x) coordinate
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
return csbiInfo.dwCursorPosition.X;
}

/*-------------------------wherey()----------------------------------*/

int wherey() //returns current text cursor (y) coordinate
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
return csbiInfo.dwCursorPosition.Y;
}

Homework Equations


n/A

The Attempt at a Solution


N/A
 
Physics news on Phys.org
  • #2
If this is a character based game then have you looked at ANSI codes?

http://en.m.wikipedia.org/wiki/ANSI_codes

I've,used them a lot in my character based programs and scripts to control where I print things to the display. They work in all sorts of languages once you learn how to represent escape codes in your program.
 

1. What is "Language C"?

Language C, also known as C programming language, is a high-level programming language commonly used in developing operating systems, embedded systems, and other computer applications. It was developed in the early 1970s by Dennis Ritchie at Bell Labs.

2. How do I position my Printf in X and Y on the screen?

To position your Printf in X and Y coordinates on the screen, you can use the printf function with format specifiers such as %x and %y. These specifiers allow you to specify the horizontal and vertical positions of your Printf on the screen. You can also use the gotoxy function to move the cursor to a specific position on the screen before printing the output.

3. What is the purpose of using Printf in C?

The Printf function in C is used to print formatted output on the screen. It is commonly used for displaying text, numbers, and other data types in a specific format. It is also useful for debugging and troubleshooting code by printing the values of variables at different stages of the program.

4. Can I use different data types with Printf in C?

Yes, you can use different data types with Printf in C. The Printf function supports a variety of format specifiers that correspond to different data types, such as %d for integers, %f for floating-point numbers, and %s for strings. You can also use modifiers to specify the size and precision of the data being printed.

5. Is it possible to customize the position and appearance of Printf output?

Yes, you can customize the position and appearance of Printf output in C. Apart from using format specifiers and modifiers, you can also use escape sequences to add special characters and formatting to your output. Additionally, you can also use libraries like "ncurses" to create more complex and dynamic graphical user interfaces.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top