Code:
#include <c:\dev-cpp\source\FallingBall.h>
void drawback(){
rectfill(active_page,0,Floors.y,SCREEN_W,SCREEN_H,makecol(255,0,0));
textprintf(active_page,font,0,0,makecol(255,255,255),"Lives: %i Score: %i",lives,score);
}
void movemouse(){
circle(active_page,mouse_x,mouse_y,BallSize / 2,makecol(255,0,0));
}
void mousehold(){
set_mouse_range(0 + BallSize-3,0 + BallSize-3,SCREEN_W - BallSize+3,Floors.y - BallSize+3);
if (mouse_b & 1 && mouseh < 50){
mouseh++;
return;
}
if (mouseh >= 50){
mousex = mouse_x;
mousey = mouse_y;
while(mouse_b & 1){
textprintf(screen,font,SCREEN_W/2 - (4*19),SCREEN_H/2,makecol(255,255,255),"Dont Hold The Mouse");
}
position_mouse(mousex,mousey);
}
if(!mouse_b & 1){
mouseh = 0;
}
}
void moveball(){
//circlefill(active_page,Balls[n].x,Balls[n].y,BallSize,makecol(0,0,255));
draw_sprite(active_page,ball[i/2],Balls[n].x,Balls[n].y);
Balls[n].y = Balls[n].y + BallSpeed;//Balls.speed;
return;
}
void checkdead(){
if (mouse_x > Balls[n].x - BallSize && mouse_x < Balls[n].x + BallSize){
if (mouse_y > Balls[n].y - BallSize && mouse_y < Balls[n].y + BallSize){
if (mouse_b & 1){
dead[n] = 1;
score += 10;
}
}
}
if (Balls[n].y > Floors.y - BallSize)
{
Balls[n].y = 0 + BallSize;
Balls[n].x = rand() % (SCREEN_W - (BallSize )) + BallSize;
lives--;
}
if (dead[n] != 0){
dropball();
dead[n] = 0;
}
}
void movefloor(){
Floors.y = Floors.y - 5;
}
void dropball(){
Balls[n].y = 0 + BallSize;
Balls[n].x = rand() % SCREEN_W;
movefloor();
return;
}
void setupscreen()
{
int ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
if (ret != 0) {
allegro_message(allegro_error);
return;
}
}
BITMAP *grabframe(BITMAP *source,int width,
int height,int startx,
int starty,int columns,
int frame)
{
BITMAP *temp = create_bitmap(width,height);
int x = startx + (frame ) * width;
//int y = starty + (frame ) * height;
blit(source,temp,x,0,0,0,width,height);
return temp;
}
void loadsprites(void)
{
//temp = (BITMAP *)data[BALL_BMP].dat;
temp = load_bitmap("ball.bmp",NULL);
for (n = 0;n<6;n++)
ball[n] = grabframe(temp,17,17,0,0,1,n);
destroy_bitmap(temp);
background = load_bitmap("background.bmp",NULL);
}
int main()
{
allegro_init();
set_color_depth(16);
install_mouse();
install_keyboard();
install_timer();
srand(time(NULL));
loadsprites();
setupscreen();
set_mouse_speed(MouseSpeed,MouseSpeed);
Floors.y = SCREEN_H;
BITMAP *buffer = create_video_bitmap(SCREEN_W,SCREEN_H);
BITMAP *buffer2 = create_video_bitmap(SCREEN_W,SCREEN_H);
dropball();
active_page = buffer2;
data = load_datafile("falling.dat");
while(!key[KEY_ESC] && lives > 0){
clear_to_color(active_page, makecol(0, 0, 0));
drawback();
draw_sprite(active_page,data[BACKGROUND_BMP].dat,0,0);
i++;
if (i >= 12){
i = 0;}
for (n = 0;n < (score/100) + 1;n++)
{
checkdead();
moveball();
movemouse();
moveball();
mousehold();
}
show_video_bitmap(active_page);
if (active_page == buffer)
active_page = buffer2;
else
active_page = buffer;
}
destroy_bitmap(buffer);
destroy_bitmap(buffer2);
textprintf(screen,font,0,0,makecol(255,255,255),"Last Score was %i Press ESC to Exit",score);
while(!key[KEY_ESC]);
allegro_exit();
}
END_OF_MAIN();
and the Falling.dat source and all files needed can be gotten at