Allegro bitmap collision? how? [Archive] - C Board

PDA

View Full Version : Allegro bitmap collision? how?


elfjuice
06-08-2002, 06:04 PM
I am making a pong game and this is what i got so far


#include <allegro.h>

int x = 5;
int y = 0;
int counter;
int down = 0, up = 0;
int x2 = 615;
int y2 = 0;
int counter2;
int down2 = 0, up2 = 0;
int play1scor = 0;
int play2scor = 0;
int ballx = 300, bally = 190;

void update_counter()
{
counter++;
}
END_OF_FUNCTION(update_counter);

void update_counter2()
{
counter2++;
}
END_OF_FUNCTION(update_counter2);

int main()
{
allegro_init();
install_keyboard();

LOCK_VARIABLE(counter);
LOCK_FUNCTION(update_counter);
install_int_ex( update_counter, BPS_TO_TIMER(60));

LOCK_VARIABLE(counter2);
LOCK_FUNCTION(update_counter2);
install_int_ex( update_counter2, BPS_TO_TIMER(60));

set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);

BITMAP *bar = load_bitmap( "sprite/bar.bmp", NULL );
BITMAP *logo = load_bitmap( "sprite/logo.bmp", NULL );
BITMAP *ball = load_bitmap( "sprite/ball.bmp", NULL );

while ( !key[KEY_ESC] )
{

while ( counter > 0 )
{
if (down) y-=7;
if (up) y+=7;
counter--;
}
if ( y > 0 && key[KEY_UP])
down = 1;
else
down = 0;
if ( y < 375 && key[KEY_DOWN])
up = 1;
else
up = 0;
while ( counter2 > 0 )
{
if (down2) y2-=7;
if (up2) y2+=7;
counter2--;
}
if ( y2 > 0 && key[KEY_A])
down2 = 1;
else
down2 = 0;
if ( y2 < 375 && key[KEY_Z])
up2 = 1;
else
up2 = 0;

vsync();
clear_to_color (screen, 0);
draw_sprite ( screen, bar, x, y );
draw_sprite ( screen, bar, x2, y2 );
draw_sprite ( screen, logo, 245, 200 );
draw_sprite ( screen, ball, ballx, bally );

}


destroy_bitmap(bar);
destroy_bitmap(logo);
allegro_exit();
return 0;
}
END_OF_MAIN();


how do i make the ball move and... you know... do what the pong ball does?

thanks

JoshG
06-08-2002, 07:21 PM
That is not really a bitmap collision question :). Umm, Your code is hard to understand, this is how I would do a pong game:


while(!quit)
{
getuserinput(); // Get the input, left, right, or up and down
parseinput(); // Update the coordinates of the paddles
updateball(); // Have this function update the coordinates of the ball
drawscreen();
}


You could combine getuserinput and parseinput. Update ball should detect if the ball has hit a paddle, and have it change direction accordingly. I do not know about the calculations and stuff needed in pong, but it should handle all that. drawscreen should display the screen, the start the cycle over.

TechWins
06-08-2002, 07:23 PM
Just to let you know your bitmaps have to be likes this:

BITMAP *bar = load_bitmap( "sprite//bar.bmp", NULL );

instead of

BITMAP *bar = load_bitmap( "sprite/bar.bmp", NULL );


notice the extra / i have in there...well you have to have two in there.

JoshG
06-08-2002, 07:38 PM
I thought it was a backslash

load_bitmap("c:\\images\\bitmap.bmp");

elfjuice
06-08-2002, 08:51 PM
Ok... lol... i get this a lot... but this is my <b>first</b> day of allegro... so don't expect me to know anything ;)

I'll try to make my code a little cleaner/better.

thanks

kas2002
07-06-2002, 11:45 AM
u might want to destroy all your bitmaps too eh