• Advertisement
Sign in to follow this  

console game problem

This topic is 4489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've made this little game with MSVC> Win32 console Project; the letter 'a' is supposed to move left-right when pressing the arrows, but it doesn't. Why? #include <windows.h> #include <process.h> #include <iostream> #include <fstream> #include <conio.h> using namespace std; char ch[64]; int main(int argc, char* argv[]) { int positx= 12; int posity= 23; void gotoxy(int x, int y); HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_INTENSITY); _CONSOLE_CURSOR_INFO cc; cc.dwSize=0; cc.bVisible=true; SetConsoleCursorInfo(hStdout,&cc); COORD crd={0,0}; DWORD b; gotoxy(positx,posity);cout<<'a'; while (!(GetAsyncKeyState(VK_ESCAPE) & 0x80000000)) { if ((GetAsyncKeyState(VK_LEFT) & 0x80000000)) { positx--; FillConsoleOutputCharacter(hStdout, ' ', 5000, crd, &b); gotoxy(0,0); gotoxy(positx,posity); cout<<'a'; } if ((GetAsyncKeyState(VK_RIGHT) & 0x80000000)) { positx++; FillConsoleOutputCharacter(hStdout, ' ', 5000, crd, &b); gotoxy(0,0); gotoxy(positx , posity); cout<<'a'; } } return 0; } void gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); }

Share this post


Link to post
Share on other sites
Advertisement
It looks like it should work to me. A possible problem is that it's going too fast to notice. What does happen, does the a just stay put? What happens if you put a Sleep(50) in your main loop?

Share this post


Link to post
Share on other sites
The Sleep(50) solved the problem althought I can't understand why?

Share this post


Link to post
Share on other sites
Quote:
Original post by Aerts
The Sleep(50) solved the problem althought I can't understand why?


The A was simply moving to fast for you to notice, slowing the main loop down by 50 milliseconds(milliseconds, right?) not only drops how much of your CPU the application is using, but slows down the movement of the loop, and thus the A.

Share this post


Link to post
Share on other sites
Time based movement would fix that so you won't have to put a sleep or change it for faster or slower processes. There are many posts already about Time Based Movement. That or google for it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement