Oto kod całej gry.
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;
};
#include <allegro.h>
volatile long speed = 0;
void increment_speed()
{
speed++;
}
END_OF_FUNCTION( increment_speed );
LOCK_VARIABLE( speed );
LOCK_FUNCTION( increment_speed );
volatile long szybkosc = 0;
void increment_szybkosc()
{
szybkosc++;
}
END_OF_FUNCTION( increment_szybkosc );
LOCK_VARIABLE( szybkosc );
LOCK_FUNCTION( increment_szybkosc );
int main()
{
allegro_init();
install_keyboard();
set_color_depth( 16 );
set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0 );
clear_to_color( screen, makecol( 255, 255, 255 ) );
install_timer();
install_int_ex( increment_speed, BPS_TO_TIMER( 35 ) );
install_int_ex( increment_szybkosc, BPS_TO_TIMER( 200 ) );
BITMAP * rura = NULL;
BITMAP * ludek = NULL;
BITMAP * ziemia = NULL;
BITMAP * bufor = NULL;
bufor = create_bitmap( 800, 600 );
rura = load_bmp( "rura.bmp", default_palette );
ludek = load_bmp( "ludek.bmp", default_palette );
ziemia = load_bmp( "ziemia.bmp", default_palette );
if( !bufor )
{
set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 );
allegro_message( "nie moge zaladowac bufora" );
allegro_exit();
return 0;
}
if( !ludek &&!ziemia )
{
set_gfx_mode( GFX_TEXT, 0, 0, 0, 0 );
allegro_message( "niemożna załadowac obrazka" );
allegro_exit();
return 0;
}
int ludek_x = 100, ludek_y = 100;
int rura_x = 400, rura_y = 440;
const float poziomgruntu = 435;
static float predkosc = 0;
static float predkoscx = 0;
while( !key[ KEY_ESC ] )
{
while( szybkosc > 0 )
{
if( key[ KEY_LEFT ] ) ludek_x--;
if( key[ KEY_RIGHT ] ) ludek_x++;
while( speed > 0 )
{
if( key[ KEY_UP ] && key[ KEY_RIGHT ] && ludek_y == poziomgruntu )
{
predkosc =- 10;
predkoscx =- 2;
}
ludek_y += predkosc;
ludek_x -= predkoscx;
predkosc += 0.3;
if( ludek_y >= poziomgruntu )
{
predkosc = 0;
predkoscx = 0;
ludek_y = poziomgruntu;
}
if( key[ KEY_UP ] && key[ KEY_LEFT ] && ludek_y == poziomgruntu )
{
predkosc =- 10;
predkoscx =+ 2;
}
ludek_y += predkosc;
ludek_x -= predkoscx;
predkosc += 0.3;
if( ludek_y >= poziomgruntu )
{
predkosc = 0;
predkoscx = 0;
ludek_y = poziomgruntu;
}
speed--;
}
if( kolizja( ludek_x, ludek_y, 100, 100, rura_x, rura_y, 104, 104 ) )
{ }
szybkosc--;
}
clear_to_color( bufor, makecol( 111, 133, 255 ) );
masked_blit( ludek, bufor, 0, 0, ludek_x, ludek_y, ludek->w, ludek->h );
blit( ziemia, bufor, 0, 0, 0, 526, ziemia->w, ziemia->h );
masked_blit( rura, bufor, 0, 0, rura_x, rura_y, rura->w, rura->h );
blit( bufor, screen, 0, 0, 0, 0, 800, 600 );
}
destroy_bitmap( ludek );
destroy_bitmap( ziemia );
destroy_bitmap( bufor );
destroy_bitmap( rura );
readkey();
allegro_exit();
return 0;
}
END_OF_MAIN();