This is my code in open frameworks for a pac man game however i am wondering how
ID: 3920203 • Letter: T
Question
This is my code in open frameworks for a pac man game however i am wondering how do i change it where there is no lag when i play the game thank you for the help. This code uses a main .cpp and .h file
The pacman.h file is all the way on the bottom it starts at
#ifndef pacheadH
#define pacheadH
this is the pacman.cpp file
#include
#include //for timing
#include //for getch();
#include //for puts(" ")
#include //for clear screen
#include
#include //for initializing srand() function
#include"pacman.h"
using namespace std;
int foodEaten;
#define up 72 //*
#define down 80 //*
#define left 75 //*==> Defining Arrow Input keys for User
#define right 77 //*
#define esc 27 //*
#define enter 13 //*
void game::initializeRandom()
{
srand(time(NULL));
}
void game::MainMenu() //******MAIN MENU FUNCTION******
{
do
{
system("cls");
cout << " "; //*
for (j = 0; j<40; j++) //*
{ //* UPPER WAAL IN MENU
cout << waal; //*
} //*
cout << " WECLOME TO [PAC-MAN A><e] by ali="" farhat ";
cout << " "; //*
for (j = 0; j<40; j++) //*
{ //* LOWER WAAL IN MENU
cout << waal; //*
} //*
cout << " 1==> Hit Enter to Start the Game" << endl; //*
cout << " 2==> Choose Game Mode OR Enemy Speed Press 2" << endl; //*
cout << " 3==> Press Escape to Exit the Game" << endl; //*
chm = _getch(); //INPUT USER CHOICE TO CONTINUE OR EXIT
if (chm == '2')
{
do
{
system("CLS");
cout << " *** Press Escape to Go Back *** ";
cout << " 1==> TO PLAY SCORE MODE PRESS 1" << endl; //* OPTIONS
cout << " 2==> TO PLAY CLASSIC MODE PRESS 2" << endl; //*
cout << " 3==> TO CHANGE ENEMY SPEED PRESS 3" << endl; //*
chm1 = _getch();
if (chm1 == '1')
{
ghostMode = 0;
cout << " *** SCORE MODE ACTIVATED *** ";
}
if (chm1 == '2')
{
cout << " *** CLASSIC MODE ACTIVATED ";
}
if (chm1 == '3')
{
ghostSpeed = 2;
cout << " *** ENEMY SPEED REDUCED *** ";
}
// if(chm1!='1'||chm1!='2'||chm1!='3'||chm1!='4')
// {
// cout<<"Wrong Choice";
// }
} while (chm1 != esc);
}
if (chm == '3') //HELP GUIDE FROM OPTION CHOICE 3
{
system("CLS");
do
{
chm1 = _getch();
} while (chm1 != esc);
} //CHOICE 3 BRACKET CLOSE
if (chm == '3') //ABOUT FROM OPTION 4
{
system("CLS");
do
{
cout << " "; //*
for (j = 0; j<40; j++) //*
{ //* LOWER WAAL IN MENU
cout << waal; //*
} //*
cout << endl;
chm1 = _getch();
} while (chm1 != esc);
} //OPTION 4 BRACKET CLOSE
if (chm == esc)
{
exit(1);
}
system("cls");
} while (chm != enter);
}
void game::showScore() //******SHOW SCORE FUNCTION******
{
cout << " ************* Score is::" << score << " ************* ";
cout << " Food Eaten=" << foodEaten << endl;
}
void game::makePacMap() //******MAKING PAC-MAP FUNCTION******
{ //makePacMap bracket start
for (x = 0; x<15; x++) //making pac map [X] is Row & [Y] is Column
{
for (y = 0; y<40; y++)
{
pac[x][y] = food; //FILLING MAP WITH FOOD
if (y == 3 || y == 10 || y == 15 || y == 20 || y == 25 || y == 30 || y == 35)
{
pac[x][y] = null;
}
if (x == 12 || x == 9 || x == 3)
{
pac[x][y] = null;
}
if (x == 12 && y == 4 || x == 12 && y == 5) //*
{ //* blocking
pac[x][y] = x_axis; //*
} //*
if (y == 9 && x == 5 || y == 9 && x == 4)
{
pac[x][y] = y_axis;
}
if (x == 12 && y == 30 || x == 12 && y == 31 || x == 12 && y == 32 || x == 12 && y == 33)
{ //* blocking
pac[x][y] = x_axis; //*
} //*
if (x == 7 && y == 16 || x == 7 && y == 24) //Bonus Food
{ //Bonus Food
pac[x][y] = bonusFood; //Bonus Food
} //Bonus Food
if (x == 1 && y == 36 || x == 13 && y == 4) //Bonus Food
{ //Bonus Food
pac[x][y] = bonusFood; //Bonus Food
}
if (y == 18 || y == 22)
{
pac[x][y] = y_axis; //MAKING VERTICAL FREE WAY WAAL
if (x == 4 || x == 5 || x == 6 || x == 7 || x == 8 || x == 9 || x == 10)
{ //making FREE SPACE
pac[x][y] = null;
}
}
if (y == 1 && x == 8 || y == 2 && x == 8 || y == 3 && x == 8) //*
{ //*
pac[x][y] = x_axis; //*
} //* Making left side inside waal
if (y == 1 && x == 5 || y == 2 && x == 5 || y == 3 && x == 5) //*
{ //*
pac[x][y] = x_axis; //*
} //*
if (y == 38 && x == 8 || y == 37 && x == 8 || y == 36 && x == 8) //*
{ //*
pac[x][y] = x_axis; //*
} //* Making right side inside waal
if (y == 38 && x == 5 || y == 37 && x == 5 || y == 36 && x == 5) //*
{ //*
pac[x][y] = x_axis; //*
} //*
if (x == 3 && y == 7 || x == 3 && y == 8 || x == 3 && y == 9 || x == 3 && y == 10) //waal
{ //waal
pac[x][y] = x_axis; //waal
} //waal
//* LEFT SIDE UP LINE //waal
if (x == 3 && y == 11 || x == 3 && y == 12 || x == 3 && y == 13 || x == 3 && y == 14) //waal
{ //waal
pac[x][y] = x_axis; //waal
} //waal
//waal
if (x == 10 && y == 7 || x == 10 && y == 8 || x == 10 && y == 9 || x == 10 && y == 10) //waal
{ //waal
pac[x][y] = x_axis; //waal
} //* LEFT SIDE DOWN LINE //waal
if (x == 11 && y == 11 || x == 11 && y == 12 || x == 11 && y == 13 || x == 11 && y == 14) //waal
{ //waal
pac[x][y] = x_axis; //waal
} //waal
if (x == 3 && y == 26 || x == 3 && y == 27 || x == 3 && y == 28 || x == 3 && y == 29) //waal
{ //waal
pac[x][y] = x_axis; //waal
} //* RIGHT SIDE UP LINE //waal
if (x == 3 && y == 30 || x == 3 && y == 31 || x == 3 && y == 32 || x == 3 && y == 33) //waal
{ //waal
pac[x][y] = x_axis; //waal
} //waal
if (x == 11 && y == 26 || x == 11 && y == 27 || x == 11 && y == 28 || x == 11 && y == 29) //waal
{ //waal
pac[x][y] = x_axis; //waal
} //* RIGHT SIDE DOWN LINE //waal
if (x == 10 && y == 30 || x == 10 && y == 31 || x == 10 && y == 32 || x == 10 && y == 33) //waal
{ //waal
pac[x][y] = x_axis; //waal
} //waal
if (x == 5 && y == 14 || x == 6 && y == 14 || x == 7 && y == 14 || x == 8 && y == 14) //box
{ //BOX LEFT //box
pac[x][y] = y_axis; //box
} //box
if (x == 5 && y == 26 || x == 6 && y == 26 || x == 7 && y == 26 || x == 8 && y == 26) //box
{ //BOX RIGHT //box
pac[x][y] = y_axis; //box
} //box
//box
if (x == 5 && y == 15 || x == 5 && y == 16 || x == 5 && y == 17 || x == 5 && y == 18) //box
{ //BOX UP-LEFT //box
pac[x][y] = x_axis; //box
} //box
if (x == 5 && y == 22 || x == 5 && y == 23 || x == 5 && y == 24 || x == 5 && y == 25) //box
{ //BOX UP-RIGHT //box
pac[x][y] = x_axis; //box
} //box
//box
if (x == 9 && y == 15 || x == 9 && y == 16 || x == 9 && y == 17 || x == 9 && y == 18) //box
{ //BOX DOWN //box
pac[x][y] = x_axis; //box
} //box
if (x == 9 && y == 14) //box
{ //BOX DOWN //box
pac[x][y] = x_axis; //box
} //box
if (x == 9 && y == 19 || x == 9 && y == 20 || x == 9 && y == 21 || x == 9 && y == 22) //box
{ //BOX DOWN //box
pac[x][y] = x_axis; //box
} //box
if (x == 9 && y == 23 || x == 9 && y == 24 || x == 9 && y == 25 || x == 9 && y == 26) //box
{ //box
pac[x][y] = x_axis; //box
} //box
if (x == 14 || y == 39 || x == 0 || y == 0) //MAKING MAIN MAP BOUNDARY
{
pac[x][y] = waal;
}
if (x == 0 && y == 20 || y == 19 || y == 21 || x == 14 && y == 20)
{ //VERTICAL WAAL FREE WAY
pac[x][y] = null;
}
if (y == 0 && x == 7 || x == 6 || y == 39 && x == 7)
{ //HORIZONTAL WAAL FREE WAY
pac[x][y] = null;
}
} //Making PAC-MAP FOR LOOP X BRACKET CLOSE
} //Making PAC-MAP FOR LOOP Y BRACKET CLOSE
cout << " ";
} //makePacMap bracket close
void game::showPacMap() //******OUTPUT PAC-MAP FUNCTION******
{ //showPacMap bracket start
// cout<<"[gamingumar.blogspot.com] ";
for (y = 0; y<15; y++)
{
cout << " ";
for (x = 0; x<40; x++)
{
if (chkStart == 0)
{
pac[yi][xi] = '<';
}
if (pac[y][x] == (char)waal)
{
cout << "[1;34m" << pac[y][x]; //Setting the text color of console to blue. Code 34 is for blue
}
else if (pac[y][x] == (char)food || pac[y][x] == (char)bonusFood)
{
cout << "[1;33m" << pac[y][x]; //Setting the text color of console to yellow. Code 33 is for yellow
}
else
{
cout << "[0m" << pac[y][x]; //Setting the text color of console to default.
}
chkStart++;
}
cout << "[0m";
cout << endl;
}
} //showPacMap bracket close
void game::countDown() //******COUNT DOWN FUNCTION******
{
//60000 = 60000-100
stopWatch = stopWatch - 100;
sec = stopWatch / 1000;
cout << " Time Left [" << sec << "]" << endl;
}
void game::calFood() //******CALCULATE FOOD FUNCTION******
{
for (x = 0; x<15; x++)
{
for (y = 0; y<40; y++)
{
if (pac[x][y] == food || pac[x][y] == bonusFood)
{
foodCount++;
}
}
}
cout << "Food in The Map is=" << foodCount << endl;
}
void game::downBody() //******COMMON THINGS IN GAME FUNCTION******
{
if (sec == timeUp || input == enter || foodEaten == foodCount || pac[yi][xi] == enemy)
{ //IF THESE CONDITION ARE TRUE, SHOW MESSAGE & RESET GAME
if (sec == timeUp)
{
cout << " "; //*
for (j = 0; j<40; j++) //*
{ //* UPPER WAAL IN MESSAGE
cout << waal; //*
} //*
cout << " :::TIME UP,,, GAME OVER:::"; //WHEN TIME IS UP GAME WILL OVER
cout << " "; //*
for (j = 0; j<40; j++) //*
{ //* LOWER WAAL IN MESSAGE
cout << waal; //*
} //*
}
if (foodEaten == foodCount)
{
cout << " "; //*
for (j = 0; j<40; j++) //*
{ //* UPPER WAAL IN MESSAGE
cout << waal; //*
} //*
cout << " :::CONGRATULATIONS,,, YOU WON:::"; //WHEN PAC-MAN HAVE EATEN ALL THE FOOD
//YOU WON THE GAME
cout << " "; //*
for (j = 0; j<40; j++) //*
{ //* LOWER WAAL IN MESSAGE
cout << waal; //*
} //*
}
if (pac[yi][xi] == enemy)
{
cout << " "; //*
for (j = 0; j<40; j++) //*
{ //* UPPER WAAL IN MESSAGE
cout << waal; //*
} //*
cout << " :::GHOST ATE YOU,,, GAME OVER:::"; //WHEN GHOST ATE YOU GAME WILL OVER
cout << " "; //*
for (j = 0; j<40; j++) //*
{ //* LOWER WAAL IN MESSAGE
cout << waal; //*
} //*
}
showScore();
stopWatch = reset;
chkStart = 0; foodEaten = -1;
yi = 10; xi = 20;
score = 0;
cout << " ENTER 1 TO GO TO MAIN MENU ";
cout << " ENTER 2 TO EXIT GAME ";
cout << "Enter Your Choice=";
cin >> ch;
if (ch == 1) //This is Main Menu if choice is 1 continue the game
{
MainMenu();
makePacMap();
showPacMap();
pacmanMove();
// ghost();
}
if (ch == 2) //This is Main Menu if choice is 2 exit the game
{
exit(1);
}
else
{
if (ch != 2 || ch != 3)
{
MainMenu();
}
}
}
/* if(pac[yi][xi]==enemy) //LIVES LEFT COUNTER
{
lives--;
cout<<"lives Left=" <
Sleep(delay*60);
cout<<"lives Left=" <
}*/
if (sec != timeUp || foodEaten != foodCount || pac[yi][xi] != enemy)
{
showScore();
showPacMap();
countDown();
// ghost();
Sleep(delay); //controlling the speed of Pacman
system("CLS"); //clears the previous screen
if (ghostMode == 1)
{
if (delay += ghostSpeed)// && ghostMode==1)
{
ghost(yi, xi);
}
}
}
}
void game::pacmanMove() //******MOVING PAC-MAN FUNCTION******
{ //pacmanMove bracket start
do
{ //pacmanMove do bracket start
cout << endl;
input = _getch(); //Direction Input of Pacman
switch (input) //choosing the direction using case
{ //switch bracket start
case right: //***MOVING RIGHT SIDE CODE***
{ //case right bracket
while (!_kbhit())
{
if (xi == xiLimit)
{
pac[yi][xi] = null;
xi = xiStart;
}
if (pac[yi][xi + 1] == food) //*
{ //*
pac[yi][xi + 1] = '<'; //*
pac[yi][xi] = null; //*=====>Right Side Food Eating
xi++; //*
score++; //*
foodEaten++; //*
}
if (pac[yi][xi + 1] == waal)
{
downBody(); //if it is waal do nothing except timer
}
if (pac[yi][xi + 1] == null) //if next is nothing
{
pac[yi][xi + 1] = '<';
pac[yi][xi] = null;
xi++;
}
if (pac[yi][xi + 1] == bonusFood) //if next is bonus food
{
pac[yi][xi + 1] = '<';
pac[yi][xi] = null;
xi++;
score += 200;
foodEaten++;
}
downBody();
}
break;
} //case right bracket close
case left: //***MOVING LEFT SIDE CODE***
{ //case left bracket start
while (!_kbhit())
{ //left while bracket start
if (xi == xiStart) //if Pac-Map Limit riched
{
pac[yi][xi] = null;
xi = xiLimit;
}
if (pac[yi][xi - 1] == food) //if next is food
{
pac[yi][xi - 1] = '>';
pac[yi][xi] = null;
xi--;
score++;
foodEaten++;
}
if (pac[yi][xi - 1] == waal) //if next is waal
{
downBody();
}
if (pac[yi][xi - 1] == null) //if next is nothing
{
pac[yi][xi - 1] = '>';
pac[yi][xi] = null;
xi--;
}
if (pac[yi][xi - 1] == bonusFood) //if next is bonus food
{
pac[yi][xi - 1] = '>';
pac[yi][xi] = null;
xi--;
score += 200;
foodEaten++;
}
downBody();
} //left while bracket close
break;
} //case left bracket close
case up: //***MOVING TOWARDS UP CODE***
{ //case up bracket start
while (!_kbhit())
{ //up while bracket start
if (yi == yiStart) //if Pac-Map Limit riched
{
pac[yi][xi] = null;
yi = yiLimit;
}
if (pac[yi - 1][xi] == food) //if next is food
{
pac[yi][xi] = null;
pac[yi - 1][xi] = 'V';
yi--;
score++;
foodEaten++;
}
if (pac[yi - 1][xi] == waal) //if next is waal
{
downBody();
}
if (pac[yi - 1][xi] == null) //if next is nothing
{
pac[yi - 1][xi] = 'V';
pac[yi][xi] = null;
yi--;
}
if (pac[yi - 1][xi] == bonusFood) //if next is bonus food
{
pac[yi - 1][xi] = 'V';
pac[yi][xi] = null;
yi--;
score += 200;
foodEaten++;
}
downBody();
} //up while bracket close
break;
} //case up bracket close
case down: //***MOVING DOWN WARDS CODE***
{ //case down bracket start
while (!_kbhit())
{ //down while bracket start
if (yi == yiLimit) //if Pac-Map Limit riched
{
pac[yi][xi] = null;
yi = yiStart;
}
if (pac[yi + 1][xi] == food) //if next is food
{
pac[yi][xi] = null;
pac[yi + 1][xi] = '^';
yi++;
score++;
foodEaten++;
}
if (pac[yi + 1][xi] == waal) //if next is waal
{
downBody();
}
if (pac[yi + 1][xi] == null) //if next is nothing
{
pac[yi + 1][xi] = '^';
pac[yi][xi] = null;
yi++;
}
if (pac[yi + 1][xi] == bonusFood) //if next is bonus food
{
pac[yi + 1][xi] = '^';
pac[yi][xi] = null;
yi++;
score += 200;
foodEaten++;
}
downBody();
} //down while bracket close
break;
} //case down bracket close
case enter:
{
downBody();
}
default:
{
showPacMap();
cout << " GAME PAUSED ";
}
} //switch bracket close
} //do bracket close
while (input != esc); //IF ESCAPE KEY IS PRESSED CLOSE THE GAME
} //pacmanMove bracket close
void game::ghost(int tmp1, int tmp2) //******ENEMY GHOST FUNCTION******
{ //GHOST FUNCTION BRACKET START
pac[t1][t2] = pac[tmp1][tmp2]; //SAVING GHOST POSITION TEMPORARY
pac[ey][ex] = enemy;
ey = tmp1;
ex = tmp2;
pac[ey][ex] = pac[tmp1][tmp2];
if (pac[t1][t2] == food)
{
pac[ey][ex] = food;
}
else
{
if (pac[t1][t2] == null)
{
pac[ey][ex] = null;
}
}
}
void game::moveRandom()
{
}
pacman.h
#ifndef pacheadH
#define pacheadH
class game
{
public:
int chkStart, score, x, y, counter, ex, ey, chooseEnemyWay, mainCh, lives;
int yi, xi, xiStart, yiStart, yiLimit, xiLimit, t1, t2, ghostMode;
int ch, ch1, foodCount, foodEated, chk, null, x_axis, y_axis, j;
float delay, stopWatch, sec, reset, timeUp, ghostSpeed;
char input, pac[15][40], food, bonusFood, waal, enemy;
char chm, chm1; //Options from Main Menu
void makePacMap();
void showPacMap();
void pacmanMove();
void showScore();
void downBody(); //it will output & call common functions
void MainMenu();
void countDown();
void calFood();
void ghost(int tmp1, int tmp2);
void moveRandom();
void initializeRandom();
game() //CONSTRUCTOR INITIALIZING VALUES
{
// cout<<" Game Constructor Called";
chkStart = 0; score = 0; counter = 0; //chkStart will see if its the first time you have started game
food = '.'; null = '';
foodCount = -1; foodEated = 0; //total food count which includes bonus + simple food
yi = 10; xi = 20; //initializing the starting point of PAC-MAN
yiLimit = 14; xiLimit = 39; //PAC-MAP limit is the max 2D Array Size
xiStart = 0; yiStart = 0; //if PAC-MAN reaches end of the Map & there is no waal
//then it will come to the start of the array
timeUp = 0.0; lives = 3;
bonusFood = 157; //Special Food Item
enemy = 'X';//238; //Enemy or Ghost
waal = 219; //This is the main Waal Character ASCII value
x_axis = 205; y_axis = 186; //These are the ASCII of inner waals of the map
ghostSpeed = 0.5; ghostMode = 0;
ey = 10; ex = 19;
// ex=16; ey=8; //Enemy/Ghost Starting Point Location
delay = 40; //This is time delay used in my Stop Watch & timing
stopWatch = 60000; reset = 60000; //This is starting time of Stop Watch
t1 = 10; t2 = 19;
}
~game()
{
// cout<<" Game Destructor Called";
}
};
#endif
Source1.cpp
#include
#include"pacman.h"
#include
#include
#define esc 27
#define enter 13
using namespace std;
void main()
{
game pacman;
// do
// {
// pacman.initializeRandom();
pacman.MainMenu();
pacman.showScore();
pacman.makePacMap();
pacman.calFood();
pacman.showPacMap();
pacman.pacmanMove();
cout << "Press Escape Key to Exit Game" << endl;
// pacman.mainCh=getch();
// }
// while(pacman.mainCh!=esc || pacman.mainCh==enter);
}
Explanation / Answer
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <ctime>
using namespace std;
// 35 = walls
//111 = small pills
// 79 = power pills
//074 = pacman right
//076 = pacman left
//118 = pacman up
//94 = pacman down
// 87 = ghost1
// 88 = ghost2
// 89 = ghost3
const int MAXROWS = 32;
const int MAXCOLUMNS = 28;
char keyboardValue = 127;
//Start position is [15][0]
int pacI = 15, pacJ = 0;
//Ghost1 Start postion is [16][14]
int ghostOneI = 16, ghostOneJ = 14;
int node1i = 16, node1j = 5;
int i = 0 , j = 0;
char key = 127;
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}
};
char ghostCurrent = levelMatrix[ghostOneI][ghostOneJ];
char ghostTrail = ghostCurrent[&ghostOneI][&ghostOneJ];
void getKeyPress();
bool blockValid(int block);
void Ghost();
void GoToXY(int column, int line);
bool ghostMoveUp();
bool ghostMoveDown();
bool ghostMoveLeft();
bool ghostMoveRight();
int findNode();
int pillCount = 244;
int lives = 3;
clock_t startTime = 0, endTime= 0;
bool superPellet = false;
bool pill = false;
int main()
{
HANDLE hConsole;
int k =14;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, k);
cout << " ############### "<< endl;
cout << " ################## "<< endl;
cout << " ####################### "<< endl;
cout << " ############## ########### "<< endl;
cout << "############################## "<< endl;
cout << "####################### "<< endl;
cout << "#################### "<< endl;
cout << "################ "<< endl;
cout << "############# "<< endl;
cout << "########### "<< endl;
cout << "######### "<< endl;
cout << "############ "<< "PACMAN" << endl;
cout << "################## "<< endl;
cout << "####################### "<< endl;
cout << "########################### "<< endl;
cout << "############################## "<< endl;
cout << " ####################### "<< endl;
cout << " #################### "<< endl;
cout << " ################ "<< endl;
cout << endl;
system("pause");
char key = 0;
system("mode 80, 50");
int pills = 30;
bool stopCounting = false;
//create the matrix
//28 columns
//32 high
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);
}
cout << levelMatrix[i][j];
if ( j == 27)
cout << endl;
if ( j == 27 && i == 31)
stopCounting = true;
}
}
getKeyPress();
//Color
k = 15;
SetConsoleTextAttribute(hConsole, k);
cout << "lives remaining " << lives << endl;
endTime = clock();
int elapsed = int((endTime - startTime) / CLOCKS_PER_SEC);
//cout << elapsed;
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()
{
char ghostNextDown = levelMatrix[ghostOneI + 1][ghostOneJ];
if ( ghostNextDown != 35)
{
if ( ghostNextDown == 111 || ghostNextDown ==117)
{
levelMatrix[ghostOneI + 1][ghostOneJ] = 87;
levelMatrix[ghostOneI][ghostOneJ] = ghostNextDown;
ghostOneI++;
}
else
{
levelMatrix[ghostOneI][ghostOneJ] = 000;
levelMatrix[ghostOneI + 1][ghostOneJ] = 87;
ghostOneI++;
}
return true;
}
else
{
return false;
}
}
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)
{
if ( ghostNextRight == 111 || ghostNextRight ==117)
{
levelMatrix[ghostOneI][ghostOneJ + 1] = 87;
levelMatrix[ghostOneI][ghostOneJ] = ghostNextRight;
ghostOneJ++;
}
else
{
levelMatrix[ghostOneI][ghostOneJ] = 000;
levelMatrix[ghostOneI][ghostOneJ + 1] = 87;
ghostOneJ++;
}
return true;
}
else
{
return false;
}
}
void Ghost()
{
if (superPellet)
{
//Ghost is moving up
if ( pacI > ghostOneI)
{
//if wall found
if (!ghostMoveUp())
{
//find node path
}
}
//Ghost is moving down
else if (pacI < ghostOneI)
{
if (!ghostMoveDown())
{
}
}
//Ghost is moving left
if (pacJ > ghostOneJ)
{
if (!ghostMoveLeft())
{
}
}
//Ghost is moving right
else if (pacJ < ghostOneJ)
{
if (!ghostMoveRight())
{
}
}
}
else
{
//Ghost is moving up
if ( pacI < ghostOneI)
{
//if wall found
if (!ghostMoveUp())
{
}
}
//Ghost is moving down
else if (pacI > ghostOneI)
{
if (!ghostMoveDown())
{
}
}
//Ghost is moving left
if (pacJ < ghostOneJ)
{
if (!ghostMoveLeft())
{
}
}
//Ghost is moving right
else if (pacJ > ghostOneJ)
{
if (!ghostMoveRight())
{
}
}
}
//you caught the ghost
if (pacJ == ghostOneJ && pacI == ghostOneI && superPellet)
{
levelMatrix[ghostOneI][ghostOneJ] = 074;
ghostOneI = 16;
ghostOneJ = 14;
levelMatrix[16][14] = 87;
}
//Reset the postion of pacman you died
else if (pacJ == ghostOneJ && pacI == ghostOneI)
{
lives--;
levelMatrix[pacI][pacJ] = 000;
levelMatrix[ghostOneI][ghostOneJ] = 000;
pacI = 15;
pacJ = 0;
ghostOneI = 16;
ghostOneJ = 14;
levelMatrix[15][0] = 074;
levelMatrix[16][14] = 87;
}
}
void getKeyPress()
{
key = _getch();
char pacTemp = '<';
//get a special key
if (key == 0 || key == -32)
{
key = _getch();
if (key == 72) //up
{
if ( levelMatrix[pacI- 1][pacJ] != 35)
{
if (levelMatrix[pacI- 1][pacJ] == 79)
{
startTime = clock();
superPellet = true;
}
levelMatrix[pacI][pacJ] = 000;
levelMatrix[pacI - 1][pacJ] = 118;
pacI--;
}
}
else if (key == 75) //left
{
if ( levelMatrix[pacI][pacJ- 1] != 35)
{
if (levelMatrix[pacI][pacJ - 1] == 79)
{
startTime = clock();
superPellet = true;
}
levelMatrix[pacI][pacJ] = 000;
levelMatrix[pacI][pacJ - 1] = 076;
pacJ--;
}
}
else if (key == 77) //right
{
if ( levelMatrix[pacI][pacJ+ 1] != 35)
{
if (levelMatrix[pacI][pacJ + 1] == 79)
{
startTime = clock();
superPellet = true;
}
levelMatrix[pacI][pacJ] = 000;
levelMatrix[pacI][pacJ + 1] = 074;
pacJ++;
}
}
else if (key == 80) //down
{
if ( levelMatrix[pacI + 1][pacJ] != 35)
{
if (levelMatrix[pacI + 1][pacJ] == 79)
{
startTime = clock();
superPellet = true;
}
levelMatrix[pacI][pacJ] = 000;
levelMatrix[pacI + 1][pacJ] = 94;
pacI++;
}
}
}
}
void GoToXY(int column, int line)
{
// Create a COORD structure and fill in its members.
// This specifies the new position of the cursor that we will set.
COORD coord;
coord.X = column;
coord.Y = line;
// Obtain a handle to the console screen buffer.
// (You're just using the standard console, so you can use STD_OUTPUT_HANDLE
// in conjunction with the GetStdHandle() to retrieve the handle.)
// Note that because it is a standard handle, we don't need to close it.
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Finally, call the SetConsoleCursorPosition function.
if (!SetConsoleCursorPosition(hConsole, coord))
{
cout << "Some error happened."
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.