/
MainWindow.xaml.cs
239 lines (193 loc) · 7.59 KB
/
MainWindow.xaml.cs
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
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace PacManGame
{
public partial class MainWindow : Window
{
DispatcherTimer gameTimer = new DispatcherTimer();
bool goLeft, goRight, goDown, goUp;
bool noLeft, noRight, noDown, noUp;
int speed = 8;
Rect pacmanHitBox; //player hit box this will be used to check for collision between player to walls, ghost and coints
int ghostSpeed = 10;
int ghostMoveStep = 160;
int currentGhostStep;
int score = 0;
public MainWindow()
{
InitializeComponent();
GameSetUp();
}
private void CanvasKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Left && noLeft == false)
{
goRight = goUp = goDown = false;
noRight = noUp = noDown = false;
goLeft = true;
pacman.RenderTransform = new RotateTransform(-180, pacman.Width / 2, pacman.Height / 2);
}
if (e.Key == Key.Right && noRight == false)
{
noLeft = noUp = noDown = false;
goLeft = goUp = goDown = false;
goRight = true;
pacman.RenderTransform = new RotateTransform(0, pacman.Width / 2, pacman.Height / 2);
}
if (e.Key == Key.Up && noUp == false)
{
noRight = noDown = noLeft = false;
goRight = goDown = goLeft = false;
goUp = true;
pacman.RenderTransform = new RotateTransform(-90, pacman.Width / 2, pacman.Height / 2);
}
if (e.Key == Key.Down && noDown == false)
{
noUp = noLeft = noRight = false;
goUp = goLeft = goRight = false;
goDown = true;
pacman.RenderTransform = new RotateTransform(90, pacman.Width / 2, pacman.Height / 2);
}
}
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);
gameTimer.Start();
currentGhostStep = ghostMoveStep;
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)
{
txtScore.Content = "Score: " + score;
if (goRight)
{
Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) + speed);
}
if (goLeft)
{
Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) - speed);
}
if (goUp)
{
Canvas.SetTop(pacman, Canvas.GetTop(pacman) - speed);
}
if (goDown)
{
Canvas.SetTop(pacman, Canvas.GetTop(pacman) + speed);
}
if (goDown && Canvas.GetTop(pacman) + 80 > Application.Current.MainWindow.Height)
{
noDown = true;
goDown = false;
}
if (goUp && Canvas.GetTop(pacman) < 1)
{
noUp = true;
goUp = false;
}
if (goLeft && Canvas.GetLeft(pacman) - 10 < 1)
{
noLeft = true;
goLeft = false;
}
if (goRight && Canvas.GetLeft(pacman) + 70 > Application.Current.MainWindow.Width)
{
noRight = true;
goRight = false;
}
pacmanHitBox = new Rect(Canvas.GetLeft(pacman), Canvas.GetTop(pacman), pacman.Width, pacman.Height);
foreach (var x in MyCanvas.Children.OfType<Rectangle>())
{
Rect hitBox = new Rect(Canvas.GetLeft(x), Canvas.GetTop(x), x.Width, x.Height);
if ((string)x.Tag == "wall")
{
if (goLeft == true && pacmanHitBox.IntersectsWith(hitBox))
{
Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) + 10);
noLeft = true;
goLeft = false;
}
if (goRight == true && pacmanHitBox.IntersectsWith(hitBox))
{
Canvas.SetLeft(pacman, Canvas.GetLeft(pacman) - 10);
noRight = true;
goRight = false;
}
if (goDown == true && pacmanHitBox.IntersectsWith(hitBox))
{
Canvas.SetTop(pacman, Canvas.GetTop(pacman) - 10);
noDown = true;
goDown = false;
}
if (goUp == true && pacmanHitBox.IntersectsWith(hitBox))
{
Canvas.SetTop(pacman, Canvas.GetTop(pacman) + 10);
noUp = true;
goUp = false;
}
}
if ((string)x.Tag == "coin")
{
if (pacmanHitBox.IntersectsWith(hitBox) && x.Visibility == Visibility.Visible)
{
x.Visibility = Visibility.Hidden;
score++;
}
}
if ((string)x.Tag == "ghost")
{
if (pacmanHitBox.IntersectsWith(hitBox))
{
GameOver("Ghosts got you!Click ok to play again!");
}
if (x.Name.ToString() == "orangeGuy")
{
Canvas.SetLeft(x, Canvas.GetLeft(x) - ghostSpeed);
}
else
{
Canvas.SetLeft(x, Canvas.GetLeft(x) + ghostSpeed);
}
currentGhostStep--;
if (currentGhostStep < 1)
{
currentGhostStep = ghostMoveStep;
ghostSpeed = -ghostSpeed;
}
}
}
if (score == 85)
{
GameOver("You Win, you collected all of the coins");
}
}
private void GameOver(string message)
{
gameTimer.Stop();
MessageBox.Show(message, "The Pac Man Game WPF MOO ICT");
System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
Application.Current.Shutdown();
}
}
}