Gronek8624 Temat założony przez niniejszego użytkownika |
[allegro] Problem z prędkością pojazdu » 2010-05-13 19:49:37 Zrobiłem wyścigi lecz ma troch błędów Miedzy innymi Jak prędkość spadnie poniżej zera to nie da się jechać do przodu , do tego jak puści się gaz to gdy prędkość dojdzie do zera to samochód się cofa . Oto kod #include <allegro.h> #include <cmath>
volatile long speed = 0; void increment_speed() { speed++; } END_OF_FUNCTION( increment_speed ); LOCK_VARIABLE( speed ); LOCK_FUNCTION( increment_speed );
double tab_sin[ 72 ]; double tab_cos[ 72 ];
void laduj_sin_cos() { for( int i = 0; i < 72; i++ ) { tab_sin[ i ] = sin(( M_PI *( i * 5 ) ) / 180 ); tab_cos[ i ] = cos(( M_PI *( i * 5 ) ) / 180 ); } }
int main() { allegro_init(); install_keyboard(); set_color_depth( 32 ); set_gfx_mode( GFX_AUTODETECT_FULLSCREEN, 1024, 768, 0, 0 ); install_timer(); install_int_ex( increment_speed, BPS_TO_TIMER( 50 ) ); BITMAP * bufor = NULL; BITMAP * samochod = NULL; BITMAP * tor = NULL; bufor = create_bitmap( 1024, 768 ); if( !bufor ) { set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 ); allegro_message( "Nie mogę utworzyć bufora !" ); allegro_exit(); return 0; } int x = 550, y = 90; double dx = 0, dy = 0; int kierunek = 36; double v = 0; laduj_sin_cos(); tor = load_bmp( "tlo.bmp", default_palette ); if( !tor ) { set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 ); allegro_message( "nie mogę załadowac obrazek toru !" ); allegro_exit(); return 0; } samochod = load_bmp( "samochod.bmp", default_palette ); if( !samochod ) { set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 ); allegro_message( "nie mogę załadować obrazka Samochod !" ); allegro_exit(); return 0; } while( !key[ KEY_ESC ] ) { while( speed > 0 ) { if( key[ KEY_LEFT ] ) kierunek--; if( key[ KEY_RIGHT ] ) kierunek++; if( key[ KEY_UP ] ) v += 0.09; if( key[ KEY_DOWN ] ) v -= 0.05; if( key[ KEY_R ] ) x = 550, y = 90, v = 0; if( getpixel( tor, x, y ) != makecol( 192, 192, 192 ) && getpixel( tor, x, y ) != makecol( 192, 192, 192 ) && y > 90 ) y--; if( getpixel( tor, x, y ) == makecol( 192, 192, 192 ) ) y = 90, x = 550, v = 0, kierunek = 36; if( getpixel( tor, x, y ) != makecol( 0, 0, 0 ) && getpixel( tor, x, y ) != makecol( 0, 0, 0 ) && v < - 0.01 ) v--; if( getpixel( tor, x, y ) == makecol( 0, 0, 0 ) ) v -= 0.03; if( getpixel( tor, x, y ) != makecol( 0, 128, 0 ) && getpixel( tor, x, y ) != makecol( 0, 128, 0 ) && v < - 0.01 ) v--; if( getpixel( tor, x, y ) == makecol( 0, 128, 0 ) ) v -= 0.08; speed--; } clear_to_color( bufor, makecol( 150, 150, 150 ) ); if( kierunek > 71 ) kierunek = 0; if( kierunek < 0 ) kierunek = 71; if( v > 6 ) v = 6; if( v <- 1 ) v =- 1; dx += v * tab_cos[ kierunek ]; dy += v * tab_sin[ kierunek ]; if( dx >= 1 || dx <= - 1 ) { x += int( dx ); dx = dx - int( dx ); } if( dy >= 1 || dy <= - 1 ) { y += int( dy ); dy = dy - int( dy ); } blit( tor, bufor, 0, 0, 0, 0, tor->w, tor->h ); masked_blit( samochod, bufor,( kierunek % 18 ) * 30,( kierunek / 18 ) * 30, x, y, 30, 30 ); textprintf_ex( bufor, font, 900, 708, makecol( 255, 255, 255 ), 1, "X: %d", x ); textprintf_ex( bufor, font, 900, 698, makecol( 255, 255, 255 ), 1, "Y: %d", y ); textprintf_ex( bufor, font, 900, 688, makecol( 255, 255, 255 ), 1, "szybkosc: %.2f", v * 30 ); blit( bufor, screen, 0, 0, 0, 0, 1024, 768 ); } remove_int( increment_speed ); destroy_bitmap( tor ); destroy_bitmap( bufor ); destroy_bitmap( samochod ); allegro_exit(); return 0; } END_OF_MAIN(); Z góry dzięki za pomoc z bugami |
DejaVu |
» 2010-05-13 22:11:59 if( getpixel( tor, x, y ) != makecol( 192, 192, 192 ) && getpixel( tor, x, y ) != makecol( 192, 192, 192 ) && y > 90 ) y--;
if( getpixel( tor, x, y ) == makecol( 192, 192, 192 ) ) y = 90, x = 550, v = 0, kierunek = 36;
if( getpixel( tor, x, y ) != makecol( 0, 0, 0 ) && getpixel( tor, x, y ) != makecol( 0, 0, 0 ) && v < - 0.01 ) v--;
if( getpixel( tor, x, y ) == makecol( 0, 0, 0 ) ) v -= 0.03;
if( getpixel( tor, x, y ) != makecol( 0, 128, 0 ) && getpixel( tor, x, y ) != makecol( 0, 128, 0 ) && v < - 0.01 ) v--;
if( getpixel( tor, x, y ) == makecol( 0, 128, 0 ) ) v -= 0.08;
Wykomentuj powyższy kod i sprawdź czy problem występuje. Jeżeli nie to problem jest w tym fragmencie kodu. Dodam, że zapewne wynika z zapisu v--, który prawdopodobnie powinien wyglądać v = -v;. |