Funções TAD funcgrafica.h
Autor: Fabiola Garcia | Publicado: 2012-06-15T05:32:00-07:00

A TAD funcgrafica.h fica responsável pela parte gráfica do jogo, todas as funções que utilizam allegro estão contidas nesta TAD.


void IniciaAllegro()
{
&nbsp; &nbsp; allegro_init();
&nbsp; &nbsp; install_keyboard();
&nbsp; &nbsp; install_mouse();
&nbsp; &nbsp; set_color_depth(32);
&nbsp; &nbsp; set_gfx_mode(GFX_AUTODETECT_WINDOWED,800,600,0,0);
&nbsp; &nbsp; set_window_title("Angry Snake 5.0");
&nbsp; &nbsp; clear_to_color(screen,BRANCO);
}

void FinalizaAllegro()
{
&nbsp; &nbsp; &nbsp;allegro_exit();
}

/*
&nbsp; Controla(); Função onde o usuário controla a
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; direção da matriz(cobra) através
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dos direcionais do teclado(tecla
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UP, DOWN, LEFT, RIGHT).
*/
void Controla(int &amp;direcao)
{
&nbsp; &nbsp; &nbsp;if (key[KEY_UP])
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; if (direcao == DOWN)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;direcao = DOWN;
&nbsp; &nbsp; &nbsp; &nbsp; }else{direcao = UP;}
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;else if (key[KEY_RIGHT])
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; if (direcao == LEFT)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;direcao = LEFT;
&nbsp; &nbsp; &nbsp; &nbsp; }else{direcao = RIGHT;}
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;else if (key[KEY_DOWN])
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; if (direcao == UP)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;direcao = UP;
&nbsp; &nbsp; &nbsp; &nbsp; }else{direcao = DOWN;}
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;else if (key[KEY_LEFT])
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; if (direcao == RIGHT)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;direcao = RIGHT;
&nbsp; &nbsp; &nbsp; &nbsp; }else{direcao = LEFT;}
&nbsp; &nbsp; &nbsp;}
}

/*Função responsável por carregar os bitmaps e printá-los na tela*/

void MostraJogo(int cobra[linhas][colunas], int x, int y, int score, int bestScore)
{
&nbsp; &nbsp; &nbsp;int contador = 1;
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;BITMAP *Comidinha = load_bitmap("comidinha.bmp", NULL);
&nbsp; &nbsp; &nbsp;BITMAP *Cabeca = load_bitmap("cabeca.bmp", NULL);
&nbsp; &nbsp; &nbsp;BITMAP *Corpo = load_bitmap("corpo.bmp", NULL);
&nbsp; &nbsp; &nbsp;BITMAP *buffer = load_bitmap("buffer.bmp", NULL);
&nbsp; &nbsp; &nbsp;rect(buffer,TelaInicio,TelaInicio,TelaX,TelaY,PRETO);
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;draw_sprite(buffer,Cabeca,cobra[0][0],cobra[0][1]);
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;while (cobra[contador][0] != vazio)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;draw_sprite(buffer,Corpo,cobra[contador][0],cobra[contador][1]);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contador++;
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;textprintf_ex(buffer,font,640,40,PRETO,-1,"Score: %i",score);
&nbsp; &nbsp; &nbsp;textprintf_ex(buffer,font,50,40,PRETO,-1,"Best Score: %i",bestScore);
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;draw_sprite(buffer,Comidinha,x,y);
&nbsp; &nbsp; &nbsp;blit(buffer,screen,0,0,0,0,800,600);
&nbsp; &nbsp; &nbsp;destroy_bitmap(Comidinha);
&nbsp; &nbsp; &nbsp;destroy_bitmap(Cabeca);
&nbsp; &nbsp; &nbsp;destroy_bitmap(Corpo);
&nbsp; &nbsp; &nbsp;destroy_bitmap(buffer);
}

/*função responsável pela chamada de todas as funções do botão Start.&nbsp;
Essa função inicializa o jogo assim que o usuário clica em Start.*/ &nbsp;

void Play()
{
&nbsp; &nbsp; &nbsp;int snake[linhas][colunas], dir, score = 0, level = 200;
&nbsp; &nbsp; &nbsp;int x = vazio, y = vazio, exit = 1, bestScore;
&nbsp; &nbsp; &nbsp;char nome[30]; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;Reseta(snake, dir);
&nbsp; &nbsp; &nbsp;Comidinha(x, y);
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;ConsultarBS(bestScore);
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp;while (!key[KEY_ESC] &amp;&amp; !exit == 0 || !exit == -1)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Controla(dir);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MostraJogo(snake, x, y, score, bestScore);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Crescer(snake, x);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Movimenta(snake, dir);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Comidinha(x, y);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Colisao(snake, exit);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Comer(snake, x, y, score, level);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rest(level);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (key[KEY_ESC])
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit = -1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;if (exit == 0)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; allegro_message("|Game Over|");
&nbsp; &nbsp; &nbsp; &nbsp; FinalizaAllegro();
&nbsp; &nbsp; &nbsp; &nbsp; Incluir(score);
&nbsp; &nbsp; &nbsp; &nbsp; system("cls");
&nbsp; &nbsp; &nbsp; &nbsp; IniciaAllegro();
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;else if (exit == -1)
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allegro_message("|Redirecionando p/ menu|");
&nbsp; &nbsp; &nbsp;}
}

void Ranking()
{
&nbsp; &nbsp; &nbsp;
}

void Help()
{
&nbsp; &nbsp; &nbsp;
}

int Quit()
{
&nbsp; &nbsp; &nbsp;return 0;
}

/*Função que carrega as imagens utilizadas no menu,&nbsp;
bem como suas funcionalidades.*/

void Menu()
{
 IniciaAllegro();
&nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; BITMAP *start = load_bitmap("start.bmp", NULL);
 BITMAP *ranking = load_bitmap("ranking.bmp", NULL);
 BITMAP *help = load_bitmap("help.bmp", NULL);
 BITMAP *quit = load_bitmap("quit.bmp", NULL);
 BITMAP *buffer = create_bitmap(800,600);
 
 int opcao = 1, fim = 1;

 while (!key[KEY_ESC] &amp;&amp; !fim == 0)
 {
  
  
  
  if (key[KEY_UP])
  {
   if (opcao == 0)
   {
    opcao = 4;
   }
   else {opcao--;}
  }

  else if (key[KEY_DOWN])
  {
   if (opcao == 5)
   {
    opcao = 1;
   }
   else {opcao++;}
  }
  
  
  
  
  if (opcao == 1)
  {
   draw_sprite(buffer,start,0,0);
  }

  else if (opcao == 2)
  {
   draw_sprite(buffer,ranking,0,0);
  }

  else if (opcao == 3)
  {
   draw_sprite(buffer,help,0,0);
  }

  else if (opcao == 4)
  {
   draw_sprite(buffer,quit,0,0);
  }
  
  
  
  
  if (opcao == 1 &amp;&amp; key[KEY_ENTER])
  {
   Play();
  }

  else if (opcao == 2 &amp;&amp; key[KEY_ENTER])
  {
   //Ranking();
  }

  else if (opcao == 3 &amp;&amp; key[KEY_ENTER])
  {
   //Help();
  }

  else if (opcao == 4 &amp;&amp; key[KEY_ENTER])
  {
   fim = Quit();
  }

  blit (buffer,screen,0,0,0,0,800,600);
  
  rest(200);
 }
}