This repository has been archived by the owner on Nov 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGorillasScreen.xaml.cs
150 lines (126 loc) · 5.04 KB
/
GorillasScreen.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
namespace WPF_Game_Gorillas
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static readonly Regex _numOnly = new Regex("^[0-9]*$");
private bool exceptionThrown = false;
Gorillas gorillasGame;
public MainWindow()
{
InitializeComponent();
this.PreviewKeyDown += new KeyEventHandler(ESCapeKey);
}
private void Window_SourceInitialized(object sender, EventArgs e)
{
gorillasGame = new Gorillas(this.Width, this.Height, gameCanvas, int.Parse(((GameSettings)Application.Current.MainWindow).textBox_playerSize.Text));
player1Name.Content = ((GameSettings)Application.Current.MainWindow).textBox_player1.Text; //get values from other window
player2Name.Content = ((GameSettings)Application.Current.MainWindow).textBox_player2.Text;
player1Lives.Content = "Počet životů: " + ((GameSettings)Application.Current.MainWindow).textBox_gamesToWin.Text; //live values initialization on screen
player2Lives.Content = player1Lives.Content;
gorillasGame.gameStatusLabel = gameStatusLabel;
gorillasGame.playersLives[0] = player1Lives;
gorillasGame.playersLives[1] = player2Lives;
gorillasGame.player1[2] = gorillasGame.player2[2] = int.Parse(((GameSettings)Application.Current.MainWindow).textBox_gamesToWin.Text);
gorillasGame.playersNames[0] = player1Name.Content.ToString();
gorillasGame.playersNames[1] = player2Name.Content.ToString();
nextRoundButton.Content = "Hraje: " + player1Name.Content;
}
private void ESCapeKey(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
this.Close();
}
private void player1Angle_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!_numOnly.IsMatch(e.Text))
e.Handled = true;
}
private void player1Power_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!_numOnly.IsMatch(e.Text))
e.Handled = true;
}
private void player2Angle_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!_numOnly.IsMatch(e.Text))
e.Handled = true;
}
private void player2Power_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!_numOnly.IsMatch(e.Text))
e.Handled = true;
}
private int CheckNumber(string text)
{
int outNumber = 0;
try
{
outNumber = int.Parse(text);
if (outNumber > 270 || outNumber < 0)
{
MessageBox.Show("Zadal jste neplatné hodnoty. Úhel nejsmí být větší jak 270° a síla také.");
exceptionThrown = true;
return 0;
}
else
return outNumber;
}
catch (Exception)
{
MessageBox.Show("Zadal jste neplatné hodnoty. Úhel nejsmí být větší jak 270° a síla také.");
exceptionThrown = true;
return 0;
}
}
private void nextRoundButton_Click(object sender, RoutedEventArgs e)
{
exceptionThrown = false;
gameStatusLabel.Content = "";
if (gorillasGame.player1Starts)
{
gorillasGame.player1[0] = CheckNumber(player1Angle.Text);
if (!exceptionThrown)
{
gorillasGame.player1[1] = CheckNumber(player1Power.Text);
if (!exceptionThrown)
nextRoundButton.Content = player2Name.Content;
}
}
else
{
gorillasGame.player2[0] = CheckNumber(player2Angle.Text);
if (!exceptionThrown)
{
gorillasGame.player2[1] = CheckNumber(player2Power.Text);
if (!exceptionThrown)
nextRoundButton.Content = player1Name.Content;
}
}
//Reset input values to make it harder (you need to remeber your previous values)
player1Angle.Text = "";
player1Power.Text = "";
player2Angle.Text = "";
player2Power.Text = "";
if (!exceptionThrown)
gorillasGame.ThrowCalculation();
}
}
}