Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <time.h>
- #include <pthread.h>
- #include <string.h>
- #define XX 40
- #define YY 20
- #define TEMPO 10
- #define X_INICIAL 3
- #define Y_INICIAL 4
- #define DELAY 35
- //===============================================================================
- int snake[XX*YY - 1][3], dir = 1, game_over = 0, bol = 0, ponto[1], tam = 0, ct = 0, pausa = 0;
- void gotoxy(int coluna, int linha)
- {
- COORD point;
- point.X = coluna;
- point.Y = linha;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
- }
- //===============================================================================
- void sleep(int dif)
- {
- clock_t t1, t2;
- t1 = t2 = clock();
- while ((t2 - t1) < (dif))
- {
- t2 = clock();
- }
- }
- //===============================================================================
- void criar_mapa()
- {
- int x, y;
- for (x = 0; x < XX + 1; x++)
- {
- gotoxy(x, 0);
- printf("#");
- sleep(TEMPO);
- }
- for (y = 0; y < YY + 1; y++)
- {
- gotoxy(XX + 1, y);
- printf("#");
- sleep(TEMPO);
- }
- for (x = XX + 1; x > 0; x--)
- {
- gotoxy(x, YY + 1);
- printf("#");
- sleep(TEMPO);
- }
- for (y = YY + 1; y > 0; y--)
- {
- gotoxy(0, y);
- printf("#");
- sleep(TEMPO);
- }
- }
- //===============================================================================
- void inicializar_corpo()
- {
- int i;
- for (i = 0; i < XX*YY; i++)
- {
- snake[i][0] = snake[i][1] = snake[i][2] = -1;
- }
- snake[0][0] = X_INICIAL;
- snake[0][1] = Y_INICIAL;
- snake[0][2] = 1;
- snake[1][2] = 3;
- }
- //===============================================================================
- void * identificar_seta(void *vargp)
- {
- int ch;
- ch = getch();
- if (ch == 224) ch = getch();
- if (ct == 0)
- {
- switch (ch)
- {
- case 72:
- if (dir != 2)
- dir = 1;
- ct = 1;
- break;
- case 80:
- if (dir != 1)
- dir = 2;
- ct = 1;
- break;
- case 77:
- if (dir != 4)
- dir = 3;
- ct = 1;
- break;
- case 75:
- if (dir != 3)
- dir = 4;
- ct = 1;
- break;
- }
- }
- }
- //===============================================================================
- void movimentacao()
- {
- switch (dir)
- {
- case 1:
- if (pausa == 0)
- {
- if (snake[0][1] > 1)
- snake[0][1] -= 1;
- else
- snake[0][1] = YY;
- }
- break;
- case 2:
- if (pausa == 0)
- {
- if (snake[0][1] < YY)
- snake[0][1] += 1;
- else
- snake[0][1] = 1;
- }
- break;
- case 3:
- if (pausa == 0)
- {
- if (snake[0][0] < XX)
- snake[0][0] += 1;
- else
- snake[0][0] = 1;
- }
- break;
- case 4:
- if (pausa == 0)
- {
- if (snake[0][0] > 1)
- snake[0][0] -= 1;
- else
- snake[0][0] = XX;
- }
- break;
- }
- }
- //===============================================================================
- void desenhar()
- {
- int i = 0;
- while (snake[i][2] != 3 && snake[i][2] > 0)
- {
- if (snake[i][2] == 1)
- {
- gotoxy(snake[i][0], snake[i][1]);
- printf("X");
- }
- else if (snake[i][2] == 2)
- {
- gotoxy(snake[i][0], snake[i][1]);
- printf("*");
- }
- i++;
- }
- if (bol == 1)
- {
- gotoxy(ponto[0], ponto[1]);
- printf(".");
- bol = 2;
- }
- }
- //===============================================================================
- void apagar()
- {
- sleep(DELAY);
- int i = 0;
- while (snake[i][2] <= 3 && snake[i][2] > 0)
- {
- if (snake[i][2] == 3)
- {
- gotoxy(snake[i - 1][0], snake[i - 1][1]);
- printf(" ");
- }
- i++;
- }
- if (bol == 0)
- {
- gotoxy(ponto[0], ponto[1]);
- printf(" ");
- }
- }
- //===============================================================================
- void gerar_pontos()
- {
- if (bol == 0)
- {
- pausa = 1;
- int x, y, sair = 0, i;
- while (sair != 1)
- {
- srand(time(NULL));
- x = rand() % XX;
- y = rand() % YY;
- i = 0;
- while (snake[i][2] != 3)
- {
- if ((x == snake[i][0] && y == snake[i][1]) || x == 0 || y == 0)
- sair = -1;
- i++;
- }
- sair++;
- }
- bol = 1;
- ponto[0] = x;
- ponto[1] = y;
- }
- pausa = 0;
- }
- //===============================================================================
- void verificar_colisao()
- {
- int i = 1;
- while (snake[i][2] != 3)
- {
- if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1])
- game_over = 1;
- i++;
- }
- if (snake[0][0] == ponto[0] && snake[0][1] == ponto[1])
- {
- bol = 0;
- tam += 1;
- scoreboard();
- }
- }
- //===============================================================================
- void atualizar_corpo()
- {
- int i, j;
- if (tam >= 1)
- {
- for (i = tam; i > 0; i--)
- {
- snake[i][0] = snake[i - 1][0];
- snake[i][1] = snake[i - 1][1];
- snake[i][2] = 2;
- }
- }
- snake[0][2] = 1;
- snake[tam + 1][2] = 3;
- }
- //===============================================================================
- void scoreboard()
- {
- gotoxy(XX + 3, 4);
- printf("SCOREBOARD");
- gotoxy(XX + 3, 5);
- printf("%d", tam);
- }
- //===============================================================================
- void gravar_resultado()
- {
- system("cls");
- char nome[200];
- FILE *resultados;
- gotoxy(2, 2);
- printf("Digite o seu nome: ");
- fflush(stdin);
- gets(nome);
- resultados = fopen("resultados.txt", "a");
- fprintf(resultados, "%s\n%d\n", nome, tam);
- fclose(resultados);
- }
- //===============================================================================
- void mostrar_resultados()
- {
- system("cls");
- int i, letra = 0, ct = 1, result[100];
- char nomes[100][201];
- FILE *resultados;
- resultados = fopen("resultados.txt", "r");
- printf("\n\n *******");
- printf("\n SCORE");
- printf("\n *******\n\n");
- i = 0;
- while (fgets(nomes[i], 200, resultados) != NULL)
- {
- tam = strlen(nomes[i]);
- letra = 0;
- while (letra < tam)
- {
- if (nomes[i][letra] != '\n')
- printf("%c", nomes[i][letra]);
- letra++;
- }
- ct = ct * -1;
- if (ct == 1)
- {
- printf("\n");
- }
- else
- {
- printf(" - ");
- }
- i++;
- }
- fclose(resultados);
- getchar();
- }
- //===============================================================================
- void tela_final()
- {
- system("cls");
- gotoxy(7, 1);
- printf("GAME OVER!");
- gotoxy(7, 3);
- printf("SEU SCORE FOI");
- gotoxy(14, 4);
- printf("%d\n\n", tam);
- system("pause");
- }
- //===============================================================================
- int main()
- {
- pthread_t thread_id, thread_id2;
- criar_mapa();
- inicializar_corpo();
- scoreboard();
- while (game_over == 0)
- {
- pthread_create(&thread_id, NULL, identificar_seta, NULL);
- gerar_pontos();
- //pthread_create(&thread_id2, NULL, gerar_pontos, NULL);
- movimentacao();
- verificar_colisao();
- desenhar();
- apagar();
- atualizar_corpo();
- ct = 0;
- }
- gravar_resultado();
- mostrar_resultados();
- tela_final();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement