SeaMonster131 |
» 2012-06-06 23:24:08 To pokaż aktualny kod jak wygląda? |
|
matiasz Temat założony przez niniejszego użytkownika |
» 2012-06-06 23:45:47 Tak kod wygląda teraz #include <allegro.h>
int x, y, i, shot = 0; int gora = false; int dol = false; int lewo = false; int prawo = false; const int MAX_BULLETS = 100;
BITMAP * obrazek = NULL; BITMAP * ludek = NULL; BITMAP * ludek01 = NULL; BITMAP * ludek02 = NULL; BITMAP * ludek03 = NULL; BITMAP * ludek04 = NULL; BITMAP * pocisk = NULL; BITMAP * bufor = NULL;
typedef struct some_object { int x, y, alive; int gora; int dol; int lewo; int prawo; } some_object;
some_object bullets[ MAX_BULLETS ];
int add_bullet( int x, int y ) { int i; for( i = 0; i < MAX_BULLETS; i++ ) { if( bullets[ i ].gora == 0 ) { bullets[ i ].x = x; bullets[ i ].y = y; return i; } if( bullets[ i ].dol == 0 ) { bullets[ i ].x = x; bullets[ i ].y = y; return i; } if( bullets[ i ].lewo == 0 ) { bullets[ i ].x = x; bullets[ i ].y = y; return i; } if( bullets[ i ].prawo == 0 ) { bullets[ i ].x = x; bullets[ i ].y = y; return i; } } return( 0 ); }
inline void init() { allegro_init(); set_color_depth( 16 ); set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0 ); install_timer(); install_keyboard(); install_mouse(); }
inline void deinit() { clear_keybuf(); allegro_exit(); } bool kolizja( 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 return false; };
volatile long speed = 0; void increment_speed() { speed++; } END_OF_FUNCTION( increment_speed );
LOCK_VARIABLE( speed ); LOCK_FUNCTION( increment_speed );
int main() { init(); obrazek = load_bmp( "obrazek.bmp", default_palette ); if( !obrazek ) { set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 ); allegro_message( "nie moge za³adowaæ obrazka" ); allegro_exit(); } ludek = load_bmp( "ludek.bmp", default_palette ); if( !ludek ) { set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 ); allegro_message( "nie mogê za³adowaæ ludek !" ); allegro_exit(); return 0; } ludek01 = load_bmp( "ludek02.bmp", default_palette ); ludek02 = load_bmp( "ludek01.bmp", default_palette ); ludek03 = load_bmp( "ludek03.bmp", default_palette ); ludek04 = load_bmp( "ludek04.bmp", default_palette ); pocisk = load_bmp( "pocisk.bmp", default_palette ); bufor = create_bitmap( 640, 480 ); if( !bufor ) { set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 ); allegro_message( "Nie mogê utworzyæ bufora !" ); allegro_exit(); return 0; } char kierunek = 1; int obrazek_x = 0; int obrazek_y = 400; int ludek_x = 300; int ludek_y = 200; int frame = 1; int shoot = 0; int over = 0; int temp; install_timer(); install_int_ex( increment_speed, BPS_TO_TIMER( 100 ) ); for( i = 0; i < MAX_BULLETS; i++ ) { bullets[ i ].gora = 0; bullets[ i ].dol = 0; bullets[ i ].lewo = 0; bullets[ i ].prawo = 0; } while( !key[ KEY_ESC ] ) { for( i = 0; i < MAX_BULLETS; i++ ) { if( bullets[ i ].gora == 1 ) { blit( pocisk, bufor, 0, 0, bullets[ i ].x, bullets[ i ].y, 4, 4 ); } if( bullets[ i ].dol == 1 ) { blit( pocisk, bufor, 0, 0, bullets[ i ].x, bullets[ i ].y, 4, 4 ); } if( bullets[ i ].lewo == 1 ) { blit( pocisk, bufor, 0, 0, bullets[ i ].x, bullets[ i ].y, 4, 4 ); } if( bullets[ i ].prawo == 1 ) { blit( pocisk, bufor, 0, 0, bullets[ i ].x, bullets[ i ].y, 4, 4 ); } } if( key[ KEY_SPACE ] ) { if( shot == 0 ) { temp = add_bullet( ludek_x, ludek_y + 70 ); shot = 1; bullets[ temp ].gora = 1; bullets[ temp ].dol = 1; bullets[ temp ].lewo = 1; bullets[ temp ].prawo = 1; } } if( !key[ KEY_SPACE ] ) { if( shot == 1 ) { shot = 0; } } for( i = 0; i < MAX_BULLETS; i++ ) { if( bullets[ i ].prawo && gora == true ) { bullets[ i ].y--; } if( bullets[ i ].dol && dol == true ) { bullets[ i ].y++; } if( bullets[ i ].lewo && lewo == true ) { bullets[ i ].x++; } if( bullets[ i ].prawo && prawo == true ) { bullets[ i ].x--; } } while( speed > 0 ) { obrazek_x += kierunek; if( obrazek_x > 600 ) { kierunek = - 1; } else if( obrazek_x < 0 ) { kierunek = 1; } if( key[ KEY_UP ] ) { ludek_y--; frame = 20; if( ludek_y == 0 ) { ludek_y++; } } if( key[ KEY_DOWN ] ) { ludek_y++; frame = 40; if( ludek_y == 440 ) { ludek_y--; } } if( key[ KEY_LEFT ] ) { ludek_x--; frame = 80; if( ludek_x == 0 ) { ludek_x++; } } if( key[ KEY_RIGHT ] ) { ludek_x++; frame = 60; if( ludek_x == 600 ) { ludek_x--; } } speed--; } blit( bufor, screen, 0, 0, 0, 0, 640, 480 ); clear_to_color( bufor, makecol( 150, 150, 150 ) ); masked_blit( obrazek, bufor, 0, 0, obrazek_x, obrazek_y, obrazek->w, obrazek->h ); if( frame == 20 ) { draw_sprite( bufor, ludek01, ludek_x, ludek_y ); gora = true; dol = false; prawo = false; lewo = false; } else if( frame == 40 ) { draw_sprite( bufor, ludek02, ludek_x, ludek_y ); gora = false; dol = true; prawo = false; lewo = false; } else if( frame == 60 ) { draw_sprite( bufor, ludek03, ludek_x, ludek_y ); gora = false; dol = false; prawo = false; lewo = true; } else if( frame == 80 ) { draw_sprite( bufor, ludek04, ludek_x, ludek_y ); gora = false; dol = false; prawo = true; lewo = false; } } deinit(); destroy_bitmap( ludek ); return 0; } END_OF_MAIN() |
|
SeaMonster131 |
» 2012-06-06 23:56:49 Ja napisałem inaczej kod :) To tak, po pierwsze tutaj: for( i = 0; i < MAX_BULLETS; i++ ) { if( bullets[ i ].gora == 0 ) { bullets[ i ].x = x; bullets[ i ].y = y; return i; } if( bullets[ i ].dol == 0 ) { bullets[ i ].x = x; bullets[ i ].y = y; return i; } if( bullets[ i ].lewo == 0 ) { bullets[ i ].x = x; bullets[ i ].y = y; return i; } if( bullets[ i ].prawo == 0 ) { bullets[ i ].x = x; bullets[ i ].y = y; return i; } }
po co dorobiłeś te if'y, skoro w każdym jest to samo ? Tak samo tu: for( i = 0; i < MAX_BULLETS; i++ ) { if( bullets[ i ].gora == 1 ) { blit( pocisk, bufor, 0, 0, bullets[ i ].x, bullets[ i ].y, 4, 4 ); } if( bullets[ i ].dol == 1 ) { blit( pocisk, bufor, 0, 0, bullets[ i ].x, bullets[ i ].y, 4, 4 ); } if( bullets[ i ].lewo == 1 ) { blit( pocisk, bufor, 0, 0, bullets[ i ].x, bullets[ i ].y, 4, 4 ); } if( bullets[ i ].prawo == 1 ) { blit( pocisk, bufor, 0, 0, bullets[ i ].x, bullets[ i ].y, 4, 4 ); } }
Jak te if'y są Ci potrzebne, to lepiej połączyć je np w 1 większy if i już.. Po drugie tutaj: if( shot == 0 ) { temp = add_bullet( ludek_x, ludek_y + 70 ); shot = 1; bullets[ temp ].gora = 1; bullets[ temp ].dol = 1; bullets[ temp ].lewo = 1; bullets[ temp ].prawo = 1; }
dlaczego pocisk ma lecieć we wszystkich kierunkach? Oraz jeszcze tutaj: for( i = 0; i < MAX_BULLETS; i++ ) { if( bullets[ i ].prawo && gora == true ) { bullets[ i ].y--; } if( bullets[ i ].dol && dol == true ) { bullets[ i ].y++; } if( bullets[ i ].lewo && lewo == true ) { bullets[ i ].x++; } if( bullets[ i ].prawo && prawo == true ) { bullets[ i ].x--; } }
dlaczego dodałeś np "&& gora == true" itd? |
|
matiasz Temat założony przez niniejszego użytkownika |
» 2012-06-07 09:19:11 if( bullets[ i ].prawo && prawo == true ) To jak ma sprawdzić ,w którą stronę ludzik jest obrócony i w jakim kierunku ma lecieć ? Np. prawo == true << ludzik obrócony jest w prawo i ma strzelać w prawą stronę. Dobrze to jest czy nie ? |
|
SeaMonster131 |
» 2012-06-07 10:41:07 To raczej zrób: if( strzal ) { bullets[ i ].prawo = prawo; bullets[ i ].lewo = lewo; ... }
Żeby kierunek pocisku nadawać tylko i wyłącznie w momencie strzału, a nie podczas lotu pocisku. |
|
matiasz Temat założony przez niniejszego użytkownika |
» 2012-06-07 10:52:10 Dzięki mistrzu! Teraz działa! |
|
SeaMonster131 |
» 2012-06-07 11:00:09 Nie ma za co, ale jak problem rozwiązany to zamyka się temat ;) |
|
1 « 2 » |