-
Please Review my Program
I don't really have any specific problem I'm trying to fix. This is my first "graphics" program, which moves an asterisk around the screen using the arrow keys. If one of you experienced programmers could look over this and tell me if there was a better method of going about this task, please tell me. I have noticed the bug of being able to move off the screen, but haven't tried to fix it yet. I've only been programming for maybe a month ( I'm learning from a book). Somebody please tell me how I'm doing. By the way, I'm running under windows.
#define ENTER '\r'
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
main()
{
int key,xval,yval;
xval=yval=0;
clrscr();
printf("Use the arrow keys to move the dot, ENTER to quit: ");
gotoxy(40,12);
printf("*");
getkey:
key=getch();
switch(key)
{
case 0:
key=getch();
break;
case ENTER:
exit(EXIT_SUCCESS);
default:
goto getkey;
}
gotoxy(xval+40,yval+12);
printf(" ");
switch(key)
{
case UP:
--yval;
gotoxy(xval+40,yval+12);
printf("*");
goto getkey;
case DOWN:
++yval;
gotoxy(xval+40,yval+12);
printf("*");
goto getkey;
case LEFT:
--xval;
gotoxy(xval+40,yval+12);
printf("*");
goto getkey;
case RIGHT:
++xval;
gotoxy(xval+40,yval+12);
printf("*");
goto getkey;
}
}
-
>If one of you experienced programmers could look over this and
>tell me if there was a better method of going about this task
I had to define my own clrscr and gotoxy because Borland is crazy about nonstandard functions, but this seems to work peachy except for the off the screen bug:
Code:
void print ( int x, int y )
{
clrscr();
gotoxy( x, y );
printf ( "*" );
}
int main ( void )
{
int key = 0, xval = 40, yval = 12;
printf("Use the arrow keys to move the dot, ENTER to quit: ");
print ( xval, yval );
while ( key != ENTER ) {
switch ( ( key = getch() ) )
{
case UP: print ( xval, --yval ); break;
case DOWN: print ( xval, ++yval ); break;
case LEFT: print ( --xval, yval ); break;
case RIGHT: print ( ++xval, yval ); break;
}
}
return EXIT_SUCCESS;
}
The bug I'll leave to you since it's simple enough to fix with a little extra testing. Upon running your program I noticed that the asterisk tended towards multiplying instead of remaining the only one and while I could move it down and to the right, up and left had problems.
In the case of this program, a loop is drastically easier to follow than goto and one of your switch statements was apparently redundant. If you had a reason for the first one let me know because I removed it. You'll notice that I moved the relocation and printing operation to a single function since you use it quite a bit, this makes the code shorter and a bit more elegant than the way you had it.
Notes:
Please use the code tags from now on, it makes helping you quite a bit easier if we don't have to reformat the code first. And when you paste code, be sure to include the includes as that makes it easier to determine both what platform and compiler you have, but also saves us having to guess or rewrite your code to cover for an unknown feature.
-Prelude
-
thank you for your help. my includes were only stdio, stdlib, and math, but I don't know if I really needed stdlib. I just needed that first switch statement because that's how the book said to get the arrow keys. I work under Win98 and DJGPP(gcc). THANK YOU!