-
gotoxy question
I have made a program that moves a dot around the screen. The program doesn't work right all the time(leaves dots on the screen) unless I have a certain gotoxy function in. When I look through the program, the function doesn't appear necessary, but it makes all the difference to my compiler.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define ENTER '\r'
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
main()
{
int key,xval,yval;
xval=40;
yval=12;
key=0;
clrscr();
printf("Use the arrow keys to move the dot, ENTER to quit: ");
gotoxy(xval,yval);
printf("*");
while(key!=ENTER) {
key=getch();
if(key==0) /*this statement blocks other input*/
key=getch();
else
break;
gotoxy(xval,yval); /*this line is the key*/
printf(" ");
switch(key) { /*moving the cursor*/
case UP:
gotoxy(xval,--yval);
break;
case DOWN:
gotoxy(xval,++yval);
break;
case LEFT:
gotoxy(--xval,yval);
break;
case RIGHT:
gotoxy(++xval,yval);
break;
}
printf("*");
}
}
can anybody tell me why this happens??
p.s. are these tags good enough prelude?
-
> p.s. are these tags good enough prelude?
Hehe, no. You have to use [code] to start your block of code, and [/c...] to end it. (where c... is actually the word code)
> gotoxy(xval,yval); /*this line is the key*/
> printf(" ");
Of course it's the key. What you're doing is simply writing a space over top of the current star. Remember: when you put anything on the screen, it advances the cursor one place. Without moving back to x/y, you'd be writing the space just PAST the star. So, you have to go back to where the star is, then overwrite it with a space.
Quzah.
-
If you didn't have the gotoxy() there, it would print a space right after the original position. The gotoxy moves it back so that the space prints over the '*' and not after it.
::EDIT:: Sorry, Quzah got it while I was posting.
-
thanks. that sounds familiar......and I forgot about the cursor advancing.
-
I posted code for this very problem about a week ago:
http://www.cprogramming.com/cboard/s...ghlight=gotoxy
-Prelude
-
<conio.h>
I don't know about your compiler but i use gcc from DJGPP you need to include <conio.h> to use gotoxy();