-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
292 lines (230 loc) · 6.44 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 CrappyGame;
using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace Snaker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : GameWindow
{
Storyboard WalkStoryboard { get; set; }
Storyboard JumpStoryboard { get; set; }
Storyboard EndGameStoryboard { get; set; }
Storyboard LevelCompleteSotryboard { get; set; }
List<Rect> Snakes { get; set; }
List<Image> SnakeImages { get; set; }
List<DispatcherTimer> SnakeTimers { get; set; }
bool GameOver = false;
bool Jumping = false;
int RemainingSnakes = 0;
bool Advancing = false;
double CupToX;
private int _Level;
protected int Level
{
get
{
return _Level;
}
set
{
_Level = value;
LevelDisplay = String.Format("Level {0}", value);
}
}
public static readonly DependencyProperty LevelDisplayProperty = DependencyProperty.Register("LevelDisplay", typeof(String), typeof(MainWindow));
public string LevelDisplay
{
get
{
return (string)GetValue(LevelDisplayProperty);
}
set
{
SetValue(LevelDisplayProperty, value);
}
}
public MainWindow()
{
InitializeComponent();
WalkStoryboard = (Storyboard)FindResource("WalkStoryboard");
JumpStoryboard = (Storyboard)FindResource("JumpStoryboard");
JumpStoryboard.Completed += (o, e) =>
{
Jumping = false;
if(GameOver)
return;
WalkStoryboard.Begin(this, true);
};
EndGameStoryboard = (Storyboard)FindResource("EndGameStoryboard");
LevelCompleteSotryboard = (Storyboard)FindResource("LevelCompleteSotryboard");
LevelCompleteSotryboard.Completed += (o, e) =>
{
cupTranslate.X = CupToX;
DispatcherTimer advanceTimer = new DispatcherTimer(TimeSpan.FromSeconds(3), DispatcherPriority.Normal, AdvanceTimer_Tick, Dispatcher);
LevelCompleteSotryboard.Stop();
};
Input.RegisterKeyDown(Key.Space, GirlJump);
Input.RegisterKeyDown(Key.Space, (k, gt) => { if(GameOver) ResetGame(); });
Snakes = new List<Rect>();
SnakeImages = new List<Image>();
SnakeTimers = new List<DispatcherTimer>();
ResetGame();
}
private void ResetGame()
{
GameOver = false;
Level = 0;
girlRotate.Angle = 0;
girlTranslate.X = 0;
girlTranslate.Y = 0;
cupRotate.Angle = 0;
cupTranslate.X = 0;
cupTranslate.Y = 0;
Jumping = true;
JumpStoryboard.Begin(this, true);
AdvanceLevel();
}
private void AdvanceLevel()
{
Level++;
RemainingSnakes = 0;
Snakes.Clear();
foreach(var snakeImage in SnakeImages)
Container.Children.Remove(snakeImage);
SnakeImages.Clear();
foreach(var snakeTimer in SnakeTimers)
snakeTimer.Stop();
SnakeTimers.Clear();
// Always have the snakes in the same pattern
Random rng = new Random(Level);
double cumulativeTime = 0;
for(int i = 0; i < Level + 2; i++)
{
// Snakes will appear every 3 to 8 seconds
double snakeDuration = 3 + (rng.NextDouble() * 5);
cumulativeTime += snakeDuration;
DispatcherTimer snakeTimer = new DispatcherTimer(TimeSpan.FromSeconds(cumulativeTime), DispatcherPriority.Normal, SnakeTimer_Tick, Dispatcher);
SnakeTimers.Add(snakeTimer);
RemainingSnakes++;
}
Advancing = false;
}
protected void SnakeTimer_Tick(object sender, EventArgs e)
{
DispatcherTimer timer = ((DispatcherTimer)sender);
timer.Stop();
SnakeTimers.Remove(timer);
if(GameOver)
return;
Rect snake = new Rect(Container.ActualWidth, Container.ActualHeight - 150, 100, 100);
Snakes.Add(snake);
BitmapImage bitmap = (BitmapImage)FindResource("Snake");
Image image = new Image();
image.Width = snake.Width;
snake.Height = image.ActualHeight;
image.Source = bitmap;
SnakeImages.Add(image);
Container.Children.Add(image);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
GroundLine.X2 = this.ActualWidth;
DoubleAnimation horizAnim = (DoubleAnimation)LevelCompleteSotryboard.Children.First();
Rect girlBox = GetBoundingBox(GirlImage);
Rect cupBox = GetBoundingBox(GoldenPImage);
CupToX = (girlBox.X + girlBox.Width) - (cupBox.X - cupBox.Width);
horizAnim.To = CupToX;
}
protected override void RenderFrame(GameTime time)
{
base.RenderFrame(time);
if(GameOver || Advancing)
return;
DrawSnakes(time);
CheckCollisions(time);
if(RemainingSnakes <= 0)
GetGoldenCup();
}
private void GetGoldenCup()
{
Advancing = true;
LevelCompleteSotryboard.Begin(this, true);
}
private void AdvanceTimer_Tick(object sender, EventArgs e)
{
((DispatcherTimer)sender).Stop();
LevelCompleteSotryboard.Stop();
cupRotate.Angle = 0;
cupTranslate.Y = 0;
cupTranslate.X = 0;
LevelCompleteSotryboard.Resume();
AdvanceLevel();
}
private void CheckCollisions(GameTime time)
{
Rect girlBox = GetBoundingBox(GirlImage);
foreach(var snake in Snakes)
if(girlBox.IntersectsWith(snake))
EndGame();
}
private Rect GetBoundingBox(Image control)
{
var xform = control.TransformToAncestor(Container);
Point corner = xform.Transform(new Point(0, 0));
Rect box = new Rect(corner.X, corner.Y, control.ActualWidth, control.ActualHeight);
return box;
}
private void EndGame()
{
GameOver = true;
JumpStoryboard.Stop();
WalkStoryboard.Stop();
girlRotate.Angle = 0;
EndGameStoryboard.Begin(this, true);
}
private void DrawSnakes(GameTime time)
{
for(int i = 0; i < Snakes.Count; i++)
{
Rect snake = Snakes[i];
Image snakeImage = SnakeImages[i];
snake.Offset(time.ElapsedSeconds * -(Container.ActualWidth / 5), 0);
Snakes[i] = snake;
snakeImage.SetValue(Canvas.LeftProperty, snake.Left);
snakeImage.SetValue(Canvas.TopProperty, snake.Top);
if(snake.X + snake.Width < 0)
{
SnakeImages.Remove(snakeImage);
Container.Children.Remove(snakeImage);
Snakes.Remove(snake);
RemainingSnakes--;
}
}
}
protected void GirlJump(Key key, GameTime time)
{
if(GameOver)
return;
if(Jumping)
return;
Jumping = true;
WalkStoryboard.Stop(this);
JumpStoryboard.Begin(this, true);
}
}
}