Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Pong en Allegro

El Telebolito o pong en allegro, no es completamente funcional... pero es un simple ejemplo.

   1  
   2  #include <allegro.h>
   3  
   4  void init();
   5  void deinit();
   6  
   7  int main() {
   8  	init();
   9  
  10      int p1=110,p2=100;
  11  
  12      int puntos1=0,puntos2=0;
  13  
  14      int pause=0;
  15  
  16      float bx=320,by=240;
  17  
  18      float dx=1.5,dy=1.5;
  19  
  20      BITMAP *DB;
  21      
  22      DB= create_bitmap(640,480);
  23  
  24  //    show_mouse(screen);
  25  	while (!key[KEY_ESC]) {
  26  
  27            blit(DB,screen,0,0,0,0,640,480);
  28            clear_to_color(DB,0);
  29  //          rest(10);
  30            circlefill(DB,(int)bx,(int)by,20,makecol(255,255,255));
  31            rect(DB,0,0,640,480,makecol(255,255,255));
  32            rectfill(DB,25,p1,30,p1+100,makecol(255,255,255));
  33            rectfill(DB,610,p2,615,p2+100,makecol(255,255,255));
  34            textprintf_ex(DB,font,50,20,makecol(255,255,255),0,"Player 1: %i Player 2: %u",puntos2,puntos1);
  35  
  36            if (key[KEY_S])
  37               p1-=3;
  38  
  39            if (key[KEY_X])
  40               p1+=3;
  41               
  42            if(key[KEY_UP])
  43               p2-=3;
  44               
  45            if(key[KEY_DOWN])
  46               p2+=3;
  47               
  48            if (key[KEY_G]){
  49               dx+=1;
  50               dy+=1;
  51            }
  52               
  53  
  54            bx+=dx;
  55            by+=dy;
  56  
  57            if (by==20 || by==460){
  58               dy*=-1;           
  59            }
  60            
  61            if (bx>590 && by>=p2 && by<=p2+100){
  62               dx*=-1;            
  63            }
  64  
  65            if (bx<50 && by>=p1 && by<=p1+100){
  66               dx*=-1;            
  67            }
  68  
  69  
  70            if (bx<0){
  71                       // Gana el jugador de la derecha
  72             bx=320;
  73             by=240;          
  74             puntos1++;
  75             dx+=0.5;
  76             dy+=0.5;
  77            }
  78            
  79            if (bx>640){
  80                         //Gana el jugador de la izq
  81  //          textprintf_ex(screen,font,320,240,makecol())
  82             bx=320;
  83             by=240;   
  84             puntos2++;       
  85             dx+=0.5;
  86             dy-=0.5;
  87            }          
  88            
  89  
  90  	}
  91  	
  92  	
  93  	
  94  
  95  	deinit();
  96  	return 0;
  97  }
  98  END_OF_MAIN();
  99  
 100  void init() {
 101  	int depth, res;
 102  	allegro_init();
 103  	depth = desktop_color_depth();
 104  	if (depth == 0) depth = 32;
 105  	set_color_depth(depth);
 106  	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
 107  	if (res != 0) {
 108  		allegro_message(allegro_error);
 109  		exit(-1);
 110  	}
 111  
 112  	install_timer();
 113  	install_keyboard();
 114  	install_mouse();
 115  	/* add other initializations here */
 116  }
 117  
 118  void deinit() {
 119  	clear_keybuf();
 120  	/* add other deinitializations here */
 121  }
 122  
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS