Primeras pruebas a color del juego del Comecocos. Sólo está implementado el movimiento del PacMan comiendo los círculos y las frutas y el paso de un lado al otro por el pasillo. Faltan los fantasmas y la dinámica de persecución y choques...
Desarrollado para LINUX en Code::Blocks. No os correrá correctamente en Wine...
///Carrega de llibreries
#include<stdio.h>
#include<stdlib.h>
#include <termios.h>
static struct termios old, new;
///variables globals
int punts=0,vides=2,nivell=1,atac=0;
int matriu[24][28]={
35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,
35,46,46,46,46,46,46,46,46,46,46,46,46,35,35,46,46,46,46,46,46,46,46,46,46,46,46,35,
35,67,35,35,35,35,46,35,35,35,35,35,46,35,35,46,35,35,35,35,35,46,35,35,35,35,67,35,
35,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,35,
35,46,35,35,35,35,46,35,35,46,35,35,35,35,35,35,35,35,46,35,35,46,35,35,35,35,46,35,
35,46,46,46,46,46,46,35,35,46,46,46,46,35,35,46,46,46,46,35,35,46,46,46,46,46,46,35,
35,35,35,35,35,35,46,35,35,35,35,35,32,35,35,32,35,35,35,35,35,46,35,35,35,35,35,35,
32,32,32,32,32,35,46,35,35,32,32,32,32,32,32,32,32,32,32,35,35,46,35,32,32,32,32,32,
32,32,32,32,32,35,46,35,35,32,35,35,35,32,32,35,35,35,32,35,35,46,35,32,32,32,32,32,
35,35,35,35,35,35,46,35,35,32,35,32,32,32,32,32,32,35,32,35,35,46,35,35,35,35,35,35,
101,32,32,32,32,32,46,32,32,32,35,32,32,32,32,32,32,35,32,32,32,46,32,32,32,32,32,101,
35,35,35,35,35,35,46,35,35,32,35,32,32,32,32,32,32,35,32,35,35,46,35,35,35,35,35,35,
32,32,32,32,32,35,46,35,35,32,35,35,35,35,35,35,35,35,32,35,35,46,35,32,32,32,32,32,
32,32,32,32,32,35,46,35,35,32,32,32,32,32,32,32,32,32,32,35,35,46,35,32,32,32,32,32,
35,35,35,35,35,35,46,35,35,32,35,35,35,35,35,35,35,35,32,35,35,46,35,35,35,35,35,35,
35,46,46,46,46,46,46,46,46,46,46,46,46,35,35,46,46,46,46,46,46,46,46,46,46,46,46,35,
35,46,35,35,35,35,46,35,35,35,35,35,46,35,35,46,35,35,35,35,35,46,35,35,35,35,46,35,
35,67,46,46,35,35,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,35,35,46,46,67,35,
35,35,35,46,35,35,46,35,35,46,35,35,35,35,35,35,35,35,46,35,35,46,35,35,46,35,35,35,
35,46,46,46,46,46,46,35,35,46,46,46,46,35,35,46,46,46,46,35,35,46,46,46,46,46,46,35,
35,46,35,35,35,35,35,35,35,35,35,35,46,35,35,46,35,35,35,35,35,35,35,35,35,35,46,35,
35,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,35,
35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35};
int X,Y,V,Z;
int punts,vides,nivell;
/// Declaració de funcions
/* Initialize new terminal i/o settings */
void initTermios(int echo){
tcgetattr(0, &old); /* grab old terminal i/o settings */
new = old; /* make new settings same as old settings */
new.c_lflag &= ~ICANON; /* disable buffered i/o */
new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void){
tcsetattr(0, TCSANOW, &old);
}
/* Read 1 character - echo defines echo mode */
char getch_(int echo){
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
/* Read 1 character without echo */
char getch(void){
return getch_(0);
}
/* Read 1 character with echo */
char getche(void){
return getch_(1);
}
/* Funció que reemplaça el gotoxy de conio.h */
void gotoxy(int x, int y){
printf("%c[%d;%df",0x1B,y,x);
}
/* Funció que reemplaça l'ordre delay o retard */
void delay(int del){
int c,d;
for (c=1;c<= del;c++) {
for (d=1;d <=del;d++){
}
}
}
/* Funció per presentar la pantalla */
void mostra(){
int i,j;
for(i=0;i<24;i++){
printf("ntt");
for(j=0;j<28;j++){
if(matriu[i][j]==35){
printf("e[104m e[0m");
}
if(matriu[i][j]==46){
printf("e[96m*e[0m");
}
if(matriu[i][j]==64){
if(atac>0){
printf("e[5me[93m@e[0m");
}
else{
printf("e[93m@e[0m");
}
}
if(matriu[i][j]==111){
printf(" ");
}
if(matriu[i][j]==32||matriu[i][j]==101){
printf(" ");
}
if(matriu[i][j]==67){
printf("e[96m█e[0m");
}
}
}
gotoxy(49,2);
printf("e[93m+---------------+e[0m");
gotoxy(49,3);
printf("e[93m│ COMECOCOS │e[0m");
gotoxy(49,4);
printf("e[93m+---------------+e[0m");
gotoxy(49,5);
printf("Punts: %d",punts);
gotoxy(49,6);
printf("Vides: %d",vides);
gotoxy(49,7);
printf("Nivell: %d",nivell);
gotoxy(49,10);
printf("e[90mPosició X=%d Y=%de[0m",X,Y);
gotoxy(49,11);
printf("e[90mCaracter: %ce[0m",matriu[X][Y]);
gotoxy(100,29);
}
/* Funcio dreta */
void dreta(){
if(matriu[X][Y+1]!=35){
system("clear");
if(matriu[X][Y+1]==46){
punts++;
}
if(matriu[X][Y+1]==67){
atac=1;
punts=punts+10;
}
matriu[X][Y+1]=matriu[X][Y];
matriu[X][Y]=32;
Y++;
if(matriu[10][27]==64){
matriu[10][27]=32;
matriu[10][0]=64;
X=10;
Y=0;
atac=0;
}
mostra();
}
}
/* Funcio esquerra */
void esquerra(){
if(matriu[X][Y-1]!=35){
system("clear");
if(matriu[X][Y-1]==46){
punts++;
}
if(matriu[X][Y-1]==67){
atac=1;
punts=punts+10;
}
matriu[X][Y-1]=matriu[X][Y];
matriu[X][Y]=32;
Y--;
if(matriu[10][0]==64){
matriu[10][0]=32;
matriu[10][27]=64;
X=10;
Y=27;
atac=0;
}
mostra();
}
}
/* Funcio adalt */
void adalt(){
if(matriu[X-1][Y]!=35){
system("clear");
if(matriu[X-1][Y]==46){
punts++;
}
if(matriu[X-1][Y]==67){
atac=1;
punts=punts+10;
}
matriu[X-1][Y]=matriu[X][Y];
matriu[X][Y]=32;
X--;
mostra();
}
}
/* Funcio baix */
void baix(){
if(matriu[X+1][Y]!=35){
system("clear");
if(matriu[X+1][Y]==46){
punts++;
}
if(matriu[X+1][Y]==67){
atac=1;
punts=punts+10;
}
matriu[X+1][Y]=matriu[X][Y];
matriu[X][Y]=32;
X++;
mostra();
}
}
/* Funció MAIN del programa */
int main(void){
int i,j,x,y,m;
char opcio;
X=13;Y=14;
matriu[X][Y]=64;
mostra();
while(opcio!='q'&&opcio!='Q'){
opcio=getch();///scanf("%c",&opcio);
switch(opcio){
case 'd':
dreta();
break;
case'C':
dreta();
break;
case 'e':
esquerra();
break;
case'D':
esquerra();
break;
case 'a':
adalt();
break;
case'A':
adalt();
break;
case 'b':
baix();
break;
case'B':
baix();
break;
}
}
return 0;
}