From 39aefffbcdbf3e8aeff5f670dcfd97b8499322ce Mon Sep 17 00:00:00 2001 From: Maunas Mehta Date: Sun, 11 Oct 2020 15:18:05 +0200 Subject: [PATCH] PacManGame --- README.md | 1 + game-source-code/Datatypes.h | 14 + game-source-code/Game.cpp | 668 ++++++++++++++++++++++++++ game-source-code/Game.h | 109 +++++ game-source-code/Ghosts.cpp | 595 ++++++++++++++++++++++++ game-source-code/Ghosts.h | 28 ++ game-source-code/Movement.cpp | 325 +++++++++++++ game-source-code/Movement.h | 42 ++ game-source-code/PositionHandler.cpp | 176 +++++++ game-source-code/PositionHandler.h | 38 ++ game-source-code/eater.cpp | 68 +++ game-source-code/eater.h | 16 + game-source-code/main.cpp | 88 ++++ test-source-code/Datatypes.h | 14 + test-source-code/Game.cpp | 672 +++++++++++++++++++++++++++ test-source-code/Game.h | 109 +++++ test-source-code/Ghosts.cpp | 588 +++++++++++++++++++++++ test-source-code/Ghosts.h | 28 ++ test-source-code/Movement.cpp | 325 +++++++++++++ test-source-code/Movement.h | 42 ++ test-source-code/PositionHandler.cpp | 165 +++++++ test-source-code/PositionHandler.h | 37 ++ test-source-code/eater.cpp | 68 +++ test-source-code/eater.h | 16 + test-source-code/main.cpp | 293 ++++++++++++ 25 files changed, 4525 insertions(+) create mode 100644 README.md create mode 100644 game-source-code/Datatypes.h create mode 100644 game-source-code/Game.cpp create mode 100644 game-source-code/Game.h create mode 100644 game-source-code/Ghosts.cpp create mode 100644 game-source-code/Ghosts.h create mode 100644 game-source-code/Movement.cpp create mode 100644 game-source-code/Movement.h create mode 100644 game-source-code/PositionHandler.cpp create mode 100644 game-source-code/PositionHandler.h create mode 100644 game-source-code/eater.cpp create mode 100644 game-source-code/eater.h create mode 100644 game-source-code/main.cpp create mode 100644 test-source-code/Datatypes.h create mode 100644 test-source-code/Game.cpp create mode 100644 test-source-code/Game.h create mode 100644 test-source-code/Ghosts.cpp create mode 100644 test-source-code/Ghosts.h create mode 100644 test-source-code/Movement.cpp create mode 100644 test-source-code/Movement.h create mode 100644 test-source-code/PositionHandler.cpp create mode 100644 test-source-code/PositionHandler.h create mode 100644 test-source-code/eater.cpp create mode 100644 test-source-code/eater.h create mode 100644 test-source-code/main.cpp diff --git a/README.md b/README.md new file mode 100644 index 0000000..9fa3710 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Software Development II - Course Project diff --git a/game-source-code/Datatypes.h b/game-source-code/Datatypes.h new file mode 100644 index 0000000..62973b3 --- /dev/null +++ b/game-source-code/Datatypes.h @@ -0,0 +1,14 @@ +#ifndef DATATYPES_H +#define DATATYPES_H + +enum IDENTITY{Pac, Blue, Red, Orange, Pink}; +enum DIRECTION{Left, Right, Up, Down}; +enum TEXTURE {Wall, Key, Power, Super, blank, Lock, BreakLock, Food}; +struct Coordinates +{ + int x; + int y; +}; + + +#endif // DATATYPES_H diff --git a/game-source-code/Game.cpp b/game-source-code/Game.cpp new file mode 100644 index 0000000..700c27f --- /dev/null +++ b/game-source-code/Game.cpp @@ -0,0 +1,668 @@ +#include "Game.h" +#include "PositionHandler.h" +#include "Datatypes.h" +#include "Movement.h" +#include "Ghosts.h" + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace sf ; +using namespace std ; +/** @brief Game + * + * @todo: document this function + */ +Game::Game(string FileName) // store everything in the array +{ + FileName_ = FileName ; + LoadTextures(); + totalFood = 0 ; + Score = 0 ; + Tracker.Init_StorePos(FileName); + CreateMaze(); + ResetPos(); +} + +Game::Game() +{ + FileName_ = "Resoruces/Maze/m1.txt" ; + LoadTextures(); + totalFood = 0 ; + Score = 0 ; + Tracker.Init_StorePos(FileName_); + CreateMaze(); + ResetPos(); +} + +void Game::StartGame() +{ + RenderWindow window(VideoMode(Window_WIDTH, Window_HEIGHT), "PacMan Maze"); + Controler(window); +} + +void Game::LoadTextures() +{ + brick.loadFromFile("Resoruces/backgrounds/BlueBrickwall.png"); + pacManRight.loadFromFile("Resoruces/Pac and Crew/PacmanRight.PNG"); + pacManLeft.loadFromFile("Resoruces/Pac and Crew/PacmanLeft.PNG"); + pacManUp.loadFromFile("Resoruces/Pac and Crew/PacmanUP.PNG"); + pacManDown.loadFromFile("Resoruces/Pac and Crew/PacmanDown.PNG"); + ghostBlue.loadFromFile("Resoruces/Pac and Crew/Bluey.PNG"); + ghostRed.loadFromFile("Resoruces/Pac and Crew/Reddy.PNG"); + ghostOrange.loadFromFile("Resoruces/Pac and Crew/Orangey.PNG"); + ghostPink.loadFromFile("Resoruces/Pac and Crew/Pinky.PNG"); + VulnerableGhost.loadFromFile("Resoruces/Pac and Crew/Ghost.PNG"); + PacKey.loadFromFile("Resoruces/backgrounds/Key.png"); + Locker.loadFromFile("Resoruces/backgrounds/lock.png"); + Berry.loadFromFile("Resoruces/backgrounds/Fruit.png"); + YellowPellets.loadFromFile("Resoruces/backgrounds/YellowSpot.png"); + SuperPel.loadFromFile("Resoruces/backgrounds/Super.png"); + PowerPel.loadFromFile("Resoruces/backgrounds/Power.png"); +} + +void Game::CreateMaze() +{ + Score = 0; + + for(int r = 0; r < rows; r++) + { + for(int c = 0; c < cols; c++) + { + if(Tracker.getStorePos(r, c) == 1) + { + maze[r][c].setTexture(brick); + maze[r][c].setScale(0.35, 0.35); + maze[r][c].setPosition(c*Grid_size,r*Grid_size); + } + } + } + blueGhost.setTexture(ghostBlue); + blueGhost.setScale(0.11, 0.11); + + redGhost.setTexture(ghostRed); + redGhost.setScale(0.11, 0.11); + + orangeGhost.setTexture(ghostOrange); + orangeGhost.setScale(0.11, 0.11); + + pinkGhost.setTexture(ghostPink); + pinkGhost.setScale(0.11, 0.11); + //initialize player + player_.setTexture(pacManRight); + player_.setScale(0.025, 0.023); +} + +void Game::ResetPos() +{ + GPelStorC.clear(); + GPelStorR.clear(); + + int counter=0, posX=0, posY=0; + + for(int r = 0; r < rows; r++) + { + for(int c = 0; c < cols; c++) + { + if(Tracker.getStorePos(r, c)== 4) + { + counter++; + + maze[r][c].setTexture(YellowPellets); + maze[r][c].setScale(0.25, 0.25); + maze[r][c].setPosition(c*Grid_size+5,r*Grid_size+5); + totalFood++; + + if(counter == 1) + { + Tracker.SetInit(Blue,r,c); + } + else if(counter == 2) + { + Tracker.SetInit(Red,r,c); + } + else if(counter == 3) + { + Tracker.SetInit(Orange,r,c); + } + else if(counter == 4) + { + Tracker.SetInit(Pink,r,c); + } + } + if(Tracker.getStorePos(r, c) == 9) // player + { + Tracker.SetInit(Pac,r,c); + } + else if(Tracker.getStorePos(r, c) == 6) // Red Pellet + { + maze[r][c].setTexture(PowerPel); + maze[r][c].setScale(0.35, 0.35); + maze[r][c].setPosition(c*Grid_size,r*Grid_size); + } + else if(Tracker.getStorePos(r, c) == 3) + { + maze[r][c].setTexture(Berry); + maze[r][c].setScale(0.35, 0.35); + maze[r][c].setPosition(c*Grid_size,r*Grid_size); + totalFood++; + } + //Locks we need + else if(Tracker.getStorePos(r, c) == 7) + { + Locks[r][c].setTexture(Locker); + Locks[r][c].setPosition(c*Grid_size,r*Grid_size); + Locks[r][c].setScale(0.35, 0.35); + + TempCount++; + if(TempCount % 2 != 0) + { + LockCount ++; + } + if(LockCount == 4) + { + LockCount = 1; + } + setKey(r,c); + } + else if (Tracker.getStorePos(r, c) == 10) + { + maze[r][c].setTexture(SuperPel); + maze[r][c].setScale(0.40, 0.40); + maze[r][c].setPosition(c*Grid_size,r*Grid_size); + } + else if (Tracker.getStorePos(r, c) == 0) // normal pellet + { + maze[r][c].setTexture(YellowPellets); + maze[r][c].setScale(0.25, 0.25); + maze[r][c].setPosition(c*Grid_size+5,r*Grid_size+5); + totalFood++; + } + else if(Tracker.getStorePos(r, c) == 5) + { + maze[r][c].setTexture(PacKey); + maze[r][c].setScale(0.35, 0.35); + maze[r][c].setPosition(c*Grid_size,r*Grid_size); + } + } + } + Update(); +} + +void Game::setKey(int r, int c) +{ + switch(LockCount) + { + case 0: + { + GPelStorR.push_back(r); + GPelStorC.push_back(c); + break; + } + case 1: + { + GPelStorR.push_back(r); + GPelStorC.push_back(c); + break; + } + case 2: + { + GPelStorR2.push_back(r); + GPelStorC2.push_back(c); + break; + } + case 3: + { + GPelStorR3.push_back(r); + GPelStorC3.push_back(c); + break; + } + } +} + +int Game::getTotalFood() +{ + return totalFood; +} + +int Game::getScore() +{ + return Score; +} + +Ghosts Game::getGhostObj() +{ + return Enemy; +} + +Movement Game::getMovementObj() +{ + return Tracker; +} + +Eater Game::getEaterObj() +{ + return Eating; +} + +void Game::ChangesAfterPelletEaten(TEXTURE tir) +{ + ChangeTexture(tir); +} + +Sprite Game::getSprite(IDENTITY ID) +{ + switch(ID) + { + case Blue: + { + return blueGhost; + } + case Red: + { + return redGhost; + } + case Orange: + { + return orangeGhost; + } + case Pink: + { + return pinkGhost; + } + case Pac: + { + return player_; + } + } +} + +void Game::GhostMovement() +{ + if(GhostCounter == 0) + { + if(GhostModeOn == true) // Ghost mode Functions + { + if(((clock()-startGhostModeTimer)/(double)(CLOCKS_PER_SEC))<10) + { + if(Tracker.getGhostMode(Blue) == true) + { + Enemy.GhostMode(Tracker, Blue); + } + else + { + Enemy.BlueGhostMoves(Tracker, BlueMove); + } + if(Tracker.getGhostMode(Red) == true) + { + Enemy.GhostMode(Tracker, Red); + } + else + { + Enemy.RedGhostMoves(Tracker); + } + if(Tracker.getGhostMode(Orange) == true) + { + Enemy.GhostMode(Tracker, Orange); + } + else + { + Enemy.OrangeGhostMoves(Tracker, OrangeMove); + } + if(Tracker.getGhostMode(Pink) == true) + { + Enemy.GhostMode(Tracker, Pink); + } + else + { + Enemy.PinkGhostMoves(Tracker); + } + } + else + { + GhostModeOn = false ; + Tracker.TurnGhostMode(GhostModeOn); + blueGhost.setTexture(ghostBlue); + blueGhost.setScale(0.11,0.11); + redGhost.setTexture(ghostRed); + redGhost.setScale(0.11,0.11); + orangeGhost.setTexture(ghostOrange); + orangeGhost.setScale(0.11,0.11); + pinkGhost.setTexture(ghostPink); + pinkGhost.setScale(0.11,0.11); + } + } + else + { + Enemy.BlueGhostMoves(Tracker, BlueMove); + Enemy.OrangeGhostMoves(Tracker, OrangeMove); + Enemy.RedGhostMoves(Tracker); + Enemy.PinkGhostMoves(Tracker); + } + GhostCounter=3; + } + else + GhostCounter--; +} + +void Game::ChangeTexture(TEXTURE Tir) +{ + switch(Tir) + { + case Super: + { + SuperPac = true; + SuperPacTimer = clock(); + + player_.setScale(0.035, 0.035); + + ChangeGhostSuper(); + Tracker.TurnSuperMode(SuperPac); + + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); + break; + } + case Power: + { + blueGhost.setTexture(VulnerableGhost); + blueGhost.setScale(0.18,0.16); + + redGhost.setTexture(VulnerableGhost); + redGhost.setScale(0.18,0.16); + + orangeGhost.setTexture(VulnerableGhost); + orangeGhost.setScale(0.18,0.16); + + pinkGhost.setTexture(VulnerableGhost); + pinkGhost.setScale(0.18,0.16); + + GhostModeOn = true ; + Tracker.TurnGhostMode(GhostModeOn); + + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); + startGhostModeTimer = clock(); + break; + } + case Key: + { + Tracker.setStorePos(GPelStorR[0],GPelStorC[0],2); + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); + Locks[GPelStorR[0]][GPelStorC[0]].setColor(sf::Color::Transparent); + GPelStorR.erase(GPelStorR.begin()); + GPelStorC.erase(GPelStorC.begin()); + + Tracker.setStorePos(GPelStorR2[0],GPelStorC2[0],2); + Locks[GPelStorR2[0]][GPelStorC2[0]].setColor(sf::Color::Transparent); + GPelStorR2.erase(GPelStorR2.begin()); + GPelStorC2.erase(GPelStorC2.begin()); + + Tracker.setStorePos(GPelStorR3[0],GPelStorC3[0],2); + Locks[GPelStorR3[0]][GPelStorC3[0]].setColor(sf::Color::Transparent); + GPelStorR3.erase(GPelStorR3.begin()); + GPelStorC3.erase(GPelStorC3.begin()); + break; + } + case BreakLock: + { + Locks[Tracker.getCoordinates(Pac).x][Tracker.getCoordinates(Pac).y].setColor(sf::Color::Transparent); + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); + break; + } + case Food: + { + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); + Score++; + } + } +} + +void Game::ChangeGhostTexture(IDENTITY ID) +{ + switch (ID) + { + case Blue: + { + blueGhost.setTexture(ghostBlue); + blueGhost.setScale(0.11,0.11); + break; + } + case Red: + { + redGhost.setTexture(ghostRed); + redGhost.setScale(0.11,0.11); + break; + } + case Orange: + { + orangeGhost.setTexture(ghostOrange); + orangeGhost.setScale(0.11,0.11); + break; + } + case Pink: + { + pinkGhost.setTexture(ghostPink); + pinkGhost.setScale(0.11,0.11); + break; + } + } +} + +void Game::ChangeGhostSuper() +{ + blueGhost.setScale(0.05, 0.11); + redGhost.setScale(0.05, 0.11); + orangeGhost.setScale(0.05, 0.11); + pinkGhost.setScale(0.05, 0.11); +} + +void Game::Update() +{ + player_.setPosition(Tracker.getCoordinates(Pac).y*Grid_size, Tracker.getCoordinates(Pac).x*Grid_size); + + blueGhost.setPosition(Tracker.getCoordinates(Blue).y*Grid_size, Tracker.getCoordinates(Blue).x*Grid_size); + orangeGhost.setPosition(Tracker.getCoordinates(Orange).y*Grid_size, Tracker.getCoordinates(Orange).x*Grid_size); + redGhost.setPosition(Tracker.getCoordinates(Red).y*Grid_size, Tracker.getCoordinates(Red).x*Grid_size); + pinkGhost.setPosition(Tracker.getCoordinates(Pink).y*Grid_size, Tracker.getCoordinates(Pink).x*Grid_size); + maze[Tracker.getCoordinates(Pac).x][Tracker.getCoordinates(Pac).y].setColor(sf::Color::Transparent); + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); +} + +void Game::PacDead(RenderWindow& window) +{ + window.clear() ; + + window.close(); + FinalDisplay(1); +} + +void Game::Controler(RenderWindow& window) +{ + while (window.isOpen()) + { + EventTracker(window); + + if(((clock()-SuperPacTimer)/(double)(CLOCKS_PER_SEC))>15) + { + SuperPac = false; + Tracker.TurnSuperMode(SuperPac); + player_.setScale(0.025, 0.023); + + blueGhost.setScale(0.11, 0.11); + redGhost.setScale(0.11, 0.11); + orangeGhost.setScale(0.11, 0.11); + pinkGhost.setScale(0.11, 0.11); + } + + IDENTITY GhostTex; + GhostTex = Eating.PacEatGhost(Tracker); + if( GhostTex == Pac) + { + isDead = Eating.GhostEatPac(Tracker) ; + + if(SuperPac) + { + ChangeGhostSuper(); + isDead = false; + } + if(isDead) + { + PacDead(window); + } + } + if(Score == totalFood) + { + window.close(); + FinalDisplay(2); + } + + if(Keyboard::isKeyPressed(Keyboard::Up)) + { + player_.setTexture(pacManUp); + ChangeTexture(Tracker.MovePac(Up)) ; + + BlueMove = Up; + OrangeMove=Down; + } + if(Keyboard::isKeyPressed(Keyboard::Down)) + { + player_.setTexture(pacManDown); + ChangeTexture(Tracker.MovePac(Down)) ; + + BlueMove = Down; + OrangeMove=Up; + } + if(Keyboard::isKeyPressed(Keyboard::Left)) + { + player_.setTexture(pacManLeft); + ChangeTexture(Tracker.MovePac(Left)) ; + + BlueMove = Left; + OrangeMove=Right; + } + if(Keyboard::isKeyPressed(Keyboard::Right)) + { + player_.setTexture(pacManRight); + ChangeTexture(Tracker.MovePac(Right)) ; + + BlueMove = Right; + OrangeMove=Left; + } + ChangeGhostTexture(GhostTex); + GhostMovement(); + WindowDraw_Display(window); + Sleep( Speed ); + } +} + +void Game::EventTracker(RenderWindow& window) +{ + // The next 6 lines of code detect if the window is closed + // And then shuts down the program + + //Process events + Event event; + + while (window.pollEvent(event)) + { + //close window: exit + if (event.type == Event::Closed) + { + // Someone closed the window + window.close(); + } + if(event.type == Event::KeyPressed && event.key.code == Keyboard::Escape) + { + window.close(); + } + } + // Clear everything from the last run of the while loop/screen + window.clear(); + + Update(); +} + +void Game::WindowDraw_Display(RenderWindow& window) +{ + // Draw our game scene here + for(int r = 0; r < rows; r++) + { + for(int c = 0; c +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace sf; +using namespace std; + +class Game +{ +public: + Game(string FileName); + Game(); + void StartGame(); + + int getTotalFood(); + int getScore(); + + void ChangesAfterPelletEaten(TEXTURE tir); + Sprite getSprite(IDENTITY ID); + Ghosts getGhostObj(); + Movement getMovementObj(); + Eater getEaterObj(); + + virtual ~Game(); + +private: + //Functions + void LoadTextures(); + void CreateMaze(); + void ResetPos(); + void setKey(int r, int c); + + void GhostMovement(); + void ChangeTexture(TEXTURE Tir); + void ChangeGhostTexture(IDENTITY ID); + void ChangeGhostSuper(); + + void Update(); + + void PacDead(RenderWindow& window); + void Controler(RenderWindow& window); + void EventTracker(RenderWindow& window); + void WindowDraw_Display(RenderWindow& window); + + void FinalDisplay(int Fin); + + //Objects + Movement Tracker; + Ghosts Enemy; + Eater Eating; + + const int rows = 22; + const int cols = 19; + const int Grid_size = 30 ; + const unsigned Window_WIDTH = 571 ; + const unsigned Window_HEIGHT = 660 ; + string FileName_; + + int unlockCount=0; + int TempCount = 0 ; + int LockCount=0; + int Speed = 85; // speed Pac moves + + int totalFood, Score; + int GhostCounter = 3; //Delays the movement of the ghosts + int startGhostModeTimer = 0, SuperPacTimer = 0; + DIRECTION BlueMove, OrangeMove; + bool GhostModeOn = false; + bool SuperPac = false; + bool isDead; + + //Need my Sprite 2D array + Sprite maze[22][19]; + //Need player + Sprite player_; + //Ghosts + Sprite blueGhost, redGhost, orangeGhost, pinkGhost; + //Locks we need + Sprite Locks[22][19]; + + vector GPelStorR; //stores where the locks are + vector GPelStorC; + + vector GPelStorR2; //stores where the locks are + vector GPelStorC2; + + vector GPelStorR3; //stores where the locks are + vector GPelStorC3; + + //textures + Texture pacManLeft, pacManRight, pacManUp, pacManDown, ghostBlue, ghostRed, ghostOrange, ghostPink, VulnerableGhost; + Texture brick, YellowPellets, SuperPel, PowerPel, Berry, PacKey, Locker; +}; + +#endif // GAME_H diff --git a/game-source-code/Ghosts.cpp b/game-source-code/Ghosts.cpp new file mode 100644 index 0000000..dfa36e9 --- /dev/null +++ b/game-source-code/Ghosts.cpp @@ -0,0 +1,595 @@ +#include "Ghosts.h" +#include "Movement.h" +#include "Datatypes.h" +#include +#include + +using namespace std ; + +Ghosts::Ghosts() +{ + //ctor +} +void Ghosts::BlueGhostMoves(Movement& Track, DIRECTION dir) +{ + TEXTURE NextTexture = Track.CheckWalLock(dir, Blue); + + if(dir == Left && NextTexture!= Wall && NextTexture!= Lock && (PrevBlue!='r' || IsNextToLock(Track, Blue))) + { + Track.MoveBlueGhost(Left); + PrevBlue = 'l'; + } + else if(dir == Right && NextTexture!= Wall && NextTexture!= Lock && (PrevBlue!='l' || IsNextToLock(Track,Blue))) + { + Track.MoveBlueGhost(Right); + PrevBlue = 'r'; + } + else if(dir == Up && NextTexture!= Wall && NextTexture!= Lock && (PrevBlue!='d' || IsNextToLock(Track,Blue))) + { + Track.MoveBlueGhost(Up); + PrevBlue = 'u'; + } + else if(dir == Down && NextTexture!= Wall && NextTexture!= Lock && (PrevBlue!='u' || IsNextToLock(Track,Blue))) + { + Track.MoveBlueGhost(Down); + PrevBlue = 'd'; + } + else + { + vector ValidMoves; + + if(Track.CheckWalLock(Left, Blue)!= Wall && Track.CheckWalLock(Left, Blue)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Right, Blue)!= Wall && Track.CheckWalLock(Right, Blue)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Up, Blue)!= Wall && Track.CheckWalLock(Up, Blue)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Down, Blue)!= Wall && Track.CheckWalLock(Down, Blue)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(ValidMoves[0]==true && (PrevBlue!='r' || IsNextToLock(Track,Blue)))//left + { + Track.MoveBlueGhost(Left); + PrevBlue = 'l'; + } + else if(ValidMoves[1]==true && (PrevBlue!='l' || IsNextToLock(Track,Blue)))//right + { + Track.MoveBlueGhost(Right); + PrevBlue = 'r'; + } + else if(ValidMoves[2]==true && (PrevBlue!='d'|| IsNextToLock(Track,Blue)))//up + { + Track.MoveBlueGhost(Up); + PrevBlue = 'u'; + } + else if(ValidMoves[3]==true && (PrevBlue!='u' || IsNextToLock(Track,Blue)))//down + { + Track.MoveBlueGhost(Down); + PrevBlue = 'd'; + } + ValidMoves.clear(); + } +} + +void Ghosts::RedGhostMoves(Movement& Track) +{ + vector ValidMoves; + + if(Track.CheckWalLock(Left, Red)!= Wall && Track.CheckWalLock(Left, Red)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Right, Red)!= Wall && Track.CheckWalLock(Right, Red)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Up, Red)!= Wall && Track.CheckWalLock(Up, Red)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Down, Red)!= Wall && Track.CheckWalLock(Down, Red)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + int X = Track.getCoordinates(Red).x - Track.getCoordinates(Pac).x; + int Y = Track.getCoordinates(Red).y - Track.getCoordinates(Pac).y; + + if(fabs(X) < fabs(Y)) //if x co-ordinate of pac is closer to x of RedGhost + { + if(X < 0 && Track.CheckWalLock(Down, Red) != Wall && Track.CheckWalLock(Down, Red) != Lock && (PrevRed!='u' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Down); + PrevRed = 'd'; + } + else if(X > 0 && Track.CheckWalLock(Up, Red) != Wall && Track.CheckWalLock(Up, Red) != Lock && (PrevRed!='d' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Up); + PrevRed = 'u'; + } + else if( Y < 0 && Track.CheckWalLock(Right, Red) != Wall && Track.CheckWalLock(Right, Red) != Lock && (PrevRed!='l' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Right); + PrevRed = 'r'; + } + else if( Y>0 && Track.CheckWalLock(Left, Red) != Wall && Track.CheckWalLock(Left, Red) != Lock && (PrevRed!='r' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Left); + PrevRed = 'l'; + } + else + { + if(ValidMoves[0]==true && (PrevRed!='r' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Left); + PrevRed = 'l'; + } + else if(ValidMoves[1]==true && (PrevRed!='l' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Right); + PrevRed = 'r'; + } + else if(ValidMoves[2]==true && (PrevRed!='d' || IsNextToLock(Track,Red)))//up + { + Track.MoveRedGhost(Up); + PrevRed = 'u'; + } + else if(ValidMoves[3]==true && (PrevRed!='u' || IsNextToLock(Track,Red)))//down + { + Track.MoveRedGhost(Down); + PrevRed = 'd'; + } + } + } + else //if y co-ordinate of pac is closer to y of RedGhost + { + if(Y < 0 && Track.CheckWalLock(Right, Red) != Wall && Track.CheckWalLock(Right, Red) != Lock && (PrevRed!='l' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Right); + PrevRed = 'r'; + } + else if(Y>0 && Track.CheckWalLock(Left, Red) != Wall && Track.CheckWalLock(Left, Red) != Lock && (PrevRed!='r' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Left); + PrevRed = 'l'; + } + else if( X<0 && Track.CheckWalLock(Down, Red) != Wall && Track.CheckWalLock(Down, Red) != Lock && (PrevRed!='u' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Down); + PrevRed = 'd'; + } + else if( X>0 && Track.CheckWalLock(Up, Red) != Wall && Track.CheckWalLock(Up, Red) != Lock && (PrevRed!='d' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Up); + PrevRed = 'u'; + } + else + { + if(ValidMoves[0]==true && (PrevRed!='r' || IsNextToLock(Track,Red)))//left + { + Track.MoveRedGhost(Left); + PrevRed = 'l'; + } + else if(ValidMoves[1]==true && (PrevRed!='l' || IsNextToLock(Track,Red)))//right + { + Track.MoveRedGhost(Right); + PrevRed = 'r'; + } + else if(ValidMoves[2]==true && (PrevRed!='d' || IsNextToLock(Track,Red)))//up + { + Track.MoveRedGhost(Up); + PrevRed = 'u'; + } + else if(ValidMoves[3]==true && (PrevRed!='u' || IsNextToLock(Track,Red)))//down + { + Track.MoveRedGhost(Down); + PrevRed = 'd'; + } + } + } + ValidMoves.clear(); +} + +void Ghosts::OrangeGhostMoves(Movement& Track, DIRECTION dir) +{ + TEXTURE NextTexture = Track.CheckWalLock(dir, Orange); + + if(dir == Left && NextTexture!= Wall && NextTexture!= Lock && (PrevOrange!='r' || IsNextToLock(Track,Orange))) + { + Track.MoveOrangeGhost(Left); + PrevOrange = 'l'; + } + else if(dir == Right && NextTexture!= Wall && NextTexture!= Lock && (PrevOrange!='l' || IsNextToLock(Track,Orange))) + { + Track.MoveOrangeGhost(Right); + PrevOrange = 'r'; + } + else if(dir == Up && NextTexture!= Wall && NextTexture!= Lock && (PrevOrange!='d' || IsNextToLock(Track,Orange))) + { + Track.MoveOrangeGhost(Up); + PrevOrange = 'u'; + } + else if(dir == Down && NextTexture!= Wall && NextTexture!= Lock && (PrevOrange!='u' || IsNextToLock(Track,Orange))) + { + Track.MoveOrangeGhost(Down); + PrevOrange = 'd'; + } + else + { + vector ValidMoves; + + if(Track.CheckWalLock(Left, Orange)!= Wall && Track.CheckWalLock(Left, Orange)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Right, Orange)!= Wall && Track.CheckWalLock(Right, Orange)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Up, Orange)!= Wall && Track.CheckWalLock(Up, Orange)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Down, Orange)!= Wall && Track.CheckWalLock(Down, Orange)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(ValidMoves[0]==true && (PrevOrange!='r' || IsNextToLock(Track,Orange)))//left + { + Track.MoveOrangeGhost(Left); + PrevOrange = 'l'; + } + else if(ValidMoves[1]==true && (PrevOrange!='l' || IsNextToLock(Track,Orange)))//right + { + Track.MoveOrangeGhost(Right); + PrevOrange = 'r'; + } + else if(ValidMoves[2]==true && (PrevOrange!='d'|| IsNextToLock(Track,Orange)))//up + { + Track.MoveOrangeGhost(Up); + PrevOrange = 'u'; + } + else if(ValidMoves[3]==true && (PrevOrange!='u' || IsNextToLock(Track,Orange)))//down + { + Track.MoveOrangeGhost(Down); + PrevOrange = 'd'; + } + ValidMoves.clear(); + } +} + +void Ghosts::PinkGhostMoves(Movement& Track) +{ + vector ValidMoves; + + if(Track.CheckWalLock(Left, Pink)!= Wall && Track.CheckWalLock(Left, Pink)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Right, Pink)!= Wall && Track.CheckWalLock(Right, Pink)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Up, Pink)!= Wall && Track.CheckWalLock(Up, Pink)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Down, Pink)!= Wall && Track.CheckWalLock(Down, Pink)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + int X = Track.getCoordinates(Pink).x - Track.getCoordinates(Pac).x; + int Y = Track.getCoordinates(Pink).y - Track.getCoordinates(Pac).y; + + if(fabs(X) > fabs(Y)) //if x co-ordinate of pac is further to x of PinkGhost + { + if(X < 0 && Track.CheckWalLock(Down, Pink) != Wall && Track.CheckWalLock(Down, Pink) != Lock && (PrevPink!='u' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Down); + PrevPink = 'd'; + } + else if(X > 0 && Track.CheckWalLock(Up, Pink) != Wall && Track.CheckWalLock(Up, Pink) != Lock && (PrevPink!='d' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Up); + PrevPink = 'u'; + } + else if( Y < 0 && Track.CheckWalLock(Right, Pink) != Wall && Track.CheckWalLock(Right, Pink) != Lock && (PrevPink!='l' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Right); + PrevPink = 'r'; + } + else if( Y>0 && Track.CheckWalLock(Left, Pink) != Wall && Track.CheckWalLock(Left, Pink) != Lock && (PrevPink!='r' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Left); + PrevPink = 'l'; + } + else + { + if(ValidMoves[0]==true && (PrevPink!='r' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Left); + PrevPink = 'l'; + } + else if(ValidMoves[1]==true && (PrevPink!='l' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Right); + PrevPink = 'r'; + } + else if(ValidMoves[2]==true && (PrevPink!='d' || IsNextToLock(Track,Pink)))//up + { + Track.MovePinkGhost(Up); + PrevPink = 'u'; + } + else if(ValidMoves[3]==true && (PrevPink!='u' || IsNextToLock(Track,Pink)))//down + { + Track.MovePinkGhost(Down); + PrevPink = 'd'; + } + } + } + else //if y co-ordinate of pac is Further to y of PinkGhost + { + if(Y < 0 && Track.CheckWalLock(Right, Pink) != Wall && Track.CheckWalLock(Right, Pink) != Lock && (PrevPink!='l' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Right); + PrevPink = 'r'; + } + else if(Y>0 && Track.CheckWalLock(Left, Pink) != Wall && Track.CheckWalLock(Left, Pink) != Lock && (PrevPink!='r' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Left); + PrevPink = 'l'; + } + else if( X<0 && Track.CheckWalLock(Down, Pink) != Wall && Track.CheckWalLock(Down, Pink) != Lock && (PrevPink!='u' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Down); + PrevPink = 'd'; + } + else if( X>0 && Track.CheckWalLock(Up, Pink) != Wall && Track.CheckWalLock(Up, Pink) != Lock && (PrevPink!='d' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Up); + PrevPink = 'u'; + } + else + { + if(ValidMoves[0]==true && (PrevPink!='r' || IsNextToLock(Track,Pink)))//left + { + Track.MovePinkGhost(Left); + PrevPink = 'l'; + } + else if(ValidMoves[1]==true && (PrevPink!='l' || IsNextToLock(Track,Pink)))//right + { + Track.MovePinkGhost(Right); + PrevPink = 'r'; + } + else if(ValidMoves[2]==true && (PrevPink!='d' || IsNextToLock(Track,Pink)))//up + { + Track.MovePinkGhost(Up); + PrevPink = 'u'; + } + else if(ValidMoves[3]==true && (PrevPink!='u' || IsNextToLock(Track,Pink)))//down + { + Track.MovePinkGhost(Down); + PrevPink = 'd'; + } + } + } + ValidMoves.clear(); +} + +void Ghosts::GhostMode(Movement& Track, IDENTITY ID) +{ + char PrevDir = ' '; + DIRECTION toMove; + switch(ID) + { + case Blue: + { + PrevDir = PrevBlue; + break; + } + case Red: + { + PrevDir = PrevRed; + break; + } + case Orange: + { + PrevDir = PrevOrange; + break; + } + case Pink: + { + PrevDir = PrevPink; + break; + } + case Pac: { + + break; + } + } + vector ValidMoves; + + if(Track.CheckWalLock(Left, ID)!= Wall && Track.CheckWalLock(Left, ID)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Right, ID)!= Wall && Track.CheckWalLock(Right, ID)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Up, ID)!= Wall && Track.CheckWalLock(Up, ID)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Down, ID)!= Wall && Track.CheckWalLock(Down, ID)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + int X = Track.getCoordinates(ID).x - Track.getCoordinates(Pac).x; + int Y = Track.getCoordinates(ID).y - Track.getCoordinates(Pac).y; + + if(fabs(X) < fabs(Y)) //if x co-ordinate of pac is closer to x of IDGhost + { + if(X > 0 && Track.CheckWalLock(Down, ID) != Wall && Track.CheckWalLock(Down, ID) != Lock && (PrevDir!='u' || IsNextToLock(Track,ID))) + { + toMove = Down; + PrevDir = 'd'; + } + else if(X < 0 && Track.CheckWalLock(Up, ID) != Wall && Track.CheckWalLock(Up, ID) != Lock && (PrevDir!='d' || IsNextToLock(Track,ID))) + { + toMove = Up; + PrevDir = 'u'; + } + else if( Y > 0 && Track.CheckWalLock(Right, ID) != Wall && Track.CheckWalLock(Right, ID) != Lock && (PrevDir!='l' || IsNextToLock(Track,ID))) + { + toMove = Right; + PrevDir = 'r'; + } + else if( Y < 0 && Track.CheckWalLock(Left, ID) != Wall && Track.CheckWalLock(Left, ID) != Lock && (PrevDir!='r' || IsNextToLock(Track,ID))) + { + toMove = Left; + PrevDir = 'l'; + } + else + { + if(ValidMoves[0]==true && (PrevDir!='r' || IsNextToLock(Track,ID))) + { + toMove = Left; + PrevDir = 'l'; + } + else if(ValidMoves[1]==true && (PrevDir!='l' || IsNextToLock(Track,ID))) + { + toMove = Right; + PrevDir = 'r'; + } + else if(ValidMoves[2]==true && (PrevDir!='d' || IsNextToLock(Track,ID)))//up + { + toMove = Up; + PrevDir = 'u'; + } + else if(ValidMoves[3]==true && (PrevDir!='u' || IsNextToLock(Track,ID)))//down + { + toMove = Down; + PrevDir = 'd'; + } + } + } + else //if y co-ordinate of pac is closer to y of IDGhost + { + if(Y > 0 && Track.CheckWalLock(Right, ID) != Wall && Track.CheckWalLock(Right, ID) != Lock && (PrevDir!='l' || IsNextToLock(Track,ID))) + { + toMove = Right; + PrevDir = 'r'; + } + else if(Y < 0 && Track.CheckWalLock(Left, ID) != Wall && Track.CheckWalLock(Left, ID) != Lock && (PrevDir!='r' || IsNextToLock(Track,ID))) + { + toMove = Left; + PrevDir = 'l'; + } + else if( X > 0 && Track.CheckWalLock(Down, ID) != Wall && Track.CheckWalLock(Down, ID) != Lock && (PrevDir!='u' || IsNextToLock(Track,ID))) + { + toMove = Down; + PrevDir = 'd'; + } + else if( X < 0 && Track.CheckWalLock(Up, ID) != Wall && Track.CheckWalLock(Up, ID) != Lock && (PrevDir!='d' || IsNextToLock(Track,ID))) + { + toMove = Up; + PrevDir = 'u'; + } + else + { + if(ValidMoves[0]==true && (PrevDir!='r' || IsNextToLock(Track,ID)))//left + { + toMove = Left; + PrevDir = 'l'; + } + else if(ValidMoves[1]==true && (PrevDir!='l' || IsNextToLock(Track,ID)))//right + { + toMove = Right; + PrevDir = 'r'; + } + else if(ValidMoves[2]==true && (PrevDir!='d' || IsNextToLock(Track,ID)))//up + { + toMove = Up; + PrevDir = 'u'; + } + else if(ValidMoves[3]==true && (PrevDir!='u' || IsNextToLock(Track,ID)))//down + { + toMove = Down; + PrevDir = 'd'; + } + } + } + ValidMoves.clear(); + switch(ID) + { + case Blue: + { + PrevBlue = PrevDir; + Track.MoveBlueGhost(toMove); + break; + } + case Red: + { + PrevRed = PrevDir; + Track.MoveRedGhost(toMove); + break; + } + case Orange: + { + PrevOrange = PrevDir; + Track.MoveOrangeGhost(toMove); + break; + } + case Pink: + { + PrevPink = PrevDir; + Track.MovePinkGhost(toMove); + break; + } + case Pac:{ + break; + } + } +} + +bool Ghosts::IsNextToLock(Movement& Track, IDENTITY ID) +{ + if(Track.CheckWalLock(Left, ID) == Lock || Track.CheckWalLock(Right, ID) == Lock || Track.CheckWalLock(Up, ID) == Lock || Track.CheckWalLock(Down, ID) == Lock) + { + return true; + } + else + return false; +} + +Ghosts::~Ghosts() +{ + //dtor +} diff --git a/game-source-code/Ghosts.h b/game-source-code/Ghosts.h new file mode 100644 index 0000000..f68fdc6 --- /dev/null +++ b/game-source-code/Ghosts.h @@ -0,0 +1,28 @@ +#ifndef GHOSTS_H +#define GHOSTS_H + +#include "Movement.h" +#include "Datatypes.h" + + +class Ghosts +{ + public: + Ghosts(); + ~Ghosts(); + + void BlueGhostMoves(Movement& Track, DIRECTION dir); + void RedGhostMoves(Movement& Track); + void OrangeGhostMoves(Movement& Track, DIRECTION dir); + void PinkGhostMoves(Movement& Track); + void GhostMode(Movement& Track, IDENTITY ID); + bool IsNextToLock(Movement& Track, IDENTITY ID); + + private: + char PrevBlue = ' '; + char PrevRed = ' '; + char PrevOrange = ' '; + char PrevPink = ' '; +}; + +#endif // GHOSTS_H diff --git a/game-source-code/Movement.cpp b/game-source-code/Movement.cpp new file mode 100644 index 0000000..57a34b8 --- /dev/null +++ b/game-source-code/Movement.cpp @@ -0,0 +1,325 @@ +#include "Movement.h" +#include + +using namespace std; + +Movement::Movement() +{ + //ctor +} + +void Movement::MoveBlueGhost(DIRECTION dir) +{ + Coordinates BlueMan; + BlueMan = getCoordinates(Blue); + switch (dir) + { + case Left: + { + setSpritePos(BlueMan.x, BlueMan.y-1, Blue); + break; + } + case Right: + { + setSpritePos(BlueMan.x, BlueMan.y+1, Blue); + break; + } + case Up: + { + setSpritePos(BlueMan.x-1, BlueMan.y, Blue); + break; + } + case Down: + { + setSpritePos(BlueMan.x+1, BlueMan.y, Blue); + break; + } + } +} + +void Movement::MoveRedGhost(DIRECTION dir) +{ + Coordinates RedMan; + RedMan = getCoordinates(Red); + switch (dir) + { + case Left: + { + setSpritePos(RedMan.x, RedMan.y-1, Red); + break; + } + case Right: + { + setSpritePos(RedMan.x, RedMan.y+1, Red); + break; + } + case Up: + { + setSpritePos(RedMan.x-1, RedMan.y, Red); + break; + } + case Down: + { + setSpritePos(RedMan.x+1, RedMan.y, Red); + break; + } + } +} + +void Movement::MoveOrangeGhost(DIRECTION dir) +{ + Coordinates OrangeMan; + OrangeMan = getCoordinates(Orange); + switch (dir) + { + case Left: + { + setSpritePos(OrangeMan.x, OrangeMan.y-1, Orange); + break; + } + case Right: + { + setSpritePos(OrangeMan.x, OrangeMan.y+1, Orange); + break; + } + case Up: + { + setSpritePos(OrangeMan.x-1, OrangeMan.y, Orange); + break; + } + case Down: + { + setSpritePos(OrangeMan.x+1, OrangeMan.y, Orange); + break; + } + } +} + +void Movement::MovePinkGhost(DIRECTION dir) +{ + + Coordinates PinkMan; + PinkMan = getCoordinates(Pink); + switch (dir) + { + case Left: + { + setSpritePos(PinkMan.x, PinkMan.y-1, Pink); + + break; + } + case Right: + { + setSpritePos(PinkMan.x, PinkMan.y+1, Pink); + + break; + } + case Up: + { + setSpritePos(PinkMan.x-1, PinkMan.y, Pink); + + break; + } + case Down: + { + setSpritePos(PinkMan.x+1, PinkMan.y, Pink); + + break; + } + } +} + +void Movement::TurnGhostMode(bool Mode) +{ + GhostBluemode = Mode; + GhostOrangeMode = Mode; + GhostRedMode = Mode ; + GhostPinkMode = Mode; +} + +void Movement::TurnGhostModeOff(IDENTITY ID, bool Mode) +{ + switch (ID) + { + case Blue: + { + GhostBluemode = Mode; + break; + } + case Pink: + { + GhostPinkMode = Mode; + break; + } + case Red: + { + GhostRedMode = Mode; + break; + } + case Orange: + { + GhostOrangeMode = Mode; + break; + } + } +} + +void Movement::TurnSuperMode(bool Mode) +{ + SuperPac = Mode; +} + +bool Movement::getGhostMode(IDENTITY ID) +{ + switch(ID) + { + case Blue: + { + return GhostBluemode; + } + case Red: + { + return GhostRedMode; + } + case Orange: + { + return GhostOrangeMode; + } + case Pink: + { + return GhostPinkMode; + } + } +} + +Coordinates Movement::GetNextCoord(DIRECTION dir, IDENTITY ID) +{ + Coordinates Character ; + Character = getCoordinates(ID); + + switch (dir) + { + case Left: + { + Character.y = Character.y-1 ; + break; + } + case Right: + { + Character.y = Character.y+1; + break; + } + case Up: + { + Character.x = Character.x-1; + break; + } + case Down: + { + Character.x = Character.x+1 ; + break; + } + } + return Character; +} + +TEXTURE Movement::CheckWalLock(DIRECTION dir, IDENTITY ID) +{ + Coordinates Character ; + Character = GetNextCoord(dir,ID) ; + + if (getStorePos(Character.x,Character.y)== 1) + { + return Wall ; + } + else if( getStorePos(Character.x,Character.y)== 7) + { + return Lock; + } + else if(getStorePos(Character.x,Character.y)== 5) + { + return Key; + } + else if(getStorePos(Character.x,Character.y)== 6) + { + return Power; + } + else if(getStorePos(Character.x,Character.y)== 10) + { + return Super; + } + else if(getStorePos(Character.x,Character.y)== 3 || getStorePos(Character.x,Character.y)== 0 || getStorePos(Character.x,Character.y)== 4) + { + return Food; + } + return blank; +} + +TEXTURE Movement::MovePac(DIRECTION dir) +{ + Coordinates Pacman; + Pacman = getCoordinates(Pac); + + TEXTURE ChangeTex; + ChangeTex = CheckWalLock(dir,Pac); + + if(ChangeTex == Lock && SuperPac == true) + { + switch (dir) + { + case Left: + { + setSpritePos(Pacman.x, Pacman.y-1, Pac); + break; + } + case Right: + { + setSpritePos(Pacman.x, Pacman.y+1, Pac); + break; + } + case Up: + { + setSpritePos(Pacman.x-1, Pacman.y, Pac); + break; + } + case Down: + { + setSpritePos(Pacman.x+1, Pacman.y, Pac); + break; + } + } + return BreakLock; + } + else if(ChangeTex!= Wall && ChangeTex!= Lock) + { + switch (dir) + { + case Left: + { + setSpritePos(Pacman.x, Pacman.y-1, Pac); + break; + } + case Right: + { + setSpritePos(Pacman.x, Pacman.y+1, Pac); + break; + } + case Up: + { + setSpritePos(Pacman.x-1, Pacman.y, Pac); + break; + } + case Down: + { + setSpritePos(Pacman.x+1, Pacman.y, Pac); + break; + } + } + } + return ChangeTex; +} + +Movement::~Movement() +{ + //dtor +} diff --git a/game-source-code/Movement.h b/game-source-code/Movement.h new file mode 100644 index 0000000..6e5c2c7 --- /dev/null +++ b/game-source-code/Movement.h @@ -0,0 +1,42 @@ +#ifndef MOVEMENT_H +#define MOVEMENT_H + +#include "PositionHandler.h" + + +class Movement: public PositionHandler +{ +public: + Movement(); + ~Movement(); + + void MoveBlueGhost(DIRECTION dir); + void MoveRedGhost(DIRECTION dir); + void MoveOrangeGhost(DIRECTION dir); + void MovePinkGhost(DIRECTION dir); + + void TurnGhostMode(bool Mode); + void TurnGhostModeOff(IDENTITY ID,bool Mode); + void TurnSuperMode(bool Mode); + + bool getGhostMode(IDENTITY ID); + + Coordinates GetNextCoord(DIRECTION dir, IDENTITY ID); + TEXTURE CheckWalLock(DIRECTION dir, IDENTITY ID); + TEXTURE MovePac(DIRECTION dir); + +private: + bool GhostBluemode = false; + bool GhostRedMode = false; + bool GhostPinkMode = false; + bool GhostOrangeMode = false; + bool SuperPac = false; + + + + + + +}; + +#endif // MOVEMENT_H diff --git a/game-source-code/PositionHandler.cpp b/game-source-code/PositionHandler.cpp new file mode 100644 index 0000000..6db2e12 --- /dev/null +++ b/game-source-code/PositionHandler.cpp @@ -0,0 +1,176 @@ +#include "PositionHandler.h" + +#include +#include + +using namespace std; + +PositionHandler::PositionHandler() +{ + +} + +void PositionHandler::Init_StorePos(string Filename_) +{ + ifstream inFile; + inFile.open(Filename_); + + for(int r = 0; r < 22; r++) + { + for(int c = 0; c < 19; c++) + { + inFile >> StorePos[r][c]; + } + } +} + +void PositionHandler::setStorePos(int r, int c, int ChangeNum) +{ + StorePos[r][c]= ChangeNum; +} + +void PositionHandler::setSpritePos(int x, int y, IDENTITY ID) +{ + switch(ID) + { + case Pac: + { + PacPos.x = x; + PacPos.y = y; + break; + } + case Blue: + { + BluePos.x = x; + BluePos.y = y; + break; + } + case Red: + { + RedPos.x = x; + RedPos.y = y; + break; + } + case Orange: + { + OrangePos.x = x; + OrangePos.y = y; + break; + } + case Pink: + { + PinkPos.x = x; + PinkPos.y = y; + break; + } + } +} + +void PositionHandler::SetInit(IDENTITY ID, int r, int c) +{ + switch(ID) + { + case Blue: + { + setSpritePos(r,c,Blue); + BlueInit.x = r ; + BlueInit.y = c ; + break; + } + case Red: + { + setSpritePos(r,c,Red); + RedInit.x = r ; + RedInit.y = c ; + break; + } + case Pink: + { + setSpritePos(r,c,Pink); + PinkInit.x = r ; + PinkInit.y = c ; + break; + } + case Orange: + { + setSpritePos(r,c,Orange); + OrangeInit.x = r ; + OrangeInit.y = c ; + break; + } + case Pac:{ + setSpritePos(r,c,Pac); + PacPos.x = r; + PacPos.y = c ; + } + } +} + +int PositionHandler::getStorePos(int r, int c) +{ + return StorePos[r][c]; +} + +Coordinates PositionHandler::getCoordinates(IDENTITY ID) +{ + switch(ID) + { + case Pac: + { + return PacPos; + } + case Blue: + { + return BluePos; + } + case Red: + { + return RedPos; + } + case Orange: + { + return OrangePos; + } + case Pink: + { + return PinkPos; + } + } +} + +Coordinates PositionHandler::GetInit(IDENTITY ID) +{ + switch(ID) + { + case Blue: + { + return BlueInit; + } + case Red: + { + return RedInit; + break; + } + case Pink: + { + return PinkInit; + break; + } + case Orange: + { + return OrangeInit; + break; + } + + case Pac : + { + return PacInit; + break; + } + } +} + +PositionHandler::~PositionHandler() +{ + //dtor +} diff --git a/game-source-code/PositionHandler.h b/game-source-code/PositionHandler.h new file mode 100644 index 0000000..f28fe5f --- /dev/null +++ b/game-source-code/PositionHandler.h @@ -0,0 +1,38 @@ +#ifndef POSITIONHANDLER_H +#define POSITIONHANDLER_H +#include "Datatypes.h" + +#include + +using namespace std; + +class PositionHandler +{ + public: + PositionHandler(); + ~PositionHandler(); + void Init_StorePos(string Filename_); + void setStorePos(int r, int c, int ChangeNum); + void setSpritePos(int x, int y, IDENTITY ID); + void SetInit(IDENTITY ID, int r, int c); + int getStorePos(int r, int c); + Coordinates getCoordinates(IDENTITY ID); + Coordinates GetInit(IDENTITY ID); + + private: + int StorePos[22][19]; + + Coordinates PacPos; + Coordinates BluePos; + Coordinates RedPos; + Coordinates OrangePos; + Coordinates PinkPos; + + Coordinates BlueInit; + Coordinates PinkInit; + Coordinates RedInit; + Coordinates OrangeInit; + Coordinates PacInit; +}; + +#endif // POSITIONHANDLER_H diff --git a/game-source-code/eater.cpp b/game-source-code/eater.cpp new file mode 100644 index 0000000..2f161be --- /dev/null +++ b/game-source-code/eater.cpp @@ -0,0 +1,68 @@ +#include "eater.h" +#include "Datatypes.h" + +Eater::Eater() +{ + //ctor +} + +bool Eater::GhostEatPac(Movement& Track) +{ + Coordinates PacMan; + PacMan = Track.getCoordinates(Pac); + + if(PacMan.x == Track.getCoordinates(Blue).x && PacMan.y == Track.getCoordinates(Blue).y && Track.getGhostMode(Blue) == false) + { + return true; + } + if(PacMan.x == Track.getCoordinates(Red).x && PacMan.y == Track.getCoordinates(Red).y && Track.getGhostMode(Red) == false) + { + return true; + } + if(PacMan.x == Track.getCoordinates(Pink).x && PacMan.y == Track.getCoordinates(Pink).y && Track.getGhostMode(Pink) == false) + { + return true; + } + if(PacMan.x == Track.getCoordinates(Orange).x && PacMan.y == Track.getCoordinates(Orange).y && Track.getGhostMode(Orange) == false) + { + return true; + } + return false; +} + +IDENTITY Eater::PacEatGhost(Movement& Track) +{ + Coordinates PacMan; + PacMan = Track.getCoordinates(Pac); + + if(PacMan.x == Track.getCoordinates(Blue).x && PacMan.y == Track.getCoordinates(Blue).y && Track.getGhostMode(Blue) == true) + { + Track.setSpritePos(Track.GetInit(Blue).x,Track.GetInit(Blue).y,Blue); + Track.TurnGhostModeOff(Blue,false); + return Blue; + } + if(PacMan.x == Track.getCoordinates(Red).x && PacMan.y == Track.getCoordinates(Red).y && Track.getGhostMode(Red) == true) + { + Track.setSpritePos(Track.GetInit(Red).x,Track.GetInit(Red).y,Red); + Track.TurnGhostModeOff(Red,false); + return Red; + } + if(PacMan.x == Track.getCoordinates(Pink).x && PacMan.y == Track.getCoordinates(Pink).y && Track.getGhostMode(Pink) == true) + { + Track.setSpritePos(Track.GetInit(Pink).x,Track.GetInit(Pink).y,Pink); + Track.TurnGhostModeOff(Pink,false); + return Pink; + } + if(PacMan.x == Track.getCoordinates(Orange).x && PacMan.y == Track.getCoordinates(Orange).y && Track.getGhostMode(Orange) == true) + { + Track.setSpritePos(Track.GetInit(Orange).x,Track.GetInit(Orange).y,Orange); + Track.TurnGhostModeOff(Orange,false); + return Orange; + } + return Pac; +} + +Eater::~Eater() +{ + //dtor +} diff --git a/game-source-code/eater.h b/game-source-code/eater.h new file mode 100644 index 0000000..3399eb3 --- /dev/null +++ b/game-source-code/eater.h @@ -0,0 +1,16 @@ +#ifndef EATER_H +#define EATER_H +#include "Movement.h" +#include "Datatypes.h" + +class Eater +{ + public: + Eater(); + ~Eater(); + + bool GhostEatPac(Movement& Track); + IDENTITY PacEatGhost(Movement& Track); +}; + +#endif // EATER_H diff --git a/game-source-code/main.cpp b/game-source-code/main.cpp new file mode 100644 index 0000000..eb4d203 --- /dev/null +++ b/game-source-code/main.cpp @@ -0,0 +1,88 @@ +#include "Game.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace sf ; +int main() { + + const unsigned Window_WIDTH = 800 ; + const unsigned Window_HEIGHT = 800 ; + + + // Make a window that is 571 by 660 pixels + Splash: RenderWindow window(VideoMode(Window_WIDTH, Window_HEIGHT), "Pac Man - A Parody"); // a window to sort Admin + //go to pointer + + Text TheMessage; + + + Font PacM ; + PacM.loadFromFile("Resoruces/Fonts/PAC-FONT.TTF"); + + TheMessage.setFont(PacM); + + TheMessage.setCharacterSize(35); + + Texture welScreen ; + Sprite welcome ; + + + welScreen.loadFromFile("Resoruces/Fonts/PacMan.png") ; + welcome.setTexture(welScreen); + welcome.scale(2,2); + + + + + while (window.isOpen()) + { + + sf::Event event; + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + + window.close(); + + if ( event.type == sf::Event::KeyPressed) { + + if (event.key.code == sf::Keyboard:: A){ + + window.close(); + Game player("Resoruces/Maze/m1.txt"); + player.StartGame(); + + goto Splash; + + + } + + } + } + + + + + window.clear(); + + + window.draw(welcome); + window.draw(TheMessage); + + + // Show everything we just drew + window.display(); + } + +//Game game1("C:/Users/Home/Desktop/Final Project/1318803 - 1911499/executables/Resoruces/Maze/m1.txt"); + + + + return 0; +} diff --git a/test-source-code/Datatypes.h b/test-source-code/Datatypes.h new file mode 100644 index 0000000..62973b3 --- /dev/null +++ b/test-source-code/Datatypes.h @@ -0,0 +1,14 @@ +#ifndef DATATYPES_H +#define DATATYPES_H + +enum IDENTITY{Pac, Blue, Red, Orange, Pink}; +enum DIRECTION{Left, Right, Up, Down}; +enum TEXTURE {Wall, Key, Power, Super, blank, Lock, BreakLock, Food}; +struct Coordinates +{ + int x; + int y; +}; + + +#endif // DATATYPES_H diff --git a/test-source-code/Game.cpp b/test-source-code/Game.cpp new file mode 100644 index 0000000..dbd4982 --- /dev/null +++ b/test-source-code/Game.cpp @@ -0,0 +1,672 @@ +#include "Game.h" +#include "PositionHandler.h" +#include "Datatypes.h" +#include "Movement.h" +#include "Ghosts.h" + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace sf ; +using namespace std ; +/** @brief Game + * + * @todo: document this function + */ +Game::Game(string FileName) // store everything in the array +{ + FileName_ = FileName ; + LoadTextures(); + totalFood = 0 ; + Score = 0 ; + Tracker.Init_StorePos(FileName); + CreateMaze(); + ResetPos(); +} + +Game::Game() +{ + FileName_ = "Resoruces/Maze/m1.txt" ; + LoadTextures(); + totalFood = 0 ; + Score = 0 ; + Tracker.Init_StorePos(FileName_); + CreateMaze(); + ResetPos(); +} + +void Game::StartGame() +{ + RenderWindow window(VideoMode(Window_WIDTH, Window_HEIGHT), "PacMan Maze"); + Controler(window); +} + +void Game::LoadTextures() +{ + brick.loadFromFile("Resoruces/backgrounds/BlueBrickwall.png"); + pacManRight.loadFromFile("Resoruces/Pac and Crew/PacmanRight.PNG"); + pacManLeft.loadFromFile("Resoruces/Pac and Crew/PacmanLeft.PNG"); + pacManUp.loadFromFile("Resoruces/Pac and Crew/PacmanUP.PNG"); + pacManDown.loadFromFile("Resoruces/Pac and Crew/PacmanDown.PNG"); + ghostBlue.loadFromFile("Resoruces/Pac and Crew/Bluey.PNG"); + ghostRed.loadFromFile("Resoruces/Pac and Crew/Reddy.PNG"); + ghostOrange.loadFromFile("Resoruces/Pac and Crew/Orangey.PNG"); + ghostPink.loadFromFile("Resoruces/Pac and Crew/Pinky.PNG"); + VulnerableGhost.loadFromFile("Resoruces/Pac and Crew/GhostMode.PNG"); + PacKey.loadFromFile("Resoruces/backgrounds/Key.png"); + Locker.loadFromFile("Resoruces/backgrounds/lock.png"); + Berry.loadFromFile("Resoruces/backgrounds/Fruit.png"); + YellowPellets.loadFromFile("Resoruces/backgrounds/YellowSpot.png"); + SuperPel.loadFromFile("Resoruces/backgrounds/Super.png"); + PowerPel.loadFromFile("Resoruces/backgrounds/Power.png"); +} + +void Game::CreateMaze() +{ + Score = 0; + + for(int r = 0; r < rows; r++) + { + for(int c = 0; c < cols; c++) + { + if(Tracker.getStorePos(r, c) == 1) + { + maze[r][c].setTexture(brick); + maze[r][c].setScale(0.35, 0.35); + maze[r][c].setPosition(c*Grid_size,r*Grid_size); + } + } + } + blueGhost.setTexture(ghostBlue); + blueGhost.setScale(0.11, 0.11); + + redGhost.setTexture(ghostRed); + redGhost.setScale(0.11, 0.11); + + orangeGhost.setTexture(ghostOrange); + orangeGhost.setScale(0.11, 0.11); + + pinkGhost.setTexture(ghostPink); + pinkGhost.setScale(0.11, 0.11); + //initialize player + player_.setTexture(pacManRight); + player_.setScale(0.025, 0.023); +} + +void Game::ResetPos() +{ + GPelStorC.clear(); + GPelStorR.clear(); + + int counter=0, posX=0, posY=0; + + for(int r = 0; r < rows; r++) + { + for(int c = 0; c < cols; c++) + { + if(Tracker.getStorePos(r, c)== 4) + { + counter++; + + maze[r][c].setTexture(YellowPellets); + maze[r][c].setScale(0.25, 0.25); + maze[r][c].setPosition(c*Grid_size+5,r*Grid_size+5); + totalFood++; + + if(counter == 1) + { + Tracker.SetInit(Blue,r,c); + } + else if(counter == 2) + { + Tracker.SetInit(Red,r,c); + } + else if(counter == 3) + { + Tracker.SetInit(Orange,r,c); + } + else if(counter == 4) + { + Tracker.SetInit(Pink,r,c); + } + } + if(Tracker.getStorePos(r, c) == 9) // player + { + posX = r; + posY = c; + } + else if(Tracker.getStorePos(r, c) == 6) // Red Pellet + { + maze[r][c].setTexture(PowerPel); + maze[r][c].setScale(0.35, 0.35); + maze[r][c].setPosition(c*Grid_size,r*Grid_size); + } + else if(Tracker.getStorePos(r, c) == 3) + { + maze[r][c].setTexture(Berry); + maze[r][c].setScale(0.35, 0.35); + maze[r][c].setPosition(c*Grid_size,r*Grid_size); + totalFood++; + } + //Locks we need + else if(Tracker.getStorePos(r, c) == 7) + { + Locks[r][c].setTexture(Locker); + Locks[r][c].setPosition(c*Grid_size,r*Grid_size); + Locks[r][c].setScale(0.35, 0.35); + + TempCount++; + if(TempCount % 2 != 0) + { + LockCount ++; + } + if(LockCount == 4) + { + LockCount = 1; + } + setKey(r,c); + } + else if (Tracker.getStorePos(r, c) == 10) + { + maze[r][c].setTexture(SuperPel); + maze[r][c].setScale(0.40, 0.40); + maze[r][c].setPosition(c*Grid_size,r*Grid_size); + } + else if (Tracker.getStorePos(r, c) == 0) // normal pellet + { + maze[r][c].setTexture(YellowPellets); + maze[r][c].setScale(0.25, 0.25); + maze[r][c].setPosition(c*Grid_size+5,r*Grid_size+5); + totalFood++; + } + else if(Tracker.getStorePos(r, c) == 5) + { + maze[r][c].setTexture(PacKey); + maze[r][c].setScale(0.35, 0.35); + maze[r][c].setPosition(c*Grid_size,r*Grid_size); + } + } + } + //initialize player + Tracker.setSpritePos(posX, posY, Pac); + Update(); +} + +void Game::setKey(int r, int c) +{ + switch(LockCount) + { + case 0: + { + GPelStorR.push_back(r); + GPelStorC.push_back(c); + break; + } + case 1: + { + GPelStorR.push_back(r); + GPelStorC.push_back(c); + break; + } + case 2: + { + GPelStorR2.push_back(r); + GPelStorC2.push_back(c); + break; + } + case 3: + { + GPelStorR3.push_back(r); + GPelStorC3.push_back(c); + break; + } + } +} + +int Game::getTotalFood() +{ + return totalFood; +} + +int Game::getScore() +{ + return Score; +} + +Ghosts Game::getGhostObj() +{ + return Enemy; +} + +Movement Game::getMovementObj() +{ + return Tracker; +} + +Eater Game::getEaterObj() +{ + return Eating; +} + +void Game::ChangesAfterPelletEaten(TEXTURE tir) +{ + ChangeTexture(tir); +} + +Sprite Game::getSprite(IDENTITY ID) +{ + switch(ID) + { + case Blue: + { + return blueGhost; + } + case Red: + { + return redGhost; + } + case Orange: + { + return orangeGhost; + } + case Pink: + { + return pinkGhost; + } + case Pac: + { + return player_; + } + } +} + +void Game::GhostMovement() +{ + if(GhostCounter == 0) + { + if(GhostModeOn == true) // Ghost mode Functions + { + if(((clock()-startGhostModeTimer)/(double)(CLOCKS_PER_SEC))<10) + { + if(Tracker.getGhostMode(Blue) == true) + { + Enemy.GhostMode(Tracker, Blue); + } + else + { + Enemy.BlueGhostMoves(Tracker, BlueMove); + } + if(Tracker.getGhostMode(Red) == true) + { + Enemy.GhostMode(Tracker, Red); + } + else + { + Enemy.RedGhostMoves(Tracker); + } + if(Tracker.getGhostMode(Orange) == true) + { + Enemy.GhostMode(Tracker, Orange); + } + else + { + Enemy.OrangeGhostMoves(Tracker, OrangeMove); + } + if(Tracker.getGhostMode(Pink) == true) + { + Enemy.GhostMode(Tracker, Pink); + } + else + { + Enemy.PinkGhostMoves(Tracker); + } + } + else + { + GhostModeOn = false ; + Tracker.TurnGhostMode(GhostModeOn); + blueGhost.setTexture(ghostBlue); + blueGhost.setScale(0.11,0.11); + redGhost.setTexture(ghostRed); + redGhost.setScale(0.11,0.11); + orangeGhost.setTexture(ghostOrange); + orangeGhost.setScale(0.11,0.11); + pinkGhost.setTexture(ghostPink); + pinkGhost.setScale(0.11,0.11); + } + } + else + { + Enemy.BlueGhostMoves(Tracker, BlueMove); + Enemy.OrangeGhostMoves(Tracker, OrangeMove); + Enemy.RedGhostMoves(Tracker); + Enemy.PinkGhostMoves(Tracker); + } + GhostCounter=5; + } + else + GhostCounter--; +} + +void Game::ChangeTexture(TEXTURE Tir) +{ + switch(Tir) + { + case Super: + { + SuperPac = true; + SuperPacTimer = clock(); + + player_.setScale(0.035, 0.035); + + ChangeGhostSuper(); + Tracker.TurnSuperMode(SuperPac); + + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); + break; + } + case Power: + { + blueGhost.setTexture(VulnerableGhost); + blueGhost.setScale(0.18,0.16); + + redGhost.setTexture(VulnerableGhost); + redGhost.setScale(0.18,0.16); + + orangeGhost.setTexture(VulnerableGhost); + orangeGhost.setScale(0.18,0.16); + + pinkGhost.setTexture(VulnerableGhost); + pinkGhost.setScale(0.18,0.16); + + GhostModeOn = true ; + Tracker.TurnGhostMode(GhostModeOn); + + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); + startGhostModeTimer = clock(); + break; + } + case Key: + { + Tracker.setStorePos(GPelStorR[0],GPelStorC[0],2); + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); + Locks[GPelStorR[0]][GPelStorC[0]].setColor(sf::Color::Transparent); + GPelStorR.erase(GPelStorR.begin()); + GPelStorC.erase(GPelStorC.begin()); + + Tracker.setStorePos(GPelStorR2[0],GPelStorC2[0],2); + Locks[GPelStorR2[0]][GPelStorC2[0]].setColor(sf::Color::Transparent); + GPelStorR2.erase(GPelStorR2.begin()); + GPelStorC2.erase(GPelStorC2.begin()); + + Tracker.setStorePos(GPelStorR3[0],GPelStorC3[0],2); + Locks[GPelStorR3[0]][GPelStorC3[0]].setColor(sf::Color::Transparent); + GPelStorR3.erase(GPelStorR3.begin()); + GPelStorC3.erase(GPelStorC3.begin()); + break; + } + case BreakLock: + { + Locks[Tracker.getCoordinates(Pac).x][Tracker.getCoordinates(Pac).y].setColor(sf::Color::Transparent); + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); + break; + } + case Food: + { + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); + Score++; + } + } +} + +void Game::ChangeGhostTexture(IDENTITY ID) +{ + switch (ID) + { + case Blue: + { + blueGhost.setTexture(ghostBlue); + blueGhost.setScale(0.11,0.11); + break; + } + case Red: + { + redGhost.setTexture(ghostRed); + redGhost.setScale(0.11,0.11); + break; + } + case Orange: + { + orangeGhost.setTexture(ghostOrange); + orangeGhost.setScale(0.11,0.11); + break; + } + case Pink: + { + pinkGhost.setTexture(ghostPink); + pinkGhost.setScale(0.11,0.11); + break; + } + } +} + +void Game::ChangeGhostSuper() +{ + blueGhost.setScale(0.05, 0.11); + redGhost.setScale(0.05, 0.11); + orangeGhost.setScale(0.05, 0.11); + pinkGhost.setScale(0.05, 0.11); +} + +void Game::Update() +{ + player_.setPosition(Tracker.getCoordinates(Pac).y*Grid_size, Tracker.getCoordinates(Pac).x*Grid_size); + + blueGhost.setPosition(Tracker.getCoordinates(Blue).y*Grid_size, Tracker.getCoordinates(Blue).x*Grid_size); + orangeGhost.setPosition(Tracker.getCoordinates(Orange).y*Grid_size, Tracker.getCoordinates(Orange).x*Grid_size); + redGhost.setPosition(Tracker.getCoordinates(Red).y*Grid_size, Tracker.getCoordinates(Red).x*Grid_size); + pinkGhost.setPosition(Tracker.getCoordinates(Pink).y*Grid_size, Tracker.getCoordinates(Pink).x*Grid_size); + maze[Tracker.getCoordinates(Pac).x][Tracker.getCoordinates(Pac).y].setColor(sf::Color::Transparent); + Tracker.setStorePos(Tracker.getCoordinates(Pac).x,Tracker.getCoordinates(Pac).y,2); +} + +void Game::PacDead(RenderWindow& window) +{ + window.clear() ; + + window.close(); + FinalDisplay(1); +} + +void Game::Controler(RenderWindow& window) +{ + while (window.isOpen()) + { + EventTracker(window); + + if(((clock()-SuperPacTimer)/(double)(CLOCKS_PER_SEC))>15) + { + SuperPac = false; + Tracker.TurnSuperMode(SuperPac); + player_.setScale(0.025, 0.023); + + blueGhost.setScale(0.11, 0.11); + redGhost.setScale(0.11, 0.11); + orangeGhost.setScale(0.11, 0.11); + pinkGhost.setScale(0.11, 0.11); + } + + IDENTITY GhostTex; + GhostTex = Eating.PacEatGhost(Tracker); + if( GhostTex == Pac) + { + isDead = Eating.GhostEatPac(Tracker) ; + + if(SuperPac) + { + ChangeGhostSuper(); + isDead = false; + } + if(isDead) + { + PacDead(window); + } + } + if(Score == totalFood) + { + window.close(); + FinalDisplay(2); + } + + if(Keyboard::isKeyPressed(Keyboard::Up)) + { + player_.setTexture(pacManUp); + ChangeTexture(Tracker.MovePac(Up)) ; + + BlueMove = Up; + OrangeMove=Down; + } + if(Keyboard::isKeyPressed(Keyboard::Down)) + { + player_.setTexture(pacManDown); + ChangeTexture(Tracker.MovePac(Down)) ; + + BlueMove = Down; + OrangeMove=Up; + } + if(Keyboard::isKeyPressed(Keyboard::Left)) + { + player_.setTexture(pacManLeft); + ChangeTexture(Tracker.MovePac(Left)) ; + + BlueMove = Left; + OrangeMove=Right; + } + if(Keyboard::isKeyPressed(Keyboard::Right)) + { + player_.setTexture(pacManRight); + ChangeTexture(Tracker.MovePac(Right)) ; + + BlueMove = Right; + OrangeMove=Left; + } + ChangeGhostTexture(GhostTex); + GhostMovement(); + WindowDraw_Display(window); + Sleep( Speed ); + } +} + +void Game::EventTracker(RenderWindow& window) +{ + // The next 6 lines of code detect if the window is closed + // And then shuts down the program + + //Process events + Event event; + + while (window.pollEvent(event)) + { + //close window: exit + if (event.type == Event::Closed) + { + // Someone closed the window + window.close(); + } + if(event.type == Event::KeyPressed && event.key.code == Keyboard::Escape) + { + window.close(); + } + } + // Clear everything from the last run of the while loop/screen + window.clear(); + + Update(); +} + +void Game::WindowDraw_Display(RenderWindow& window) +{ + // Draw our game scene here + for(int r = 0; r < rows; r++) + { + for(int c = 0; c +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace sf; +using namespace std; + +class Game +{ +public: + Game(string FileName); + Game(); + void StartGame(); + + int getTotalFood(); + int getScore(); + + void ChangesAfterPelletEaten(TEXTURE tir); + Sprite getSprite(IDENTITY ID); + Ghosts getGhostObj(); + Movement getMovementObj(); + Eater getEaterObj(); + + virtual ~Game(); + +private: + //Functions + void LoadTextures(); + void CreateMaze(); + void ResetPos(); + void setKey(int r, int c); + + void GhostMovement(); + void ChangeTexture(TEXTURE Tir); + void ChangeGhostTexture(IDENTITY ID); + void ChangeGhostSuper(); + + void Update(); + + void PacDead(RenderWindow& window); + void Controler(RenderWindow& window); + void EventTracker(RenderWindow& window); + void WindowDraw_Display(RenderWindow& window); + + void FinalDisplay(int Fin); + + //Objects + Movement Tracker; + Ghosts Enemy; + Eater Eating; + + const int rows = 22; + const int cols = 19; + const int Grid_size = 30 ; + const unsigned Window_WIDTH = 571 ; + const unsigned Window_HEIGHT = 660 ; + string FileName_; + + int unlockCount=0; + int TempCount = 0 ; + int LockCount=0; + int Speed = 85; // speed Pac moves + + int totalFood, Score; + int GhostCounter = 5; //Delays the movement of the ghosts + int startGhostModeTimer = 0, SuperPacTimer = 0; + DIRECTION BlueMove, OrangeMove; + bool GhostModeOn = false; + bool SuperPac = false; + bool isDead; + + //Need my Sprite 2D array + Sprite maze[22][19]; + //Need player + Sprite player_; + //Ghosts + Sprite blueGhost, redGhost, orangeGhost, pinkGhost; + //Locks we need + Sprite Locks[22][19]; + + vector GPelStorR; //stores where the locks are + vector GPelStorC; + + vector GPelStorR2; //stores where the locks are + vector GPelStorC2; + + vector GPelStorR3; //stores where the locks are + vector GPelStorC3; + + //textures + Texture pacManLeft, pacManRight, pacManUp, pacManDown, ghostBlue, ghostRed, ghostOrange, ghostPink, VulnerableGhost; + Texture brick, YellowPellets, SuperPel, PowerPel, Berry, PacKey, Locker; +}; + +#endif // GAME_H diff --git a/test-source-code/Ghosts.cpp b/test-source-code/Ghosts.cpp new file mode 100644 index 0000000..839c20f --- /dev/null +++ b/test-source-code/Ghosts.cpp @@ -0,0 +1,588 @@ +#include "Ghosts.h" +#include "Movement.h" +#include "Datatypes.h" +#include +#include + +using namespace std ; + +Ghosts::Ghosts() +{ + //ctor +} +void Ghosts::BlueGhostMoves(Movement& Track, DIRECTION dir) +{ + TEXTURE NextTexture = Track.CheckWalLock(dir, Blue); + + if(dir == Left && NextTexture!= Wall && NextTexture!= Lock && (PrevBlue!='r' || IsNextToLock(Track, Blue))) + { + Track.MoveBlueGhost(Left); + PrevBlue = 'l'; + } + else if(dir == Right && NextTexture!= Wall && NextTexture!= Lock && (PrevBlue!='l' || IsNextToLock(Track,Blue))) + { + Track.MoveBlueGhost(Right); + PrevBlue = 'r'; + } + else if(dir == Up && NextTexture!= Wall && NextTexture!= Lock && (PrevBlue!='d' || IsNextToLock(Track,Blue))) + { + Track.MoveBlueGhost(Up); + PrevBlue = 'u'; + } + else if(dir == Down && NextTexture!= Wall && NextTexture!= Lock && (PrevBlue!='u' || IsNextToLock(Track,Blue))) + { + Track.MoveBlueGhost(Down); + PrevBlue = 'd'; + } + else + { + vector ValidMoves; + + if(Track.CheckWalLock(Left, Blue)!= Wall && Track.CheckWalLock(Left, Blue)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Right, Blue)!= Wall && Track.CheckWalLock(Right, Blue)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Up, Blue)!= Wall && Track.CheckWalLock(Up, Blue)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Down, Blue)!= Wall && Track.CheckWalLock(Down, Blue)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(ValidMoves[0]==true && (PrevBlue!='r' || IsNextToLock(Track,Blue)))//left + { + Track.MoveBlueGhost(Left); + PrevBlue = 'l'; + } + else if(ValidMoves[1]==true && (PrevBlue!='l' || IsNextToLock(Track,Blue)))//right + { + Track.MoveBlueGhost(Right); + PrevBlue = 'r'; + } + else if(ValidMoves[2]==true && (PrevBlue!='d'|| IsNextToLock(Track,Blue)))//up + { + Track.MoveBlueGhost(Up); + PrevBlue = 'u'; + } + else if(ValidMoves[3]==true && (PrevBlue!='u' || IsNextToLock(Track,Blue)))//down + { + Track.MoveBlueGhost(Down); + PrevBlue = 'd'; + } + ValidMoves.clear(); + } +} + +void Ghosts::RedGhostMoves(Movement& Track) +{ + vector ValidMoves; + + if(Track.CheckWalLock(Left, Red)!= Wall && Track.CheckWalLock(Left, Red)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Right, Red)!= Wall && Track.CheckWalLock(Right, Red)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Up, Red)!= Wall && Track.CheckWalLock(Up, Red)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Down, Red)!= Wall && Track.CheckWalLock(Down, Red)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + int X = Track.getCoordinates(Red).x - Track.getCoordinates(Pac).x; + int Y = Track.getCoordinates(Red).y - Track.getCoordinates(Pac).y; + + if(fabs(X) < fabs(Y)) //if x co-ordinate of pac is closer to x of RedGhost + { + if(X < 0 && Track.CheckWalLock(Down, Red) != Wall && Track.CheckWalLock(Down, Red) != Lock && (PrevRed!='u' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Down); + PrevRed = 'd'; + } + else if(X > 0 && Track.CheckWalLock(Up, Red) != Wall && Track.CheckWalLock(Up, Red) != Lock && (PrevRed!='d' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Up); + PrevRed = 'u'; + } + else if( Y < 0 && Track.CheckWalLock(Right, Red) != Wall && Track.CheckWalLock(Right, Red) != Lock && (PrevRed!='l' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Right); + PrevRed = 'r'; + } + else if( Y>0 && Track.CheckWalLock(Left, Red) != Wall && Track.CheckWalLock(Left, Red) != Lock && (PrevRed!='r' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Left); + PrevRed = 'l'; + } + else + { + if(ValidMoves[0]==true && (PrevRed!='r' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Left); + PrevRed = 'l'; + } + else if(ValidMoves[1]==true && (PrevRed!='l' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Right); + PrevRed = 'r'; + } + else if(ValidMoves[2]==true && (PrevRed!='d' || IsNextToLock(Track,Red)))//up + { + Track.MoveRedGhost(Up); + PrevRed = 'u'; + } + else if(ValidMoves[3]==true && (PrevRed!='u' || IsNextToLock(Track,Red)))//down + { + Track.MoveRedGhost(Down); + PrevRed = 'd'; + } + } + } + else //if y co-ordinate of pac is closer to y of RedGhost + { + if(Y < 0 && Track.CheckWalLock(Right, Red) != Wall && Track.CheckWalLock(Right, Red) != Lock && (PrevRed!='l' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Right); + PrevRed = 'r'; + } + else if(Y>0 && Track.CheckWalLock(Left, Red) != Wall && Track.CheckWalLock(Left, Red) != Lock && (PrevRed!='r' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Left); + PrevRed = 'l'; + } + else if( X<0 && Track.CheckWalLock(Down, Red) != Wall && Track.CheckWalLock(Down, Red) != Lock && (PrevRed!='u' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Down); + PrevRed = 'd'; + } + else if( X>0 && Track.CheckWalLock(Up, Red) != Wall && Track.CheckWalLock(Up, Red) != Lock && (PrevRed!='d' || IsNextToLock(Track,Red))) + { + Track.MoveRedGhost(Up); + PrevRed = 'u'; + } + else + { + if(ValidMoves[0]==true && (PrevRed!='r' || IsNextToLock(Track,Red)))//left + { + Track.MoveRedGhost(Left); + PrevRed = 'l'; + } + else if(ValidMoves[1]==true && (PrevRed!='l' || IsNextToLock(Track,Red)))//right + { + Track.MoveRedGhost(Right); + PrevRed = 'r'; + } + else if(ValidMoves[2]==true && (PrevRed!='d' || IsNextToLock(Track,Red)))//up + { + Track.MoveRedGhost(Up); + PrevRed = 'u'; + } + else if(ValidMoves[3]==true && (PrevRed!='u' || IsNextToLock(Track,Red)))//down + { + Track.MoveRedGhost(Down); + PrevRed = 'd'; + } + } + } + ValidMoves.clear(); +} + +void Ghosts::OrangeGhostMoves(Movement& Track, DIRECTION dir) +{ + TEXTURE NextTexture = Track.CheckWalLock(dir, Orange); + + if(dir == Left && NextTexture!= Wall && NextTexture!= Lock && (PrevOrange!='r' || IsNextToLock(Track,Orange))) + { + Track.MoveOrangeGhost(Left); + PrevOrange = 'l'; + } + else if(dir == Right && NextTexture!= Wall && NextTexture!= Lock && (PrevOrange!='l' || IsNextToLock(Track,Orange))) + { + Track.MoveOrangeGhost(Right); + PrevOrange = 'r'; + } + else if(dir == Up && NextTexture!= Wall && NextTexture!= Lock && (PrevOrange!='d' || IsNextToLock(Track,Orange))) + { + Track.MoveOrangeGhost(Up); + PrevOrange = 'u'; + } + else if(dir == Down && NextTexture!= Wall && NextTexture!= Lock && (PrevOrange!='u' || IsNextToLock(Track,Orange))) + { + Track.MoveOrangeGhost(Down); + PrevOrange = 'd'; + } + else + { + vector ValidMoves; + + if(Track.CheckWalLock(Left, Orange)!= Wall && Track.CheckWalLock(Left, Orange)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Right, Orange)!= Wall && Track.CheckWalLock(Right, Orange)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Up, Orange)!= Wall && Track.CheckWalLock(Up, Orange)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Down, Orange)!= Wall && Track.CheckWalLock(Down, Orange)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(ValidMoves[0]==true && (PrevOrange!='r' || IsNextToLock(Track,Orange)))//left + { + Track.MoveOrangeGhost(Left); + PrevOrange = 'l'; + } + else if(ValidMoves[1]==true && (PrevOrange!='l' || IsNextToLock(Track,Orange)))//right + { + Track.MoveOrangeGhost(Right); + PrevOrange = 'r'; + } + else if(ValidMoves[2]==true && (PrevOrange!='d'|| IsNextToLock(Track,Orange)))//up + { + Track.MoveOrangeGhost(Up); + PrevOrange = 'u'; + } + else if(ValidMoves[3]==true && (PrevOrange!='u' || IsNextToLock(Track,Orange)))//down + { + Track.MoveOrangeGhost(Down); + PrevOrange = 'd'; + } + ValidMoves.clear(); + } +} + +void Ghosts::PinkGhostMoves(Movement& Track) +{ + vector ValidMoves; + + if(Track.CheckWalLock(Left, Pink)!= Wall && Track.CheckWalLock(Left, Pink)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Right, Pink)!= Wall && Track.CheckWalLock(Right, Pink)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Up, Pink)!= Wall && Track.CheckWalLock(Up, Pink)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Down, Pink)!= Wall && Track.CheckWalLock(Down, Pink)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + int X = Track.getCoordinates(Pink).x - Track.getCoordinates(Pac).x; + int Y = Track.getCoordinates(Pink).y - Track.getCoordinates(Pac).y; + + if(fabs(X) > fabs(Y)) //if x co-ordinate of pac is further to x of PinkGhost + { + if(X < 0 && Track.CheckWalLock(Down, Pink) != Wall && Track.CheckWalLock(Down, Pink) != Lock && (PrevPink!='u' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Down); + PrevPink = 'd'; + } + else if(X > 0 && Track.CheckWalLock(Up, Pink) != Wall && Track.CheckWalLock(Up, Pink) != Lock && (PrevPink!='d' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Up); + PrevPink = 'u'; + } + else if( Y < 0 && Track.CheckWalLock(Right, Pink) != Wall && Track.CheckWalLock(Right, Pink) != Lock && (PrevPink!='l' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Right); + PrevPink = 'r'; + } + else if( Y>0 && Track.CheckWalLock(Left, Pink) != Wall && Track.CheckWalLock(Left, Pink) != Lock && (PrevPink!='r' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Left); + PrevPink = 'l'; + } + else + { + if(ValidMoves[0]==true && (PrevPink!='r' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Left); + PrevPink = 'l'; + } + else if(ValidMoves[1]==true && (PrevPink!='l' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Right); + PrevPink = 'r'; + } + else if(ValidMoves[2]==true && (PrevPink!='d' || IsNextToLock(Track,Pink)))//up + { + Track.MovePinkGhost(Up); + PrevPink = 'u'; + } + else if(ValidMoves[3]==true && (PrevPink!='u' || IsNextToLock(Track,Pink)))//down + { + Track.MovePinkGhost(Down); + PrevPink = 'd'; + } + } + } + else //if y co-ordinate of pac is Further to y of PinkGhost + { + if(Y < 0 && Track.CheckWalLock(Right, Pink) != Wall && Track.CheckWalLock(Right, Pink) != Lock && (PrevPink!='l' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Right); + PrevPink = 'r'; + } + else if(Y>0 && Track.CheckWalLock(Left, Pink) != Wall && Track.CheckWalLock(Left, Pink) != Lock && (PrevPink!='r' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Left); + PrevPink = 'l'; + } + else if( X<0 && Track.CheckWalLock(Down, Pink) != Wall && Track.CheckWalLock(Down, Pink) != Lock && (PrevPink!='u' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Down); + PrevPink = 'd'; + } + else if( X>0 && Track.CheckWalLock(Up, Pink) != Wall && Track.CheckWalLock(Up, Pink) != Lock && (PrevPink!='d' || IsNextToLock(Track,Pink))) + { + Track.MovePinkGhost(Up); + PrevPink = 'u'; + } + else + { + if(ValidMoves[0]==true && (PrevPink!='r' || IsNextToLock(Track,Pink)))//left + { + Track.MovePinkGhost(Left); + PrevPink = 'l'; + } + else if(ValidMoves[1]==true && (PrevPink!='l' || IsNextToLock(Track,Pink)))//right + { + Track.MovePinkGhost(Right); + PrevPink = 'r'; + } + else if(ValidMoves[2]==true && (PrevPink!='d' || IsNextToLock(Track,Pink)))//up + { + Track.MovePinkGhost(Up); + PrevPink = 'u'; + } + else if(ValidMoves[3]==true && (PrevPink!='u' || IsNextToLock(Track,Pink)))//down + { + Track.MovePinkGhost(Down); + PrevPink = 'd'; + } + } + } + ValidMoves.clear(); +} + +void Ghosts::GhostMode(Movement& Track, IDENTITY ID) +{ + char PrevDir = ' '; + DIRECTION toMove; + switch(ID) + { + case Blue: + { + PrevDir = PrevBlue; + break; + } + case Red: + { + PrevDir = PrevRed; + break; + } + case Orange: + { + PrevDir = PrevOrange; + break; + } + case Pink: + { + PrevDir = PrevPink; + break; + } + } + vector ValidMoves; + + if(Track.CheckWalLock(Left, ID)!= Wall && Track.CheckWalLock(Left, ID)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Right, ID)!= Wall && Track.CheckWalLock(Right, ID)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Up, ID)!= Wall && Track.CheckWalLock(Up, ID)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + if(Track.CheckWalLock(Down, ID)!= Wall && Track.CheckWalLock(Down, ID)!= Lock) + ValidMoves.push_back(true); + else + ValidMoves.push_back(false); + + int X = Track.getCoordinates(ID).x - Track.getCoordinates(Pac).x; + int Y = Track.getCoordinates(ID).y - Track.getCoordinates(Pac).y; + + if(fabs(X) < fabs(Y)) //if x co-ordinate of pac is closer to x of IDGhost + { + if(X > 0 && Track.CheckWalLock(Down, ID) != Wall && Track.CheckWalLock(Down, ID) != Lock && (PrevDir!='u' || IsNextToLock(Track,ID))) + { + toMove = Down; + PrevDir = 'd'; + } + else if(X < 0 && Track.CheckWalLock(Up, ID) != Wall && Track.CheckWalLock(Up, ID) != Lock && (PrevDir!='d' || IsNextToLock(Track,ID))) + { + toMove = Up; + PrevDir = 'u'; + } + else if( Y > 0 && Track.CheckWalLock(Right, ID) != Wall && Track.CheckWalLock(Right, ID) != Lock && (PrevDir!='l' || IsNextToLock(Track,ID))) + { + toMove = Right; + PrevDir = 'r'; + } + else if( Y < 0 && Track.CheckWalLock(Left, ID) != Wall && Track.CheckWalLock(Left, ID) != Lock && (PrevDir!='r' || IsNextToLock(Track,ID))) + { + toMove = Left; + PrevDir = 'l'; + } + else + { + if(ValidMoves[0]==true && (PrevDir!='r' || IsNextToLock(Track,ID))) + { + toMove = Left; + PrevDir = 'l'; + } + else if(ValidMoves[1]==true && (PrevDir!='l' || IsNextToLock(Track,ID))) + { + toMove = Right; + PrevDir = 'r'; + } + else if(ValidMoves[2]==true && (PrevDir!='d' || IsNextToLock(Track,ID)))//up + { + toMove = Up; + PrevDir = 'u'; + } + else if(ValidMoves[3]==true && (PrevDir!='u' || IsNextToLock(Track,ID)))//down + { + toMove = Down; + PrevDir = 'd'; + } + } + } + else //if y co-ordinate of pac is closer to y of IDGhost + { + if(Y > 0 && Track.CheckWalLock(Right, ID) != Wall && Track.CheckWalLock(Right, ID) != Lock && (PrevDir!='l' || IsNextToLock(Track,ID))) + { + toMove = Right; + PrevDir = 'r'; + } + else if(Y < 0 && Track.CheckWalLock(Left, ID) != Wall && Track.CheckWalLock(Left, ID) != Lock && (PrevDir!='r' || IsNextToLock(Track,ID))) + { + toMove = Left; + PrevDir = 'l'; + } + else if( X > 0 && Track.CheckWalLock(Down, ID) != Wall && Track.CheckWalLock(Down, ID) != Lock && (PrevDir!='u' || IsNextToLock(Track,ID))) + { + toMove = Down; + PrevDir = 'd'; + } + else if( X < 0 && Track.CheckWalLock(Up, ID) != Wall && Track.CheckWalLock(Up, ID) != Lock && (PrevDir!='d' || IsNextToLock(Track,ID))) + { + toMove = Up; + PrevDir = 'u'; + } + else + { + if(ValidMoves[0]==true && (PrevDir!='r' || IsNextToLock(Track,ID)))//left + { + toMove = Left; + PrevDir = 'l'; + } + else if(ValidMoves[1]==true && (PrevDir!='l' || IsNextToLock(Track,ID)))//right + { + toMove = Right; + PrevDir = 'r'; + } + else if(ValidMoves[2]==true && (PrevDir!='d' || IsNextToLock(Track,ID)))//up + { + toMove = Up; + PrevDir = 'u'; + } + else if(ValidMoves[3]==true && (PrevDir!='u' || IsNextToLock(Track,ID)))//down + { + toMove = Down; + PrevDir = 'd'; + } + } + } + ValidMoves.clear(); + switch(ID) + { + case Blue: + { + PrevBlue = PrevDir; + Track.MoveBlueGhost(toMove); + break; + } + case Red: + { + PrevRed = PrevDir; + Track.MoveRedGhost(toMove); + break; + } + case Orange: + { + PrevOrange = PrevDir; + Track.MoveOrangeGhost(toMove); + break; + } + case Pink: + { + PrevPink = PrevDir; + Track.MovePinkGhost(toMove); + break; + } + } +} + +bool Ghosts::IsNextToLock(Movement& Track, IDENTITY ID) +{ + if(Track.CheckWalLock(Left, ID) == Lock || Track.CheckWalLock(Right, ID) == Lock || Track.CheckWalLock(Up, ID) == Lock || Track.CheckWalLock(Down, ID) == Lock) + { + return true; + } + else + return false; +} + +Ghosts::~Ghosts() +{ + //dtor +} diff --git a/test-source-code/Ghosts.h b/test-source-code/Ghosts.h new file mode 100644 index 0000000..f68fdc6 --- /dev/null +++ b/test-source-code/Ghosts.h @@ -0,0 +1,28 @@ +#ifndef GHOSTS_H +#define GHOSTS_H + +#include "Movement.h" +#include "Datatypes.h" + + +class Ghosts +{ + public: + Ghosts(); + ~Ghosts(); + + void BlueGhostMoves(Movement& Track, DIRECTION dir); + void RedGhostMoves(Movement& Track); + void OrangeGhostMoves(Movement& Track, DIRECTION dir); + void PinkGhostMoves(Movement& Track); + void GhostMode(Movement& Track, IDENTITY ID); + bool IsNextToLock(Movement& Track, IDENTITY ID); + + private: + char PrevBlue = ' '; + char PrevRed = ' '; + char PrevOrange = ' '; + char PrevPink = ' '; +}; + +#endif // GHOSTS_H diff --git a/test-source-code/Movement.cpp b/test-source-code/Movement.cpp new file mode 100644 index 0000000..57a34b8 --- /dev/null +++ b/test-source-code/Movement.cpp @@ -0,0 +1,325 @@ +#include "Movement.h" +#include + +using namespace std; + +Movement::Movement() +{ + //ctor +} + +void Movement::MoveBlueGhost(DIRECTION dir) +{ + Coordinates BlueMan; + BlueMan = getCoordinates(Blue); + switch (dir) + { + case Left: + { + setSpritePos(BlueMan.x, BlueMan.y-1, Blue); + break; + } + case Right: + { + setSpritePos(BlueMan.x, BlueMan.y+1, Blue); + break; + } + case Up: + { + setSpritePos(BlueMan.x-1, BlueMan.y, Blue); + break; + } + case Down: + { + setSpritePos(BlueMan.x+1, BlueMan.y, Blue); + break; + } + } +} + +void Movement::MoveRedGhost(DIRECTION dir) +{ + Coordinates RedMan; + RedMan = getCoordinates(Red); + switch (dir) + { + case Left: + { + setSpritePos(RedMan.x, RedMan.y-1, Red); + break; + } + case Right: + { + setSpritePos(RedMan.x, RedMan.y+1, Red); + break; + } + case Up: + { + setSpritePos(RedMan.x-1, RedMan.y, Red); + break; + } + case Down: + { + setSpritePos(RedMan.x+1, RedMan.y, Red); + break; + } + } +} + +void Movement::MoveOrangeGhost(DIRECTION dir) +{ + Coordinates OrangeMan; + OrangeMan = getCoordinates(Orange); + switch (dir) + { + case Left: + { + setSpritePos(OrangeMan.x, OrangeMan.y-1, Orange); + break; + } + case Right: + { + setSpritePos(OrangeMan.x, OrangeMan.y+1, Orange); + break; + } + case Up: + { + setSpritePos(OrangeMan.x-1, OrangeMan.y, Orange); + break; + } + case Down: + { + setSpritePos(OrangeMan.x+1, OrangeMan.y, Orange); + break; + } + } +} + +void Movement::MovePinkGhost(DIRECTION dir) +{ + + Coordinates PinkMan; + PinkMan = getCoordinates(Pink); + switch (dir) + { + case Left: + { + setSpritePos(PinkMan.x, PinkMan.y-1, Pink); + + break; + } + case Right: + { + setSpritePos(PinkMan.x, PinkMan.y+1, Pink); + + break; + } + case Up: + { + setSpritePos(PinkMan.x-1, PinkMan.y, Pink); + + break; + } + case Down: + { + setSpritePos(PinkMan.x+1, PinkMan.y, Pink); + + break; + } + } +} + +void Movement::TurnGhostMode(bool Mode) +{ + GhostBluemode = Mode; + GhostOrangeMode = Mode; + GhostRedMode = Mode ; + GhostPinkMode = Mode; +} + +void Movement::TurnGhostModeOff(IDENTITY ID, bool Mode) +{ + switch (ID) + { + case Blue: + { + GhostBluemode = Mode; + break; + } + case Pink: + { + GhostPinkMode = Mode; + break; + } + case Red: + { + GhostRedMode = Mode; + break; + } + case Orange: + { + GhostOrangeMode = Mode; + break; + } + } +} + +void Movement::TurnSuperMode(bool Mode) +{ + SuperPac = Mode; +} + +bool Movement::getGhostMode(IDENTITY ID) +{ + switch(ID) + { + case Blue: + { + return GhostBluemode; + } + case Red: + { + return GhostRedMode; + } + case Orange: + { + return GhostOrangeMode; + } + case Pink: + { + return GhostPinkMode; + } + } +} + +Coordinates Movement::GetNextCoord(DIRECTION dir, IDENTITY ID) +{ + Coordinates Character ; + Character = getCoordinates(ID); + + switch (dir) + { + case Left: + { + Character.y = Character.y-1 ; + break; + } + case Right: + { + Character.y = Character.y+1; + break; + } + case Up: + { + Character.x = Character.x-1; + break; + } + case Down: + { + Character.x = Character.x+1 ; + break; + } + } + return Character; +} + +TEXTURE Movement::CheckWalLock(DIRECTION dir, IDENTITY ID) +{ + Coordinates Character ; + Character = GetNextCoord(dir,ID) ; + + if (getStorePos(Character.x,Character.y)== 1) + { + return Wall ; + } + else if( getStorePos(Character.x,Character.y)== 7) + { + return Lock; + } + else if(getStorePos(Character.x,Character.y)== 5) + { + return Key; + } + else if(getStorePos(Character.x,Character.y)== 6) + { + return Power; + } + else if(getStorePos(Character.x,Character.y)== 10) + { + return Super; + } + else if(getStorePos(Character.x,Character.y)== 3 || getStorePos(Character.x,Character.y)== 0 || getStorePos(Character.x,Character.y)== 4) + { + return Food; + } + return blank; +} + +TEXTURE Movement::MovePac(DIRECTION dir) +{ + Coordinates Pacman; + Pacman = getCoordinates(Pac); + + TEXTURE ChangeTex; + ChangeTex = CheckWalLock(dir,Pac); + + if(ChangeTex == Lock && SuperPac == true) + { + switch (dir) + { + case Left: + { + setSpritePos(Pacman.x, Pacman.y-1, Pac); + break; + } + case Right: + { + setSpritePos(Pacman.x, Pacman.y+1, Pac); + break; + } + case Up: + { + setSpritePos(Pacman.x-1, Pacman.y, Pac); + break; + } + case Down: + { + setSpritePos(Pacman.x+1, Pacman.y, Pac); + break; + } + } + return BreakLock; + } + else if(ChangeTex!= Wall && ChangeTex!= Lock) + { + switch (dir) + { + case Left: + { + setSpritePos(Pacman.x, Pacman.y-1, Pac); + break; + } + case Right: + { + setSpritePos(Pacman.x, Pacman.y+1, Pac); + break; + } + case Up: + { + setSpritePos(Pacman.x-1, Pacman.y, Pac); + break; + } + case Down: + { + setSpritePos(Pacman.x+1, Pacman.y, Pac); + break; + } + } + } + return ChangeTex; +} + +Movement::~Movement() +{ + //dtor +} diff --git a/test-source-code/Movement.h b/test-source-code/Movement.h new file mode 100644 index 0000000..6e5c2c7 --- /dev/null +++ b/test-source-code/Movement.h @@ -0,0 +1,42 @@ +#ifndef MOVEMENT_H +#define MOVEMENT_H + +#include "PositionHandler.h" + + +class Movement: public PositionHandler +{ +public: + Movement(); + ~Movement(); + + void MoveBlueGhost(DIRECTION dir); + void MoveRedGhost(DIRECTION dir); + void MoveOrangeGhost(DIRECTION dir); + void MovePinkGhost(DIRECTION dir); + + void TurnGhostMode(bool Mode); + void TurnGhostModeOff(IDENTITY ID,bool Mode); + void TurnSuperMode(bool Mode); + + bool getGhostMode(IDENTITY ID); + + Coordinates GetNextCoord(DIRECTION dir, IDENTITY ID); + TEXTURE CheckWalLock(DIRECTION dir, IDENTITY ID); + TEXTURE MovePac(DIRECTION dir); + +private: + bool GhostBluemode = false; + bool GhostRedMode = false; + bool GhostPinkMode = false; + bool GhostOrangeMode = false; + bool SuperPac = false; + + + + + + +}; + +#endif // MOVEMENT_H diff --git a/test-source-code/PositionHandler.cpp b/test-source-code/PositionHandler.cpp new file mode 100644 index 0000000..afbc26a --- /dev/null +++ b/test-source-code/PositionHandler.cpp @@ -0,0 +1,165 @@ +#include "PositionHandler.h" + +#include +#include + +using namespace std; + +PositionHandler::PositionHandler() +{ + +} + +void PositionHandler::Init_StorePos(string Filename_) +{ + ifstream inFile; + inFile.open(Filename_); + + for(int r = 0; r < 22; r++) + { + for(int c = 0; c < 19; c++) + { + inFile >> StorePos[r][c]; + } + } +} + +void PositionHandler::setStorePos(int r, int c, int ChangeNum) +{ + StorePos[r][c]= ChangeNum; +} + +void PositionHandler::setSpritePos(int x, int y, IDENTITY ID) +{ + switch(ID) + { + case Pac: + { + PacPos.x = x; + PacPos.y = y; + break; + } + case Blue: + { + BluePos.x = x; + BluePos.y = y; + break; + } + case Red: + { + RedPos.x = x; + RedPos.y = y; + break; + } + case Orange: + { + OrangePos.x = x; + OrangePos.y = y; + break; + } + case Pink: + { + PinkPos.x = x; + PinkPos.y = y; + break; + } + } +} + +void PositionHandler::SetInit(IDENTITY ID, int r, int c) +{ + switch(ID) + { + case Blue: + { + setSpritePos(r,c,Blue); + BlueInit.x = r ; + BlueInit.y = c ; + break; + } + case Red: + { + setSpritePos(r,c,Red); + RedInit.x = r ; + RedInit.y = c ; + break; + } + case Pink: + { + setSpritePos(r,c,Pink); + PinkInit.x = r ; + PinkInit.y = c ; + break; + } + case Orange: + { + setSpritePos(r,c,Orange); + OrangeInit.x = r ; + OrangeInit.y = c ; + break; + } + } +} + +int PositionHandler::getStorePos(int r, int c) +{ + return StorePos[r][c]; +} + +Coordinates PositionHandler::getCoordinates(IDENTITY ID) +{ + switch(ID) + { + case Pac: + { + return PacPos; + } + case Blue: + { + return BluePos; + } + case Red: + { + return RedPos; + } + case Orange: + { + return OrangePos; + } + case Pink: + { + return PinkPos; + } + } +} + +Coordinates PositionHandler::GetInit(IDENTITY ID) +{ + switch(ID) + { + case Blue: + { + return BlueInit; + } + case Red: + { + return RedInit; + break; + } + case Pink: + { + return PinkInit; + break; + } + case Orange: + { + return OrangeInit; + break; + } + } +} + +PositionHandler::~PositionHandler() +{ + //dtor +} diff --git a/test-source-code/PositionHandler.h b/test-source-code/PositionHandler.h new file mode 100644 index 0000000..bf77beb --- /dev/null +++ b/test-source-code/PositionHandler.h @@ -0,0 +1,37 @@ +#ifndef POSITIONHANDLER_H +#define POSITIONHANDLER_H +#include "Datatypes.h" + +#include + +using namespace std; + +class PositionHandler +{ + public: + PositionHandler(); + ~PositionHandler(); + void Init_StorePos(string Filename_); + void setStorePos(int r, int c, int ChangeNum); + void setSpritePos(int x, int y, IDENTITY ID); + void SetInit(IDENTITY ID, int r, int c); + int getStorePos(int r, int c); + Coordinates getCoordinates(IDENTITY ID); + Coordinates GetInit(IDENTITY ID); + + private: + int StorePos[22][19]; + + Coordinates PacPos; + Coordinates BluePos; + Coordinates RedPos; + Coordinates OrangePos; + Coordinates PinkPos; + + Coordinates BlueInit; + Coordinates PinkInit; + Coordinates RedInit; + Coordinates OrangeInit; +}; + +#endif // POSITIONHANDLER_H diff --git a/test-source-code/eater.cpp b/test-source-code/eater.cpp new file mode 100644 index 0000000..2f161be --- /dev/null +++ b/test-source-code/eater.cpp @@ -0,0 +1,68 @@ +#include "eater.h" +#include "Datatypes.h" + +Eater::Eater() +{ + //ctor +} + +bool Eater::GhostEatPac(Movement& Track) +{ + Coordinates PacMan; + PacMan = Track.getCoordinates(Pac); + + if(PacMan.x == Track.getCoordinates(Blue).x && PacMan.y == Track.getCoordinates(Blue).y && Track.getGhostMode(Blue) == false) + { + return true; + } + if(PacMan.x == Track.getCoordinates(Red).x && PacMan.y == Track.getCoordinates(Red).y && Track.getGhostMode(Red) == false) + { + return true; + } + if(PacMan.x == Track.getCoordinates(Pink).x && PacMan.y == Track.getCoordinates(Pink).y && Track.getGhostMode(Pink) == false) + { + return true; + } + if(PacMan.x == Track.getCoordinates(Orange).x && PacMan.y == Track.getCoordinates(Orange).y && Track.getGhostMode(Orange) == false) + { + return true; + } + return false; +} + +IDENTITY Eater::PacEatGhost(Movement& Track) +{ + Coordinates PacMan; + PacMan = Track.getCoordinates(Pac); + + if(PacMan.x == Track.getCoordinates(Blue).x && PacMan.y == Track.getCoordinates(Blue).y && Track.getGhostMode(Blue) == true) + { + Track.setSpritePos(Track.GetInit(Blue).x,Track.GetInit(Blue).y,Blue); + Track.TurnGhostModeOff(Blue,false); + return Blue; + } + if(PacMan.x == Track.getCoordinates(Red).x && PacMan.y == Track.getCoordinates(Red).y && Track.getGhostMode(Red) == true) + { + Track.setSpritePos(Track.GetInit(Red).x,Track.GetInit(Red).y,Red); + Track.TurnGhostModeOff(Red,false); + return Red; + } + if(PacMan.x == Track.getCoordinates(Pink).x && PacMan.y == Track.getCoordinates(Pink).y && Track.getGhostMode(Pink) == true) + { + Track.setSpritePos(Track.GetInit(Pink).x,Track.GetInit(Pink).y,Pink); + Track.TurnGhostModeOff(Pink,false); + return Pink; + } + if(PacMan.x == Track.getCoordinates(Orange).x && PacMan.y == Track.getCoordinates(Orange).y && Track.getGhostMode(Orange) == true) + { + Track.setSpritePos(Track.GetInit(Orange).x,Track.GetInit(Orange).y,Orange); + Track.TurnGhostModeOff(Orange,false); + return Orange; + } + return Pac; +} + +Eater::~Eater() +{ + //dtor +} diff --git a/test-source-code/eater.h b/test-source-code/eater.h new file mode 100644 index 0000000..3399eb3 --- /dev/null +++ b/test-source-code/eater.h @@ -0,0 +1,16 @@ +#ifndef EATER_H +#define EATER_H +#include "Movement.h" +#include "Datatypes.h" + +class Eater +{ + public: + Eater(); + ~Eater(); + + bool GhostEatPac(Movement& Track); + IDENTITY PacEatGhost(Movement& Track); +}; + +#endif // EATER_H diff --git a/test-source-code/main.cpp b/test-source-code/main.cpp new file mode 100644 index 0000000..0e9d6cf --- /dev/null +++ b/test-source-code/main.cpp @@ -0,0 +1,293 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "Game.h" +#include "eater.h" +#include "Ghosts.h" +#include "Movement.h" +#include "PositionHandler.h" +#include "Datatypes.h" +#include + +#include +using namespace sf; + +////////////////INITIALISATION TESTS///////////////// +TEST_CASE("Set Position Function sets the correct position for Pac") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.setSpritePos(7, 14, Pac); + CHECK(Moving.getCoordinates(Pac).x == 7); + CHECK(Moving.getCoordinates(Pac).y == 14); +} +TEST_CASE("Set Position Function sets the correct position for Ghost") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.setSpritePos(19, 8, Blue); + CHECK(Moving.getCoordinates(Blue).x == 19); + CHECK(Moving.getCoordinates(Blue).y == 8); +} +TEST_CASE("Get Position Function Returns Correct Positons") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.setSpritePos(10, 6, Pac); + CHECK(Moving.getCoordinates(Pac).x == 10); + CHECK(Moving.getCoordinates(Pac).y == 6); +} +TEST_CASE("PacMan Spawns in the correct Position") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + CHECK(Moving.getCoordinates(Pac).x == 16); + CHECK(Moving.getCoordinates(Pac).y == 9); +} +TEST_CASE("BlueGhost Spawns in the correct Position") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + CHECK(Moving.getCoordinates(Blue).x == 8); + CHECK(Moving.getCoordinates(Blue).y == 8); +} +TEST_CASE("RedGhost Spawns in the correct Position") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + CHECK(Moving.getCoordinates(Red).x == 8); + CHECK(Moving.getCoordinates(Red).y == 10); +} +TEST_CASE("OrangeGhost Spawns in the correct Position") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + CHECK(Moving.getCoordinates(Orange).x == 11); + CHECK(Moving.getCoordinates(Orange).y == 8); +} +TEST_CASE("PinkGhost Spawns in the correct Position") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + CHECK(Moving.getCoordinates(Pink).x == 11); + CHECK(Moving.getCoordinates(Pink).y == 10); +} +TEST_CASE("Get TotalFood that user has to reach to win") +{ + Game Play; + CHECK(Play.getTotalFood()== 167); +} + +////////////////////////CHECKING FUNCTIONS FOR MOVEMENT/////////////////////// +TEST_CASE("Getting Next co-ordinates in specified direction for pacman works") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + CHECK(Moving.GetNextCoord(Right, Pac).x == 16); + CHECK(Moving.GetNextCoord(Right, Pac).y == 10); +} +TEST_CASE("Getting Next co-ordinates in specified direction for Ghost works") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + CHECK(Moving.GetNextCoord(Left, Red).x == 8); + CHECK(Moving.GetNextCoord(Left, Red).y == 9); +} +TEST_CASE("Check wall returns Texture when looking all around pacman/ghost") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + CHECK(Moving.CheckWalLock(Up, Pac)== Wall); + CHECK(Moving.CheckWalLock(Down, Pac)== Wall); + CHECK(Moving.CheckWalLock(Right, Pac)== Food); + CHECK(Moving.CheckWalLock(Left, Pac)== Food); +} +TEST_CASE("Moving PacMan left, right, up and down if conditions are met") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.setSpritePos(4, 17, Pac); + Moving.MovePac(Left); + CHECK(Moving.getCoordinates(Pac).y == 16); + Moving.MovePac(Right); + CHECK(Moving.getCoordinates(Pac).y == 17); + Moving.MovePac(Down); + CHECK(Moving.getCoordinates(Pac).x == 5); + Moving.MovePac(Up); + CHECK(Moving.getCoordinates(Pac).x == 4); +} +TEST_CASE("Not Moving PacMan left, right, up and down if conditions are not met") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + + Moving.setSpritePos(6, 16, Pac); + Moving.MovePac(Left); //Cannot move left because of Wall + CHECK(Moving.getCoordinates(Pac).y == 16); + CHECK(Moving.getCoordinates(Pac).x == 6); + + Moving.MovePac(Down); //Cannot move down because of Wall + CHECK(Moving.getCoordinates(Pac).y == 16); + CHECK(Moving.getCoordinates(Pac).x == 6); + + Moving.MovePac(Up); //Cannot move up because of wall + CHECK(Moving.getCoordinates(Pac).y == 16); + CHECK(Moving.getCoordinates(Pac).x == 6); + + Moving.setSpritePos(4, 2, Pac); //Cannot move right because of Lock + Moving.MovePac(Right); + CHECK(Moving.getCoordinates(Pac).y == 2); + CHECK(Moving.getCoordinates(Pac).x == 4); + + +} +TEST_CASE("Moving Ghost left, right, up and down if conditions are met") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Ghosts Lisa = Play.getGhostObj(); + + Lisa.OrangeGhostMoves(Moving, Left); + CHECK(Moving.getCoordinates(Orange).x == 11); + CHECK(Moving.getCoordinates(Orange).y == 7); + + Moving.setSpritePos(15, 4, Orange); + Lisa.OrangeGhostMoves(Moving, Up); + CHECK(Moving.getCoordinates(Orange).x == 14); + CHECK(Moving.getCoordinates(Orange).y == 4); + + Moving.setSpritePos(14, 4, Orange); + Lisa.OrangeGhostMoves(Moving, Right); + CHECK(Moving.getCoordinates(Orange).x == 14); + CHECK(Moving.getCoordinates(Orange).y == 5); + + Moving.setSpritePos(14, 4, Orange); + Lisa.OrangeGhostMoves(Moving, Down); + CHECK(Moving.getCoordinates(Orange).x == 15); + CHECK(Moving.getCoordinates(Orange).y == 4); +} +TEST_CASE("Ghost cannot move backward when not next to locks") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Ghosts Lisa = Play.getGhostObj(); + + Lisa.OrangeGhostMoves(Moving, Left); + Lisa.OrangeGhostMoves(Moving, Right); + CHECK(Moving.getCoordinates(Orange).y != 8); + + Moving.setSpritePos(15, 8, Orange); + Lisa.OrangeGhostMoves(Moving, Up); + Lisa.OrangeGhostMoves(Moving, Down); + CHECK(Moving.getCoordinates(Orange).x != 15); +} +TEST_CASE("Ghost can move backward when next to locks") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Ghosts Lisa = Play.getGhostObj(); + + Moving.setSpritePos(4, 17, Orange); + Lisa.OrangeGhostMoves(Moving, Left); + Lisa.OrangeGhostMoves(Moving, Right); + CHECK(Moving.getCoordinates(Orange).y == 17); +} + +//////////////////////Modes (Super and Power)//////////////////// +TEST_CASE("Pacman eats power pellet and activates ghostMode, ghost scale changes") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.setStorePos(16, 1, 6); + Moving.setSpritePos(16, 2, Pac); + TEXTURE Tex = Moving.MovePac(Left); + CHECK(Tex == Power); + Play.ChangesAfterPelletEaten(Tex); + Moving = Play.getMovementObj(); + CHECK(Moving.getGhostMode(Blue)==true); + CHECK(Moving.getGhostMode(Red)==true); + CHECK(Moving.getGhostMode(Orange)==true); + CHECK(Moving.getGhostMode(Pink)==true); + + Sprite Lipa = Play.getSprite(Red); + CHECK(Lipa.getScale().x == 0.18f); + CHECK(Lipa.getScale().y == 0.16f); +} +TEST_CASE("Pacman eats super pellet and activates SuperPacManMode, Ghost scale changes") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.setSpritePos(4, 8, Pac); + TEXTURE Tex = Moving.MovePac(Right); + CHECK(Tex == Super); + Play.ChangesAfterPelletEaten(Tex); + Sprite Lira = Play.getSprite(Blue); + CHECK(Lira.getScale().x == 0.05f); + CHECK(Lira.getScale().y == 0.11f); +} + +////////////////////////////PacMan and Ghosts Eating////////////////////////////////////////// +TEST_CASE("Ghosts Eat PacMan - No ghost mode or super mode") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.setSpritePos(15,10, Red); + Moving.setSpritePos(15,10, Pac); + Eater Eats = Play.getEaterObj(); + CHECK(Eats.GhostEatPac(Moving)); +} +TEST_CASE("Pac Eats Ghosts - Ghostmode") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.TurnGhostMode(true); + Moving.setSpritePos(15,10, Red); + Moving.setSpritePos(15,10, Pac); + Eater Eats = Play.getEaterObj(); + CHECK_FALSE(Eats.GhostEatPac(Moving)); + CHECK(Eats.PacEatGhost(Moving) == Red); +} +TEST_CASE("Ghosts return to spawning point when Pacman Eats Ghost during ghostmode") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.TurnGhostMode(true); + Moving.setSpritePos(15,10, Red); + Moving.setSpritePos(15,10, Pac); + Eater Eats = Play.getEaterObj(); + Eats.PacEatGhost(Moving); + CHECK(Moving.getCoordinates(Red).x == 8); + CHECK(Moving.getCoordinates(Red).y == 10); +} +TEST_CASE("Ghosts return to normal when Pacman Eats Ghost during ghostmode while others stay in ghostmode") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.TurnGhostMode(true); + Moving.setSpritePos(15,10, Red); + Moving.setSpritePos(15,10, Pac); + Eater Eats = Play.getEaterObj(); + Eats.PacEatGhost(Moving); + CHECK(Moving.getGhostMode(Red)==false); + CHECK(Moving.getGhostMode(Blue)==true); + CHECK(Moving.getGhostMode(Orange)==true); + CHECK(Moving.getGhostMode(Pink)==true); +} +TEST_CASE("PacMan eats key and unlocks locks") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.setSpritePos(4,5,Pac); + TEXTURE Tex = Moving.MovePac(Left); + CHECK(Tex == Key); + Play.ChangesAfterPelletEaten(Tex); + Moving = Play.getMovementObj(); + CHECK(Moving.getStorePos(Moving.getCoordinates(Pac).x, Moving.getCoordinates(Pac).y) == 2); +} +TEST_CASE("SuperPacMan unlocks locks without Key") +{ + Game Play; + Movement Moving = Play.getMovementObj(); + Moving.TurnSuperMode(true); + Moving.setSpritePos(1,4,Pac); + CHECK(Moving.MovePac(Up)==Left); +}