Last active
May 17, 2024 17:14
-
-
Save rkTinelli/be533edabf44e13e6366549081d10897 to your computer and use it in GitHub Desktop.
Jogo FogeFoge (Pacman) Funcional feito em linguagem C
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 CIMA 'w' | |
#define BAIXO 's' | |
#define DIREITA 'd' | |
#define ESQUERDA 'a' | |
#define BOMBA 'b' | |
void move_pers(char comando); | |
int ehdirecao (char comando); | |
void fantasma(); | |
int paraondefantasmavai (int xatual, int yatual, | |
int* xdestino, int*ydestino); | |
void explodepilula(); | |
void explodepilula2 (int x, int y, int somax, int somay, int qtd); | |
int acabou(); | |
#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 "foge.h" | |
#include "mapa.h" | |
#include "ui.h" | |
MAPA m; | |
POSICAO heroi; | |
int tempilula = 0; | |
int acabou(){ | |
POSICAO pos; | |
int perdeu = !encontra_no_mapa(&m,&pos,HEROI); | |
int ganhou = !encontra_no_mapa(&m,&pos,FANTASMA); | |
return ganhou || perdeu; | |
} | |
int ehdirecao (char comando){ | |
return | |
comando == ESQUERDA || | |
comando == CIMA || | |
comando == BAIXO || | |
comando == DIREITA; | |
} | |
void move_pers(char comando){ | |
if (!ehdirecao(comando)) | |
return; | |
int prox_x = heroi.x; | |
int prox_y = heroi.y; | |
switch (comando){ | |
case ESQUERDA: | |
prox_y--; | |
break; | |
case CIMA: | |
prox_x--; | |
break; | |
case BAIXO: | |
prox_x++; | |
break; | |
case DIREITA: | |
prox_y++; | |
break; | |
} | |
if (!pode_andar(&m, HEROI, prox_x, prox_y)) | |
return; | |
if (ehpersonagem(&m, PILULA, prox_x, prox_y)) | |
tempilula=1; | |
andanomapa(&m, heroi.x, heroi.y, prox_x, prox_y); | |
heroi.x = prox_x; | |
heroi.y = prox_y; | |
} | |
void fantasma(){ | |
MAPA copia; | |
copiamapa(&copia, &m); | |
int i; | |
int j; | |
for (i=0 ; i< copia.linhas; i++){ | |
for (j=0; j< copia.colunas; j++){ | |
if (copia.mapa[i][j] == FANTASMA){ | |
int xdestino; | |
int ydestino; | |
int encontrou = paraondefantasmavai(i, j, &xdestino, &ydestino); | |
if (encontrou){ | |
andanomapa(&m, i, j, xdestino, ydestino); | |
} | |
} | |
} | |
} | |
libera_mapa(&copia); | |
} | |
int paraondefantasmavai (int xatual, int yatual, | |
int* xdestino, int*ydestino){ | |
int opcoes[4][2] = { | |
{xatual, yatual+1}, | |
{xatual+1, yatual}, | |
{xatual, yatual-1}, | |
{xatual-1, yatual} | |
}; | |
srand(time(0)); | |
int i; | |
for (i=0; i<10 ; i++){ | |
int escolha = rand() % 4; | |
if (pode_andar(&m, FANTASMA, opcoes[escolha][0],opcoes[escolha][1])){ | |
*xdestino = opcoes[escolha][0]; | |
*ydestino = opcoes[escolha][1]; | |
return 1; | |
} | |
} | |
return 0; | |
} | |
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 qtd){ | |
if (qtd == 0) return; | |
int novox = x + somax; | |
int novoy = y + somay; | |
if (!ehvalida(&m, novox, novoy)) return; | |
if (ehparede(&m, novox, novoy)) return; | |
m.mapa[novox][novoy] = VAZIO; | |
explodepilula2(novox, novoy, somax, somay, qtd - 1); | |
} | |
int main(){ | |
le_mapa(&m); | |
encontra_no_mapa(&m, &heroi, HEROI); | |
do{ | |
printf("Pilula: %s\n", (tempilula ? "SIM" : "NAO")); | |
imprime_mapa(&m); | |
printf("Qual seu comando? (a/w/s/d)\n"); | |
char comando; | |
scanf(" %c",&comando); //espaço em branco para ENTER ser ignorado | |
if (ehdirecao(comando)) move_pers(comando); | |
if (comando == BOMBA) explodepilula(); | |
fantasma(); | |
} while (!acabou()); | |
libera_mapa(&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 le_mapa(MAPA* m){ | |
FILE* f; | |
f = fopen("mapa.txt","r"); | |
if (f==0){ //tratamentto de erro | |
printf("Erro de leitura do arquivo do m->mapa! \n"); | |
exit(1); | |
} | |
fscanf(f,"%d %d",&m->linhas,&m->colunas); | |
aloca_mapa(m); | |
int i; | |
for (i = 0; i<5; i++){ | |
fscanf(f,"%s",m->mapa[i]); | |
} | |
fclose(f); | |
} | |
int encontra_no_mapa(MAPA* m, POSICAO* p, char c){ | |
int i; | |
int j; | |
for (i=0;i<m->linhas;i++){ | |
for (j=0;j<m->colunas;j++){ | |
if (m->mapa[i][j]== c ){ | |
p->x=i; | |
p->y=j; | |
return 1; | |
} | |
} | |
} | |
return 0; | |
} | |
void aloca_mapa(MAPA* m){ | |
m->mapa = malloc(sizeof(char*)*m->linhas); | |
int j; | |
for (j=0;j<m->linhas;j++){ | |
m->mapa[j] = malloc(sizeof(char*)*(m->colunas+1)); | |
} | |
} | |
void libera_mapa(MAPA* m){ | |
int i; | |
for (i=0;i<m->linhas;i++){ | |
free(m->mapa[i]); | |
} | |
free(m->mapa); | |
} | |
int ehvalida (MAPA* m, int x, int y){ | |
if (x>=m->linhas || y>=m->colunas) | |
return 0; | |
return 1; | |
} | |
int ehvazio (MAPA* m, int x, int y){ | |
return m->mapa[x][y] == VAZIO; | |
} | |
int ehparede (MAPA* m, int x, int y){ | |
return | |
m->mapa[x][y] == PAREDE_HORIZONTAL || | |
m->mapa[x][y] == PAREDE_VERTICAL; | |
} | |
int ehpersonagem(MAPA* m,char personagem,int x,int y){ | |
return | |
m->mapa[x][y] == personagem; | |
} | |
int pode_andar(MAPA* m, char personagem , int x, int y){ | |
return | |
ehvalida(m,x,y) && | |
!ehparede(m,x,y)&& | |
!ehpersonagem(m,personagem,x,y); | |
} | |
void andanomapa (MAPA* m, int xorigem, int yorigem, | |
int xdestino, int ydestino){ | |
char personagem = m->mapa[xorigem][yorigem]; | |
m->mapa[xdestino][ydestino] = personagem; | |
m->mapa[xorigem][yorigem] = VAZIO; | |
} | |
void copiamapa (MAPA* destino, MAPA* origem){ | |
destino->linhas = origem->linhas; | |
destino->colunas = origem->colunas; | |
aloca_mapa(destino); | |
int i; | |
for ( i=0 ; i < origem->linhas ; i++){ | |
strcpy(destino->mapa[i], origem->mapa[i]); | |
} | |
} |
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 PILULA 'P' | |
#define VAZIO '.' | |
#define PAREDE_VERTICAL '|' | |
#define PAREDE_HORIZONTAL '-' | |
struct matriz{ | |
char** mapa; //[5][10+1]; //10+1 para que seja identificado o \0 no final de cada linha | |
int linhas; | |
int colunas; | |
}; | |
typedef struct matriz MAPA; | |
struct posicao{ | |
int x; | |
int y; | |
}; | |
typedef struct posicao POSICAO; | |
//Assinatura das funções | |
void libera_mapa(MAPA* m); | |
void le_mapa(MAPA* m); | |
void aloca_mapa(MAPA* m); | |
void imprime_mapa(MAPA* m); | |
int encontra_no_mapa(MAPA* m, POSICAO* p, char c); | |
int ehvalida (MAPA* m, int x, int y); | |
int ehvazio (MAPA* m, int x, int y); | |
int ehparede (MAPA* m, int x, int y); | |
int ehpersonagem(MAPA* m,char personagem,int x,int y); | |
int pode_andar(MAPA* m, char personagem , int x, int y); | |
void andanomapa (MAPA* m, int xorigem, int yorigem, | |
int xdestino, int ydestino); | |
void copiamapa (MAPA* destino, MAPA* origem); | |
#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
5 10 | |
|--------| | |
|.F.|..-.| | |
|.._|.@P.| | |
|..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
#include <stdio.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]); | |
} | |
void imprime_mapa(MAPA* m) { | |
int i; | |
for( i = 0; i < m->linhas; i++) { | |
int parte; | |
for( parte = 0; parte < 4; parte++) { | |
int j; | |
for( j = 0; j < m->colunas; j++) { | |
switch(m->mapa[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 imprimeparte(char desenho[4][7], int parte); | |
void imprime_mapa(MAPA* m); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pac-man game.
Working !