Advertisement
feehgodoi08

Cobrinha C

May 14th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <time.h>
  5. #include <pthread.h>
  6. #include <string.h>
  7.  
  8.  
  9. #define XX 40
  10. #define YY 20
  11. #define TEMPO 10
  12. #define X_INICIAL 3
  13. #define Y_INICIAL 4
  14. #define DELAY 35
  15. //===============================================================================
  16.  
  17. int snake[XX*YY - 1][3], dir = 1, game_over = 0, bol = 0, ponto[1], tam = 0, ct = 0, pausa = 0;
  18.  
  19. void gotoxy(int coluna, int linha)
  20. {
  21.     COORD point;
  22.     point.X = coluna;
  23.     point.Y = linha;
  24.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
  25. }
  26. //===============================================================================
  27.  
  28. void sleep(int dif)
  29. {
  30.     clock_t t1, t2;
  31.  
  32.  
  33.     t1 = t2 = clock();
  34.  
  35.  
  36.     while ((t2 - t1) < (dif))
  37.     {
  38.         t2 = clock();
  39.     }
  40. }
  41. //===============================================================================
  42.  
  43. void criar_mapa()
  44. {
  45.     int x, y;
  46.  
  47.     for (x = 0; x < XX + 1; x++)
  48.     {
  49.         gotoxy(x, 0);
  50.         printf("#");
  51.         sleep(TEMPO);
  52.     }
  53.  
  54.     for (y = 0; y < YY + 1; y++)
  55.     {
  56.         gotoxy(XX + 1, y);
  57.         printf("#");
  58.         sleep(TEMPO);
  59.     }
  60.  
  61.     for (x = XX + 1; x > 0; x--)
  62.     {
  63.         gotoxy(x, YY + 1);
  64.         printf("#");
  65.         sleep(TEMPO);
  66.     }
  67.  
  68.     for (y = YY + 1; y > 0; y--)
  69.     {
  70.         gotoxy(0, y);
  71.         printf("#");
  72.         sleep(TEMPO);
  73.     }
  74. }
  75. //===============================================================================
  76.  
  77. void inicializar_corpo()
  78. {
  79.     int i;
  80.     for (i = 0; i < XX*YY; i++)
  81.     {
  82.         snake[i][0] = snake[i][1] = snake[i][2] = -1;
  83.     }
  84.  
  85.     snake[0][0] = X_INICIAL;
  86.     snake[0][1] = Y_INICIAL;
  87.     snake[0][2] = 1;
  88.     snake[1][2] = 3;
  89. }
  90. //===============================================================================
  91.  
  92. void * identificar_seta(void *vargp)
  93. {
  94.     int ch;
  95.     ch = getch();
  96.     if (ch == 224) ch = getch();
  97.     if (ct == 0)
  98.     {
  99.         switch (ch)
  100.         {
  101.         case 72:
  102.             if (dir != 2)
  103.                 dir = 1;
  104.             ct = 1;
  105.             break;
  106.  
  107.         case 80:
  108.             if (dir != 1)
  109.                 dir = 2;
  110.             ct = 1;
  111.             break;
  112.  
  113.         case 77:
  114.             if (dir != 4)
  115.                 dir = 3;
  116.             ct = 1;
  117.             break;
  118.  
  119.         case 75:
  120.             if (dir != 3)
  121.                 dir = 4;
  122.             ct = 1;
  123.             break;
  124.         }
  125.     }
  126. }
  127. //===============================================================================
  128.  
  129. void movimentacao()
  130. {
  131.     switch (dir)
  132.     {
  133.     case 1:
  134.         if (pausa == 0)
  135.         {
  136.             if (snake[0][1] > 1)
  137.                 snake[0][1] -= 1;
  138.             else
  139.                 snake[0][1] = YY;
  140.         }
  141.         break;
  142.  
  143.     case 2:
  144.         if (pausa == 0)
  145.         {
  146.             if (snake[0][1] < YY)
  147.                 snake[0][1] += 1;
  148.             else
  149.                 snake[0][1] = 1;
  150.         }
  151.         break;
  152.  
  153.     case 3:
  154.         if (pausa == 0)
  155.         {
  156.             if (snake[0][0] < XX)
  157.                 snake[0][0] += 1;
  158.             else
  159.                 snake[0][0] = 1;
  160.         }
  161.         break;
  162.  
  163.     case 4:
  164.         if (pausa == 0)
  165.         {
  166.             if (snake[0][0] > 1)
  167.                 snake[0][0] -= 1;
  168.             else
  169.                 snake[0][0] = XX;
  170.         }
  171.         break;
  172.     }
  173. }
  174. //===============================================================================
  175. void desenhar()
  176. {
  177.     int i = 0;
  178.     while (snake[i][2] != 3 && snake[i][2] > 0)
  179.     {
  180.         if (snake[i][2] == 1)
  181.         {
  182.             gotoxy(snake[i][0], snake[i][1]);
  183.             printf("X");
  184.         }
  185.         else if (snake[i][2] == 2)
  186.         {
  187.             gotoxy(snake[i][0], snake[i][1]);
  188.             printf("*");
  189.         }
  190.         i++;
  191.     }
  192.  
  193.     if (bol == 1)
  194.     {
  195.         gotoxy(ponto[0], ponto[1]);
  196.         printf(".");
  197.         bol = 2;
  198.     }
  199. }
  200. //===============================================================================
  201.  
  202. void apagar()
  203. {
  204.     sleep(DELAY);
  205.     int i = 0;
  206.     while (snake[i][2] <= 3 && snake[i][2] > 0)
  207.     {
  208.         if (snake[i][2] == 3)
  209.         {
  210.             gotoxy(snake[i - 1][0], snake[i - 1][1]);
  211.             printf(" ");
  212.         }
  213.         i++;
  214.     }
  215.  
  216.     if (bol == 0)
  217.     {
  218.         gotoxy(ponto[0], ponto[1]);
  219.         printf(" ");
  220.     }
  221. }
  222. //===============================================================================
  223.  
  224. void gerar_pontos()
  225. {
  226.     if (bol == 0)
  227.     {
  228.         pausa = 1;
  229.         int x, y, sair = 0, i;
  230.  
  231.         while (sair != 1)
  232.         {
  233.             srand(time(NULL));
  234.             x = rand() % XX;
  235.             y = rand() % YY;
  236.  
  237.             i = 0;
  238.             while (snake[i][2] != 3)
  239.             {
  240.                 if ((x == snake[i][0] && y == snake[i][1]) || x == 0 || y == 0)
  241.                     sair = -1;
  242.                 i++;
  243.             }
  244.  
  245.             sair++;
  246.         }
  247.  
  248.         bol = 1;
  249.         ponto[0] = x;
  250.         ponto[1] = y;
  251.     }
  252.     pausa = 0;
  253.  
  254. }
  255. //===============================================================================
  256.  
  257. void verificar_colisao()
  258. {
  259.     int i = 1;
  260.     while (snake[i][2] != 3)
  261.     {
  262.         if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1])
  263.             game_over = 1;
  264.         i++;
  265.     }
  266.     if (snake[0][0] == ponto[0] && snake[0][1] == ponto[1])
  267.     {
  268.         bol = 0;
  269.         tam += 1;
  270.         scoreboard();
  271.     }
  272. }
  273. //===============================================================================
  274.  
  275. void atualizar_corpo()
  276. {
  277.     int i, j;
  278.  
  279.     if (tam >= 1)
  280.     {
  281.         for (i = tam; i > 0; i--)
  282.         {
  283.             snake[i][0] = snake[i - 1][0];
  284.             snake[i][1] = snake[i - 1][1];
  285.             snake[i][2] = 2;
  286.         }
  287.     }
  288.  
  289.     snake[0][2] = 1;
  290.     snake[tam + 1][2] = 3;
  291. }
  292. //===============================================================================
  293.  
  294. void scoreboard()
  295. {
  296.     gotoxy(XX + 3, 4);
  297.     printf("SCOREBOARD");
  298.     gotoxy(XX + 3, 5);
  299.     printf("%d", tam);
  300. }
  301. //===============================================================================
  302.  
  303. void gravar_resultado()
  304. {
  305.     system("cls");
  306.  
  307.     char nome[200];
  308.     FILE *resultados;
  309.  
  310.     gotoxy(2, 2);
  311.     printf("Digite o seu nome: ");
  312.     fflush(stdin);
  313.     gets(nome);
  314.  
  315.     resultados = fopen("resultados.txt", "a");
  316.     fprintf(resultados, "%s\n%d\n", nome, tam);
  317.     fclose(resultados);
  318. }
  319. //===============================================================================
  320.  
  321. void mostrar_resultados()
  322. {
  323.     system("cls");
  324.  
  325.     int i, letra = 0, ct = 1, result[100];
  326.     char nomes[100][201];
  327.  
  328.     FILE *resultados;
  329.     resultados = fopen("resultados.txt", "r");
  330.  
  331.     printf("\n\n   *******");
  332.     printf("\n    SCORE");
  333.     printf("\n   *******\n\n");
  334.  
  335.     i = 0;
  336.     while (fgets(nomes[i], 200, resultados) != NULL)
  337.     {
  338.         tam = strlen(nomes[i]);
  339.         letra = 0;
  340.         while (letra < tam)
  341.         {
  342.             if (nomes[i][letra] != '\n')
  343.                 printf("%c", nomes[i][letra]);
  344.             letra++;
  345.         }
  346.  
  347.         ct = ct * -1;
  348.         if (ct == 1)
  349.         {
  350.             printf("\n");
  351.         }
  352.         else
  353.         {
  354.             printf(" - ");
  355.         }
  356.         i++;
  357.     }
  358.  
  359.     fclose(resultados);
  360.     getchar();
  361. }
  362. //===============================================================================
  363.  
  364. void tela_final()
  365. {
  366.     system("cls");
  367.     gotoxy(7, 1);
  368.     printf("GAME OVER!");
  369.     gotoxy(7, 3);
  370.     printf("SEU SCORE FOI");
  371.     gotoxy(14, 4);
  372.     printf("%d\n\n", tam);
  373.  
  374.     system("pause");
  375. }
  376. //===============================================================================
  377.  
  378. int main()
  379. {
  380.     pthread_t thread_id, thread_id2;
  381.  
  382.     criar_mapa();
  383.     inicializar_corpo();
  384.     scoreboard();
  385.  
  386.     while (game_over == 0)
  387.     {
  388.         pthread_create(&thread_id, NULL, identificar_seta, NULL);
  389.         gerar_pontos();
  390. //pthread_create(&thread_id2, NULL, gerar_pontos, NULL);
  391.         movimentacao();
  392.         verificar_colisao();
  393.         desenhar();
  394.         apagar();
  395.         atualizar_corpo();
  396.         ct = 0;
  397.     }
  398.  
  399.     gravar_resultado();
  400.     mostrar_resultados();
  401.     tela_final();
  402.  
  403.     return 0;
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement