DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Pong En Allegro

09.22.2006
| 3183 views |
  • submit to reddit
        El Telebolito o pong en allegro, no es completamente funcional... pero es un simple ejemplo.

#include <allegro.h>

void init();
void deinit();

int main() {
	init();

    int p1=110,p2=100;

    int puntos1=0,puntos2=0;

    int pause=0;

    float bx=320,by=240;

    float dx=1.5,dy=1.5;

    BITMAP *DB;
    
    DB= create_bitmap(640,480);

//    show_mouse(screen);
	while (!key[KEY_ESC]) {

          blit(DB,screen,0,0,0,0,640,480);
          clear_to_color(DB,0);
//          rest(10);
          circlefill(DB,(int)bx,(int)by,20,makecol(255,255,255));
          rect(DB,0,0,640,480,makecol(255,255,255));
          rectfill(DB,25,p1,30,p1+100,makecol(255,255,255));
          rectfill(DB,610,p2,615,p2+100,makecol(255,255,255));
          textprintf_ex(DB,font,50,20,makecol(255,255,255),0,"Player 1: %i Player 2: %u",puntos2,puntos1);

          if (key[KEY_S])
             p1-=3;

          if (key[KEY_X])
             p1+=3;
             
          if(key[KEY_UP])
             p2-=3;
             
          if(key[KEY_DOWN])
             p2+=3;
             
          if (key[KEY_G]){
             dx+=1;
             dy+=1;
          }
             

          bx+=dx;
          by+=dy;

          if (by==20 || by==460){
             dy*=-1;           
          }
          
          if (bx>590 && by>=p2 && by<=p2+100){
             dx*=-1;            
          }

          if (bx<50 && by>=p1 && by<=p1+100){
             dx*=-1;            
          }


          if (bx<0){
                     // Gana el jugador de la derecha
           bx=320;
           by=240;          
           puntos1++;
           dx+=0.5;
           dy+=0.5;
          }
          
          if (bx>640){
                       //Gana el jugador de la izq
//          textprintf_ex(screen,font,320,240,makecol())
           bx=320;
           by=240;   
           puntos2++;       
           dx+=0.5;
           dy-=0.5;
          }          
          

	}
	
	
	

	deinit();
	return 0;
}
END_OF_MAIN();

void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
	/* add other initializations here */
}

void deinit() {
	clear_keybuf();
	/* add other deinitializations here */
}