Hey, I'm having a problem with my code. I am trying to move the character "#" left and right along the x axis. However, it is only letting me move 1 left, and 1 right, as well as skipping spaces, and not going to the next coordinate. All help is appreciated! Thanks
Code:
#ifdef WIN32
#include <windows.h>
void gotoxy(int x, int y)
{
COORD cur = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
}
#else
void gotoxy(int x, int y)
{
printf("\033[%dG\033[%dd", x+1, y+1);
}
#endif
#include <stdio.h>
#define length 77
#define width 20
#define left 'k'
#define right 'l'
#define pause_length 150000
char player[] = "#";
void setup();
void init();
void move();
int main()
{
int j;
setup();
init();
for(;;)
{
// for(j = 0; j < 14*pause_length; j++)
// j = 1 + j;
move();
}
getch();
return(0);
}
void setup()
{
int across;
int down;
gotoxy(1, 3);
for(across = 0; across < length; across++)
printf("=");
for(down = 0; down < width; down++)
{
gotoxy(1,down + 4);
printf("|");
gotoxy(length, down + 4);
printf("|");
}
gotoxy(1, 24);
for(across = 0; across < length; across++)
printf("=");
gotoxy(1,1);
printf("~~ Game ~~");
}
void init()
{
gotoxy(37,23);
printf("%s",player);
}
void move()
{
char keypress;
char direction;
int x = 38;
int y = 23;
if(kbhit())
{
keypress = (char)getch();
direction = keypress;
switch(direction)
{
case left:
x--;
gotoxy(x,y);
//x = x + 1;
printf(" ");
x++;
x = x - 2;
gotoxy(x,y);
printf("%c",player);
break;
case right:
x--;
gotoxy(x,y);
//x = x + 1;
printf(" ");
x++;
//gotoxy(x,y);
printf("%c",player);
break;
}
}
}
Some things have been commented out from my testing purposes. I'm stumped here. Thanks.