Comecocos en C
Publicado por redtrev12 (1 intervención) el 03/05/2001 23:47:02
Querría conseguir el comecocos en lenguaje C, así que si me lo pudieras mandar...
Gracias
Gracias
Valora esta pregunta


0
#include <stdio.h>
#include <conio.h>
#define FILAS 20
#define COLUMNAS 20
int x, y; // Posición del comecocos
int fantasmaX, fantasmaY; // Posición del fantasma
int puntuacion; // Puntuación del jugador
char tablero[FILAS][COLUMNAS]; // Tablero del juego
void inicializarTablero() {
int i, j;
for (i = 0; i < FILAS; i++) {
for (j = 0; j < COLUMNAS; j++) {
tablero[i][j] = ' ';
}
}
}
void dibujarTablero() {
int i, j;
for (i = 0; i < FILAS; i++) {
for (j = 0; j < COLUMNAS; j++) {
printf("%c", tablero[i][j]);
}
printf("\n");
}
}
void moverComecocos(char direccion) {
tablero[y][x] = ' ';
switch (direccion) {
case 'w':
y--;
break;
case 's':
y++;
break;
case 'a':
x--;
break;
case 'd':
x++;
break;
}
tablero[y][x] = 'C';
}
void moverFantasma() {
tablero[fantasmaY][fantasmaX] = ' ';
if (x < fantasmaX) {
fantasmaX--;
} else if (x > fantasmaX) {
fantasmaX++;
}
if (y < fantasmaY) {
fantasmaY--;
} else if (y > fantasmaY) {
fantasmaY++;
}
tablero[fantasmaY][fantasmaX] = 'F';
}
int main() {
char tecla;
inicializarTablero();
x = COLUMNAS / 2;
y = FILAS / 2;
fantasmaX = COLUMNAS / 2 - 1;
fantasmaY = FILAS / 2 - 1;
tablero[y][x] = 'C';
tablero[fantasmaY][fantasmaX] = 'F';
puntuacion = 0;
while (1) {
system("cls");
dibujarTablero();
printf("Puntuación: %d\n", puntuacion);
printf("Muévete con las teclas w, a, s, d. Pulsa q para salir.\n");
tecla = getch();
if (tecla == 'q') {
break;
}
moverComecocos(tecla);
moverFantasma();
if (x == fantasmaX && y == fantasmaY) {
printf("Juego terminado\n");
break;
}
puntuacion++;
}
return 0;
}