/*
* pacman.c Juego tipo Pacman en modo texto
*
* Autores originales:
* Dorian Butrón C.
* Dr. Gustavo Calderón
* Versión original: 2003 (Turbo C / MS-DOS)
*
* Actualizado a C estándar con ncurses: 2025
* - Reemplazados dos.h y conio.h por ncurses.h
* - Entrada no bloqueante con nodelay()
* - random()/randomize() sustituidos por rand()/srand()
* - delay() sustituido por napms()
* - Colores con color_pair de ncurses
*
* Compilar (Linux/macOS): gcc pacman.c -lncurses -o pacman
* Compilar (Windows): gcc pacman.c -lpdcurses -o pacman
*
* Versión PHP: https://programacion.net/codigo/pacman-php_1893
* Versión Python: https://programacion.net/codigo/pacman-python_1894
*/
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#define FILAS 10
#define COLS 20
#define RETARDO 120 /* ms entre fotogramas */
void generar_tablero(char m[][COLS], int filas, int cols) {
for (int i = 0; i < filas; i++)
for (int j = 0; j < cols; j++)
m[i][j] = '.';
}
int tablero_vacio(char m[][COLS], int filas, int cols) {
for (int i = 0; i < filas; i++)
for (int j = 0; j < cols; j++)
if (m[i][j] == '.') return 0;
return 1;
}
void dibujar(char m[][COLS], int filas, int cols, int puntos) {
clear();
attron(COLOR_PAIR(3) | A_BOLD);
mvprintw(0, 0, "Flechas=mover ESC=salir Puntos: %d", puntos * 15);
attroff(COLOR_PAIR(3) | A_BOLD);
for (int i = 0; i < filas; i++) {
for (int j = 0; j < cols; j++) {
char c = m[i][j];
if (c == '@') {
attron(COLOR_PAIR(1) | A_BOLD);
mvaddch(i + 1, j * 2, c);
attroff(COLOR_PAIR(1) | A_BOLD);
} else if (c == 1 || c == 2) {
attron(COLOR_PAIR(2) | A_BOLD);
mvaddch(i + 1, j * 2, 'M');
attroff(COLOR_PAIR(2) | A_BOLD);
} else {
mvaddch(i + 1, j * 2, c);
}
}
}
refresh();
}
int main(void) {
char m[FILAS][COLS];
int py = 0, px = 0;
int m1x = COLS-1, m1y = 0;
int m2x = COLS-1, m2y = FILAS-1;
int puntos = 0;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
curs_set(0);
start_color();
init_pair(1, COLOR_YELLOW, COLOR_BLACK); /* pacman */
init_pair(2, COLOR_RED, COLOR_BLACK); /* monstruos */
init_pair(3, COLOR_GREEN, COLOR_BLACK); /* HUD */
srand((unsigned)time(NULL));
generar_tablero(m, FILAS, COLS);
m[py][px] = '@';
m[m1y][m1x] = 1;
m[m2y][m2x] = 2;
while (1) {
dibujar(m, FILAS, COLS, puntos);
napms(RETARDO);
m[m1y][m1x] = ' ';
m[m2y][m2x] = ' ';
if (tablero_vacio(m, FILAS, COLS)) {
clear();
attron(COLOR_PAIR(3) | A_BOLD);
mvprintw(FILAS/2, 4, "*** ¡Ganaste! *** Puntos: %d", puntos * 15);
attroff(COLOR_PAIR(3) | A_BOLD);
refresh();
nodelay(stdscr, FALSE);
getch();
break;
}
int tecla = getch();
if (tecla != ERR) {
m[py][px] = ' ';
switch (tecla) {
case KEY_LEFT: if (px > 0) px--; break;
case KEY_RIGHT: if (px < COLS-1) px++; break;
case KEY_UP: if (py > 0) py--; break;
case KEY_DOWN: if (py < FILAS-1) py++; break;
case 27: endwin(); return 0;
}
if (m[py][px] == '.') puntos++;
m[py][px] = '@';
}
if ((m1x == px && m1y == py) || (m2x == px && m2y == py)) {
m[py][px] = 'X';
dibujar(m, FILAS, COLS, puntos);
nodelay(stdscr, FALSE);
attron(COLOR_PAIR(2) | A_BOLD);
mvprintw(FILAS/2, 4, "*** ¡Perdiste! *** Puntos: %d", puntos * 15);
attroff(COLOR_PAIR(2) | A_BOLD);
refresh();
getch();
break;
}
m1x = (rand()%2) ? (m1x < COLS-1 ? m1x+1 : m1x) : (m1x > 0 ? m1x-1 : m1x);
m1y = (rand()%2) ? (m1y < FILAS-1 ? m1y+1 : m1y) : (m1y > 0 ? m1y-1 : m1y);
m[m1y][m1x] = 1;
m2x = (rand()%2) ? (m2x < COLS-1 ? m2x+1 : m2x) : (m2x > 0 ? m2x-1 : m2x);
m2y = (rand()%2) ? (m2y < FILAS-1 ? m2y+1 : m2y) : (m2y > 0 ? m2y-1 : m2y);
m[m2y][m2x] = 2;
}
endwin();
return 0;
}
Pacman en C con ncurses
Versión actualizada del clásico juego Pacman en modo texto para C, ahora con ncurses en lugar de las cabeceras DOS originales. Mueve a Pacman con las flechas y esquiva los dos monstruos que se desplazan de forma aleatoria. Compilación directa con gcc en Linux o macOS.
Descargar adjuntos
COMPARTE ESTE TUTORIAL
COMPARTIR EN FACEBOOK
COMPARTIR EN TWITTER
COMPARTIR EN LINKEDIN
COMPARTIR EN WHATSAPP