-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
916 lines (767 loc) · 33.4 KB
/
Program.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using StickFight.Audio;
using StickFight.Properties;
namespace StickFight
{
internal class Program
{
#region Constants
/// <summary>
/// The initial Height offset for the map
/// </summary>
public const int Offset = 5;
#endregion
#region Fields
private static bool soundEnabled = true;
#endregion
#region Properties
public static AiCombat AiCombat { get; set; }
/// <summary>
/// Gets or sets Player 1
/// </summary>
public static Player Player1 { get; set; }
/// <summary>
/// Gets or sets Player 2
/// </summary>
public static Player Player2 { get; set; }
/// <summary>
/// Gets or sets Player 1 status bar
/// </summary>
//public static StatusBar P1Status { get; set; }
/// <summary>
/// Gets or sets Player 2 status bar
/// </summary>
//public static StatusBar P2Status { get; set; }
/// <summary>
/// Gets or sets a value indicating if the game has been won
/// </summary>
private static bool GameWon { get; set; }
/// <summary>
/// Gets or sets the background music controller
/// </summary>
public static BackgroundMusic Music { get; set; }
/// <summary>
/// Gets or sets a boolean value representing if sound should be played or not.
/// </summary>
public static bool SoundEnabled
{
get { return soundEnabled; }
set
{
soundEnabled = value;
if (value)
{
ConsoleOptions.ChangeAudio = Resources.ChangeChoice;
ConsoleOptions.SelectAudio = Resources.SelectChoice;
}
else
{
ConsoleOptions.ChangeAudio = null;
ConsoleOptions.SelectAudio = null;
}
}
}
#endregion
/// <summary>
/// Starting entry for the program. Brings up the initial menu.
/// </summary>
private static void Main()
{
ConsoleOptions.ChangeAudio = Resources.ChangeChoice;
ConsoleOptions.SelectAudio = Resources.SelectChoice;
Console.Title = "Stick Fight - By Keta";
string userPick = "";
while (!userPick.StartsWith("4"))
{
if (userPick.StartsWith("1"))
{
Initialize_Game_PVAI();
}
else if (userPick.StartsWith("2"))
{
Initialize_Game_PVP();
}
else if (userPick.StartsWith("3"))
{
SettingsScreen();
Settings.Default.Save();
}
Console.Clear();
Console.Out.Write(Settings.Default.EntryScreen); // Writes out the standard main menu screen.
userPick = ConsoleOptions.SendChoices("", new List<string>
{
"1: Start Human vs. AI",
"2: Start Human vs. Human",
"3: Settings",
"4: Exit"
});
}
Environment.Exit(0);
}
/// <summary>
/// Displays the settings screen, containing the control settings for Player 1 and 2.
/// </summary>
private static void SettingsScreen()
{
string selected = "";
while (!selected.StartsWith("4"))
{
int cursorPosition = 0;
if (selected.StartsWith("1"))
{
ChangePlayer1Settings();
}
else if (selected.StartsWith("2"))
{
ChangePlayer2Settings();
}
else if (selected.StartsWith("3"))
{
SoundEnabled = !SoundEnabled;
cursorPosition = 3;
}
Console.Clear();
Console.WriteLine("\n Player 1:");
Console.WriteLine("\tLeft: " + (ConsoleKey) Settings.Default.Plr1Left);
Console.WriteLine("\tRight: " + (ConsoleKey) Settings.Default.Plr1Right);
Console.WriteLine("\tUp: " + (ConsoleKey) Settings.Default.Plr1Up);
Console.WriteLine("\tDown: " + (ConsoleKey) Settings.Default.Plr1Down);
Console.WriteLine("\tHit: " + (ConsoleKey) Settings.Default.Plr1Hit);
Console.WriteLine("\tKick: " + (ConsoleKey) Settings.Default.Plr1Kick);
Console.WriteLine("\tBlock: " + (ConsoleKey) Settings.Default.Plr1Block);
Console.WriteLine("\n Player 2:");
Console.WriteLine("\tLeft: " + (ConsoleKey) Settings.Default.Plr2Left);
Console.WriteLine("\tRight: " + (ConsoleKey) Settings.Default.Plr2Right);
Console.WriteLine("\tUp: " + (ConsoleKey) Settings.Default.Plr2Up);
Console.WriteLine("\tDown: " + (ConsoleKey) Settings.Default.Plr2Down);
Console.WriteLine("\tHit: " + (ConsoleKey) Settings.Default.Plr2Hit);
Console.WriteLine("\tKick: " + (ConsoleKey) Settings.Default.Plr2Kick);
Console.WriteLine("\tBlock: " + (ConsoleKey) Settings.Default.Plr2Block);
selected = ConsoleOptions.SendChoices("",
new List<string>
{
"1: Change player 1 controls",
"2: Change player 2 controls",
"3: Disable/Enable music",
"4: Back",
}, cursorPosition);
}
}
/// <summary>
/// Displays the Player 1 settings screen, allowing the user to set keybindings for all controls.
/// </summary>
private static void ChangePlayer1Settings()
{
string selected = "0";
while (!selected.StartsWith("8"))
{
if (selected.StartsWith("1"))
{
string Header = "Press a key bind Left to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr1Left = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("2"))
{
string Header = "Press a key bind Right to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr1Right = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("3"))
{
string Header = "Press a key bind Up to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr1Up = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("4"))
{
string Header = "Press a key bind Down to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr1Down = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("5"))
{
string Header = "Press a key bind Hit to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr1Hit = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("6"))
{
string Header = "Press a key bind Kick to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr1Kick = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("7"))
{
string Header = "Press a key bind Block to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr1Block = (int) (Console.ReadKey(true).Key);
}
Console.Clear();
Console.WriteLine("Select control to edit:");
selected = ConsoleOptions.SendChoices("", new List<string>
{
"1: Left",
"2: Right",
"3: Up",
"4: Down",
"5: Hit",
"6: Kick",
"7: Block",
"8: Back",
}, Convert.ToInt32(selected.Substring(0, 1)));
}
}
/// <summary>
/// Displays the Player 2 settings screen, allowing the user to set keybindings for all controls.
/// </summary>
private static void ChangePlayer2Settings()
{
string selected = "0";
while (!selected.StartsWith("8"))
{
if (selected.StartsWith("1"))
{
string Header = "Press a key bind Left to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr2Left = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("2"))
{
string Header = "Press a key bind Right to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr2Right = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("3"))
{
string Header = "Press a key bind Up to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr2Up = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("4"))
{
string Header = "Press a key bind Down to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr2Down = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("5"))
{
string Header = "Press a key bind Hit to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr2Hit = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("6"))
{
string Header = "Press a key bind Kick to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr2Kick = (int) (Console.ReadKey(true).Key);
}
else if (selected.StartsWith("7"))
{
string Header = "Press a key bind Block to:";
SettingsHeaderMessage(Header);
Settings.Default.Plr2Block = (int) (Console.ReadKey(true).Key);
}
Console.Clear();
Console.WriteLine("Select control to edit:");
selected = ConsoleOptions.SendChoices("", new List<string>
{
"1: Left",
"2: Right",
"3: Up",
"4: Down",
"5: Hit",
"6: Kick",
"7: Block",
"8: Back",
}, Convert.ToInt32(selected.Substring(0, 1)));
}
}
/// <summary>
/// Sets the header message in the keybinding menu.
/// </summary>
/// <param name="message">The message to display</param>
private static void SettingsHeaderMessage(string message)
{
int startPosition = 40;
Console.SetCursorPosition(startPosition - (message.Length/2), 2);
Console.Out.Write(message);
Console.WriteLine();
}
/// <summary>
/// Starts the game, prompting the user to enter player 1 and 2 names and select color.
/// </summary>
private static void Initialize_Game_PVP()
{
bool Replay = true;
Console.Title = "Stick Fight - Player versus Player";
Console.Out.WriteLine("Enter Player 1 name.");
string plr1Name = Console.ReadLine();
while (plr1Name == "")
{
Console.Out.WriteLine("You need a name.");
plr1Name = Console.ReadLine();
}
Console.Out.WriteLine("Enter Player 2 name.");
string plr2Name = Console.ReadLine();
while (plr2Name == "" || plr2Name.ToLower() == plr1Name.ToLower())
{
Console.Out.WriteLine("You need a name.");
plr2Name = Console.ReadLine();
}
#region Player 1 Color Selecting
ConsoleColor Plr1Color;
#region Player 1 color listing and selection
var colorName1 = new List<string>();
var colorConsole1 = new List<ConsoleColor>();
foreach (ConsoleColor item in Enum.GetValues(typeof (ConsoleColor)))
{
string colorName = item.ToString().ToLower();
if (!colorName.Contains("black"))
{
colorName1.Add(item.ToString());
colorConsole1.Add(item);
}
}
#endregion
Console.Clear();
if (
!Enum.TryParse(
ConsoleOptions.SendChoices("Select " + plr1Name + "'s Color", colorName1, colorConsole1),
out Plr1Color))
throw new Exception("Error on converting player 1's color");
#endregion
#region Player 2 Color Selecting
ConsoleColor plr2Color;
#region Player 2 color listing and selection
var colorName2 = new List<string>();
var colorConsole2 = new List<ConsoleColor>();
foreach (ConsoleColor item in Enum.GetValues(typeof (ConsoleColor)))
{
string colorName = item.ToString().ToLower();
if (!colorName.Contains("black") && item != Plr1Color)
{
colorName2.Add(item.ToString());
colorConsole2.Add(item);
}
}
#endregion
Console.Clear();
if (
!Enum.TryParse(
ConsoleOptions.SendChoices("Select " + plr2Name + "'s Color", colorName2, colorConsole2),
out plr2Color))
throw new Exception("Error on converting player 2's color");
#endregion
#region Handles Map Selection
if (!Directory.Exists("Maps"))
Directory.CreateDirectory("Maps");
List<string> MapPaths = Directory.GetFiles("Maps").ToList();
if (MapPaths.Count == 0)
{
File.WriteAllText(@"Maps\Map_00.txt", Settings.Default.StandardMap);
MapPaths = Directory.GetFiles("Maps").ToList();
}
for (int i = 0; i < MapPaths.Count; i++)
{
MapPaths[i] = MapPaths[i].Replace(@"Maps\", "");
MapPaths[i] = MapPaths[i].Replace(".txt", "");
}
string chosenMap = @"Maps\" + ConsoleOptions.SendChoices("Choose your Arena", MapPaths) + ".txt";
#endregion
Console.Out.WriteLine("Game Starting in 3 seconds");
Thread.Sleep(3000);
Player.EffectPlayer = new List<Effect>
{
new Effect(Resources.Block),
new Effect(Resources.Punch_1),
new Effect(Resources.Punch_2),
new Effect(Resources.Punch_3)
};
Music = new BackgroundMusic("BackgroundMusic", Resources.Background_Theme, true);
#region Controls the actual game process.
while (Replay)
{
try
{
Player1 = new Player(
plr1Name,
Plr1Color,
new Point(20, 20 + Offset))
{
HpBarPosition = 1
};
Player2 = new Player(
plr2Name,
plr2Color,
new Point(60, 20 + Offset))
{
HpBarPosition = 59
};
//P1Status = new StatusBar(new Point(1, 31), 30, true);
//P2Status = new StatusBar(new Point(79, 31), 30, false);
GameWon = false;
Console.CursorVisible = false;
Map.CreateMap(chosenMap);
Player1.UpdateAnim();
Player2.UpdateAnim();
//P1Status.DrawText("0123456789012345678901234567895234", new TimeSpan(0, 0, 3));
//P2Status.DrawText("012345678901234567890123456789", new TimeSpan(0, 0, 3));
#region Countdown to start.
Thread.Sleep(1000);
SetHeaderMessage("Ready");
//P1Status.DrawText("Blargh fish", new TimeSpan(0, 0, 10));
Thread.Sleep(1000);
//P2Status.DrawText("Fiskemad", new TimeSpan(0, 0, 4));
SetHeaderMessage("Set!");
if (!Music.IsPlaying)
Music.StartMusic();
Thread.Sleep(1000);
SetHeaderMessage("GO!");
#endregion
#region The actual main loop, checking for user input, controlling the players according to the buttons pressed.
int plr1Left = Settings.Default.Plr1Left;
int plr1Right = Settings.Default.Plr1Right;
int plr1Down = Settings.Default.Plr1Down;
int plr1Up = Settings.Default.Plr1Up;
int plr1Hit = Settings.Default.Plr1Hit;
int plr1Kick = Settings.Default.Plr1Kick;
int plr1Block = Settings.Default.Plr1Block;
int plr2Left = Settings.Default.Plr2Left;
int plr2Right = Settings.Default.Plr2Right;
int plr2Down = Settings.Default.Plr2Down;
int plr2Up = Settings.Default.Plr2Up;
int plr2Hit = Settings.Default.Plr2Hit;
int plr2Kick = Settings.Default.Plr2Kick;
int plr2Block = Settings.Default.Plr2Block;
while (!GameWon)
{
if (NativeKeyboard.IsKeyDown(plr1Left))
Player1.Move(Direction.Left);
else if (NativeKeyboard.IsKeyDown(plr1Right))
Player1.Move(Direction.Right);
else if (NativeKeyboard.IsKeyDown(plr1Down))
Player1.Move(Direction.Down);
if (NativeKeyboard.IsKeyDown(plr1Up))
Player1.Jump();
if (NativeKeyboard.IsKeyDown(plr1Hit))
Player1.Hit();
if (NativeKeyboard.IsKeyDown(plr1Kick))
Player1.Kick();
if (NativeKeyboard.IsKeyDown(plr1Block))
Player1.Block();
Player1.UpdateMovement();
if (NativeKeyboard.IsKeyDown(plr2Left))
Player2.Move(Direction.Left);
else if (NativeKeyboard.IsKeyDown(plr2Right))
Player2.Move(Direction.Right);
else if (NativeKeyboard.IsKeyDown(plr2Down))
Player2.Move(Direction.Down);
if (NativeKeyboard.IsKeyDown(plr2Up))
Player2.Jump();
if (NativeKeyboard.IsKeyDown(plr2Hit))
Player2.Hit();
if (NativeKeyboard.IsKeyDown(plr2Kick))
Player2.Kick();
if (NativeKeyboard.IsKeyDown(plr2Block))
Player2.Block();
Player2.UpdateMovement();
GameWon = CheckForWinner();
Thread.Sleep(30);
}
#endregion
#region After game ends, moves the player models back to default position.
for (int i = Player1.Collision.X; i < Player1.Collision.X + 5; i++)
{
for (int u = Player1.Collision.Y; u < Player1.Collision.Y + 5; u++)
{
Console.SetCursorPosition(i, u);
Console.Out.Write(" ");
}
}
for (int i = Player2.Collision.X; i < Player2.Collision.X + 5; i++)
{
for (int u = Player2.Collision.Y; u < Player2.Collision.Y + 5; u++)
{
Console.SetCursorPosition(i, u);
Console.Out.Write(" ");
}
}
Player1.Collision = new Rectangle(20, 20 + Offset, 5, 5);
Player2.Collision = new Rectangle(60, 20 + Offset, 5, 5);
Player1.UpdateAnim();
Player2.UpdateAnim();
#endregion
if (Player1.Health == 0 && Player2.Health == 0)
{
SetHeaderMessage("It is tied!");
}
else if (Player1.Health == 0)
{
SetHeaderMessage(Player2.Name + " has won the Game!");
}
else if (Player2.Health == 0)
{
SetHeaderMessage(Player1.Name + " has won the Game!");
}
else
{
SetHeaderMessage("Unknown Player has won the Game!");
}
Thread.Sleep(3000); // Sleeps to allow for the winning to be displayed.
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
Console.ReadLine();
Environment.Exit(0);
}
SetHeaderMessage("Play Again? Y/N");
while (true)
{
if (NativeKeyboard.IsKeyDown(Convert.ToInt32('Y')))
{
Replay = true;
break;
}
else if (NativeKeyboard.IsKeyDown(Convert.ToInt32('N')))
{
Replay = false;
break;
}
Thread.Sleep(10);
}
}
#endregion
}
/// <summary>
/// Starts the game, prompting the user to enter player 1 and 2 names and select color.
/// </summary>
private static void Initialize_Game_PVAI()
{
bool Replay = true;
Console.Title = "Stick Fight - Player versus Artificial Intelligence";
Console.Out.WriteLine("Enter Player 1 name.");
string plr1Name = Console.ReadLine();
while (plr1Name == "")
{
Console.Out.WriteLine("You need a name.");
plr1Name = Console.ReadLine();
}
#region Player 1 Color Selecting
ConsoleColor Plr1Color;
#region Player 1 color listing and selection
var colorName1 = new List<string>();
var colorConsole1 = new List<ConsoleColor>();
foreach (ConsoleColor item in Enum.GetValues(typeof(ConsoleColor)))
{
string colorName = item.ToString().ToLower();
if (!colorName.Contains("black"))
{
colorName1.Add(item.ToString());
colorConsole1.Add(item);
}
}
#endregion
Console.Clear();
if (
!Enum.TryParse(
ConsoleOptions.SendChoices("Select " + plr1Name + "'s Color", colorName1, colorConsole1),
out Plr1Color))
throw new Exception("Error on converting player 1's color");
#endregion
#region Player 2 Color Selecting
List<ConsoleColor> colors = Enum.GetValues(typeof (ConsoleColor)).Cast<ConsoleColor>().Where(item => item != Plr1Color && item != ConsoleColor.Black).ToList();
ConsoleColor plr2Color = colors[new Random().Next(0, colors.Count)];
Console.Clear();
#endregion
#region Handles Map Selection
if (!Directory.Exists("Maps"))
Directory.CreateDirectory("Maps");
List<string> mapPaths = Directory.GetFiles("Maps").ToList();
if (mapPaths.Count == 0)
{
File.WriteAllText(@"Maps\Map_00.txt", Settings.Default.StandardMap);
mapPaths = Directory.GetFiles("Maps").ToList();
}
for (int i = 0; i < mapPaths.Count; i++)
{
mapPaths[i] = mapPaths[i].Replace(@"Maps\", "");
mapPaths[i] = mapPaths[i].Replace(".txt", "");
}
string chosenMap = @"Maps\" + ConsoleOptions.SendChoices("Choose your Arena", mapPaths) + ".txt";
#endregion
Console.Out.WriteLine("Game Starting in 3 seconds");
Thread.Sleep(3000);
Player.EffectPlayer = new List<Effect>
{
new Effect(Resources.Block),
new Effect(Resources.Punch_1),
new Effect(Resources.Punch_2),
new Effect(Resources.Punch_3)
};
Music = new BackgroundMusic("BackgroundMusic", Resources.Background_Theme, true);
#region Controls the actual game process.
while (Replay)
{
try
{
Player1 = new Player(
plr1Name,
Plr1Color,
new Point(20, 20 + Offset))
{
HpBarPosition = 1
};
Player2 = new Player(
"Computer AI",
plr2Color,
new Point(60, 20 + Offset))
{
HpBarPosition = 59
};
AiCombat = new AiCombat(Player2);
//P1Status = new StatusBar(new Point(1, 31), 30, true);
//P2Status = new StatusBar(new Point(79, 31), 30, false);
GameWon = false;
Console.CursorVisible = false;
Map.CreateMap(chosenMap);
Player1.UpdateAnim();
Player2.UpdateAnim();
#region Countdown to start.
Thread.Sleep(1000);
SetHeaderMessage("Ready");
Thread.Sleep(1000);
SetHeaderMessage("Set!");
if (!Music.IsPlaying)
Music.StartMusic();
Thread.Sleep(1000);
SetHeaderMessage("GO!");
#endregion
#region The actual main loop, checking for user input, controlling the players according to the buttons pressed.
int plr1Left = Settings.Default.Plr1Left;
int plr1Right = Settings.Default.Plr1Right;
int plr1Down = Settings.Default.Plr1Down;
int plr1Up = Settings.Default.Plr1Up;
int plr1Hit = Settings.Default.Plr1Hit;
int plr1Kick = Settings.Default.Plr1Kick;
int plr1Block = Settings.Default.Plr1Block;
while (!GameWon)
{
if (NativeKeyboard.IsKeyDown(plr1Left))
Player1.Move(Direction.Left);
else if (NativeKeyboard.IsKeyDown(plr1Right))
Player1.Move(Direction.Right);
else if (NativeKeyboard.IsKeyDown(plr1Down))
Player1.Move(Direction.Down);
if (NativeKeyboard.IsKeyDown(plr1Up))
Player1.Jump();
if (NativeKeyboard.IsKeyDown(plr1Hit))
Player1.Hit();
if (NativeKeyboard.IsKeyDown(plr1Kick))
Player1.Kick();
if (NativeKeyboard.IsKeyDown(plr1Block))
Player1.Block();
Player1.UpdateMovement();
AiCombat.Update(Player1);
Player2.UpdateMovement();
GameWon = CheckForWinner();
Thread.Sleep(30);
}
#endregion
#region After game ends, moves the player models back to default position.
for (int i = Player1.Collision.X; i < Player1.Collision.X + 5; i++)
{
for (int u = Player1.Collision.Y; u < Player1.Collision.Y + 5; u++)
{
Console.SetCursorPosition(i, u);
Console.Out.Write(" ");
}
}
for (int i = Player2.Collision.X; i < Player2.Collision.X + 5; i++)
{
for (int u = Player2.Collision.Y; u < Player2.Collision.Y + 5; u++)
{
Console.SetCursorPosition(i, u);
Console.Out.Write(" ");
}
}
Player1.Collision = new Rectangle(20, 20 + Offset, 5, 5);
Player2.Collision = new Rectangle(60, 20 + Offset, 5, 5);
Player1.UpdateAnim();
Player2.UpdateAnim();
#endregion
if (Player1.Health == 0 && Player2.Health == 0)
{
SetHeaderMessage("It is tied!");
}
else if (Player1.Health == 0)
{
SetHeaderMessage(Player2.Name + " has won the Game!");
}
else if (Player2.Health == 0)
{
SetHeaderMessage(Player1.Name + " has won the Game!");
}
else
{
SetHeaderMessage("Unknown Player has won the Game!");
}
Thread.Sleep(3000); // Sleeps to allow for the winning to be displayed.
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
Console.ReadLine();
Environment.Exit(0);
}
SetHeaderMessage("Play Again? Y/N");
while (true)
{
if (NativeKeyboard.IsKeyDown(Convert.ToInt32('Y')))
{
Replay = true;
break;
}
else if (NativeKeyboard.IsKeyDown(Convert.ToInt32('N')))
{
Replay = false;
break;
}
Thread.Sleep(10);
}
}
#endregion
}
/// <summary>
/// Returns a value indication if player 1 or player 2 has 0 health.
/// </summary>
/// <returns></returns>
public static bool CheckForWinner()
{
return (Player1.Health == 0 || Player2.Health == 0);
}
/// <summary>
/// Sets the header in the game's message to the specified string.
/// </summary>
/// <param name="Message">The header string to display</param>
private static void SetHeaderMessage(string Message)
{
int startPosition = 40;
Console.SetCursorPosition(21, 2);
for (int i = 0; i < 38; i++)
{
Console.Out.Write(" ");
}
Console.SetCursorPosition(startPosition - (Message.Length/2), 2);
Console.Out.Write(Message);
Console.SetCursorPosition(0, 26 + Offset);
}
}
}