david4006 Temat założony przez niniejszego użytkownika |
Snake w C++, aplikacja wysypuje się przy zbyt dużej ilości eventów z klawiatury » 2013-06-05 16:57:15 Witam, zacząłem uczyć się C++ i postanowiłem napisać Snake'a w trybie tekstowym, mam problem z wysypywanie, zakańczaniem się aplikacji, jak zbyt często zacznę w chaotyczny sposób wciskać klawisze sterujące, wtedy aplikacja zwraca "0" i kończy działanie. Ma ktoś pomysł co z tym może być nie tak? Kod porgramu: #include <iostream> #include <conio.h> #include <windows.h> #include <cstdlib> #include <time.h>
#define sizex 30 #define sizey 20 #define wymiary 2
using namespace std;
struct dane { char tablica[ sizex ][ sizey ][ wymiary ]; string opis; int glowax, gloway; int kierunek; int zabity; } data;
void cls() { COORD coordScreen = { 0, 0 }; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwConSize; HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE ); GetConsoleScreenBufferInfo( hConsole, & csbi ); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter( hConsole,( TCHAR ) ' ', dwConSize, coordScreen, & cCharsWritten ); GetConsoleScreenBufferInfo( hConsole, & csbi ); FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, & cCharsWritten ); SetConsoleCursorPosition( hConsole, coordScreen ); }
void wyczysc_plansze( struct dane * plansza ) { int x, y; for( y = 0; y < sizey; y++ ) { for( x = 0; x < sizex; x++ ) { plansza->tablica[ x ][ y ][ 0 ] = ' '; plansza->tablica[ x ][ y ][ 1 ] = ' '; } } }
void wyswietl_plansze( struct dane * plansza ) { int x, y; cls(); for( y = 0; y < sizey; y++ ) { for( x = 0; x < sizex; x++ ) cout << plansza->tablica[ x ][ y ][ 0 ]; cout << "\n"; } cout << plansza->opis; }
void umiesc_objekt( struct dane * plansza ) { srand( time( NULL ) ); int x = 0, y = 0; while( plansza->tablica[ x ][ y ][ 0 ] == '#' || plansza->tablica[ x ][ y ][ 0 ] == 'S' || plansza->tablica[ x ][ y ][ 0 ] == 'O' ) { x = rand() % sizex; y = rand() % sizey; } plansza->tablica[ x ][ y ][ 0 ] = 'X'; }
void start( struct dane * plansza ) { int x, y; for( y = 0; y < sizey; y++ ) { for( x = 0; x < sizex; x++ ) { if( y == 0 || y == sizey - 1 ) plansza->tablica[ x ][ y ][ 0 ] = '#'; } plansza->tablica[ 0 ][ y ][ 0 ] = '#'; plansza->tablica[ sizex - 1 ][ y ][ 0 ] = '#'; } srand( time( NULL ) ); plansza->glowax = rand() %( sizex / 2 ) + 3; plansza->gloway = rand() %( sizey / 2 ) + 3; plansza->kierunek = 2 *( rand() % 4 ) + 2; plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 0 ] = 'S'; if( plansza->kierunek == 2 ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 1 ] = '8'; plansza->tablica[ plansza->glowax ][ plansza->gloway - 1 ][ 1 ] = '8'; plansza->tablica[ plansza->glowax ][ plansza->gloway - 2 ][ 1 ] = '0'; plansza->tablica[ plansza->glowax ][ plansza->gloway - 1 ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax ][ plansza->gloway - 2 ][ 0 ] = 'O'; } if( plansza->kierunek == 4 ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 1 ] = '6'; plansza->tablica[ plansza->glowax + 1 ][ plansza->gloway ][ 1 ] = '6'; plansza->tablica[ plansza->glowax + 2 ][ plansza->gloway ][ 1 ] = '0'; plansza->tablica[ plansza->glowax + 1 ][ plansza->gloway ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax + 2 ][ plansza->gloway ][ 0 ] = 'O'; } if( plansza->kierunek == 6 ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 1 ] = '4'; plansza->tablica[ plansza->glowax - 1 ][ plansza->gloway ][ 1 ] = '4'; plansza->tablica[ plansza->glowax - 2 ][ plansza->gloway ][ 1 ] = '0'; plansza->tablica[ plansza->glowax - 1 ][ plansza->gloway ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax - 2 ][ plansza->gloway ][ 0 ] = 'O'; } if( plansza->kierunek == 8 ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 1 ] = '2'; plansza->tablica[ plansza->glowax ][ plansza->gloway + 1 ][ 1 ] = '2'; plansza->tablica[ plansza->glowax ][ plansza->gloway + 2 ][ 1 ] = '0'; plansza->tablica[ plansza->glowax ][ plansza->gloway + 1 ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax ][ plansza->gloway + 2 ][ 0 ] = 'O'; } plansza->zabity = 0; umiesc_objekt( & data ); }
void ruch( struct dane * plansza ) { int x, y, prex, prey, zmieniono, wykonano_ruch; wykonano_ruch = 0; if( plansza->kierunek == 2 && wykonano_ruch == 0 ) { if( plansza->tablica[ plansza->glowax ][ plansza->gloway + 1 ][ 0 ] == 'O' || plansza->tablica[ plansza->glowax ][ plansza->gloway + 1 ][ 0 ] == '#' ) plansza->zabity++; if( plansza->zabity == 0 && plansza->tablica[ plansza->glowax ][ plansza->gloway + 1 ][ 0 ] != 'X' ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax ][ plansza->gloway + 1 ][ 0 ] = 'S'; plansza->tablica[ plansza->glowax ][ plansza->gloway + 1 ][ 1 ] = '8'; x = plansza->glowax; y = plansza->gloway + 1; while( plansza->tablica[ x ][ y ][ 1 ] != '0' ) { prex = x; prey = y; zmieniono = 0; if( plansza->tablica[ x ][ y ][ 1 ] == '2' && zmieniono == 0 ) { y++; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '8' && zmieniono == 0 ) { y--; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '6' && zmieniono == 0 ) { x++; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '4' && zmieniono == 0 ) { x--; zmieniono++; } } plansza->gloway += 1; plansza->tablica[ x ][ y ][ 0 ] = ' '; plansza->tablica[ x ][ y ][ 1 ] = ' '; plansza->tablica[ prex ][ prey ][ 1 ] = '0'; } if( plansza->zabity == 0 && plansza->tablica[ plansza->glowax ][ plansza->gloway + 1 ][ 0 ] == 'X' ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax ][ plansza->gloway + 1 ][ 0 ] = 'S'; plansza->tablica[ plansza->glowax ][ plansza->gloway + 1 ][ 1 ] = '8'; plansza->gloway += 1; umiesc_objekt( & data ); } wykonano_ruch++; } if( plansza->kierunek == 8 && wykonano_ruch == 0 ) { if( plansza->tablica[ plansza->glowax ][ plansza->gloway - 1 ][ 0 ] == 'O' || plansza->tablica[ plansza->glowax ][ plansza->gloway - 1 ][ 0 ] == '#' ) plansza->zabity++; if( plansza->zabity == 0 && plansza->tablica[ plansza->glowax ][ plansza->gloway - 1 ][ 0 ] != 'X' ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax ][ plansza->gloway - 1 ][ 0 ] = 'S'; plansza->tablica[ plansza->glowax ][ plansza->gloway - 1 ][ 1 ] = '2'; x = plansza->glowax; y = plansza->gloway - 1; while( plansza->tablica[ x ][ y ][ 1 ] != '0' ) { prex = x; prey = y; zmieniono = 0; if( plansza->tablica[ x ][ y ][ 1 ] == '2' && zmieniono == 0 ) { y++; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '8' && zmieniono == 0 ) { y--; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '6' && zmieniono == 0 ) { x++; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '4' && zmieniono == 0 ) { x--; zmieniono++; } } plansza->gloway -= 1; plansza->tablica[ x ][ y ][ 0 ] = ' '; plansza->tablica[ x ][ y ][ 1 ] = ' '; plansza->tablica[ prex ][ prey ][ 1 ] = '0'; } if( plansza->zabity == 0 && plansza->tablica[ plansza->glowax ][ plansza->gloway - 1 ][ 0 ] == 'X' ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax ][ plansza->gloway - 1 ][ 0 ] = 'S'; plansza->tablica[ plansza->glowax ][ plansza->gloway - 1 ][ 1 ] = '2'; plansza->gloway -= 1; umiesc_objekt( & data ); } wykonano_ruch++; } if( plansza->kierunek == 4 && wykonano_ruch == 0 ) { if( plansza->tablica[ plansza->glowax - 1 ][ plansza->gloway ][ 0 ] == 'O' || plansza->tablica[ plansza->glowax - 1 ][ plansza->gloway ][ 0 ] == '#' ) plansza->zabity++; if( plansza->zabity == 0 && plansza->tablica[ plansza->glowax - 1 ][ plansza->gloway ][ 0 ] != 'X' ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax - 1 ][ plansza->gloway ][ 0 ] = 'S'; plansza->tablica[ plansza->glowax - 1 ][ plansza->gloway ][ 1 ] = '6'; x = plansza->glowax - 1; y = plansza->gloway; while( plansza->tablica[ x ][ y ][ 1 ] != '0' ) { prex = x; prey = y; zmieniono = 0; if( plansza->tablica[ x ][ y ][ 1 ] == '2' && zmieniono == 0 ) { y++; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '8' && zmieniono == 0 ) { y--; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '6' && zmieniono == 0 ) { x++; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '4' && zmieniono == 0 ) { x--; zmieniono++; } } plansza->glowax -= 1; plansza->tablica[ x ][ y ][ 0 ] = ' '; plansza->tablica[ x ][ y ][ 1 ] = ' '; plansza->tablica[ prex ][ prey ][ 1 ] = '0'; } if( plansza->zabity == 0 && plansza->tablica[ plansza->glowax - 1 ][ plansza->gloway ][ 0 ] == 'X' ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax - 1 ][ plansza->gloway ][ 0 ] = 'S'; plansza->tablica[ plansza->glowax - 1 ][ plansza->gloway ][ 1 ] = '6'; plansza->glowax -= 1; umiesc_objekt( & data ); } wykonano_ruch++; } if( plansza->kierunek == 6 && wykonano_ruch == 0 ) { if( plansza->tablica[ plansza->glowax + 1 ][ plansza->gloway ][ 0 ] == 'O' || plansza->tablica[ plansza->glowax + 1 ][ plansza->gloway ][ 0 ] == '#' ) plansza->zabity++; if( plansza->zabity == 0 && plansza->tablica[ plansza->glowax + 1 ][ plansza->gloway ][ 0 ] != 'X' ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax + 1 ][ plansza->gloway ][ 0 ] = 'S'; plansza->tablica[ plansza->glowax + 1 ][ plansza->gloway ][ 1 ] = '4'; x = plansza->glowax + 1; y = plansza->gloway; while( plansza->tablica[ x ][ y ][ 1 ] != '0' ) { prex = x; prey = y; zmieniono = 0; if( plansza->tablica[ x ][ y ][ 1 ] == '2' && zmieniono == 0 ) { y++; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '8' && zmieniono == 0 ) { y--; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '6' && zmieniono == 0 ) { x++; zmieniono++; } if( plansza->tablica[ x ][ y ][ 1 ] == '4' && zmieniono == 0 ) { x--; zmieniono++; } } plansza->glowax += 1; plansza->tablica[ x ][ y ][ 0 ] = ' '; plansza->tablica[ x ][ y ][ 1 ] = ' '; plansza->tablica[ prex ][ prey ][ 1 ] = '0'; } if( plansza->zabity == 0 && plansza->tablica[ plansza->glowax + 1 ][ plansza->gloway ][ 0 ] == 'X' ) { plansza->tablica[ plansza->glowax ][ plansza->gloway ][ 0 ] = 'O'; plansza->tablica[ plansza->glowax + 1 ][ plansza->gloway ][ 0 ] = 'S'; plansza->tablica[ plansza->glowax + 1 ][ plansza->gloway ][ 1 ] = '4'; plansza->glowax += 1; umiesc_objekt( & data ); } wykonano_ruch++; } } void sprawdz_klawisz( int klawisz, struct dane * plansza ) { if( klawisz == 27 ) plansza->zabity++; if( klawisz == 13 ) plansza->kierunek = 0; if( klawisz == 115 || klawisz == 83 ) if( plansza->kierunek != 8 ) plansza->kierunek = 2; if( klawisz == 97 || klawisz == 65 ) if( plansza->kierunek != 6 ) plansza->kierunek = 4; if( klawisz == 100 || klawisz == 68 ) if( plansza->kierunek != 4 ) plansza->kierunek = 6; if( klawisz == 119 || klawisz == 87 ) if( plansza->kierunek != 2 ) plansza->kierunek = 8; }
DWORD WINAPI FunkcjaWatku1( PVOID pvParam ) { struct dane * plansza =& data; int key; while( plansza->zabity == 0 ) { wyswietl_plansze( & data ); ruch( & data ); Sleep( 300 ); } return 0; }
DWORD WINAPI FunkcjaWatku2( PVOID pvParam ) { struct dane * plansza =& data; int key; while( plansza->zabity == 0 ) { if( kbhit() ) { key = getch(); cin.clear(); cin.sync(); sprawdz_klawisz( key, & data ); } Sleep( 10 ); } return 0; }
int main() { struct dane * plansza =& data; plansza->opis = "\nGlowa weza oznaczona jest symbolem 'S', cialo weza symbolem 'O', a przeszkody i ograniczenia oznaczone sa symbolem '#'. "; plansza->opis += "\nJedzenie dla weza oznaczone jest symbolem 'X'.Poruszamy sie strzalkami, klawisz ESC wychodzi do glownego menu, a klawisz Enter, zatrzymuje gre."; DWORD IdWatku1, IdWatku2; wyczysc_plansze( & data ); start( & data ); plansza->zabity == 0; CreateThread( NULL, 0, FunkcjaWatku1,( PVOID ) NULL, 0, & IdWatku1 ); CreateThread( NULL, 0, FunkcjaWatku2,( PVOID ) NULL, 0, & IdWatku2 ); while( plansza->zabity == 0 ) { Sleep( 10 ); } getch(); return 0; }
|
|
DejaVu |
» 2013-06-05 23:17:20 Być może wciskając chaotycznie klawisze zabijasz swojego snake-a, po czym gra się kończy bo życia się skończyły :)
/edit: Swoją drogą jeszcze nie widziałem snake-a na kilku wątkach więc ciekaw jestem ostatecznego efektu jaki uzyskasz :) |
|
david4006 Temat założony przez niniejszego użytkownika |
Wiadomość » 2013-06-06 09:28:50 Dzięki za wskazówkę, rzeczywiście zmienna plansza->zabity jest 1 przy kończeniu, ale po usunięciu tego warunku z if'ów i pętle while na 1 jak ustawie, to gra się zwyczajnie zawiesza, nic zupełnie się nie dzieje, losowo zawiesza się też wtedy wątek wyświetlający, wstawiałem cout << "tutaj"; do wszystkich while'ów z funkcji z wątku który się wiesza, ale nie loop'uje na żadnym while'u. Nie mam pojęcia co to może być i dlaczego funkcja klawiszy wpływa na zmiane tej zmiennej, po wywaleniu Esc jako wyjście też nie działa, czyli to też sprawdzone ;P |
|
DejaVu |
» 2013-06-09 11:44:01 Już rozwiązałeś problem? :) Temat wisi i nic się z nim nie dzieje... |
|
david4006 Temat założony przez niniejszego użytkownika |
Odpowiedź » 2013-06-10 09:11:29 Niestety nie, podejrzewam, że może to być jakiś problem ewentualnie z samą funkcją kbhit() albo getch() ;) Temat zamykam, na przyszłość skorzystam z bardziej zaawansowanych narzędzi do obsługi zdarzeń klawiatury ;) |
|
« 1 » |