Mam problem, który polega na wychodzeniu mojego pojazdu poza ekran. ktoś może pomóc?
#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 mapa_x = 0, mapa_y = 0;
BITMAP * teren = NULL;
BITMAP * bufor = NULL;
BITMAP * samochod = NULL;
BITMAP * tlo = NULL;
short int map[ 15 ][ 20 ] =
{
3, 3, 3, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 4, 3, 3,
3, 3, 3, 8, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 8, 3, 3,
3, 3, 3, 8, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 8, 3, 3,
3, 3, 3, 8, 0, 0, 7, 9, 9, 9, 9, 9, 9, 9, 4, 0, 0, 8, 3, 3,
3, 3, 3, 8, 0, 0, 8, 3, 3, 3, 3, 3, 3, 3, 8, 0, 0, 8, 3, 3,
3, 3, 3, 8, 0, 0, 8, 3, 3, 3, 3, 3, 3, 3, 8, 0, 0, 8, 3, 3,
3, 3, 3, 8, 0, 0, 8, 3, 3, 3, 3, 3, 3, 3, 8, 0, 0, 8, 3, 3,
3, 3, 3, 8, 0, 0, 8, 3, 3, 3, 3, 3, 3, 3, 8, 0, 0, 8, 3, 3,
3, 3, 3, 8, 0, 0, 8, 3, 3, 3, 3, 3, 3, 3, 8, 0, 0, 8, 3, 3,
3, 3, 3, 8, 10, 10, 8, 3, 3, 3, 3, 3, 3, 3, 8, 0, 0, 8, 3, 3,
3, 3, 3, 8, 0, 0, 8, 3, 3, 3, 3, 3, 3, 3, 8, 0, 0, 8, 3, 3,
3, 3, 3, 8, 0, 0, 6, 9, 9, 9, 9, 9, 9, 9, 5, 0, 0, 8, 3, 3,
3, 3, 3, 8, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 8, 3, 3,
3, 3, 3, 8, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 8, 3, 3,
3, 3, 3, 6, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 3, 3
};
void wys_mape()
{
int licznik_x, licznik_y;
for( licznik_x = 0; licznik_x < 20; licznik_x++ )
{
for( licznik_y = 0; licznik_y < 15; licznik_y++ )
{
blit( teren, bufor,
( map[ licznik_y + mapa_y ][ licznik_x + mapa_x ] % 4 ) * 40,
( map[ licznik_y + mapa_y ][ licznik_x + mapa_x ] / 4 ) * 40,
licznik_x * 40, licznik_y * 40, 40, 40 );
}
}
};
int main()
{
allegro_init();
install_keyboard();
set_color_depth( 16 );
set_gfx_mode( GFX_AUTODETECT_FULLSCREEN, 800, 600, 0, 0 );
install_timer();
install_int_ex( increment_speed, BPS_TO_TIMER( 40 ) );
int x = 200, y = 200;
double dx = 0, dy = 0;
int kierunek = 0;
double v = 0;
laduj_sin_cos();
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;
}
tlo = load_bmp( "tlo.bmp", default_palette );
if( !samochod ) {
set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 );
allegro_message( "nie mogê za³adowaæ tla !" );
allegro_exit();
return 0;
}
bufor = create_bitmap( 800, 600 );
if( !bufor )
{
set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 );
allegro_message( "Nie mogê utworzyæ bufora !" );
allegro_exit();
return 0;
}
teren = load_bmp( "teren.bmp", default_palette );
int samochod_x = 100, samochod_y = 100;
while( !key[ KEY_ESC ] )
{
while( speed > 0 )
{
if( key[ KEY_LEFT ] ) kierunek--;
if( key[ KEY_RIGHT ] ) kierunek++;
if( key[ KEY_UP ] ) v += 0.02;
if( key[ KEY_DOWN ] ) v -= 0.02;
if( key[ KEY_SPACE ] ) v = 0;
speed--;
}
if( kierunek > 71 ) kierunek = 0;
if( kierunek < 0 ) kierunek = 71;
if( v > 0.4 ) v = 0.4;
if( v <- 0.2 ) v =- 0.2;
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 ); }
masked_blit( samochod, bufor,( kierunek % 18 ) * 30,( kierunek / 18 ) * 30, x, y, 30, 30 );
blit( bufor, screen, 0, 0, 0, 0, 800, 600 );
clear_to_color( bufor, makecol( 150, 150, 150 ) );
wys_mape();
}
remove_int( increment_speed );
destroy_bitmap( teren );
destroy_bitmap( bufor );
destroy_bitmap( samochod );
allegro_exit();
return 0;
}
END_OF_MAIN();