Guest User

Untitled

a guest
Apr 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.92 KB | None | 0 0
  1. #include <allegro.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // enum ValoresCelula { Vazia = 0, Parede = 1, Premio = 2, Robo = 3 };
  6. enum ValoresCelula { Vazia, Parede, Premio };
  7. enum Direcao { Norte, Sul, Leste, Oeste };
  8.  
  9.  
  10. int corCelula[4];
  11.  
  12. void init();
  13. void deinit();
  14.  
  15. struct Robo {
  16.   int x, y;
  17.   enum Direcao d;
  18. };
  19.  
  20. void constroiRobo( struct Robo* r, int xInicial, int yInicial ) {
  21.   r->x = xInicial;
  22.   r->y = yInicial;
  23.   r->d = Oeste;
  24. }
  25.  
  26. struct Tabuleiro {
  27.   int x, y;
  28.   int tamanho_celula;
  29.   int numero_celulas;
  30.   int **celula;
  31. };
  32.  
  33. struct Jogo {
  34.   struct Tabuleiro tab;
  35.   struct Robo joe;
  36. };
  37.  
  38. void constroiTabuleiro( struct Tabuleiro* tab, int x, int y ) {
  39.   int i, j;
  40.  
  41.   tab->x = x;
  42.   tab->y = y;
  43.   tab->tamanho_celula = 35;
  44.   tab->numero_celulas = 8;
  45.   tab->celula = (int **) malloc( tab->numero_celulas * sizeof( int* ) );
  46.   for( i = 0; i < tab->numero_celulas; i++ )
  47.     tab->celula[i] = (int *) malloc( tab->numero_celulas * sizeof( int ) );
  48.  
  49.   for( i = 0; i < tab->numero_celulas; i++ )
  50.     for( j = 0; j < tab->numero_celulas; j++ )
  51.       tab->celula[i][j] = Vazia;
  52.  
  53.   tab->celula[4][5] = Premio;
  54.   //tab->celula[2][2] = Parede;
  55.   //tab->celula[2][2] = Parede;
  56.   tab->celula[2][3] = Parede;
  57.   tab->celula[2][4] = Parede;
  58.   tab->celula[2][5] = Parede;
  59.  
  60. }
  61.  
  62. void constroiJogo( struct Jogo* j ) {
  63.   constroiTabuleiro( &j->tab, 50, 10);
  64.   constroiRobo( &j->joe, 0, 0);
  65. }
  66.  
  67. void desenhaRobo( BITMAP *bmp, struct Jogo *j ) {
  68.   int x = j->tab.x + j->joe.x * j->tab.tamanho_celula;
  69.   int y = j->tab.y + j->joe.y * j->tab.tamanho_celula;
  70.   int tamanho = j->tab.tamanho_celula;
  71.  
  72.  
  73.   switch( j->joe.d ) {
  74.     case Norte:
  75.       rectfill( bmp, x + 3, y + 15,
  76.                     x + tamanho - 3, y + tamanho - 10,
  77.                     makecol( 255, 255, 0 ) );
  78.       rectfill( bmp, x + 15, y + 15,
  79.                     x + tamanho - 15, y + 5,
  80.                     makecol( 255, 255, 0 ) );
  81.       break;
  82.  
  83.     case Sul:
  84.       rectfill( bmp, x + 3, y + tamanho - 15,
  85.                     x + tamanho - 3, y + 10,
  86.                     makecol( 255, 255, 0 ) );
  87.       rectfill( bmp, x + 15, y + tamanho - 15,
  88.                     x + tamanho - 15, y + tamanho - 5,
  89.                     makecol( 255, 255, 0 ) );
  90.       break;
  91.  
  92.     case Oeste:
  93.       rectfill( bmp, x + 15, y + 3,
  94.                     x + tamanho - 10, y + tamanho - 3,
  95.                     makecol( 255, 255, 0 ) );
  96.       rectfill( bmp, x + 15, y + 15,
  97.                     x + 5, y + tamanho - 15,
  98.                     makecol( 255, 255, 0 ) );
  99.       break;
  100.  
  101.     case Leste:
  102.       rectfill( bmp, x + tamanho - 15, y + 3,
  103.                     x + 10, y + tamanho - 3,
  104.                     makecol( 255, 255, 0 ) );
  105.       rectfill( bmp, x + tamanho - 15, y + 15,
  106.                     x + tamanho - 5, y + tamanho - 15,
  107.                     makecol( 255, 255, 0 ) );
  108.       break;
  109.   }
  110. }
  111.  
  112. void desenhaTabuleiro( BITMAP *bmp, struct Tabuleiro* tab ) {
  113.   int i, j;
  114.  
  115.   for( i = 0; i <= tab->numero_celulas; i++ )
  116.     line( bmp, tab->x + i * tab->tamanho_celula,
  117.               tab->y,
  118.               tab->x + i * tab->tamanho_celula,
  119.               tab->y + tab->numero_celulas * tab->tamanho_celula,
  120.               makecol( 0, 0, 0 ) );
  121.  
  122.   for( i = 0; i <= tab->numero_celulas; i++ )
  123.     line( bmp, tab->x,
  124.               tab->y + i * tab->tamanho_celula,
  125.               tab->x + tab->numero_celulas * tab->tamanho_celula,
  126.               tab->y + i * tab->tamanho_celula,
  127.               makecol( 0, 0, 0 ) );
  128.              
  129.   for( i = 0; i < tab->numero_celulas; i++ )
  130.     for( j = 0; j < tab->numero_celulas; j++ )
  131.       rectfill( bmp, tab->x + i * tab->tamanho_celula + 1,
  132.                     tab->y + j * tab->tamanho_celula + 1,
  133.                     tab->x + (i+1) * tab->tamanho_celula - 1,
  134.                     tab->y + (j+1) * tab->tamanho_celula - 1,
  135.                     corCelula[ tab->celula[i][j] ] );
  136. }
  137.  
  138.  
  139. int main() {
  140.   struct Jogo jogo;
  141.  
  142.   init();
  143.    
  144.   constroiJogo( &jogo );
  145.   show_mouse(screen);
  146.  
  147.   rectfill( screen, 0, 0, 639, 319, makecol( 128, 128, 128 ) );
  148.   desenhaTabuleiro( screen, &jogo.tab );
  149.   desenhaRobo( screen, &jogo );
  150.  
  151.   while (!key[KEY_ESC]) {
  152.     BITMAP *parede;
  153.         parede = load_bitmap ("parede.bmp", NULL);
  154.     blit(parede, screen, 0, 0, 51, 11, 34, 34);
  155.     }
  156.   deinit();
  157.   return 0;
  158. }
  159. END_OF_MAIN()
  160.  
  161. void init() {
  162.   int depth, res;
  163.   allegro_init();
  164.   depth = desktop_color_depth();
  165.   if (depth == 0) depth = 32;
  166.   set_color_depth(depth);
  167.   res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 320, 0, 0);
  168.   if (res != 0) {
  169.     allegro_message(allegro_error);
  170.     exit(-1);
  171.   }
  172.  
  173.   install_timer();
  174.   install_keyboard();
  175.   install_mouse();
  176.   /* add other initializations here */
  177.  
  178.   corCelula[Vazia] = makecol( 160, 160, 160 );
  179.   corCelula[Parede] = makecol( 90, 100, 90 );
  180.   corCelula[Premio] = makecol( 0, 0, 160 );
  181. }
  182.  
  183. void deinit() {
  184.   clear_keybuf();
  185.   /* add other deinitializations here */
  186. }
Add Comment
Please, Sign In to add comment