-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
307 lines (228 loc) · 8.69 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
using System;
using System.Collections.Generic;
using System.IO;
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.Media;
using System.Windows.Threading;
namespace RocketMan
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer gameTimer = new DispatcherTimer();
Random random = new Random();
bool goLeft, goRight, gameOver;
ImageBrush playerImage = new ImageBrush();
ImageBrush starImage = new ImageBrush();
double speed = 10;
int playerSpeed = 8;
double starSpeed = 5;
int starFrequency = 50;
int starCount = 0;
int speedUp = 200;
double duration = 0;
Rect playerHitBox;
List<Rectangle> ToRemove = new List<Rectangle>();
private MediaPlayer player1 = new MediaPlayer();
private MediaPlayer player2 = new MediaPlayer();
public MainWindow()
{
InitializeComponent();
myCanvas.Focus();
gameTimer.Tick += GameLoop;
gameTimer.Interval = TimeSpan.FromMilliseconds(20);
StartGame();
}
private void GameLoop(object sender, EventArgs e)
{
duration += 0.05;
--starFrequency;
--speedUp;
if (speedUp<0)
{
speed += 1;
starSpeed += 0.9;
speedUp = random.Next(200, 600);
}
StarCount.Content = starCount.ToString();
playerHitBox = new Rect(Canvas.GetLeft(Player), Canvas.GetTop(Player), Player.ActualWidth, Player.ActualHeight);
// pomjeranje rakete
if (goLeft == true && Canvas.GetLeft(Player) > 5)
{
Canvas.SetLeft(Player, Canvas.GetLeft(Player) - playerSpeed);
}
if(goRight == true && Canvas.GetLeft(Player) + 80 < Application.Current.MainWindow.Width)
{
Canvas.SetLeft(Player, Canvas.GetLeft(Player) + playerSpeed);
}
// postavljanje nove zvijezde
if (starFrequency < 1)
{
MakeStar();
starFrequency = random.Next(50, 100);
}
// provjera kolizije sa drugim planetama i sakupljanje zvjezdica
foreach (var x in myCanvas.Children.OfType<Rectangle>())
{
if ((string)x.Tag == "Planet")
{
Canvas.SetTop(x, Canvas.GetTop(x) + speed);
if (Canvas.GetTop(x) > 500)
{
ChangePlanets(x);
}
Rect planetHitBox = new Rect(Canvas.GetLeft(x), Canvas.GetTop(x), x.Width-25 , x.Height-25);
if (playerHitBox.IntersectsWith(planetHitBox))
{
gameOver = true;
gameTimer.Stop();
FinishWindow finishWindow = new FinishWindow();
finishWindow.Closed += (send, ee) => StartGame();
var projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string text = System.IO.File.ReadAllText(projectPath+"/resources/scores.txt");
int highestScore = Int32.Parse(text);
if(starCount>highestScore)
{
highestScore = starCount;
System.IO.File.WriteAllText(projectPath+"/resources/scores.txt", highestScore.ToString());
}
player2.Open(new Uri(projectPath + "/resources/gameOverSound.wav", UriKind.Absolute));
player2.Play();
finishWindow.Show();
}
}
if ((string)x.Tag == "Star")
{
Canvas.SetTop(x, Canvas.GetTop(x) + starSpeed);
Rect starHitBox = new Rect(Canvas.GetLeft(x), Canvas.GetTop(x), x.Width, x.Height);
if (playerHitBox.IntersectsWith(starHitBox))
{
++starCount;
playStarSound();
ToRemove.Add(x);
}
if(Canvas.GetTop(x) > 500)
{
ToRemove.Add(x);
}
}
}
foreach(Rectangle rectangle in ToRemove)
{
myCanvas.Children.Remove(rectangle);
}
}
private void OnKeyUp(object sender, KeyEventArgs e)
{
if(e.Key == Key.Left)
{
goLeft = false;
}
if(e.Key == Key.Right)
{
goRight = false;
}
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Left)
{
goLeft = true;
}
if (e.Key == Key.Right)
{
goRight = true;
}
if (e.Key == Key.Space && gameOver == true)
{
StartGame();
}
}
public void StartGame()
{
playBackGroundMusic();
speed = 5;
playerSpeed = 12;
starSpeed = 4;
starCount = 0;
goLeft = false;
goRight = false;
gameOver = false;
playerImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/resources/player2.png"));
starImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/resources/1.png"));
starImage.Stretch = Stretch.UniformToFill;
playerImage.Stretch = Stretch.UniformToFill;
Player.Fill = playerImage;
// postavljanje prepreka
foreach (var x in myCanvas.Children.OfType<Rectangle>())
{
if ((string)x.Tag == "Planet")
{
ChangePlanets(x);
//Canvas.SetTop(x, Canvas.GetTop(x) + speed);
//if (Canvas.GetTop(x) > 500)
//{
// Canvas.SetTop(x, random.Next(100, 400) * -1);
// Canvas.SetLeft(x, random.Next(0, 430));
// ChangePlanets(x);
//}
}
if((string)x.Tag == "Star")
{
ToRemove.Add(x);
}
}
ToRemove.Clear();
gameTimer.Start();
}
private void playBackGroundMusic()
{
var projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
player1.Open(new Uri(projectPath + "/resources/backgroundMusic.wav", UriKind.Absolute));
player1.Position = TimeSpan.Zero;
player1.Play();
}
private void playStarSound()
{
var projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
player2.Open(new Uri(projectPath + "/resources/starSound.wav", UriKind.Absolute));
player2.Play();
}
private void ChangePlanets(Rectangle planet)
{
int planetNumber = random.Next(1, 10);
ImageBrush planetImage = new ImageBrush();
planetImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/resources/planet" + planetNumber + ".png"));
planetImage.Stretch = Stretch.Uniform;
planet.Fill = planetImage;
Canvas.SetTop(planet, random.Next(100, 400) * -1);
Canvas.SetLeft(planet, random.Next(0, 430));
}
private void MakeStar()
{
Rectangle newStar = new Rectangle
{
Height = 30,
Width = 30,
Tag = "Star",
Fill = starImage
};
Canvas.SetTop(newStar, random.Next(100, 400) * -1);
Canvas.SetLeft(newStar, random.Next(0, 430));
myCanvas.Children.Add(newStar);
}
}
}