-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
511 lines (460 loc) · 18 KB
/
MainPage.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RSA;
using System.IO.IsolatedStorage;
using System.Windows.Navigation;
namespace RightColor
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
this.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(MainPage_BackKeyPress);
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
btn1.Click += new RoutedEventHandler(btnColorPick_Click);
btn2.Click += new RoutedEventHandler(btnColorPick_Click);
btn3.Click += new RoutedEventHandler(btnColorPick_Click);
btn4.Click += new RoutedEventHandler(btnColorPick_Click);
btn5.Click += new RoutedEventHandler(btnColorPick_Click);
btn6.Click += new RoutedEventHandler(btnColorPick_Click);
anim_BarIn.Completed += new EventHandler(anim_BarIn_Completed);
anim_Correct.Completed += new EventHandler(anim_Correct_Completed);
anim_Correct_Out.Completed += new EventHandler(anim_Correct_Out_Completed);
GameTimer.Tick += new EventHandler(GameTimer_Tick);
btnStartGame.Click += new RoutedEventHandler(btnStartGame_Click);
btnStartTimedGame.Click += new RoutedEventHandler(btnStartTimedGame_Click);
btnHowTo.Click += new RoutedEventHandler(btnHowTo_Click);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// Save the ListBox control's SelectedIndex in page state
//State["Index"] = LB.SelectedIndex;
if (GameRunning)
{
State["GameRunning"] = true;
State["GameScore"] = int.Parse(txtScore.Text);
State["LivesLeft"] = LivesLeft;
if (GameTimed)
{
State["GameTimed"] = true;
State["SecondsLeft"] = SecondsLeft;
}
}
else
{
State["GameRunning"] = false;
State["GameTimed"] = false;
State["GameScore"] = 0;
State["LivesLeft"] = 3;
State["SecondsLeft"] = 120;
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (State.ContainsKey("GameRunning"))
{
if ((bool)State["GameRunning"] == true)
{
txtScore.Text = State["GameScore"].ToString();
LivesLeft = (int)State["LivesLeft"];
UpdateLivesLeftText();
if (State.ContainsKey("GameTimed"))
{
SecondsLeft = (int)State["SecondsLeft"];
GameTimed = true;
GameTimer.Interval = TimeSpan.FromSeconds(1);
txtTimeLeft.Text = TimeSpan.FromSeconds(SecondsLeft).Minutes.ToString().PadLeft(2, "0"[0]) + ":" + TimeSpan.FromSeconds(SecondsLeft).Seconds.ToString().PadLeft(2, "0"[0]);
}
btnStartGame_Click(null, null);
}
}
}
void GameTimer_Tick(object sender, EventArgs e)
{
if (SecondsLeft == 1)
{
anim_BarIn_Completed(sender, e);
}
else
{
SecondsLeft -= 1;
txtTimeLeft.Text = TimeSpan.FromSeconds(SecondsLeft).Minutes.ToString().PadLeft(2, "0"[0]) + ":" + TimeSpan.FromSeconds(SecondsLeft).Seconds.ToString().PadLeft(2, "0"[0]);
}
}
void btnStartTimedGame_Click(object sender, RoutedEventArgs e)
{
GameTimed = true;
GameTimer.Interval = TimeSpan.FromSeconds(1);
txtTimeLeft.Text = TimeSpan.FromSeconds(SecondsLeft).Minutes.ToString().PadLeft(2, "0"[0]) + ":" + TimeSpan.FromSeconds(SecondsLeft).Seconds.ToString().PadLeft(2, "0"[0]);
btnStartGame_Click(sender, e);
}
void btnHowTo_Click(object sender, RoutedEventArgs e)
{
Screen_HowTo.IsHitTestVisible = true;
VisualStateManager.GoToState(this, "HowTo", true);
}
bool GameRunning = false;
bool GameTimed = false;
int SecondsLeft = 120;
System.Windows.Threading.DispatcherTimer GameTimer = new System.Windows.Threading.DispatcherTimer();
int LivesLeft = 3;
void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
if (GameRunning)
{
GotoWelcomeScreen();
e.Cancel = true;
}
else if (Screen_HowTo.IsHitTestVisible)
{
GotoWelcomeScreen();
e.Cancel = true;
}
}
public void GotoWelcomeScreen()
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("HighScore") == true)
{
long Highest = (long)IsolatedStorageSettings.ApplicationSettings["HighScore"];
txtHighestScore.Text = IsolatedStorageSettings.ApplicationSettings["HighScore"].ToString();
if (Highest == 0)
{
txtHighestScore.Text += ". pfff...";
}
}
Screen_Welcome.IsHitTestVisible = true;
Screen_HowTo.IsHitTestVisible = false;
VisualStateManager.GoToState(this, "Welcome", true);
GameRunning = false;
GameTimed = false;
GameTimer.Stop();
SecondsLeft = 120;
txtScore.Text = "0";
LivesLeft = 3;
UpdateLivesLeftText();
}
void btnStartGame_Click(object sender, RoutedEventArgs e)
{
List<Color> CurrentColors = RandomizeColors();
btn1.Content = ColorToString(CurrentColors[0]);
btn2.Content = ColorToString(CurrentColors[1]);
btn3.Content = ColorToString(CurrentColors[2]);
btn4.Content = ColorToString(CurrentColors[3]);
btn5.Content = ColorToString(CurrentColors[4]);
btn6.Content = ColorToString(CurrentColors[5]);
btn1.Tag = CurrentColors[0];
btn2.Tag = CurrentColors[1];
btn3.Tag = CurrentColors[2];
btn4.Tag = CurrentColors[3];
btn5.Tag = CurrentColors[4];
btn6.Tag = CurrentColors[5];
VisualStateManager.GoToState(this, "Game", true);
Screen_Welcome.IsHitTestVisible = false;
GameRunning = true;
anim_BarIn.Begin();
Randomize(true);
if (GameTimed)
{
GameTimer.Start();
}
else
{
txtTimeLeft.Text = "";
}
}
void anim_Correct_Out_Completed(object sender, EventArgs e)
{
anim_BarIn.Begin();
}
void anim_Correct_Completed(object sender, EventArgs e)
{
Randomize();
}
void UpdateLivesLeftText()
{
txtLivesLeft.Text = "";
for (int i = 0; i < LivesLeft; i++)
{
txtLivesLeft.Text += Convert.ToChar(128).ToString();
}
}
void anim_BarIn_Completed(object sender, EventArgs e)
{
if (GameRunning)
{
LoosingLive();
}
}
void LoosingLive()
{
if (LivesLeft > 0 && SecondsLeft != 0)
{
LivesLeft -= 1;
UpdateLivesLeftText();
anim_BarIn.Pause();
txtAnswerStatus.Text = "Ouch!";
AnswerStatusColor.Fill = new SolidColorBrush(Colors.Red);
anim_Correct.Begin();
}
else
{
//THE END
if (IsolatedStorageSettings.ApplicationSettings.Contains("HighScore") == false)
{
IsolatedStorageSettings.ApplicationSettings.Add("HighScore", long.Parse(txtScore.Text));
}
else
{
long Highest = (long)IsolatedStorageSettings.ApplicationSettings["HighScore"];
long Current = long.Parse(txtScore.Text);
if (Current > Highest)
{
IsolatedStorageSettings.ApplicationSettings["HighScore"] = Current;
}
}
GotoWelcomeScreen();
}
}
bool Change_ButtonBackgrounds = false;
bool Change_ButtonPositions = false;
bool EvalInProgress = false;
void btnColorPick_Click(object sender, RoutedEventArgs e)
{
if (EvalInProgress == false)
{
EvalInProgress = true;
Button Current = sender as Button;
Color SelectedColor = (Color)Current.Tag;
if (SelectedColor == TargetColor)
{
txtAnswerStatus.Text = "Correct!";
AnswerStatusColor.Fill = new SolidColorBrush(Color.FromArgb(255,90,255,0));
//SCORE!
int CurrentScore = int.Parse(txtScore.Text);
txtScore.Text = (CurrentScore + 1).ToString();
if(CurrentScore > 15)
{
Change_ButtonPositions = true;
}
else
{
Change_ButtonPositions= false;
}
if (CurrentScore > 25)
{
Change_ButtonBackgrounds = true;
}
else
{
Change_ButtonBackgrounds = false;
}
if (CurrentScore > 35)
{
var Diff = (CurrentScore - 35) *10;
if (Diff < 4500)
{
Anim_TargetPoint.KeyTime = TimeSpan.FromMilliseconds(5000 - Diff);
}
}
anim_BarIn.Pause();
anim_Correct.Begin();
}
else
{
EvalInProgress = false;
LoosingLive();
}
}
}
gameservice.rightcolorSoapClient Service = new gameservice.rightcolorSoapClient();
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
UpdateLivesLeftText();
List<Color> CurrentColors = RandomizeColors();
btn1.Content = ColorToString(CurrentColors[0]);
btn2.Content = ColorToString(CurrentColors[1]);
btn3.Content = ColorToString(CurrentColors[2]);
btn4.Content = ColorToString(CurrentColors[3]);
btn5.Content = ColorToString(CurrentColors[4]);
btn6.Content = ColorToString(CurrentColors[5]);
btn1.Tag = CurrentColors[0];
btn2.Tag = CurrentColors[1];
btn3.Tag = CurrentColors[2];
btn4.Tag = CurrentColors[3];
btn5.Tag = CurrentColors[4];
btn6.Tag = CurrentColors[5];
GotoWelcomeScreen();
OnNavigatedTo(null);
}
public List<Color> ReturnAllColors()
{
List<Color> ColorsToPick = new List<Color>();
ColorsToPick.Add(Colors.Black);
ColorsToPick.Add(Colors.Red);
ColorsToPick.Add(Colors.Blue);
ColorsToPick.Add(Colors.Orange);
ColorsToPick.Add(Color.FromArgb(255, 188, 188, 0));
ColorsToPick.Add(Colors.Green);
return ColorsToPick;
}
Color TargetColor = new Color();
Random RND = new Random();
public List<Color> RandomizeColors()
{
return (ReturnAllColors().OrderBy(a => Guid.NewGuid())).ToList();
}
Color OldTargetColor;
public void Randomize(bool FirstRun = false)
{
List<Color> ColorsToPick = RandomizeColors();
do
{
TargetColor = ColorsToPick[RND.Next(0, 5)];
} while (TargetColor == OldTargetColor );
OldTargetColor = TargetColor;
txtColorName.Foreground = new SolidColorBrush(TargetColor);
ColorsToPick.Remove(TargetColor);
txtColorName.Text = ColorToString(ColorsToPick[RND.Next(0, 4)]);
if (Change_ButtonBackgrounds)
{
//Randomizing button background colors
List<Color> BackColors = RandomizeColors();
btn1.Background = new SolidColorBrush(BackColors[0]);
btn2.Background = new SolidColorBrush(BackColors[1]);
btn3.Background = new SolidColorBrush(BackColors[2]);
btn4.Background = new SolidColorBrush(BackColors[3]);
btn5.Background = new SolidColorBrush(BackColors[4]);
btn6.Background = new SolidColorBrush(BackColors[5]);
//Fix the text colors of the buttons.
if (BackColors[0] == Color.FromArgb(255, 188, 188, 0))
{
btn1.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
btn1.Foreground = new SolidColorBrush(Colors.White);
}
if (BackColors[1] == Color.FromArgb(255, 188, 188, 0))
{
btn2.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
btn2.Foreground = new SolidColorBrush(Colors.White);
}
if (BackColors[2] == Color.FromArgb(255, 188, 188, 0))
{
btn3.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
btn3.Foreground = new SolidColorBrush(Colors.White);
}
if (BackColors[3] == Color.FromArgb(255, 188, 188, 0))
{
btn4.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
btn4.Foreground = new SolidColorBrush(Colors.White);
}
if (BackColors[4] == Color.FromArgb(255, 188, 188, 0))
{
btn5.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
btn5.Foreground = new SolidColorBrush(Colors.White);
}
if (BackColors[5] == Color.FromArgb(255, 188, 188, 0))
{
btn6.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
btn6.Foreground = new SolidColorBrush(Colors.White);
}
}
else
{
btn1.Background = new SolidColorBrush(Colors.Black);
btn2.Background = new SolidColorBrush(Colors.Black);
btn3.Background = new SolidColorBrush(Colors.Black);
btn4.Background = new SolidColorBrush(Colors.Black);
btn5.Background = new SolidColorBrush(Colors.Black);
btn6.Background = new SolidColorBrush(Colors.Black);
btn1.Foreground = new SolidColorBrush(Colors.White);
btn2.Foreground = new SolidColorBrush(Colors.White);
btn3.Foreground = new SolidColorBrush(Colors.White);
btn4.Foreground = new SolidColorBrush(Colors.White);
btn5.Foreground = new SolidColorBrush(Colors.White);
btn6.Foreground = new SolidColorBrush(Colors.White);
}
if (Change_ButtonPositions)
{
//Randomizing Button Colors
List<Color> CurrentColors = RandomizeColors();
btn1.Content = ColorToString(CurrentColors[0]);
btn2.Content = ColorToString(CurrentColors[1]);
btn3.Content = ColorToString(CurrentColors[2]);
btn4.Content = ColorToString(CurrentColors[3]);
btn5.Content = ColorToString(CurrentColors[4]);
btn6.Content = ColorToString(CurrentColors[5]);
btn1.Tag = CurrentColors[0];
btn2.Tag = CurrentColors[1];
btn3.Tag = CurrentColors[2];
btn4.Tag = CurrentColors[3];
btn5.Tag = CurrentColors[4];
btn6.Tag = CurrentColors[5];
}
if (!FirstRun)
{
anim_Correct_Out.Begin();
}
EvalInProgress = false;
}
public string ColorToString(Color C)
{
if (C == Colors.Black)
{
return "Black";
}
else if (C == Colors.Red)
{
return "Red";
}
else if (C == Colors.Blue)
{
return "Blue";
}
else if (C == Colors.Orange)
{
return "Orange";
}
else if (C == Color.FromArgb(255, 188, 188, 0))
{
return "Yellow";
}
else if (C == Colors.Green)
{
return "Green";
}
else
{
return "";
}
}
}
}