Camper Temat założony przez niniejszego użytkownika |
» 2009-06-05 22:27:55 Wkleję cały kod, może kogoś zaciekawi i mi pomoże ;> Chodzi mi o samo AI. Więc zjedź do "//AI" Zatrzymywanie polega na dojechaniu do x, jeśli x się zgadza to przechodzi do sprawdzania y, jeśli y się zgadza to ustawia "chodzenie" na false, jeśli chodzenie jest false to pętla zaczyna się od nowa, wybiera randomy, przypisuje pozycje i oznacza chodzenie na true. Liczy kąt itd. i sprawdza czy x się zgadza. itd. Według mnie powinno być dobrze, ale gdzieś jest błąd którego nie potrafię wyłapać. Dzięki za fatygę. class Cpostac { public: short int x, y; short int s, w; };
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; };
Cpostac player1, bot, x; #include <allegro.h> #include <cmath> #include <cstdlib> void srand( unsigned seed );
int random( int nMin, int nMax ) { return rand() % nMax + nMin; }
volatile long speed = 0; void increment_speed() { speed++; } END_OF_FUNCTION( increment_speed ); LOCK_VARIABLE( speed ); LOCK_FUNCTION( increment_speed );
int mx, my, mb;
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, 800, 600, 0, 0 ); install_timer(); install_int_ex( increment_speed, BPS_TO_TIMER( 1000 ) ); install_sound( DIGI_AUTODETECT, MIDI_AUTODETECT, "" ); set_volume( 255, 255 ); install_mouse(); show_mouse( screen ); unscare_mouse(); BITMAP * bufor = NULL; BITMAP * player1 = NULL; BITMAP * bot = NULL; SAMPLE * dzwiek = NULL; BITMAP * x = NULL; BITMAP * hp1 = NULL; BITMAP * hp2 = NULL; BITMAP * hp11 = NULL; BITMAP * hp22 = NULL; BITMAP * xb = NULL; hp1 = create_bitmap( 20, 5 ); hp2 = create_bitmap( 20, 5 ); clear_to_color( hp1, makecol( 255, 0, 0 ) ); clear_to_color( hp2, makecol( 255, 0, 0 ) ); bufor = create_bitmap( 800, 600 ); x = load_bmp( "strzal.bmp", default_palette ); xb = load_bmp( "strzal.bmp", default_palette ); dzwiek = load_sample( "dzwiek.wav" ); player1 = load_bmp( "ludyik1.bmp", default_palette ); bot = load_bmp( "ludyik2.bmp", default_palette ); int bot_x = 200, bot_y = 200, bot_s = 20, bot_w = 20, bot_hp = 100; int player1_x = 400, player1_y = 300, player1_s = 20, player1_w = 20, player1_hp = 100; bool strzalv = false, strzalbv = false, chodzenie = false; double predkosc = 0.5; double predkosc2 = 0.1; double kat, katb, katb2; double ix, iy, ibx, iby, ib2x, ib2y; int i_x, i_y, ib_x, ib_y, ib2_x, ib2_y; int is = 20, iw = 20, ibs = 20, ibw = 20; int celx, cely, celbx, celby, celb2x, celb2y; double deltaX, deltaY, deltabX, deltabY, deltab2X, deltab2Y; int a, b; while( !key[ KEY_ESC ] ) { hp11 = create_bitmap( bot_hp / 4, 5 ); hp22 = create_bitmap( player1_hp / 4, 5 ); clear_to_color( hp11, makecol( 0, 255, 0 ) ); clear_to_color( hp22, makecol( 0, 255, 0 ) ); clear_to_color( bufor, makecol( 150, 150, 150 ) ); myszka(); while( speed > 0 ) { textprintf_ex( bufor, font, a, b, makecol( 200, 200, 200 ), - 1, "Myszka x = %d, y = %d, klawisz = %d !", a, b, mb ); if( strzalv == false ) { if( mb == 1 ) { ix = player1_x; iy = player1_y; celx = mx; cely = my; deltaX = ix - celx; deltaY = iy - cely; strzalv = true; } } if(( deltaY ) == 0 ) { kat = 0; } else { kat = atan(( deltaX ) /( deltaY ) ); } if( deltaY >= 0 ) { kat += M_PI; } if( strzalv == true ) { if( iy != cely || ix != celx ) { ix += sin( kat ) * predkosc; iy += cos( kat ) * predkosc; } } if( iy <- 20 || iy > 620 || ix <- 20 || ix > 820 ) { strzalv = false; } if( strzalbv == false ) { if(( bot_x - player1_x ) < 300 &&( bot_x - player1_x ) >- 300 &&( bot_y - player1_y ) < 300 &&( bot_y - player1_y ) >- 300 ) { ibx = bot_x; iby = bot_y; celbx = player1_x; celby = player1_y; deltabX = ibx - celbx; deltabY = iby - celby; strzalbv = true; } } if(( deltabY ) == 0 ) { katb = 0; } else { katb = atan(( deltabX ) /( deltabY ) ); } if( deltabY >= 0 ) { katb += M_PI; } if( strzalbv == true ) { if( iby != celby || ibx != celbx ) { ibx += sin( katb ) * predkosc; iby += cos( katb ) * predkosc; } } if( iby <- 20 || iby > 620 || ibx <- 20 || ibx > 820 ) { strzalbv = false; } if( chodzenie == false ) { srand( time( NULL ) ); a = random( 50, 750 ); b = random( 50, 450 ); } if( chodzenie == false ) { if(( a + b ) > 0 ) { ib2x = bot_x; ib2y = bot_y; celb2x = a; celb2y = b; deltab2X = ib2x - celb2x; deltab2Y = ib2y - celb2y; chodzenie = true; } } if(( deltab2Y ) == 0 ) { katb2 = 0; } else { katb2 = atan(( deltab2X ) /( deltab2Y ) ); } if( deltab2Y >= 0 ) { katb2 += M_PI; } if( chodzenie == true ) { if( ib2x != celb2x ) { ib2x += sin( katb2 ) * predkosc2; } if(( celb2x - ib2x ) < predkosc2 ) { ib2x = celb2x; } if( ib2x == celb2x ) { if( ib2y != celb2y ) { ib2y += cos( katb2 ) * predkosc2; } } if(( celb2y - ib2y ) < predkosc2 ) { ib2y = celb2y; } if( ib2y == celb2y ) { chodzenie = false; } } bot_x = ib2x; bot_y = ib2y; draw_sprite( bufor, bot, bot_x, bot_y ); ib_x = ibx; ib_y = iby; masked_blit( xb, bufor, 0, 0,( int ) ibx,( int ) iby, 200, 200 ); speed--; i_x = ix; i_y = iy; masked_blit( x, bufor, 0, 0,( int ) ix,( int ) iy, 200, 200 ); } if( key[ KEY_LEFT ] ) player1_x = player1_x - 2; if( key[ KEY_RIGHT ] ) player1_x = player1_x + 2; if( key[ KEY_UP ] ) player1_y = player1_y - 2; if( key[ KEY_DOWN ] ) player1_y = player1_y + 2; draw_sprite( bufor, player1, player1_x, player1_y ); blit( hp1, bufor, 0, 0, bot_x, bot_y + 21, hp1->w, hp1->h ); blit( hp2, bufor, 0, 0, player1_x, player1_y - 6, hp2->w, hp2->h ); blit( hp11, bufor, 0, 0, bot_x, bot_y + 21, hp11->w, hp11->h ); blit( hp22, bufor, 0, 0, player1_x, player1_y - 6, hp22->w, hp22->h ); blit( bufor, screen, 0, 0, 0, 0, 800, 600 ); clear_keybuf(); if( kolizja( bot_x, bot_y, bot_s, bot_w, i_x, i_y, is, iw ) == true ) { play_sample( dzwiek, 255, 127, 3000, 0 ); bot_hp--; } if( kolizja( player1_x, player1_y, player1_s, player1_w, ib_x, ib_y, ibs, ibw ) == true ) { play_sample( dzwiek, 255, 127, 3000, 0 ); player1_hp--; } } destroy_bitmap( bufor ); destroy_bitmap( player1 ); destroy_bitmap( x ); destroy_bitmap( bot ); destroy_sample( dzwiek ); allegro_exit(); return 0; } END_OF_MAIN(); |