-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
285 lines (231 loc) · 7.9 KB
/
Form1.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;
//Rules of the Game –
//Player can move up down left and right//
//Player will collect coins – collect all coin and you WIN the game//
//Player cannot touch Wall or Ghost. If they do GAME OVER//
//2 (Red and Yellow)ghost will have static left to right movement//
//1 (PINK) ghost will have a random movement which will scale across the form.//
//When the game is over there will be a game over text or when the player won the game it will show You WIN.//
namespace Ms_PacMan_Pc
{
public partial class Form1 : Form
{
// start the variables
bool goup, godown, goleft, goright, isGameOver;
int score, playerSpeed, redGhostSpeed, yellowGhostSpeed, pinkGhostX, pinkGhostY;
public Form1()
{
InitializeComponent();
resetGame();
}
//In this function we are tracking 4 keys. Up, down, let and right. When either of these keys are pressed by the user we change the directional images of pacman dynamically so resemble the movement.//
private void keyisdown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
goup = true;
}
if (e.KeyCode == Keys.Down)
{
godown = true;
}
if (e.KeyCode == Keys.Left)
{
goleft = true;
}
if (e.KeyCode == Keys.Right)
{
goright = true;
}
}
//In this key up function we are checking for the left, right, up and down keys again.//
//Once the user has pressed them and left them we can turn those Booleans to false.//
private void keyisup(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
goup = false;
}
if (e.KeyCode == Keys.Down)
{
godown = false;
}
if (e.KeyCode == Keys.Left)
{
goleft = false;
}
if (e.KeyCode == Keys.Right)
{
goright = false;
}
if (e.KeyCode == Keys.Enter && isGameOver == true)
{
resetGame();
}
}
private void mainGameTimer(object sender, EventArgs e)
{
txtScore.Text = "Score: " + score; // show the score on the board
//player movement codes start//
//IF key code is LEFT//
//Go left is set to true//
//Change pac man image to LEFT//
if (goleft == true)
{
mspacman.Left -= playerSpeed;
mspacman.Image = Properties.Resources.left;
//moving player to the left.
}
//IF key code is RIGHT//
//Go right is set to true//
//Change pac man image to RIGHT//
if (goright == true)
{
mspacman.Left += playerSpeed;
mspacman.Image = Properties.Resources.right;
//moving player to the right
}
//IF key code is DOWN//
//Go down is set to true//
//Change pac man image to down//
if (godown == true)
{
mspacman.Top += playerSpeed;
mspacman.Image = Properties.Resources.down;
//moving down
}
//IF key code is UP//
//Go up is set to true//
//Change pac man image to UP//
if (goup == true)
{
mspacman.Top -= playerSpeed;
mspacman.Image = Properties.Resources.Up;
//moving to the top
}
//teleporting to each side ability//
if (mspacman.Left < -10)
{
mspacman.Left = 680;
}
if (mspacman.Left > 680)
{
mspacman.Left = -10;
}
if (mspacman.Top < -10)
{
mspacman.Top = 550;
}
if (mspacman.Top > 550)
{
mspacman.Top = 0;
}
//teleporting ability ends//
//player movements code end//
//coin section//
foreach (Control x in this.Controls)
{
if (x is PictureBox)
{
//checking if the player hits the coin picturebox then we can add to the score//
if ((string)x.Tag == "coin" && x.Visible == true)
{
if (mspacman.Bounds.IntersectsWith(x.Bounds))
{
score += 1;
x.Visible = false;
}
}
//wall check section//
if((string)x.Tag == "wall")
{
if(mspacman.Bounds.IntersectsWith (x.Bounds))
{
gameOver("You Lose :(");
}
if (pinkGhost.Bounds.IntersectsWith(x.Bounds))
{
pinkGhostX = -pinkGhostX;
}
}
//ghost check//
if((string)x.Tag == "ghost")
{
if (mspacman.Bounds.IntersectsWith(x.Bounds))
{
gameOver("You Lose :(");
}
}
}
}
//moving ghosts//
redGhost.Left += redGhostSpeed;
if (redGhost.Bounds.IntersectsWith(pictureBox1.Bounds) || redGhost.Bounds.IntersectsWith(pictureBox2.Bounds))
{
redGhostSpeed = -redGhostSpeed;
}
yellowGhost.Left -= yellowGhostSpeed;
if (yellowGhost.Bounds.IntersectsWith(pictureBox3.Bounds) || yellowGhost.Bounds.IntersectsWith(pictureBox4.Bounds))
{
yellowGhostSpeed = -yellowGhostSpeed;
}
pinkGhost.Left -= pinkGhostX;
pinkGhost.Top -= pinkGhostY;
if (pinkGhost.Top < 0 || pinkGhost.Top > 520)
{
pinkGhostY = -pinkGhostY ;
}
if (pinkGhost.Left < 0 || pinkGhost.Left >620)
{
pinkGhostX= -pinkGhostX ;
}
//Score//
if (score == 51)
{
gameOver("You Win!");
}
}
private void resetGame()
{
txtScore.Text = "Score: 0";
score= 0;
redGhostSpeed = 5;
yellowGhostSpeed = 5;
pinkGhostX= 5;
pinkGhostY= 5;
playerSpeed= 8;
isGameOver= false;
mspacman.Left = 33;
mspacman.Top = 67;
redGhost.Left = 196;
redGhost.Top = 67;
yellowGhost.Left = 455;
yellowGhost.Top = 397;
pinkGhost.Left = 548;
pinkGhost.Top = 236;
foreach (Control x in this.Controls)
{
if (x is PictureBox)
{
x.Visible = true;
}
}
gameTimer.Start();
}
private void gameOver(string message)
{
isGameOver = true;
gameTimer.Stop();
txtScore.Text = "Score: " + score + Environment.NewLine + message;
}
}
}