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

Click For Summary
The user is experiencing issues with positioning the output of `printf` in specific X and Y coordinates on the screen when a 2D array labyrinth map is printed. The `gotoxy` function works correctly to position text only when the map is not displayed. The code provided for positioning the cursor includes functions to get the current cursor position but fails when the map is printed. A suggestion is made to explore ANSI codes for better control over text positioning in character-based programs. Utilizing ANSI codes could provide a solution to the positioning problem encountered.
Max0007
Messages
66
Reaction score
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
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
Replies
2
Views
2K
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K