WPF C# Tutorial – Make a PAC MAN Game in Visual Studio
- Subject: WPF C# Tutorials
- Learning Time: 2 hours
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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
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(); } } } |