-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
217 lines (182 loc) · 8.13 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
namespace CrazyBird
{
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;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// create a new instance of the timer class called game timer
readonly DispatcherTimer gameTimer = new DispatcherTimer();
// new gravity integer hold the vlaue 8
int gravity = 8;
// score keeper
double score;
// new rect class to help us detect collisions
Rect FlappyRect;
// new boolean for checking if the game is over or not
bool gameover = false;
public MainWindow()
{
InitializeComponent();
// set the default settings for the timer
gameTimer.Tick += GameEngine; // link the timer tick to the game engine event
gameTimer.Interval = TimeSpan.FromMilliseconds(20); // set the interval to 20 miliseconds
// run the start game function
StartGame();
}
private void Canvas_KeyisDown(object sender, KeyEventArgs e)
{
// if the space key is pressed
if (e.Key == Key.Space)
{
// rotate the bird image to -20 degrees from the center position
flappyBird.RenderTransform = new RotateTransform(-20, flappyBird.Width / 2, flappyBird.Height / 2);
// change gravity so it will move upwards
gravity = -8;
}
if (e.Key == Key.R && gameover == true)
{
// if the r key is pressed AND game over boolean is set to true
// run the start game function
StartGame();
}
}
private void Canvas_KeyisUp(object sender, KeyEventArgs e)
{
// if the keys are released then we will change the rotation of the flappy bird to 5 degrees from the center
flappyBird.RenderTransform = new RotateTransform(5, flappyBird.Width / 2, flappyBird.Height / 2);
// change the gravity to 8 so the bird will go downwards and not up
gravity = 8;
}
private void StartGame()
{
// this is the start game function
// this function will load all of the default values to start this game
// first make a temp integer with the value 200
int temp = 200;
// set score to 0
score = 0;
// set the flappy bird top position to 100 pixels
Canvas.SetTop(flappyBird, 100);
// set game over to false
gameover = false;
// the loop below will simply check for each image in this game and set them to their default positions
foreach (var x in MyCanvas.Children.OfType<Image>())
{
// set obs1 pipes to its default position
if (x is Image && (string)x.Tag == "obs1")
{
Canvas.SetLeft(x, 500);
}
// set obs2 pipes to its default position
if (x is Image && (string)x.Tag == "obs2")
{
Canvas.SetLeft(x, 800);
}
// set obs3 pipes to its default position
if (x is Image && (string)x.Tag == "obs3")
{
Canvas.SetLeft(x, 1000);
}
// set the clouds to its default position
if (x is Image && (string)x.Tag == "clouds")
{
Canvas.SetLeft(x, (300 + temp));
temp = 800;
}
}
// start the main game timer
gameTimer.Start();
}
private void GameEngine(object sender, EventArgs e)
{
// this is the game engine event linked to the timer
// first update the score text label with the score integer
scoreText.Content = "Score: " + score;
// link the flappy bird image to the flappy rect class
FlappyRect = new Rect(Canvas.GetLeft(flappyBird), Canvas.GetTop(flappyBird), flappyBird.Width -12, flappyBird.Height - 6);
// set the gravity to the flappy bird image in the canvas
Canvas.SetTop(flappyBird, Canvas.GetTop(flappyBird) + gravity);
// check if the bird has either gone off the screen from top or bottom
if (Canvas.GetTop(flappyBird) + flappyBird.Height > 490 || Canvas.GetTop(flappyBird) < -30)
{
// if it has then we end the game and show the reset game text
gameTimer.Stop();
gameover = true;
scoreText.Content += " Press R to Try Again";
}
// below is the main loop, this loop will go through each image in the canvas
// if it finds any image with the tags and follow the instructions with them
foreach (var x in MyCanvas.Children.OfType<Image>())
{
if ((string)x.Tag == "obs1" || (string)x.Tag == "obs2" || (string)x.Tag == "obs3")
{
// if we found an image with the tag obs1,2 or 3 then we will move it towards left of the scree
Canvas.SetLeft(x, Canvas.GetLeft(x) - 5);
// create a new rect called pillars and link the rectangles to it
Rect pillars = new Rect(Canvas.GetLeft(x), Canvas.GetTop(x), x.Width, x.Height);
// if the flappy rect and the pillars rect collide
if (FlappyRect.IntersectsWith(pillars))
{
// stop the timer, set the game over to true and show the reset text
gameTimer.Stop();
gameover = true;
scoreText.Content += " Press R to Try Again";
}
}
// if the first layer of pipes leave the scene and go to -100 pixels from the left
// we need to reset the pipe to come back again
if ((string)x.Tag == "obs1" && Canvas.GetLeft(x) < -100)
{
// reset the pipe to 800 pixels
Canvas.SetLeft(x, 800);
// add 1 to the score
score += .5;
}
// if the second layer of pipes leave the scene and go to -200 pixels from the left
if ((string)x.Tag == "obs2" && Canvas.GetLeft(x) < -200)
{
// we set that pipe to 800 pixels
Canvas.SetLeft(x, 800);
// add 1 to the score
score += .5;
}
// if the third layer of pipes leave the scene and go to -250 pixels from the left
if ((string)x.Tag == "obs3" && Canvas.GetLeft(x) < -250)
{
// we set the pipe to 800 pixels
Canvas.SetLeft(x, 800);
// add 1 to the score
score += .5;
}
// if find any of the images with the clouds tag on it
if ((string)x.Tag == "clouds")
{
// then we will slowly move the cloud towards left of the screen
Canvas.SetLeft(x, Canvas.GetLeft(x) - .6);
// if the clouds have reached -220 pixels then we will reset it
if (Canvas.GetLeft(x) < -220)
{
// reset the cloud images to 550 pixels
Canvas.SetLeft(x, 550);
}
}
}
}
}
}