Pac-Man

> Recreation of Pac-Man using the S2D libraries - Provided by Staffordshire University.
> My Hand-in for Fundamentals of Game and Graphical Systems Assignment



Learn More

View On Github

Project Overview

Pac-Man was my level 4 Fundamentals of Game and Graphical System Development project for semester 1. Our task was to create a very simple version of Pac-Man using the S2D libraries provided to us by Staffordshire University. To pass the module the bare minimum was to have a moving Pac-Man with simple animations which could move around an empty space collecting munchies while avoiding ghosts. However, to achieve higher marks you must add further features into the game to represent the original Pac-Man.

Additional Features - Achieve Higher Marks

  • > More Advanced Pac-Man Movement
  • > Title Screen
  • > Walls
  • > Collisions with Walls
  • > Connected Wall Textures

Code Snippets / Screenshots

Movement

Pac-Man is able to move via keyboard inputs using WASD, a small issue arose early into the project where Pac-Man would stutter while moving as the game would have to check if the key was down every tick. However, Pac-Man is supposed to move continuously in one direction until told otherwise or he hits a wall. To solve this, I created a Enum which stored which direction Pac-Man would be moving. However, to make movement more advanced I created a system to store the next keyboard input and move Pac-Man when he is next allowed to do so. This also solved very smaller issues like Pac-Man being 32px x 32px and each tile being 32px x 32px meaning a user would have to be pixel perfect to move between walls while with this system it would move Pac-Man perfectly when a space is detected in the direction the person wishes to go.

enum Direction
{
	LEFT,
	RIGHT,
	UP,
	DOWN,
	STOP
};

Direction currentDirection;
Direction nextDirection;

Pacman::Tick()
{
	MovementLogic();
	.....
}

void Pacman::MovementLogic()
{
	//Moving right
	if (keyboardState->IsKeyDown(Input::Keys::D))
	{
		nextDirection = RIGHT;
	}
	//Moving down
	else if (keyboardState->IsKeyDown(Input::Keys::S))
	{
		nextDirection = DOWN;
	}
	//Moving left
	else if (keyboardState->IsKeyDown(Input::Keys::A))
	{
		nextDirection = LEFT;
	}
	//Moving up
	else if (keyboardState->IsKeyDown(Input::Keys::W))
	{
		nextDirection = UP;
	}
}						

Title Screen

Most games have a physical title screen where the user clicks play to play the game allowing them to prepare for when the game starts as opposed to running a game and having to be prepared as soon as all the loading has completed to play the game. Because of this I decided to create a very simple title screen which composes of two buttons, one for play and one for exit, and a simple title. For the title screen I created a struct for the buttons and added features like when the button has been hovered over and when it has been clicked to allow me to perform logic to change screen or show a hovering icon over the button as shown in the screenshot to the right.

Walls

Walls are a key part in Pac-Man as a wall limits where the player can go. This ultimately can make the game harder or easier depending on how a map is created using these walls. Each wall has a x, y, width, height and texture value therefore I made a class to handle each wall as there would be multiple. Due to the game being tile based and being loaded in from a text file each wall will have the same width and height due to it being a tile map. The way the walls originally looked when each wall had been loaded in did not look very good (shown in image to left). This then led me to create a function which would check the current walls adjacent tiles and change the texture to create a much better-looking wall using connecting textures.

void MapLoader::FixWallTexture(Wall* wall, int i, int j)
{
	bool wallAbove = false,
		wallBelow = false,
		wallLeft = false,
		wallRight = false;

	if (mapArray[i - 1][j] == '#')
	{
		wallAbove = true;
	}

	if (mapArray[i + 1][j] == '#')
	{
		wallBelow = true;
	}

	if (j != 0)
	{
		if (mapArray[i][j - 1] == '#')
		{
			wallLeft = true;
		}
	}

	if (j != MAPTILEDWIDTH-1)
		{
			if (mapArray[i][j + 1] == '#')
			{
				wallRight = true;
			}
		}

	//Change the texture dependant on which walls are where
	if (wallAbove && wallBelow && wallRight && wallLeft)
	{
		wall->setTexture("Textures/Wall/WallCross.png");
	}

	else if (wallAbove && !wallBelow && wallRight && wallLeft)
	{
		wall->setTexture("Textures/Wall/WallDownT.png");
	}
	... // More logic for each combination of adjacent walls
}						

I am happy to go into further detail for this or any projects on my portfolio if needs be.

View On Github