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
/
MainWindow.xaml.cs
262 lines (218 loc) · 10.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
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.Windows.Threading;
namespace WPF_Game_SaveLetters
{
public struct Letters //Each individial letter
{
public Label letter { get; }
public bool aboard { get; set; }
public bool isFalling { get; set; }
public Letters(Label letter, bool aboard, bool isFalling)
{
this.letter = letter;
this.aboard = aboard;
this.isFalling = isFalling;
}
}
public partial class MainWindow : Window
{
//Game object
Rectangle leftSideShore = new Rectangle { Fill = Brushes.Green }; //left shore
Rectangle rightSideShore = new Rectangle { Fill = Brushes.Green }; //right shore
Rectangle bottomSideShore = new Rectangle { Fill = Brushes.Blue }; //water
Rectangle boat = new Rectangle { Fill = Brushes.SaddleBrown };
private bool boatIsMoving = false;
private bool boatIsUnloading = false;
//Timers
DispatcherTimer physicsUpdate = new DispatcherTimer();
DispatcherTimer spawnRate = new DispatcherTimer();
//playerMovement
private bool keyLeftDown = false;
private bool keyRightDown = false;
//Letters
private int lettersRemoved = 0, lettersSaved = 0;
private char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private List<Letters> lettersList = new List<Letters>();
public MainWindow()
{
InitializeComponent();
physicsUpdate.Tick += new EventHandler(physicsUpdate_Tick);
physicsUpdate.Interval = new TimeSpan(0, 0, 0, 0, 20); //40ms => 50fps
spawnRate.Tick += new EventHandler(spawnRate_Tick);
spawnRate.Interval = new TimeSpan(0, 0, 0, 3); //every 3 seconds
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
GenerateEnvironment();
physicsUpdate.Start();
spawnRate.Start();
}
private void GenerateEnvironment() //left + right shores + water
{
int fractionOfScreenWidth = 5; // one fift of width
int fractionIfScreenHeight = 3; //one third of the height
leftSideShore.Width = gameCanvas.ActualWidth / fractionOfScreenWidth;
leftSideShore.Height = gameCanvas.ActualHeight / fractionIfScreenHeight;
rightSideShore.Width = gameCanvas.ActualWidth / fractionOfScreenWidth;
rightSideShore.Height = gameCanvas.ActualHeight / fractionIfScreenHeight;
bottomSideShore.Width = gameCanvas.ActualWidth - (leftSideShore.Width + rightSideShore.Width);
bottomSideShore.Height = gameCanvas.ActualHeight / (fractionOfScreenWidth -1) + 10; //10 is offset
Canvas.SetLeft(leftSideShore, 0);
Canvas.SetTop(leftSideShore, gameCanvas.ActualHeight - leftSideShore.Height);
Canvas.SetLeft(rightSideShore, gameCanvas.ActualWidth - rightSideShore.Width);
Canvas.SetTop(rightSideShore, gameCanvas.ActualHeight - leftSideShore.Height);
Canvas.SetLeft(bottomSideShore, leftSideShore.Width);
Canvas.SetTop(bottomSideShore, gameCanvas.ActualHeight - bottomSideShore.Height);
gameCanvas.Children.Add(leftSideShore);
gameCanvas.Children.Add(rightSideShore);
gameCanvas.Children.Add(bottomSideShore);
GenerateBoat();
}
private void GenerateBoat()
{
boat.Width = bottomSideShore.Width / 4;
boat.Height = 50;
Canvas.SetLeft(boat, leftSideShore.Width);
Canvas.SetTop(boat, gameCanvas.ActualHeight - leftSideShore.Height);
gameCanvas.Children.Add(boat);
}
private void physicsUpdate_Tick(object? sender, EventArgs e) //game physics timer
{
int boatSpeed = 10; //px per tick
int letterSpeed = 2; //px per tick
//Check if specific key is pressed on keyboard
keyLeftDown = KeyBoardHoldCheck(Key.Left);
keyRightDown = KeyBoardHoldCheck(Key.Right);
if (keyLeftDown && !CollisionDetect(boat, leftSideShore))
{
Canvas.SetLeft(boat, Canvas.GetLeft(boat) - boatSpeed); //move by x pixels per physics_update (tick)
boatIsMoving = true;
}
else if (keyRightDown)
{
if (!CollisionDetect(boat, rightSideShore))
{
Canvas.SetLeft(boat, Canvas.GetLeft(boat) + boatSpeed);
boatIsMoving = true;
}
else
boatIsUnloading = true;
}
//Move every letter in list
for (int i = lettersList.Count -1; i >= 0; i--)
{
if (!lettersList[i].aboard)
Canvas.SetLeft(lettersList[i].letter, Canvas.GetLeft(lettersList[i].letter) + letterSpeed);
if (!CollisionDetect(lettersList[i].letter, leftSideShore) && !CollisionDetect(lettersList[i].letter, rightSideShore)) //When there is no groud on left side or right side
{
if (!CollisionDetect(lettersList[i].letter, boat) || lettersList[i].isFalling) //when there is no boat
{
Letters lt = lettersList[i]; //set it to permanent fall (this avoids clipping inside the boat)
lt.isFalling = true;
lettersList[i] = lt;
Canvas.SetTop(lettersList[i].letter, Canvas.GetTop(lettersList[i].letter) + 10); //move down by 10px
}
else //the letters are aboard
{
if (keyRightDown && boatIsMoving) //stop movement of numbers present on the platform
{
Letters lt = lettersList[i];
lt.aboard = true;
lettersList[i] = lt;
Canvas.SetLeft(lettersList[i].letter, Canvas.GetLeft(lettersList[i].letter) + boatSpeed);
}
else if (keyLeftDown && boatIsMoving)
Canvas.SetLeft(lettersList[i].letter, Canvas.GetLeft(lettersList[i].letter) - boatSpeed);
}
}
if (boatIsUnloading)
{
Letters lt = lettersList[i];
lt.aboard = false;
lettersList[i] = lt;
}
//When letter gets behind canvas it gets destroyed
if (Canvas.GetLeft(lettersList[i].letter) > gameCanvas.ActualWidth)
{
gameCanvas.Children.Remove(lettersList[i].letter);
lettersList.RemoveAt(i);
lettersSaved++;
labelSaved.Content = "Počet zachráněných písmenek: " + lettersSaved; //change text in UI
}
//When letter gets behind canvas it gets destroyed (in water)
else if (Canvas.GetTop(lettersList[i].letter) > gameCanvas.ActualHeight)
{
gameCanvas.Children.Remove(lettersList[i].letter);
lettersList.RemoveAt(i);
lettersRemoved++;
labelDrowned.Content = "Počet utopených písmenek: " + lettersRemoved; //change text in UI
}
}
if ((lettersRemoved + lettersSaved) == alphabet.Length)
{
MessageBox.Show($"Konec hry! Počet zachráněných písmenek: {lettersSaved} a počet utopených písmenek byl: {lettersRemoved}");
physicsUpdate.Stop();
}
boatIsUnloading = false;
boatIsMoving = false;
}
private void spawnRate_Tick(object? sender, EventArgs e) //SpawnRate Timer for letters
{
//Creating new label
if ((lettersList.Count + lettersRemoved + lettersSaved) < alphabet.Length)
{
Label newLetter = new Label();
newLetter.Content = alphabet[lettersList.Count + lettersRemoved + lettersSaved];
newLetter.Background = Brushes.OrangeRed;
newLetter.FontSize = 18;
Canvas.SetLeft(newLetter, 0);
Canvas.SetTop(newLetter, gameCanvas.ActualHeight - leftSideShore.Height - (newLetter.FontSize + 15)); //15px offset for maintaining collision state (one pixel in leftSideShore)
gameCanvas.Children.Add(newLetter);
lettersList.Add(new Letters(newLetter, false, false));
}
else
spawnRate.Stop();
}
private bool CollisionDetect(Shape shape1, Shape shape2) //shape X shape
{
Rect rect1 = new Rect(Canvas.GetLeft(shape1), Canvas.GetTop(shape1), shape1.Width, shape1.Height);
Rect rect2 = new Rect(Canvas.GetLeft(shape2), Canvas.GetTop(shape2), shape2.Width, shape2.Height);
if (rect1.IntersectsWith(rect2))
return true;
else
return false;
}
private bool CollisionDetect(Label label, Shape shape) //label X shape
{
Rect rect1 = new Rect();
rect1.Location = new Point(Canvas.GetLeft(label), Canvas.GetTop(label));
rect1.Height = label.ActualHeight;
rect1.Width = label.ActualWidth;
Rect rect2 = new Rect(Canvas.GetLeft(shape), Canvas.GetTop(shape), shape.Width, shape.Height);
if (rect1.IntersectsWith(rect2))
return true;
else
return false;
}
private bool KeyBoardHoldCheck(Key key)
{
if (Keyboard.IsKeyDown(key))
return true;
else
return false;
}
}
}