-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
370 lines (265 loc) · 11.2 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;
using System.Timers;
using System.Drawing;
using System.Windows.Threading;
using System.Windows.Navigation;
using System.Linq;
using System.Threading.Tasks;
//Only issue is multiple warning boxes pop up
namespace AOIGameSnake
{
public partial class Window1 : Window
{
// public int numLives = Convert.ToInt32(Application.Current.Properties["NumLeft"]);
//public static int ToInt32(object value);
public int score = Convert.ToInt32(Application.Current.Properties["Score"]);
public Window1()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
/* Here user can change the speed of the snake.
* Possible speeds are FAST, MODERATE, SLOW and DAMNSLOW */
timer.Interval = MODERATE;
timer.Start();
//Application.Current.Properties["Score"] = 0;
this.KeyDown += new KeyEventHandler(OnButtonKeyDown);
paintSnake(startingPoint);
currentPosition = startingPoint;
// Instantiate Food Objects
for (int n = 0; n < 10; n++)
{
paintBonus(n);
}
}
// This list describes the Bonus Red pieces of Food on the Canvas
private List<Point> bonusPoints = new List<Point>();
// This list describes the body of the snake on the Canvas
private List<Point> snakePoints = new List<Point>();
private Brush snakeColor = Brushes.Green;
private enum SIZE
{
THIN = 4,
NORMAL = 6,
THICK = 8
};
private enum MOVINGDIRECTION
{
UPWARDS = 8,
DOWNWARDS = 2,
TOLEFT = 4,
TORIGHT = 6
};
private TimeSpan FAST = new TimeSpan(1);
private TimeSpan MODERATE = new TimeSpan(10000);
private TimeSpan SLOW = new TimeSpan(50000);
private TimeSpan DAMNSLOW = new TimeSpan(500000);
private Point startingPoint = new Point(100, 100);
private Point currentPosition = new Point();
// Movement direction initialisation
private int direction = 0;
/* Placeholder for the previous movement direction
* The snake needs this to avoid its own body. */
private int previousDirection = 0;
/* Here user can change the size of the snake.
* Possible sizes are THIN, NORMAL and THICK */
private int headSize = (int)SIZE.THICK;
private int length = 100;
//private int score = Application.Current.Properties["Score"];
private Random rnd = new Random();
private void paintSnake(Point currentposition)
{
/* This method is used to paint a frame of the snake´s body
* each time it is called. */
Ellipse newEllipse = new Ellipse();
newEllipse.Fill = snakeColor;
newEllipse.Width = headSize;
newEllipse.Height = headSize;
Canvas.SetTop(newEllipse, currentposition.Y);
Canvas.SetLeft(newEllipse, currentposition.X);
int count = paintCanvas.Children.Count;
paintCanvas.Children.Add(newEllipse);
snakePoints.Add(currentposition);
// Restrict the tail of the snake
if (count > length)
{
paintCanvas.Children.RemoveAt(count - length + 9);
snakePoints.RemoveAt(count - length);
}
}
private void paintBonus(int index)
{
Point bonusPoint = new Point(rnd.Next(5, 620), rnd.Next(5, 380));
Ellipse newEllipse = new Ellipse();
newEllipse.Fill = Brushes.Red;
newEllipse.Width = headSize;
newEllipse.Height = headSize;
Canvas.SetTop(newEllipse, bonusPoint.Y);
Canvas.SetLeft(newEllipse, bonusPoint.X);
paintCanvas.Children.Insert(index, newEllipse);
bonusPoints.Insert(index, bonusPoint);
}
public static bool IsWindowOpen<Window2>(string name = "") where Window2 : Window
{
return string.IsNullOrEmpty(name) ? Application.Current.Windows.OfType<Window2>().Any()
: Application.Current.Windows.OfType<Window2>().Any(w => w.Name.Equals(name));
}
private void timer_Tick(object sender, EventArgs e)
{
// Expand the body of the snake to the direction of movement
switch (direction)
{
case (int)MOVINGDIRECTION.DOWNWARDS:
currentPosition.Y += 1;
paintSnake(currentPosition);
break;
case (int)MOVINGDIRECTION.UPWARDS:
currentPosition.Y -= 1;
paintSnake(currentPosition);
break;
case (int)MOVINGDIRECTION.TOLEFT:
currentPosition.X -= 1;
paintSnake(currentPosition);
break;
case (int)MOVINGDIRECTION.TORIGHT:
currentPosition.X += 1;
paintSnake(currentPosition);
break;
}
// Restrict to boundaries of the Canvas
if ((currentPosition.X < 5) || (currentPosition.X > 620) ||
(currentPosition.Y < 5) || (currentPosition.Y > 380))
{
GameOver();
this.Close();
//Application.Current.Windows[1].Close();
//Application.Current.Windows[2].Close();
// this.Close();
}
// Hitting a bonus Point causes the lengthen-Snake Effect
int n = 0;
foreach (Point point in bonusPoints)
{
if ((Math.Abs(point.X - currentPosition.X) < headSize) &&
(Math.Abs(point.Y - currentPosition.Y) < headSize))
{
length += 10;
score += 10;
// In the case of food consumption, erase the food object
// from the list of bonuses as well as from the canvas
/* Window2 win2 = new Window2();
win2.Show();*/
//this.Hide();
bonusPoints.RemoveAt(n);
paintCanvas.Children.RemoveAt(n);
paintBonus(n);
break;
}
n++;
}
// Restrict hits to body of Snake
for (int q = 0; q < (snakePoints.Count - headSize * 2); q++)
{
Point point = new Point(snakePoints[q].X, snakePoints[q].Y);
if ((Math.Abs(point.X - currentPosition.X) < (headSize)) &&
(Math.Abs(point.Y - currentPosition.Y) < (headSize)))
{
GameOver();
// break;
}
}
}
private void OnButtonKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Down:
if (previousDirection != (int)MOVINGDIRECTION.UPWARDS)
direction = (int)MOVINGDIRECTION.DOWNWARDS;
break;
case Key.Up:
if (previousDirection != (int)MOVINGDIRECTION.DOWNWARDS)
direction = (int)MOVINGDIRECTION.UPWARDS;
break;
case Key.Left:
if (previousDirection != (int)MOVINGDIRECTION.TORIGHT)
direction = (int)MOVINGDIRECTION.TOLEFT;
break;
case Key.Right:
if (previousDirection != (int)MOVINGDIRECTION.TOLEFT)
direction = (int)MOVINGDIRECTION.TORIGHT;
break;
}
previousDirection = direction;
}
private void GameOver()
{
//numLives = 5 - Convert.ToInt32(Application.Current.Properties["NumLeft"]);
//numLives -= 1;
MessageBoxResult result = MessageBox.Show("Save Yourself?", "Try Again?", MessageBoxButton.YesNo, MessageBoxImage.Question);
Application.Current.Properties["Score"] = score;
//Application.Current.Properties["NumLeft"] = numLives;
if (result== MessageBoxResult.Yes)
{
Window2 win2 = new Window2();
win2.Show();
this.Close();
System.Windows.Threading.Dispatcher.Run();
}
else //if (result == MessageBoxResult.No)
{
MessageBox.Show("Your score was " + score, "Game Over!", MessageBoxButton.OK);
//Application.Current.Shutdown();
this.Close();
}
//break;
}
/*class Question
{
private string question;
//private string aoiType;
//private int questionNum;
private string image;
// private int answer;
//public Question(string q = "", /*string aoi, int num/*, int a,*/ /*string i = "")*/
/*{
this.question = q;
//this.aoiType = aoi;
//this.questionNum = num;
this.image = i;
//this.answer = a;
}
public Question()
{
question = "";
image = "";
}
public Question(string i)
{
this.image = i;
}
public string returnQuestion()
{
return question;
}
/* public int returnQuestionNum()
{
return questionNum;
}
public string returnImage()
{
return image;
}
}*/
}
}