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-6 of 6 total  RSS 

Allegro Dibujos de posicion relativa

Dibujos en allegro dependiendo de la posicion del mouse

#include <allegro.h>


void  dibujo(int x,int y){
      circle (screen, x+100,y+100,100,makecol(128,0,0));
      circlefill (screen,x+50,y+50,20,makecol(0,255,0));
      circlefill (screen,x+150,y+50,20,makecol(0,255,0));
      rect (screen,x+80,y+100,x+120,y+150,makecol(0,0,128));
      line (screen,x+50,y+180,x+150,y+180,makecol(128,128,0));
}

void init();
void deinit();


int main() {
	init();
	show_mouse(screen);
	while (!key[KEY_ESC]) {  
          clear_to_color(screen,0);
          dibujo(mouse_x,mouse_y);
	}

	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 */
}

Allegro Patron

Patron para la creacion de programas en allegro iniciando algunas de sus rutinas basicas.

#include <allegro.h>

void init();
void deinit();

int main() {
	init();

	while (!key[KEY_ESC]) {
		/* put your code here */
	}

	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 */
}

Circulos en Allegro

Un codigo para dibujar un circulo en allegro con degradado

#include <allegro.h>

void init();
void deinit();

int main() {
	int cont;
    
    init();
	
	
	for (cont=0;cont<255;cont++){
        circlefill(screen,320,240,255-cont,makecol(cont,0,0));
    }

	while (!key[KEY_ESC]) {
		/* put your code here */
	}

	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 */
}

Pong en Allegro

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 */
}

Manejo de mouse en Allegro

Manejo de mouse en Allegro

#include <allegro.h>

void init();
void deinit();

int main() {
	init();


    int x,y;
    show_mouse(screen);
	while (!key[KEY_ESC]) {
		/* put your code here */
  
       for (x=10;x<600;x+=50){
           for (y=10;y<420;y+=60){        
               if (mouse_x > x && mouse_x < x+40 && mouse_y >y && mouse_y < y+50){
                  circlefill(screen, x+20,y+25,20,makecol(128,y%255,0));    
               } else {
                  rectfill(screen, x,y,x+40,y+50,makecol(0,255,0));           
               }
           }    
       }
	}

	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 */
}

Animacion en allegro

Codigo que muestra una pequena animacion en Allegro


#include <allegro.h>

void init();
void deinit();

int piramide(int a, int b, int c){
    line(screen,a+(c/2),b,a,b+c,makecol(0,128,0));    
    line(screen,a,b+c,a+c,b+c,makecol(0,128,0));
    line(screen,a+c,b+c,a+(c/2),b,makecol(0,128,0));    
}

int main() {
	init();
	int a,b,inca,incb;
	a=0,b=0;
	inca=1, incb=1;
	while (!key[KEY_ESC]) {
          clear_to_color(screen,0);
            circlefill(screen,a,b,50,makecol(128,0,128));
            a=a+inca;
            b=b+incb;
            
            if (a==200){
               inca=-1;            
            }
        rest(10);
	}

	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 */
}
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS