Pac Man

The youtube link is here:

Pac Man is my first little game as Full Sail Game Development student. It’s the programming 1 class final project, and it’s a C++ console style game. The game is used class aspect to make this game easier, including dynamic new array function to make the ghost array. The game puzzle pattern is by instructor, and the music is by school free Westar resource.

Here is the sample code at main.cpp

</pre>
&nbsp;

 // TODO Part 1: Create player
 COORD playerStart = CreateCoord(27, 17);
 //Player player(playerStart, name, maze);
 Player player(playerStart, name, maze);
 player.setDot(dotNumber);
 // TODO Part 2: Create ghosts
 COORD startPos[] = { { 27, 11 }, { 23, 14 }, { 27, 14 }, { 31, 14 }, { 23, 13 }, { 27, 13 }, { 31, 13 },
 { 23, 15 }, { 27, 15 }, { 31, 15 } };
 ConsoleColor startColor[4] = { Red, Cyan, Magenta, DarkYellow };
 static int maxLevel = 0;
 int level = 3 + stage.getLevel();

 Ghost* ghost[NUM_GHOSTS] = {};

 for (int i = 0; i < 4; i++) // at this point, we did not learn linked list, or I will use it here
 {
 ghost[i] = new Ghost(startColor[i], startPos[i]);
 }

// TODO Part 1: Display HUD and reset cursor
 player.DisplayHUD();
 ResetCursor();
 // TODO Part 1: Game loop
 // Inside game loop:
 // TODO Part 1: Player input
 // TODO Part 1: Move player
 bool keepPlaying = true;

 //Play the music
 stage.music(stage.getLevel());
 Console::SetCursorPosition(26, 9);
 std::cout << "LV" << stage.getLevel();
 ResetCursor();

 while (keepPlaying)
 {

 char input = _getch();
 input = tolower(input);

 COORD temp = { 0, 0 };

 switch (input)
 {
 case 'a':
 temp.X = -2;
 temp.Y = 0;
 break;
 case 'w':
 temp.X = 0;
 temp.Y = -1;
 break;
 case 'd':
 temp.X = 2;
 temp.Y = 0;
 break;
 case 's':
 temp.X = 0;
 temp.Y = 1;
 break;
 case 'e':
 keepPlaying = false;
 break;
 default:
 break;
 }
 player.Move(temp, maze);
 bool dead = false;
 // TODO Part 3: Check collision
 bool bcheck = bCheckCollision(maze, &player, ghost, &stage);
 if (bcheck)
 dead = true;
 // TODO Part 2: Move ghosts
 bool bScared = player.GetPowerPellet();

 if (!bcheck) // player is alive, so allow ghost can move
 for (int i = 0; i < 3 + stage.getLevel(); i++)
 {
 ghost[i]->Move(maze, ghost, bScared, stage.getLevel() + 3);//using &?
 // TODO Part 3: Check collision
 bcheck = bCheckCollision(maze, &player, ghost[i], &stage);
 if (bcheck)
 dead = true;
 }

 // TODO Part 3: Handle player death
 if (dead) // player is dead
 if (player.GetLives() > 0) // still has life remain
 {
 player.reset(maze, playerStart);
 for (int i = 0; i ❤ + stage.getLevel(); i++)
 {
 int ghostPos = rand() % 9 + 1;
 if (ghost[i]->getGhostColor() == Red)
 ghost[i]->reset(maze, startPos[0]);
 else
 ghost[i]->reset(maze, startPos[ghostPos]);
 ghost[i]->Draw(false);
 }
 }
 else
 {
 //Game Over

 player.DisplayHUD();
 Console::SetCursorPosition(20,13);
 std::cout << " Game Over !!";
 Console::SetCursorPosition(21, 14);
 std::cout << 13500 << " Pts";
 stage.music(0);
 Console::SetCursorPosition(21, 15);

 break;
 }

 // TODO Part 1: Display HUD and reset cursor
 player.DisplayHUD();
 ResetCursor();

 if (player.getDot() == 0)
 { 

 if (stage.getLevel() == 5)
 {
 Console::SetCursorPosition(20, 13);
 std::cout << " Thanks U !!";
 Console::SetCursorPosition(22, 14);
 std::cout << player.getScores() << " Pts";

 break;
 }
 player.reset(maze, playerStart);
 stage.resetDot(maze);
 stage.resetPower(maze);
 int temp = stage.getNumberOfDot(maze);

 int ghostColor = rand() % 4;
 int ghostPosition = ghostColor;
 if (ghostColor != 0)
 ghost[3 + stage.getLevel()] = new Ghost(startColor[ghostColor], startPos[ghostPosition]);
 else
 ghost[3 + stage.getLevel()] = new Ghost(startColor[ghostColor], startPos[ghostPosition]);
 stage.upLevel();

 player.setDot(temp);

 //Play the music
 stage.music(stage.getLevel());

 Console::SetCursorPosition(26, 9);
 cout << "LV" <<stage.getLevel();
 ResetCursor();
 for (int i = 0; i ❤ + stage.getLevel(); i++)
 {
 int ghostPos = rand() % 9 + 1;
 if (ghost[i]->getGhostColor() == Red)
 ghost[i]->reset(maze, startPos[0]);
 else
 ghost[i]->reset(maze, startPos[ghostPos]);
 ghost[i]->Draw(false);
 }
 }
 }
 // After game loop:
 // TODO Part 1 & 2: Free memory

 for (int i = 0; i < 3 + stage.getLevel(); i++)
 {
 delete ghost[i];
 ghost[i] = nullptr;
 }

 Console::ResetColor();
 ResetCursor();

 system("pause");
 return 0;
}

bool bCheckCollision(char maze[][MAZE_COLS], Player* pPlayer, Ghost* ghost, Stage* stage)
{

if (pPlayer->GetPos().X == ghost->GetPos().X &&
pPlayer->GetPos().Y == ghost->GetPos().Y) // ghost and player collision
{
if (pPlayer->GetPowerPellet()) // If player have power pellet
{
ghost->kill(maze);
pPlayer->GhostKilled();

}
else
{
pPlayer->Kill();
return true;//player is killed, return true
}
}

return false;//player is not killed, return false
}

Leave a comment