Nullware
Guru
Programmed it in Windows mostly for Windows but I did get it to compile and run successfully on my Linux virtual machine after some small changes. I don't know the proper way to clear the Linux terminal though so the previous screens just get pushed up unless you add the clear command.
It's commented for the most part but I got lazy and stopped because I'm tired maybe I'll do the rest later. Post your comments/opinions on my methods of doing things or what you think of it in general.
The game layout can be changed my modifying the big tiles array and anyone can pretty much do that. Also, it shouldn't be too difficult for anyone with some C/C++ experience to add a scoring counter based on the number of stars eaten or a number of other small things to make it more competitive or like an actual game of Pacman.
It's commented for the most part but I got lazy and stopped because I'm tired maybe I'll do the rest later. Post your comments/opinions on my methods of doing things or what you think of it in general.
The game layout can be changed my modifying the big tiles array and anyone can pretty much do that. Also, it shouldn't be too difficult for anyone with some C/C++ experience to add a scoring counter based on the number of stars eaten or a number of other small things to make it more competitive or like an actual game of Pacman.
Code:
/*
Author: Nullware
Date: 26/03/08 22:44
Description: Basic Command Prompt Pacman in C
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef _WIN32
#include <conio.h>
#endif
#ifdef linux
#include <termios.h>
#include <unistd.h>
// a Linux compatible version of getch() - I don't take any credit for making this function
int mygetch() {
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
#endif
int character[2] = { 0, 0 }; // Holds the location of our character at all times
char state[15] = { ' ', '@', '|', '-', '#', '*'}; // The possible states a tile can have
int tile[25][80] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
// Draws the screen from the array of tiles
void drawScreen() {
#ifdef _WIN32
system("cls"); // If running Windows use cls to clear the command prompt
#endif
#ifdef linux
// Command to clear Linux terminal will go here when I find one that does what I want
#endif
int i, j; // For loop variables
for(j=0; j < 24; j++) { // Loop through rows
for(i=0; i < 80; i++) { // Loop hrough columns
printf("%c", state[tile[j][i]]); // Print out the tile the loops are at
}
}
}
// Updates the player's position and objects' states based on user input
void updatePlayer(char direction) {
switch(direction) { // Check different cases based on the user's input
case 'W':
if(tile[character[0]-1][character[1]] == 5) {
tile[character[0]-1][character[1]] = 0;
}
if(character[0] > 0 && tile[character[0]-1][character[1]] == 0) {
tile[character[0]][character[1]] = 0;
character[0]--;
tile[character[0]][character[1]] = 1;
}
break;
case 'A':
if(tile[character[0]][character[1]-1] == 5) {
tile[character[0]][character[1]-1] = 0;
}
if(character[1] > 0 && tile[character[0]][character[1]-1] == 0) {
tile[character[0]][character[1]] = 0;
character[1]--;
tile[character[0]][character[1]] = 1;
}
break;
case 'S':
if(tile[character[0]+1][character[1]] == 5) {
tile[character[0]+1][character[1]] = 0;
}
if(character[0] < 22 && tile[character[0]+1][character[1]] == 0) {
tile[character[0]][character[1]] = 0;
character[0]++;
tile[character[0]][character[1]] = 1;
}
break;
case 'D':
if(tile[character[0]][character[1]+1] == 5) {
tile[character[0]][character[1]+1] = 0;
}
if(character[1] < 79 && tile[character[0]][character[1]+1] == 0) {
tile[character[0]][character[1]] = 0;
character[1]++;
tile[character[0]][character[1]] = 1;
}
break;
}
}
int main(int argc, char *argv[])
{
char input; // Our user input variable
drawScreen(); // Draw the screen at the beginning of the game
do {
printf("'W', 'A', 'S', 'D' to move. 'X' to Exit.");
#ifdef _WIN32
input = getch(); // Get user input on Windows
#endif
#ifdef linux
input = mygetch(); // Get user input on Linux
#endif
input = toupper((int)input); // Uppercase the input
updatePlayer(input); // Check if the player/objects needs to be updated based on the input
drawScreen(); // Call our function to draw the tiles now that they've been updated as necessary
} while(input != 'X'); // Loop while the user hasn't pressed 'X' meaning he would want to exit
return 0;
}

