-
-
Save cmsfeitosa/0ba1fd8f3bcf37d52ee059a0e73ec61d to your computer and use it in GitHub Desktop.
Jogo Foge Foge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _FOGEFOGE_H_ | |
#define _FOGEFOGE_H_ | |
#define ACIMA 'w' | |
#define ABAIXO 's' | |
#define DIREITA 'd' | |
#define ESQUERDA 'a' | |
#define BOMBA 'b' | |
int acabou(); | |
void move(char direcao); | |
int ehdirecao(char direcao); | |
void fantasmas(); | |
int praondefantasmavai(int xatual, int yatual, int* xdestino, int* ydestino); | |
void explodepilula(); | |
void explodepilula2(int x, int y, int somax, int somay, int qtde); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include "FogeFoge.h" | |
#include "Mapa.h" | |
#include "UI.h" | |
// grupo de variáveis que precisam viver juntas dentro da struct | |
MAPA m; | |
// guarda a posicao atual do heroi sem ficar varrendo a cada instante | |
POSICAO heroi; | |
int tempilula = 0; | |
int praondefantasmavai(int xatual, int yatual, int* xdestino, int* ydestino) { | |
int opcoes[4][2] = { | |
{ xatual, yatual + 1}, | |
{ xatual, yatual - 1}, | |
{ xatual + 1, yatual}, | |
{ xatual - 1, yatual} | |
}; | |
srand(time(0)); | |
for(int i = 0; i < 10; i++) { | |
int posicao = rand() % 4; | |
if(podeandar(&m, FANTASMA, opcoes[posicao][0], opcoes[posicao][1])) { | |
*xdestino = opcoes[posicao][0]; | |
*ydestino = opcoes[posicao][1]; | |
return 1; | |
} | |
} | |
return 0; | |
} | |
void fantasmas() { | |
MAPA copia; | |
copiamapa(&copia, &m); | |
for(int i = 0; i < copia.linhas; i++) { | |
for (int j = 0; copia.colunas; j++) { | |
if(copia.matriz[i][j] == FANTASMA) { | |
int xdestino; | |
int ydestino; | |
int encontrou = praondefantasmavai(i, j, &xdestino, &ydestino); | |
if(encontrou) { | |
andanomapa(&m, i, j, xdestino, ydestino); | |
} | |
} | |
} | |
} | |
liberamapa(&copia); | |
} | |
int acabou() { | |
POSICAO pos; | |
int fogefogenomapa = encontramapa(&m, &pos, HEROI); | |
return !fogefogenomapa; | |
} | |
int ehdirecao(char direcao) { | |
return direcao == 'ACIMA' || direcao == 'ABAIXO' || direcao == 'DIREITA' || direcao == 'ESQUERDA'; | |
} | |
void move(char direcao) { | |
if(!ehdirecao(direcao)) | |
return; | |
int proximox = heroi.x; | |
int proximoy = heroi.y; | |
switch(direcao) { | |
case ESQUERDA: | |
proximoy--; | |
break; | |
case DIREITA: | |
proximoy++; | |
break; | |
case ACIMA: | |
proximox--; | |
break; | |
case ABAIXO: | |
proximox++; | |
break; | |
} | |
if(!podeandar(&m, HEROI, proximox, proximoy)) | |
return; | |
if(ehpersonagem(&m, PILULA, proximox, proximoy)) { | |
tempilula = 1; | |
} | |
andanomapa(&m, heroi.x, heroi.y, proximox, proximoy); | |
heroi.x = proximox; | |
heroi.y = proximoy; | |
} | |
void explodepilula() { | |
if(!tempilula) return; | |
explodepilula2(heroi.x, heroi.y, 0, 1, 3); | |
explodepilula2(heroi.x, heroi.y, 0, -1, 3); | |
explodepilula2(heroi.x, heroi.y, 1, 0, 3); | |
explodepilula2(heroi.x, heroi.y, -1, 0, 3); | |
tempilula = 0; | |
} | |
void explodepilula2(int x, int y, int somax, int somay, int qtde) { | |
if(qtde == 0 ) return; | |
int novox = x + somax; | |
int novoy = y + somay; | |
if(!ehvalida(&m, novox, novoy)) return; | |
if(ehparede(&m, novox, novoy)) return; | |
m.matriz[novox][novoy] = VAZIO; | |
explodepilula2(novox, novoy, somax, somay, qtde - 1); | |
} | |
int main() { | |
lemapa(&m); | |
encontramapa(&m, &heroi, HEROI); | |
// fazer andar pelo mapa | |
do { | |
printf("Tem pilula: %s\n", (tempilula ? "SIM" : "NAO")); | |
imprimemapa(&m); | |
//pegar tecla que o jogador pressionou | |
char comando; | |
scanf(" %c", &comando); | |
move(comando); | |
if(comando == BOMBA) explodepilula(); | |
fantasmas(); | |
} while(!acabou()); | |
liberamapa(&m); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "Mapa.h" | |
void copiamapa(MAPA* destino, MAPA* origem) { | |
destino->linhas = origem->linhas; | |
destino->colunas = origem->colunas; | |
alocamapa(destino); | |
for(int i = 0; i < origem->linhas; i++) { | |
/*for(int j = 0; j < origem->coluas; j++) { | |
destino->matriz[i][j] = origem->matriz[i][j];*/ | |
strcpy(destino->matriz[i], origem->matriz[i]); | |
} | |
} | |
void andanomapa(MAPA* m, int xorigem, int yorigem, int xdestino, int ydestino) { | |
char personagem = m->matriz[xorigem][yorigem]; | |
m->matriz[xdestino][ydestino] = personagem; | |
m->matriz[xorigem][yorigem] = VAZIO; | |
} | |
int ehvalida(MAPA* m, int x, int y) { | |
if(x >= m->linhas) | |
return 0; | |
if(y >= m->colunas) | |
return 0; | |
return 1; | |
} | |
int ehvazia(MAPA* m, int x, int y) { | |
return m->matriz[x][y] == VAZIO; | |
} | |
int encontramapa(MAPA* m, POSICAO* p, char c) { | |
// acha a posição do foge foge | |
for(int i = 0; i < m->linhas; i++) { | |
for(int j = 0; j < m->colunas; j++) { | |
if(m->matriz[i][j] == c) { | |
p->x = i; | |
p->y = j; | |
return 1; | |
} | |
} | |
} | |
return 0; | |
} | |
int ehparede(MAPA* m, int x, int y) { | |
return m->matriz[x][y] == PAREDE_VERTICAL || m->matriz[x][y] == PAREDE_HORIZONTAL; | |
} | |
int ehpersonagem(MAPA* m, char personagem, int x, int y) { | |
return m->matriz[x][y] == personagem; | |
} | |
int podeandar(MAPA* m, char personagem, int x, int y) { | |
return ehvalida(m, x, y) && !ehparede(m, x, y) && !ehpersonagem(m, personagem, x, y); | |
} | |
void liberamapa(MAPA* m) { | |
//liberar memória | |
for(int i = 0; i < m->linhas; i++) { | |
free(m->matriz[i]); | |
} | |
free(m->matriz); | |
} | |
void alocamapa(MAPA* m) { | |
//alocação do mapa de forma dinâmica | |
m->matriz = malloc(sizeof(char*) * m->linhas); | |
for(int i = 0; i < m->linhas; i++) { | |
m->matriz[i] = malloc(sizeof(char) * (m->colunas + 1)); | |
} | |
} | |
void lemapa(MAPA* m) { | |
// ler de arquivo | |
FILE* f; | |
f = fopen("mapa.txt", "r"); | |
if(f == 0) { | |
printf("Erro na leitura do mapa.\n"); | |
exit(1); | |
} | |
// ler tamanho do mapa | |
fscanf(f, "%d %d", &(m->linhas), &(m->colunas)); | |
alocamapa(m); | |
// ler linha | |
for (int i = 0; i < m->linhas; i++) { | |
fscanf(f, "%s", m->matriz[i]); | |
} | |
// fechar arquivo | |
fclose(f); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _MAPA_H_ | |
#define _MAPA_H_ | |
#define HEROI '@' | |
#define FANTASMA 'F' | |
#define VAZIO '.' | |
#define PAREDE_VERTICAL '|' | |
#define PAREDE_HORIZONTAL '-' | |
#define PILULA 'P' | |
struct mapa { | |
char** matriz; | |
int linhas; | |
int colunas; | |
}; | |
typedef struct mapa MAPA; | |
struct posicao { | |
int x; | |
int y; | |
}; | |
typedef struct posicao POSICAO; | |
void liberamapa(MAPA* m); | |
void lemapa(MAPA* m); | |
void alocamapa(MAPA* m); | |
void imprimemapa(MAPA* m); | |
int encontramapa(MAPA* m, POSICAO* p, char c); | |
int ehvalida(MAPA* m, int x, int y); | |
int ehvazia(MAPA* m, int x, int y); | |
void andanomapa(MAPA* m, int xorigem, int yorigem, int xdestino, int ydestino); | |
void copiamapa(MAPA* destino, MAPA* origem); | |
int podeandar(MAPA* m, char personagem, int x, int y); | |
int ehparede(MAPA* m, int x, int y); | |
int ehpersonagem(MAPA* m, char personagem, int x, int y); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "UI.h" | |
#include "Mapa.h" | |
char desenhoparede[4][7] = { | |
{"......" }, | |
{"......" }, | |
{"......" }, | |
{"......" } | |
}; | |
char desenhofantasma[4][7] = { | |
{" .-. " }, | |
{"| OO| " }, | |
{"| | " }, | |
{"'^^^' " } | |
}; | |
char desenhoheroi[4][7] = { | |
{" .--. " }, | |
{"/ _.' " }, | |
{"\\ '-." }, | |
{" '--' " } | |
}; | |
char desenhopilula[4][7] = { | |
{" "}, | |
{" .-. "}, | |
{" '-' "}, | |
{" "} | |
}; | |
char desenhovazio[4][7] = { | |
{" "}, | |
{" "}, | |
{" "}, | |
{" "} | |
}; | |
void imprimeparte(char desenho[4][7], int parte) { | |
printf("%s", desenho[parte]); | |
} | |
// imprimir mapa | |
void imprimemapa(MAPA* m) { | |
for(int i = 0; i < m->linhas; i++) { | |
for(int parte = 0; parte < 4; parte++) { | |
for(int j = 0; j < m->colunas; j++) { | |
switch(m->matriz[i][j]) { | |
case FANTASMA: | |
imprimeparte(desenhofantasma, parte); | |
break; | |
case HEROI: | |
imprimeparte(desenhoheroi, parte); | |
break; | |
case PILULA: | |
imprimeparte(desenhopilula, parte); | |
break; | |
case PAREDE_VERTICAL: | |
case PAREDE_HORIZONTAL: | |
imprimeparte(desenhoparede, parte); | |
break; | |
case VAZIO: | |
imprimeparte(desenhovazio, parte); | |
break; | |
} | |
} | |
printf("\n"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _UI_H_ | |
#define _UI_H_ | |
#include "Mapa.h" | |
void imprimemapa(MAPA* m); | |
void imprimeparte(char desenho[4][7], int parte); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment