Advertisement
HFelizzola

Untitled

Nov 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.19 KB | None | 0 0
  1. #include "main.h"
  2. #include "menu_inicial.h"
  3. #include "le_mapa.h"
  4. #include "gera_tela.h"
  5. #include "imprime_tela.h"
  6.  
  7. int main()
  8. {
  9.     setlocale(LC_ALL, "Portuguese_Brazil");
  10.  
  11.     FILE *arq;
  12.  
  13.     char opcao;
  14.     char mapa[LINHAS][COLUNAS];
  15.     char tela[LINHAS][COLUNAS_TELA];
  16.  
  17.     int flag_repetir_menu = 0;
  18.     int coluna_atual = 0;
  19.  
  20.     arq = fopen(FILE_MAPA, "r");
  21.     if(!arq)
  22.     {
  23.         printf("Erro ao abrir o arquivo do mapa.");
  24.         return 0;
  25.     }
  26.  
  27.     do
  28.     {
  29.         opcao = menu_inicial();
  30.         switch(opcao)
  31.         {
  32.             case '1':
  33.                 printf("Você escolheu a opção 'Novo Jogo'.\n");
  34.                 le_mapa(arq, mapa);
  35.                 gera_tela(mapa, tela, coluna_atual);
  36.                 imprime_tela(tela);
  37.                 break;
  38.             case '2':
  39.                 printf("Você escolheu a opção 'Carregar Jogo'.");
  40.                 break;
  41.             case '3':
  42.                 printf("Você escolheu a opção 'Sair'.");
  43.                 break;
  44.             default:
  45.                 printf("Opção inválida.");
  46.                 flag_repetir_menu = 1;
  47.                 break;
  48.         }
  49.     } while (flag_repetir_menu);
  50.  
  51.     return 0;
  52. }
  53. #ifndef MENU_INICIAL_H
  54.     #define MENU_INICIAL_H
  55.     #include "main.h"
  56.  
  57.     char menu_inicial();
  58.  
  59. #endif
  60. #ifndef MAIN_H
  61.     #define MAIN_H
  62.     #include <stdio.h>
  63.     #include <conio2.h>
  64.     #include <locale.h>
  65.     #include <windows.h>
  66.  
  67.     #define LINHAS 35
  68.     #define COLUNAS 415
  69.     #define COLUNAS_TELA 105
  70.     #define FILE_MAPA "mapa.txt"
  71.  
  72.     typedef struct
  73.     {
  74.         int linha;
  75.         int coluna;
  76.     } COORDENADA;
  77.  
  78. #endif
  79. #ifndef MAIN_H
  80.     #define MAIN_H
  81.     #include <stdio.h>
  82.     #include <conio2.h>
  83.     #include <locale.h>
  84.     #include <windows.h>
  85.  
  86.     #define LINHAS 35
  87.     #define COLUNAS 415
  88.     #define COLUNAS_TELA 105
  89.     #define FILE_MAPA "mapa.txt"
  90.  
  91.     typedef struct
  92.     {
  93.         int linha;
  94.         int coluna;
  95.     } COORDENADA;
  96.  
  97. #endif
  98. #ifndef GERA_TELA_H
  99.     #define GERA_TELA_H
  100.     #include "main.h"
  101.  
  102.     void gera_tela(char mapa[LINHAS][COLUNAS], char tela[LINHAS][COLUNAS_TELA], int coluna_atual);
  103. #endif // GERA_TELA_H
  104. #include "gera_tela.h"
  105.  
  106. void gera_tela(char mapa[LINHAS][COLUNAS], char tela[LINHAS][COLUNAS_TELA], int coluna_atual)
  107. {
  108.     int i, j;
  109.     for(i = 0; i < LINHAS; i++)
  110.         for(j = 0; j < COLUNAS_TELA; j++)
  111.             if((j + coluna_atual) >= COLUNAS)
  112.                 tela[i][j] = mapa[i][(j + coluna_atual) - COLUNAS];
  113.             else
  114.                 tela[i][j] = mapa[i][j + coluna_atual];
  115. }
  116. #include "menu_inicial.h"
  117.  
  118. char menu_inicial()
  119. {
  120.     char opcao;
  121.     gotoxy(30, 9);
  122.     printf("DEFENDER\n");
  123.     gotoxy(30, 11);
  124.     printf("Por Henrique Bernardes e Marthyna Weber");
  125.     Sleep(1000);
  126.     gotoxy(30, 18);
  127.     printf("SELECIONE UMA OPÇÃO:\n");
  128.     gotoxy(30, 19);
  129.     printf("1 - Novo Jogo\n");
  130.     gotoxy(30, 20);
  131.     printf("2 - Continuar Jogando\n");
  132.     gotoxy(30, 21);
  133.     printf("3 - Sair\n");
  134.     gotoxy(30, 22);
  135.     opcao = getch();
  136.     clrscr();
  137.     return opcao;
  138. }
  139. #include "le_mapa.h"
  140.  
  141. void le_mapa(FILE *arq, char mapa[LINHAS][COLUNAS])
  142. {
  143.     int i, j;
  144.     char c;
  145.  
  146.     for(i = 0; i < LINHAS; i++)
  147.     {
  148.         for(j = 0; j < COLUNAS; j++)
  149.         {
  150.             c = fgetc(arq);
  151.             mapa[i][j] = c;
  152.  
  153.             // completa a nave do jogador
  154.             if(c == '@')
  155.             {
  156.                 mapa[i-1][j] = c;
  157.                 mapa[i][j+1] = c;
  158.                 mapa[i][j+2] = c;
  159.                 mapa[i][j+3] = c;
  160.                 j += 3;
  161.             }
  162.             // completa as naves dos inimigos
  163.             if(c == 'X')
  164.             {
  165.                 mapa[i-1][j] = c;
  166.                 mapa[i-1][j+1] = c;
  167.                 mapa[i][j+1] = c;
  168.                 j += 1;
  169.  
  170.             }
  171.         }
  172.         // captura o \n no fim de cada linha
  173.         c = fgetc(arq);
  174.     }
  175. }
  176. #include "imprime_tela.h"
  177.  
  178. void imprime_tela(char tela[LINHAS][COLUNAS_TELA])
  179. {
  180.     int i, j;
  181.     for(i = 0; i < LINHAS; i++)
  182.     {
  183.         for(j = 0; j < COLUNAS_TELA; j++)
  184.         {
  185.             printf("%c", tela[i][j]);
  186.         }
  187.         printf("\n");
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement