WPF C# Tutorial – Make a PAC MAN Game in Visual Studio

In this tutorial we will make a fun PAC man game in visual studio using WPF and C# programming. This will be a fun game to make inside of visual studio because we will use all stock components of WPF and we will not be using any third-party libraries to make this game. We will be using lots and lots of rectangles and use C# identify and interact with different ones through out this game. We will custom code all of the elements inside of this game including the Pac man movements, wall collision, collision with ghosts and ghosts’ movements. The main objective of this game is to collect all of the coins inside of it and avoid the ghosts.

We will create solid walls inside of this game so you will not be able to go over them in all 4 directions, so if the player is moving left and hits the wall then it stop, if the player is moving right and hits the wall it stop and so on. Also you we will program the collision between the coins and the ghosts. This will be a fun tutorial so lets get started

Lesson Objectives –

  • Make a simple PAC Man Game in Visual Studio Using WPF and C#
  • Move Pac man character in one direction at a time
  • Stop the pac man character from going over the walls in all 4 direction
  • Collect coins in the game
  • Collide with Ghosts
  • Animate ghosts going left to right inside of the game loop
  • Restart the game when player collides with the ghosts or collects all the coins

Video Tutorial

Download The Images for this game

 

XAML Code –

<Window x:Class="PAC_Man_Game_WPF_MOO_ICT.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PAC_Man_Game_WPF_MOO_ICT"
        mc:Ignorable="d"
        Title="PAC Man Game WPF MOO ICT" Height="600" Width="800">
    <Canvas Name="MyCanvas" KeyDown="CanvasKeyDown" Focusable="True" Background="Black">

        <Label FontSize="20" Name="txtScore" Foreground="White" Content="Score: 0" />

        <Rectangle Name="pacman" Width="30" Height="30" Fill="Yellow" Canvas.Left="50" Canvas.Top="104" />
        <Rectangle Name="redGuy" Tag="ghost" Width="30" Height="30" Fill="Red" Canvas.Left="173" Canvas.Top="29" />
        <Rectangle Name="orangeGuy" Tag="ghost" Width="30" Height="30" Fill="Orange" Canvas.Left="651" Canvas.Top="104" />
        <Rectangle Name="pinkGuy" Tag="ghost" Width="30" Height="30" Fill="Pink" Canvas.Left="173" Canvas.Top="404" />


        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="20" Width="578" Canvas.Left="142" Canvas.Top="70" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="20" Width="402" Canvas.Left="231" Canvas.Top="159" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="20" Width="124" Canvas.Left="142" Canvas.Top="339" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="20" Width="124" Canvas.Left="142" Canvas.Top="467" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="20" Width="124" Canvas.Left="596" Canvas.Top="467" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="20" Width="124" Canvas.Left="596" Canvas.Top="339" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="187" Width="20" Canvas.Left="142" Canvas.Top="155" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="187" Width="20" Canvas.Left="700" Canvas.Top="155" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="109" Width="20" Canvas.Left="613" Canvas.Top="178" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="109" Width="20" Canvas.Left="231" Canvas.Top="178" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="82" Width="20" Canvas.Left="142" Canvas.Top="485" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="82" Width="20" Canvas.Left="700" Canvas.Top="485" />
        <Rectangle Tag="wall" Stroke="Aqua" StrokeThickness="3" Height="82" Width="76" Canvas.Left="395" Canvas.Top="487" />


        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="204" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="160" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="278" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="241" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="378" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="325" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="482" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="421" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="266" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="266" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="298" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="298" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="325" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="325" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="357" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="357" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="383" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="383" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="410" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="410" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="437" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="437" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="469" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="469" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="497" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="497" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="529" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="529" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="556" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="556" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="588" Canvas.Top="248" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="588" Canvas.Top="200" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="187" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="143" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="261" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="224" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="361" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="308" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="465" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="404" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="557" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="520" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="657" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="604" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="700" Canvas.Top="49" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="530" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="634" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="574" Canvas.Top="134" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="187" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="143" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="261" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="224" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="361" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="308" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="465" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="404" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="557" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="520" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="657" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="604" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="700" Canvas.Top="404" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="504" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="504" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="531" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="531" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="563" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="563" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="592" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="592" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="619" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="619" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="651" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="651" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="204" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="204" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="230" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="230" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="262" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="262" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="292" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="292" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="318" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="318" Canvas.Top="510" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="350" Canvas.Top="544" />
        <Rectangle Tag="coin" Height="10" Width="10" Fill="Gold" Canvas.Left="350" Canvas.Top="510" />



    </Canvas>
</Window>

 

 

 

C# Code for the game – All the lines are commented and explained inside of the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Threading; // import the threading name space to use the dispatcher time

namespace PAC_Man_Game_WPF_MOO_ICT
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        DispatcherTimer gameTimer = new DispatcherTimer(); // create a new instance of the dispatcher timer called game timer

        bool goLeft, goRight, goDown, goUp; // 4 boolean created to move player in 4 direction
        bool noLeft, noRight, noDown, noUp; // 4 more boolean created to stop player moving in that direction

        int speed = 8; // player speed

        Rect pacmanHitBox; // player hit box, this will be used to check for collision between player to walls, ghost and coints

        int ghostSpeed = 10; // ghost image speed
        int ghostMoveStep = 160; // ghost step limits
        int currentGhostStep; // current movement limit for the ghosts
        int score = 0; // score keeping integer



        public MainWindow()
        {
            InitializeComponent();

            GameSetUp(); // run the game set up function
        }

        private void CanvasKeyDown(object sender, KeyEventArgs e)
        {
            // this is the key down event

            if (e.Key == Key.Left && noLeft == false)
            {
                // if the left key is down and the boolean noLeft is set to false
                goRight = goUp = goDown = false; // set rest of the direction booleans to false
                noRight = noUp = noDown = false; // set rest of the restriction boolean to false

                goLeft = true; // set go left true

                pacman.RenderTransform = new RotateTransform(-180, pacman.Width /2, pacman.Height / 2); // rotate the pac man image to face left
            }

            if (e.Key == Key.Right && noRight == false)
            {
                // if the right key pressed and no right boolean is false
                noLeft = noUp = noDown = false; // set rest of the direction boolean to false
                goLeft = goUp = goDown = false; // set rest of the restriction boolean to false

                goRight = true; // set go right to true

                pacman.RenderTransform = new RotateTransform(0, pacman.Width / 2, pacman.Height / 2); // rotate the pac man image to face right

            }

            if (e.Key == Key.Up && noUp == false)
            {
                // if the up key is pressed and no up is set to false
                noRight = noDown = noLeft = false; // set rest of the direction boolean to false
                goRight = goDown = goLeft = false; // set rest of the restriction boolean to false

                goUp = true; // set go up to true

                pacman.RenderTransform = new RotateTransform(-90, pacman.Width / 2, pacman.Height / 2); // rotate the pac man character to face up
            }

            if (e.Key == Key.Down && noDown == false)
            {
                // if the down key is press and the no down boolean is false
                noUp = noLeft = noRight = false; // set rest of the direction boolean to false
                goUp = goLeft = goRight = false; // set rest of the restriction boolean to false

                goDown = true; // set go down to true

                pacman.RenderTransform = new RotateTransform(90, pacman.Width / 2, pacman.Height / 2); // rotate the pac man character to face down
            }
        }

        private void GameSetUp()
        {
            // this function will run when the program loads

            MyCanvas.Focus(); // set my canvas as the main focus for the program

            gameTimer.Tick += GameLoop; // link the game loop event to the time tick
            gameTimer.Interval = TimeSpan.FromMilliseconds(20); // set time to tick every 20 milliseconds
            gameTimer.Start(); // start the time
            currentGhostStep = ghostMoveStep; // set current ghost step to the ghost move step

            // below pac man and the ghosts images are being imported from the images folder and then we are assigning the image brush to the rectangles
            ImageBrush pacmanImage = new ImageBrush();
            pacmanImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/pacman.jpg"));
            pacman.Fill = pacmanImage;

            ImageBrush redGhost = new ImageBrush();
            redGhost.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/red.jpg"));
            redGuy.Fill = redGhost;

            ImageBrush orangeGhost = new ImageBrush();
            orangeGhost.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/orange.jpg"));
            orangeGuy.Fill = orangeGhost;

            ImageBrush pinkGhost = new ImageBrush();
            pinkGhost.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/pink.jpg"));
            pinkGuy.Fill = pinkGhost;


        }

        private void GameLoop(object sender, EventArgs e)
        {

            // this is the game loop event, this event will control all of the movements, outcome, collision and score for the game

            txtScore.Content = "Score: " + score; // show the scoreo to the txtscore label. 

            // start moving the character in the movement directions

            if (goRight)
            {
                // if go right boolean is true then move pac man to the right direction by adding the speed to the left 
                Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) + speed);
            }
            if (goLeft)
            {
                // if go left boolean is then move pac man to the left direction by deducting the speed from the left
                Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) - speed);
            }
            if (goUp)
            {
                // if go up boolean is true then deduct the speed integer from the top position of the pac man
                Canvas.SetTop(pacman, Canvas.GetTop(pacman) - speed);
            }
            if (goDown)
            {
                // if go down boolean is true then add speed integer value to the pac man top position
                Canvas.SetTop(pacman, Canvas.GetTop(pacman) + speed);
            }
            // end the movement 


            // restrict the movement
            if (goDown && Canvas.GetTop(pacman) + 80 > Application.Current.MainWindow.Height)
            {
                // if pac man is moving down the position of pac man is grater than the main window height then stop down movement
                noDown = true;
                goDown = false;
            }
            if (goUp && Canvas.GetTop(pacman) < 1)
            {
                // is pac man is moving and position of pac man is less than 1 then stop up movement
                noUp = true;
                goUp = false;
            }
            if (goLeft && Canvas.GetLeft(pacman) - 10 < 1)
            {
                // if pac man is moving left and pac man position is less than 1 then stop moving left
                noLeft = true;
                goLeft = false;
            }
            if (goRight && Canvas.GetLeft(pacman) + 70 > Application.Current.MainWindow.Width)
            {
                // if pac man is moving right and pac man position is greater than the main window then stop moving right
                noRight = true;
                goRight = false;
            }

            pacmanHitBox = new Rect(Canvas.GetLeft(pacman), Canvas.GetTop(pacman), pacman.Width, pacman.Height); // asssign the pac man hit box to the pac man rectangle

            // below is the main game loop that will scan through all of the rectangles available inside of the game
            foreach (var x in MyCanvas.Children.OfType<Rectangle>())
            {
                // loop through all of the rectangles inside of the game and identify them using the x variable

                Rect hitBox = new Rect(Canvas.GetLeft(x), Canvas.GetTop(x), x.Width, x.Height); // create a new rect called hit box for all of the available rectangles inside of the game

                // find the walls, if any of the rectangles inside of the game has the tag wall inside of it
                if ((string)x.Tag == "wall")
                {
                    // check if we are colliding with the wall while moving left if true then stop the pac man movement
                    if (goLeft == true && pacmanHitBox.IntersectsWith(hitBox))
                    {
                        Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) + 10);
                        noLeft = true;
                        goLeft = false;
                    }
                    // check if we are colliding with the wall while moving right if true then stop the pac man movement
                    if (goRight == true && pacmanHitBox.IntersectsWith(hitBox))
                    {
                        Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) - 10);
                        noRight = true;
                        goRight = false;
                    }
                    // check if we are colliding with the wall while moving down if true then stop the pac man movement
                    if (goDown == true && pacmanHitBox.IntersectsWith(hitBox))
                    {
                        Canvas.SetTop(pacman, Canvas.GetTop(pacman) - 10);
                        noDown = true;
                        goDown = false;
                    }
                    // check if we are colliding with the wall while moving up if true then stop the pac man movement
                    if (goUp == true && pacmanHitBox.IntersectsWith(hitBox))
                    {
                        Canvas.SetTop(pacman, Canvas.GetTop(pacman) + 10);
                        noUp = true;
                        goUp = false;
                    }
                }

                // check if the any of the rectangles has a coin tag inside of them
                if ((string) x.Tag == "coin")
                {
                    // if pac man collides with any of the coin and coin is still visible to the screen
                    if (pacmanHitBox.IntersectsWith(hitBox) && x.Visibility == Visibility.Visible)
                    {
                        // set the coin visiblity to hidden
                        x.Visibility = Visibility.Hidden;
                        // add 1 to the score
                        score++;
                    }
                }

                // if any rectangle has the tag ghost inside of it
                if ((string) x.Tag == "ghost")
                {
                    // check if pac man collides with the ghost 
                    if (pacmanHitBox.IntersectsWith(hitBox))
                    {
                        // if collision has happened, then end the game by calling the game over function and passing in the message
                        GameOver("Ghosts got you, click ok to play again");
                    }

                    // if there is a rectangle called orange guy in the game
                    if (x.Name.ToString() == "orangeGuy")
                    {
                        // move that rectangle to towards the left of the screen
                        Canvas.SetLeft(x, Canvas.GetLeft(x) - ghostSpeed);

                    }
                    else
                    {
                        // other ones can move towards the right of the screen
                        Canvas.SetLeft(x, Canvas.GetLeft(x) + ghostSpeed);
                    }

                    // reduce one from the current ghost step integer
                    currentGhostStep--;

                    // if the current ghost step integer goes below 1 
                    if (currentGhostStep < 1)
                    {
                        // reset the current ghost step to the ghost move step value
                        currentGhostStep = ghostMoveStep;
                        // reverse the ghost speed integer
                        ghostSpeed = -ghostSpeed;
                    }
                }
            }

            // if the player collected 85 coins in the game

            if (score == 85)
            {
                // show game over function with the you win message
                GameOver("You Win, you collected all of the coins");
            }
        }

        private void GameOver(string message)
        {
            // inside the game over function we passing in a string to show the final message to the game
            gameTimer.Stop(); // stop the game timer
            MessageBox.Show(message, "The Pac Man Game WPF MOO ICT"); // show a mesage box with the message that is passed in this function

            // when the player clicks ok on the message box
            // restart the application
            System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
            Application.Current.Shutdown();
        }
    }
}

 

 

 




Comments are closed.