jf8441's picture
Upload 25 files
0ef7a64 verified
raw
history blame
7.02 kB
/*
Name:Wong Pui Shan
Sdutent ID:52611804
program: AScISD
Name: HAR Chiu Kwong Samson
Sdutent ID:52629360
program: AScISD
Name: LAM Cheuk Man
Sdutent ID:52621140
program: AScISD
Name:KO Jeffrey KO
Sdutent ID:525 695 30
program: AScISD
*/
#include <iostream>
#include <fstream>
#include <string>
#include "Utilities.h"
#include "Game.h"
#include "Ghost.h"
#include "Pacman.h"
#include "Maze.h"
#include <climits>
#include <ctime>
using namespace std;
Ghost::Ghost(){
ax = 1;
ay = 1;
direction = 5;
d = 0;
g = "A";
OrgX = 0;
OrgY = 0;
limit = 0;
speed = 0;
reduceLimit = 100;
}
Ghost::Ghost(int x, int y, string GhostG)
{
ax = x;
ay = y;
direction = 5;
d = 0;
g = GhostG;
OrgX = x;
OrgY = y;
limit = 0;
reduceLimit = 100;
}
void Ghost::increaseSpeed(int &dot){
if(limit >60){
if(dot == 20)
reduceLimit = 80;
if(dot == 40)
reduceLimit = 90;
if(dot == 60)
reduceLimit = 70;
}
}
void Ghost::ChangePosition()
{
if(g == "A")
g = "a";
else if(g == "B")
g = "b";
else if(g == "C")
g = "c";
else if(g == "D")
g = "d";
}
void Ghost::showGhost(Utilities a)
{
//Set up the color of the ghosts.
a.gotoXY(ax, ay);
if (g == "A")
a.changeColour(FOREGROUND_RED);
if (g == "B")
a.changeColour(0x000d);
if (g == "C")
a.changeColour(FOREGROUND_GREEN);
if (g == "D")
a.changeColour(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
if (g == "a" || g == "b" || g == "c" || g == "d")
a.changeColour(BACKGROUND_WHITE | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << g;
}
void Ghost::Move(Maze &m, Utilities a)
{
//Set up the speed of the ghost
if (limit > 0)
{
--limit;
return;
}
limit = reduceLimit;
//Avoid the ghost to eat the dot, power dot and the fruit on the map
a.gotoXY(ax,ay);
a.changeColour(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
if (m.getMazeArray(ax, ay)=='X')
{
m.setMazeArray(ax, ay,'X');
cout << "X";
}
else if (m.getMazeArray(ax, ay)=='.')
{
m.setMazeArray(ax, ay,'.');
cout << ".";
}
else if (m.getMazeArray(ax, ay)==' ')
{
m.setMazeArray(ax, ay,' ');
cout << " ";
}
a.gotoXY(ax,ay);
a.changeColour(FOREGROUND_GREEN | FOREGROUND_BLUE);
if (m.getMazeArray(ax, ay)=='!')
{
m.setMazeArray(ax, ay,'!');
cout << "!";
}
else if (m.getMazeArray(ax, ay)=='*')
{
m.setMazeArray(ax, ay,'*');
cout << "*";
}
else if (m.getMazeArray(ax, ay)=='#')
{
m.setMazeArray(ax, ay,'#');
cout << "#";
}
do{
// loop for granerate ghost's direction
do{
d = rand() % 4 + 1;
if (m.getMazeArray(ax, ay-1)!='-' && m.getMazeArray(ax, ay-1)!='|' && d == 1)
break;
else if (m.getMazeArray(ax, ay+1)!='-' && m.getMazeArray(ax, ay+1)!='|'&& d == 4)
break;
else if (m.getMazeArray(ax-1, ay)!='-' && m.getMazeArray(ax-1, ay)!='|'&& d == 2)
break;
else if (m.getMazeArray(ax+1, ay)!='-' && m.getMazeArray(ax+1, ay)!='|'&& d == 3)
break;
}while (true);
}while (d == 5 - direction ||
(d == 4 && (ax==8 && ay==5))||
(d == 4 && (ax==9 && ay==5))||
(d == 4 && (ax==10 && ay==5))|| //It is used for avoid go oppostie direction and go to ghost home
(d == 4 && (ax==11 && ay==5))||
(d == 4 && (ax==12 && ay==5)));
//change the diection
switch(d){
case 1:
ay--;
direction = 1;
break;
case 4: // Down arrow
ay++;
direction = 4;
break;
case 2: // Left arrow
ax--;
direction = 2;
break;
case 3: // Right arrow
ax++;
direction = 3;
break;
}
a.gotoXY(ax,ay);
cout << g;
}
//if the pacman eat the power dot, the status od the ghost change.
void Ghost::slowMove(Maze &m, Utilities a)
{
//reduce the speed of the ghosts
if (speed > 0)
{
--speed;
return;
}
speed = 200;
//set up the color of the ghosts
a.gotoXY(ax,ay);
a.changeColour(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
if (m.getMazeArray(ax, ay)=='X')
{
m.setMazeArray(ax, ay,'X');
cout << "X";
}
else if (m.getMazeArray(ax, ay)=='.')
{
m.setMazeArray(ax, ay,'.');
cout << ".";
}
else if (m.getMazeArray(ax, ay)==' ')
{
m.setMazeArray(ax, ay,' ');
cout << " ";
}
else if (m.getMazeArray(ax, ay)=='!')
{
m.setMazeArray(ax, ay,'!');
cout << "!";
}
else if (m.getMazeArray(ax, ay)=='*')
{
m.setMazeArray(ax, ay,'*');
cout << "*";
}
else if (m.getMazeArray(ax, ay)=='#')
{
m.setMazeArray(ax, ay,'#');
cout << "#";
}
do{
// loop for granerate ghost's direction
do{
d = rand() % 4 + 1;
if (m.getMazeArray(ax, ay-1)!='-' && m.getMazeArray(ax, ay-1)!='|' && d == 1)
break;
else if (m.getMazeArray(ax, ay+1)!='-' && m.getMazeArray(ax, ay+1)!='|'&& d == 4)
break;
else if (m.getMazeArray(ax-1, ay)!='-' && m.getMazeArray(ax-1, ay)!='|'&& d == 2)
break;
else if (m.getMazeArray(ax+1, ay)!='-' && m.getMazeArray(ax+1, ay)!='|'&& d == 3)
break;
}while (true);
}while (d == 5 - direction ||
(d == 4 && (ax==8 && ay==5))||
(d == 4 && (ax==9 && ay==5))||
(d == 4 && (ax==10 && ay==5))|| //It is used for avoid go oppostie direction and go to ghost home
(d == 4 && (ax==11 && ay==5))||
(d == 4 && (ax==12 && ay==5)));
//change the diection
switch(d){
case 1:
ay--;
direction = 1;
break;
case 4: // Down arrow
ay++;
direction = 4;
break;
case 2: // Left arrow
ax--;
direction = 2;
break;
case 3: // Right arrow
ax++;
direction = 3;
break;
}
a.gotoXY(ax,ay);
cout << g;
}
void Ghost::EatPacman(Pacman &p, Game &g, Utilities &a,Utilities &u,Utilities &j, Utilities &k, Utilities &l, Ghost &o, Ghost &m, Ghost &n)
{
//if ghost met pacman, pacman life reduce and the location of ghosts and Pacman reset
if (ax == p.getPacmanX() && ay == p.getPacmanY())
{
g.life--;
Ghost::ResetGhost(a);
n.ResetGhost(j);
m.ResetGhost(k);
o.ResetGhost(l);
p.Die(u);
}
}
void Ghost::ResetGhost(Utilities &a){
a.gotoXY(ax, ay);
a.changeColour(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << " ";
Ghost::ResetLocation();
a.gotoXY(ax, ay);
a.changeColour(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << g;
}
void Ghost::ResetLocation(){
//save the original location of ghost
ax = OrgX;
ay = OrgY;
}
void Ghost::RechangePosition(){
//change the status of ghost to nomal
if(g == "a")
g = "A";
else if(g == "b")
g = "B";
else if(g == "c")
g = "C";
else if(g == "d")
g = "D";
}
int Ghost::GetGhostX(){
return ax;
}
int Ghost::GetGhostY(){
return ay;
}
string Ghost::GetGhostMember(){
return g;
}
Ghost::~Ghost(){
}