Professional Documents
Culture Documents
#include <Windows.h>
#include <conio.h>
#include <ctime>
char levelMatrix[MAXROWS][MAXCOLUMNS] =
{
{ 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, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 35,
35, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 35},
{ 35, 111, 35, 35, 35, 35, 111, 35, 35, 35, 35, 35, 111, 35,
35, 111, 35, 35, 35, 35, 35, 111, 35, 35, 35, 35, 111, 35},
{ 35, 79, 35, 35, 35, 35, 111, 35, 35, 35, 35, 35, 111, 35,
35, 111, 35, 35, 35, 35, 35, 111, 35, 35, 35, 35, 79, 35},
{ 35, 111, 35, 35, 35, 35, 111, 35, 35, 35, 35, 35, 111, 35,
35, 111, 35, 35, 35, 35, 35, 111, 35, 35, 35, 35, 111, 35},
{ 35, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111,
111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 35},
{ 35, 111, 35, 35, 35, 35, 111, 35, 35, 111, 35, 35, 35, 35,
35, 35, 35, 35, 111, 35, 35, 111, 35, 35, 35, 35, 111, 35},
{ 35, 111, 35, 35, 35, 35, 111, 35, 35, 111, 35, 35, 35, 35,
35, 35, 35, 35, 111, 35, 35, 111, 35, 35, 35, 35, 111, 35},
{ 35, 111, 111, 111, 111, 111, 111, 35, 35, 111, 111, 111, 111, 35,
35, 111, 111, 111, 111, 35, 35, 111, 111, 111, 111, 111, 111, 35},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 35, 35, 35, 000, 35,
35, 000, 35, 35, 35, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 35, 35, 35, 000, 35,
35, 000, 35, 35, 35, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 000, 000, 000, 000, 000,
000, 000, 000, 000, 000, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 000, 35, 35, 000, 000,
000, 000, 35, 35, 000, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 000, 35, 000, 000, 000,
000, 000, 000, 35, 000, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 000, 35, 000, 000, 000,
000, 000, 000, 35, 000, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{074, 000, 000, 000, 000, 000, 111, 000, 000, 000, 35, 000, 000, 000,
000, 000, 000, 35, 000, 000, 000, 111, 000, 000, 000, 000, 000, 000},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 000, 35, 000, 000, 000,
000, 000, 000, 35, 000, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 000, 35, 000, 000, 000,
000, 000, 000, 35, 000, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 000, 35, 35, 000, 000,
000, 000, 35, 35, 000, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 000, 000, 000, 000, 000,
000, 000, 000, 000, 000, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{ 35, 35, 35, 35, 35, 35, 111, 35, 35, 000, 35, 35, 35, 35,
35, 35, 35, 35, 000, 35, 35, 111, 35, 35, 35, 35, 35, 35},
{ 35, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 35,
35, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 35},
{ 35, 111, 35, 35, 35, 35, 111, 35, 35, 35, 35, 35, 111, 35,
35, 111, 35, 35, 35, 35, 35, 111, 35, 35, 35, 35, 111, 35},
{ 35, 111, 35, 35, 35, 35, 111, 35, 35, 35, 35, 35, 111, 35,
35, 111, 35, 35, 35, 35, 35, 111, 35, 35, 35, 35, 111, 35},
{ 35, 79, 111, 111, 35, 35, 111, 111, 111, 111, 111, 111, 111, 111,
111, 111, 111, 111, 111, 111, 111, 111, 35, 35, 111, 111, 79, 35},
{ 35, 35, 35, 111, 35, 35, 111, 35, 35, 111, 35, 35, 35, 35,
35, 35, 35, 35, 111, 35, 35, 111, 35, 35, 111, 35, 35, 35},
{ 35, 35, 35, 111, 35, 35, 111, 35, 35, 111, 35, 35, 35, 35,
35, 35, 35, 35, 111, 35, 35, 111, 35, 35, 111, 35, 35, 35},
{ 35, 111, 111, 111, 111, 111, 111, 35, 35, 111, 111, 111, 111, 35,
35, 111, 111, 111, 111, 35, 35, 111, 111, 111, 111, 111, 111, 35},
{ 35, 111, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 111, 35,
35, 111, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 111, 35},
{ 35, 111, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 111, 35,
35, 111, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 111, 35},
{ 35, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111,
111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 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}
};
void getKeyPress();
bool blockValid(int block);
void Ghost();
void GoToXY(int column, int line);
bool ghostMoveUp();
bool ghostMoveDown();
bool ghostMoveLeft();
bool ghostMoveRight();
int findNode();
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, k);
do
{
//Set Start position
GoToXY(0, 0);
Ghost();
//Column
for (i = 0; i <= 31; i++)
{
//Row
for (j = 0; j <= 27; j++)
{
//Walls
if (levelMatrix[i][j] == 35)
{
k = 1;
SetConsoleTextAttribute(hConsole, k);
}
//Pacman
else if (levelMatrix[i][j] == 074 || levelMatrix[i][j] == 076 ||
levelMatrix[i][j] == 118 ||levelMatrix[i][j] == 94)
{
k = 14;
SetConsoleTextAttribute(hConsole, k);
}
//Blank
else if (levelMatrix[i][j] == 000)
{
k = 0;
SetConsoleTextAttribute(hConsole, k);
}
//Ghost
else if (levelMatrix[i][j] == 87)
{
if (superPellet)
{
k = 7;
SetConsoleTextAttribute(hConsole, k);
}
else
{
k = 4;
SetConsoleTextAttribute(hConsole, k);
}
}
//pill
else if (levelMatrix[i][j] == 111)
{
k = 15;
SetConsoleTextAttribute(hConsole, k);
}
//Power pill
else if (levelMatrix[i][j] == 79)
{
k = 14;
SetConsoleTextAttribute(hConsole, k);
}
if ( j == 27 && i == 31)
stopCounting = true;
}
}
getKeyPress();
//Color
k = 15;
SetConsoleTextAttribute(hConsole, k);
if (elapsed > 5)
{
superPellet = false;
elapsed = 0;
}
}
while (lives >= 0 || pillCount != 0);
system("pause");
return 0;
};
bool ghostMoveUp()
{
char ghostNextUp = levelMatrix[ghostOneI - 1][ghostOneJ];
//Check the next upward block to be sure it is not a wall
if ( ghostNextUp != 35)
{
//Check the last block to see if there was a pill
if ( ghostNextUp == 111 || ghostNextUp == 117 )
{
//put the pill back
levelMatrix[ghostOneI - 1][ghostOneJ] = 87;
levelMatrix[ghostOneI][ghostOneJ] = ghostTrail;
ghostOneI--;
}
else
{
pill = false;
levelMatrix[ghostOneI][ghostOneJ] = 000;
levelMatrix[ghostOneI - 1][ghostOneJ] = 87;
ghostOneI--;
}
return true;
}
else
{
return false;
}
}
bool ghostMoveDown()
{
bool ghostMoveLeft()
{
char ghostNextLeft = levelMatrix[ghostOneI][ghostOneJ - 1];
if ( ghostNextLeft != 35)
{
if ( ghostNextLeft == 111 || ghostNextLeft ==117)
{
levelMatrix[ghostOneI][ghostOneJ - 1] = 87;
levelMatrix[ghostOneI][ghostOneJ] = ghostNextLeft;
ghostOneJ--;
}
else
{
levelMatrix[ghostOneI][ghostOneJ] = 000;
levelMatrix[ghostOneI][ghostOneJ - 1] = 87;
ghostOneJ--;
}
return true;
}
else
{
return false;
}
bool ghostMoveRight()
{
char ghostNextRight = levelMatrix[ghostOneI][ghostOneJ + 1];
if ( ghostNextRight != 35)
{
void Ghost()
{
if (superPellet)
{
//Ghost is moving up
if ( pacI > ghostOneI)
{
//if wall found
if (!ghostMoveUp())
{
//find node path
}
}
}
}
}
}
}
}
}
else
{
//Ghost is moving up
if ( pacI < ghostOneI)
{
}
}
}
}
}
}
}
void getKeyPress()
{
key = _getch();
char pacTemp = '<';
//get a special key
if (key == 0 || key == -32)
{
key = _getch();