Ok, I've cleaned that up, but it's still hasn't changed much on the visual side. It looks the same. I'm wondering, because in my setup function, I don't create spaces for all the blanks, so by replacing nothing with a blank, is that messing everything up. Or is it something to do with the coordinate x, because I've checked that 4 times, and surely it's correct now.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 // End of Giberish #include <stdio.h> #include <conio.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); printf(" "); x = x - 2; gotoxy(x,y); printf("%s",player); break; case right: x--; gotoxy(x,y); printf(" "); //x++; bug - pushes cursor 1 ahead of " _" gotoxy(x,y); printf("%s",player); break; } } }
Last edited by binks; 04-25-2011 at 12:02 PM. Reason: Fixed Bug
I just noticed that player is a single character but you're treating it as a string...
You might also want to print something visual for testing purposes... like maybe a colon or an asterisk so you can see where everything is going.Code:char player = '#'; printf("%c",player); // or much easier. printf("#");
Last edited by CommonTater; 04-25-2011 at 12:18 PM.
I had that before, but # wasn't even displayed. For some odd reason, it's only displayed when %s is used. But when it's like this
it works. The left function works 1 to the left, but the right one only works when left is hit first, and doesn't erase the old # sign. Anyone know why it's only moving 1 space over (NOT per click, in total) i want it per click one space, but all the way across the screen if possible. ( I think it's because there aren't any spaces, I'll try adding them to setup)Code:printf("#"); // without using variable
PO is setting x and y up at the start of move(), but they aren't static so they get reset to the same value each time into move. Debugging and stepping through the code (as I suggested) makes this fairly obvious.
The reason it didn't work with "%c" before is that you have defined player as an array of characters and preset it to the string of #\0 meaning that it is actually two characters. Of you use apostrophes (single quotes) as I showed you, you would not have that problem.
Moving right should be easier than moving left... back up one space, printf(" #");
Code:x--; gotoxy(x,y); printf(" #"); x += 2;
I think you guys have gotoxy and the cursor wrong... maybe I misunderstood...
This works:
Code:char player = '#'; #define START_X 37 #define START_Y 23 void move() { char keypress; char direction; static int x = START_X; static int y = START_Y; if(kbhit()) { keypress = (char)getch(); direction = keypress; switch(direction) { case left: gotoxy(x,y); printf(" "); x--; gotoxy(x,y); printf("%c",player); break; case right: gotoxy(x,y); printf(" "); x++; printf("%c",player); break; default: break; } } }
Hey mike... it's been a LOOOOONG time since I played with conio.h so you could be right...
Ahh! Thank you! I see it now, and I can't believe I totally missed it, each time it loops, it reintializes x and y as the default.
Moving right seems to work the first time, but then after, it goes left. And left jumps two spaces. I'll try some more cleanups, and see. But is there anything either of you helpful people can spot easily that is messing it up?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 // End of Giberish #include <stdio.h> #include <conio.h> #define length 77 #define width 20 #define left 'k' #define right 'l' #define pause_length 150000 void setup(); void init(); void move(); int main() { int j; setup(); init(); // 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(2,4); for(down = 0; down < width - 1; down++) { gotoxy(2,down + 5); for(across = 0; across < length - 2; across++) printf(" "); } gotoxy(1, 24); for(across = 0; across < length; across++) printf("="); gotoxy(1,1); printf("~~ Game ~~"); } void init() { gotoxy(37,23); printf("#"); } void move() { char keypress; char direction; int x = 38; int y = 23; for(;;) { if(kbhit()) { keypress = (char)getch(); direction = keypress; switch(direction) { case left: x--; gotoxy(x,y); printf(" "); x = x - 2; gotoxy(x,y); printf("#"); break; case right: x--; gotoxy(x,y); printf(" #"); break; } } } }
EDIT: I'll mess around with it more, what your saying makes sense. Thanks
Last edited by binks; 04-25-2011 at 12:41 PM. Reason: Above 2 posts
In my design, x and y hold the current place that you wish to edit.
So at first x is equal to where you originally drew the '#'. It remains there because you WILL erase it (print space) next. Then you will redraw the '#' (move x left or right) where you want to. Seems quite simple, no?
You know you could do this and save yourself from some of the convolution...
This is one reason I suggested a code cleanup... you've a few of these and it would simplify debugging considerably.Code:// instead of... if(kbhit()) { keypress = (char)getch(); direction = keypress; switch(direction) // use... switch(getch())
Also you should include a way out of the loop ....
Code:switch(direction) { case left: //.... case right: //.... case 27: // ascii ESCape key value return;
Last edited by CommonTater; 04-25-2011 at 01:02 PM.
Hey mike, that works smoothly, except I'm not using static int's, just int's (static's aren't suppose to change, so how would that work..?). Thank you.
CommonTater, I will look into your code optimization, it looks much simpler and uses 2 less variables. Thanks.
EDIT: Those optimizations are great, it's more clean now!
Last edited by binks; 04-25-2011 at 03:28 PM. Reason: Result