krdk Temat założony przez niniejszego użytkownika |
Allegro poruszanie postaciami. » 2009-05-05 16:17:44 Mam taki kod. Stworzyłem w nim bitmapę bufor2 na której umieściłem wszystkie obiekty równolegle do bitmapy bufor. Obiekty na bitmapie bufor2 są w całości koloru niebieskiego, aby po ich zetknięciu wystąpiła kolizja. Tylko po zastosowaniu tego ludziki nie chcą się poruszać. Może ktoś naprowadzi mnie czemu tak się dzieje?. include < allegro.h >
volatile long speed = 0; void increment_speed() { speed++; } END_OF_FUNCTION( increment_speed );
LOCK_VARIABLE( speed ); LOCK_FUNCTION( increment_speed );
bool kolizja( BITMAP * bufor, int ludekx, int ludeky, int ludekw, int ludekh ) { if(( getpixel( bufor, ludekx, ludeky ) == makecol( 0, 0, 255 ) ) || ( getpixel( bufor, ludekx + ludekw, ludeky ) == makecol( 0, 0, 255 ) ) || ( getpixel( bufor, ludekx, ludeky + ludekh ) == makecol( 0, 0, 255 ) ) || ( getpixel( bufor, ludekx + ludekw, ludeky + ludekh ) == makecol( 0, 0, 255 ) ) ) return true; else return false; }
bool kolizja( float x1, float y1, float s1, float w1, float x2, float y2, float s2, float w2 ) { if( x1 + s1 >= x2 && x1 <= x2 + s2 && y1 + w1 >= y2 && y1 <= y2 + w2 ) return( true ); return( false ); }
int main() { allegro_init(); install_keyboard(); set_color_depth( 16 ); set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0 ); install_timer(); install_int_ex( increment_speed, BPS_TO_TIMER( 400 ) ); BITMAP * ludek1 = NULL; BITMAP * ludek1bfr = NULL; BITMAP * ludek2bfr = NULL; BITMAP * ludek2 = NULL; BITMAP * bufor = NULL; BITMAP * bufor2 = NULL; BITMAP * tlo = NULL; bufor = create_bitmap( 800, 600 ); bufor2 = create_bitmap( 800, 600 ); ludek1 = load_bmp( "ludekb1.bmp", default_palette ); ludek2 = load_bmp( "ludekb2.bmp", default_palette ); ludek1bfr = create_bitmap( ludek1->w, ludek1->h ); ludek2bfr = create_bitmap( ludek2->w, ludek2->h ); tlo = load_bmp( "tlo.bmp", default_palette ); int ludek1_x = 100, ludek1_y = 100, ludek2_x = 400, ludek2_y = 100; int ludek1bfr_x = 100, ludek1bfr_y = 100, ludek2bfr_x = 400, ludek2bfr_y = 100; clear_to_color( ludek1bfr, makecol( 0, 0, 255 ) ); clear_to_color( ludek2bfr, makecol( 0, 0, 255 ) ); while( !key[ KEY_ESC ] ) { while( speed > 0 && !kolizja( bufor, ludek1_x, ludek1_y, ludek1->w, ludek1->h ) ) { if( key[ KEY_LEFT ] ) { ludek1_x--; if( kolizja( bufor2, ludek1bfr_x, ludek1bfr_y, ludek1bfr->w, ludek1bfr->h ) ) ludek1_x++; } if( key[ KEY_RIGHT ] ) { ludek1_x++; if( kolizja( bufor2, ludek1bfr_x, ludek1bfr_y, ludek1bfr->w, ludek1bfr->h ) ) ludek1_x--; } if( key[ KEY_UP ] ) { ludek1_y--; if( kolizja( bufor2, ludek1bfr_x, ludek1bfr_y, ludek1bfr->w, ludek1bfr->h ) ) ludek1_y++; } if( key[ KEY_DOWN ] ) { ludek1_y++; if( kolizja( bufor2, ludek1bfr_x, ludek1bfr_y, ludek1bfr->w, ludek1bfr->h ) ) ludek1_y--; } if( key[ KEY_A ] ) { ludek2_x--; if( kolizja( bufor2, ludek2bfr_x, ludek2bfr_y, ludek2bfr->w, ludek2bfr->h ) ) ludek2_x++; } if( key[ KEY_D ] ) { ludek2_x++; if( kolizja( bufor2, ludek2bfr_x, ludek2bfr_y, ludek2bfr->w, ludek2bfr->h ) ) ludek2_x--; } if( key[ KEY_W ] ) { ludek2_y--; if( kolizja( bufor2, ludek2bfr_x, ludek2bfr_y, ludek2bfr->w, ludek2bfr->h ) ) ludek2_y++; } if( key[ KEY_S ] ) { ludek2_y++; if( kolizja( bufor2, ludek2bfr_x, ludek2bfr_y, ludek2bfr->w, ludek2bfr->h ) ) ludek2_y--; } speed--; } ludek1bfr_x = ludek1_x; ludek1bfr_y = ludek1_y; ludek2bfr_x = ludek2_x; ludek2bfr_y = ludek2_y; clear_to_color( bufor, makecol( 200, 255, 200 ) ); clear_to_color( bufor2, makecol( 200, 255, 200 ) ); blit( tlo, bufor, 0, 0, 0, 0, tlo->w, tlo->h ); masked_blit( ludek1, bufor, 0, 0, ludek1_x, ludek1_y, ludek1->w, ludek1->h ); masked_blit( ludek2, bufor, 0, 0, ludek2_x, ludek2_y, ludek2->w, ludek2->h ); masked_blit( ludek1bfr, bufor2, 0, 0, ludek1bfr_x, ludek1bfr_y, ludek1bfr->w, ludek1bfr->h ); masked_blit( ludek2bfr, bufor2, 0, 0, ludek2bfr_x, ludek2bfr_y, ludek2bfr->w, ludek1bfr->h ); blit( bufor, screen, 0, 0, 0, 0, 800, 600 ); } remove_int( increment_speed ); destroy_bitmap( ludek1 ); destroy_bitmap( ludek2 ); destroy_bitmap( bufor ); allegro_exit(); return 0; } END_OF_MAIN();
|