Ju1ian Temat założony przez niniejszego użytkownika |
[Allegro] Scrolling mapy cd. mały problem » 2008-06-28 11:13:09 Tworzę tą grę i tworzę, mam nadzieję że ją ukończę. Ostatnio podałeś mi kod do scrollingu mapy. Problem polega na tym, że postać nie stoi na środku ekranu tylko na środek+10. Próbowałem już dużo zamieniać, ale nic mi to nie dało... Oto kod do scrollingu: if( ludek.x < 0 ) ludek.x = 0;
if( ludek.x > koniec + ekran_x - 60 ) ludek.x = koniec + ekran_x - 60;
scrolling_x = ludek.x - ekran_x / 2; if( scrolling_x < 0 ) scrolling_x = 0;
if( scrolling_x > koniec ) scrolling_x = koniec; I jeszcze 2 pytanie: Jak zmienić długość mapy, od razu podam cały zagmatwany kod: 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 ); }; bool kolizja2( int x1, int y1, int s1, int w1, int x2, int y2, int s2, int w2 ) { if( x2 <= x1 + s1 && x2 > x1 && y2 >= y1 && y2 <= y1 + w1 ) return true; else if( x2 <= x1 + s1 && x2 > x1 && y2 + w2 >= y1 && y2 + w2 <= y1 + w1 ) return true; else if( x2 + s2 <= x1 + s1 && x2 + s2 > x1 && y2 >= y1 && y2 <= y1 + w1 ) return true; else if( x2 + s2 <= x1 + s1 && x2 + s2 > x1 && y2 + w2 >= y1 && y2 + w2 <= y1 + w1 ) return true; else if( x1 + s1 >= x2 && x1 <= x2 + s2 && y1 + w1 >= y2 && y1 <= y2 + w2 ) return true; else return false; };
#include <allegro.h> #include <math.h> class Cpostac { public: int x, y; short int kierunek, klatka; };
Cpostac ludek;
volatile long speed = 0; void increment_speed() { speed++; } END_OF_FUNCTION( increment_speed ); LOCK_VARIABLE( speed ); LOCK_FUNCTION( increment_speed ); int przycisk_x = 50, przycisk_y = 300;
int mx = 0, my = 0, mb = 0; void myszka() { if( mx != mouse_x || my != mouse_y || mb != mouse_b ) { mx = mouse_x; my = mouse_y; mb = mouse_b; } };
int main() { allegro_init(); install_keyboard(); set_color_depth( 16 ); set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0 ); install_timer(); install_int_ex( increment_speed, BPS_TO_TIMER( 80 ) ); install_mouse(); show_mouse( screen ); unscare_mouse(); BITMAP * klocki = NULL; BITMAP * niebo = NULL; BITMAP * ludekb = NULL; BITMAP * bufor = NULL; BITMAP * platfa = NULL; BITMAP * drzewo = NULL; BITMAP * kolce = NULL; BITMAP * przegrana = NULL; int zycie = 100; bufor = load_bmp( "data/gfx/bufor.bmp", default_palette ); if( !bufor ) { set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 ); allegro_message( "Nie moge utworzyc bufora !" ); allegro_exit(); return 0; } klocki = load_bmp( "data/gfx/klocki.bmp", default_palette ); ludekb = load_bmp( "data/gfx/ludek.bmp", default_palette ); niebo = load_bmp( "data/gfx/niebo.bmp", default_palette ); platfa = load_bmp( "data/gfx/platforma.bmp", default_palette ); drzewo = load_bmp( "data/gfx/drzewo.bmp", default_palette ); kolce = load_bmp( "data/gfx/kolce.bmp", default_palette ); przegrana = load_bmp( "data/gfx/przegrales.bmp", default_palette ); if( !ludekb ) { set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 ); allegro_message( "Nie moge zaladowac obrazka Ludek !" ); allegro_exit(); return 0; } int klocki_x = 0, klocki_y = 370; int klocki2_x = 0, klocki2_y = 370; int platfa_x = 350, platfa_y = 250; int drzewo_x = 800, drzewo_y = 80; int kolce_x = 700, kolce_y = 350; int przegrana_x = 75, przegrana_y = 175; float ludek_ruch_y = 8; int niebo_x = 0, niebo_y = 0; int niebo2_x = 0, niebo2_y = 0; ludek.y = 0; ludek.x = 100; int frame = 0; ludek.klatka = 0; ludek.kierunek = 0; int mapa_x = 0, mapa2_x = 0; int ekran_x = 640; int obraz_x = ekran_x; int scrolling_x = 0; long int koniec = 640; int amplituda = 30; float t = 0, w = M_PI * 0.9; while( !key[ KEY_ESC ] ) { niebo_x = - mapa2_x % ekran_x; niebo2_x = ekran_x - mapa2_x % ekran_x; klocki_x = - mapa_x % ekran_x; klocki2_x = ekran_x - mapa_x % ekran_x; kolce_y = amplituda * sin( w * t ) + klocki_y + 10; t += 0.01; while( speed > 0 ) { if( key[ KEY_RIGHT ] ) { if( key[ KEY_X ] ) { ludek.x += 4; } else { ludek.x += 2; } ludek.kierunek = 2; } else if( key[ KEY_LEFT ] ) { if( key[ KEY_X ] ) { ludek.x -= 4; } else { ludek.x -= 2; } ludek.kierunek = 3; } else { ludek.kierunek = 0; } if( ludek.x < 0 ) ludek.x = 0; if( ludek.x > koniec + ekran_x - 60 ) ludek.x = koniec + ekran_x - 60; scrolling_x = ludek.x - ekran_x / 2; if( scrolling_x < 0 ) scrolling_x = 0; if( scrolling_x > koniec ) scrolling_x = koniec; myszka(); if( ludek.y != klocki_y - 53 && kolizja( ludek.x, ludek.y, 60, 60, platfa_x, platfa_y, platfa->w, platfa->h ) == false ) ludek.kierunek = 1; if( kolizja2( ludek.x + 15, ludek.y, 35, 40, kolce_x, kolce_y, kolce->w, kolce->h ) == true ) { blit( przegrana, bufor, 0, 0, przegrana_x, przegrana_y, przegrana->w, przegrana->h ); blit( bufor, screen, 0, 0, 0, 0, 640, 480 ); ludek.x = 100; rest( 5000 ); } if(( key[ KEY_UP ] ) &&( ludek_ruch_y == 0 ) &&( ludek.y == klocki_y - 53 || ludek.y == platfa_y - 52 ) ) { ludek_ruch_y =- 8; } ludek.y += ludek_ruch_y; if(( ludek.y + ludek_ruch_y >= klocki_y - 53 ) &&( ludek.y <= klocki_y - 53 ) ) { ludek_ruch_y = 0; ludek.y = klocki_y - 53; } else { ludek_ruch_y += 0.2; } if( kolizja( ludek.x, ludek.y, 60, 60, platfa_x, platfa_y, platfa->w, platfa->h ) == true ) { if(( ludek_ruch_y > 0 ) &&( ludek.y >= platfa_y - 52 ) &&( ludek.y - ludek_ruch_y <= platfa_y - 52 ) ) { ludek_ruch_y = 0; ludek.y = platfa_y - 52; } } speed--; if(( key[ KEY_LEFT ] || key[ KEY_RIGHT ] ||( ludek.y != klocki_y - 53 && kolizja( ludek.x, ludek.y, 60, 60, platfa_x, platfa_y, platfa->w, platfa->h ) == false ) ) && !key[ KEY_X ] ) { frame += 1; } else if(( key[ KEY_LEFT ] || key[ KEY_RIGHT ] ||( ludek.y != klocki_y - 53 && kolizja( ludek.x, ludek.y, 60, 60, platfa_x, platfa_y, platfa->w, platfa->h ) == false ) ) && key[ KEY_X ] ) { frame += 4; } if( frame > 40 ) frame = 0; } if( frame < 20 ) { ludek.klatka = 0; } else if( frame >= 20 && frame < 40 ) { ludek.klatka = 1; } clear_to_color( bufor, makecol( 150, 255, 0 ) ); blit( niebo, bufor, 0, 0, niebo_x - scrolling_x, niebo_y, niebo->w, niebo->h ); blit( niebo, bufor, 0, 0, niebo2_x - scrolling_x, niebo2_y, niebo->w, niebo->h ); masked_blit( platfa, bufor, 0, 0, platfa_x - scrolling_x, platfa_y, platfa->w, platfa->h ); masked_blit( ludekb, bufor, ludek.kierunek * 60, ludek.klatka * 60, ludek.x - scrolling_x, ludek.y, 60, 60 ); masked_blit( drzewo, bufor, 0, 0, drzewo_x - scrolling_x, drzewo_y, drzewo->w, drzewo->h ); masked_blit( kolce, bufor, 0, 0, kolce_x - scrolling_x, kolce_y, kolce->w, kolce->h ); blit( klocki, bufor, 0, 0, klocki_x - scrolling_x, klocki_y, klocki->w, klocki->h ); blit( klocki, bufor, 0, 0, klocki2_x - scrolling_x, klocki2_y, klocki->w, klocki->h ); blit( bufor, screen, 0, 0, 0, 0, 640, 480 ); } if( key[ KEY_ESC ] ) { allegro_message( "No dobra..." ); } destroy_bitmap( klocki ); destroy_bitmap( niebo ); destroy_bitmap( ludekb ); destroy_bitmap( bufor ); destroy_bitmap( platfa ); destroy_bitmap( drzewo ); destroy_bitmap( kolce ); allegro_exit(); return 0; } END_OF_MAIN(); |