/
Window1.xaml.cs
173 lines (143 loc) · 5.66 KB
/
Window1.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
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.Shapes;
using System.Windows.Threading;
namespace Flappy_Bird_Bootleg
{ // ************************************************************************************
// Title: Flappy Bird Ripoff
// Application Type: WPF App(.Net Framework) - C#
// Description: Flappy Bird application recreation for console, with use of C# and WPF
// Author: Casey Vanderpleog
// Date Created: 6/26/2020
// Last Modified: 6/29/202
// ************************************************************************************
/// <summary>
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
DispatcherTimer gameTimer = new DispatcherTimer();
double score;
int gravity = 8;
bool gameOver;
Rect flappyBirdHitBox;
public Window1()
{
InitializeComponent();
gameTimer.Tick += MainEventTimer;
gameTimer.Interval = TimeSpan.FromMilliseconds(20); // how often we want the game to proceed
StartGame();
}
private void MainEventTimer(object sender, EventArgs e)
{
txtScore.Content = "Score: " + score; // show's score after each obstacle item crossed
flappyBirdHitBox = new Rect(Canvas.GetLeft(flappyBird), Canvas.GetTop(flappyBird), flappyBird.Width - 5, flappyBird.Height); //determines the hitbox of Flappy Bird
Canvas.SetTop(flappyBird, Canvas.GetTop(flappyBird) + gravity); // brings flappybird item along with the gravity definition
if (Canvas.GetTop(flappyBird) < -10 || Canvas.GetTop(flappyBird) > 458) // endsgame if user goes off screen
{
EndGame();
}
foreach (var x in MyCanvas.Children.OfType<Image>())
{
if ((string)x.Tag == "obs1" || (string)x.Tag == "obs2" || (string)x.Tag == "obs3") //set pipe speed and spacing
{
Canvas.SetLeft(x, Canvas.GetLeft(x) - 5);
if (Canvas.GetLeft(x) < -100)
{
Canvas.SetLeft(x, 800);
score += .5; // obtain score for each obstacle item passed
}
Rect pipeHitBox = new Rect(Canvas.GetLeft(x), Canvas.GetTop(x), x.Width, x.Height); // gives htibox for obstacles
if (flappyBirdHitBox.IntersectsWith(pipeHitBox)) // if user intersects item hitbox sends method to call end
{
EndGame();
}
}
if ((string)x.Tag == "cloud") //cloud item definition
{
Canvas.SetLeft(x, Canvas.GetLeft(x) - 2); // cloud item speed
if (Canvas.GetLeft(x) < -250) // resets cloud postion after off screen
{
Canvas.SetLeft(x, 550);
}
}
}
}
/// <summary>
/// Key bind for application
/// Space to Rise
/// R to restart
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void KeyIsDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
flappyBird.RenderTransform = new RotateTransform(-20, flappyBird.Width / 2, flappyBird.Height / 2); // rotates bird slighlty up to signify it is going up
gravity = -8; // bird will go up through this definition
}
if (e.Key == Key.R && gameOver == true) // resets game
{
StartGame();
}
}
private void KeyIsUp(object sender, KeyEventArgs e)
{
flappyBird.RenderTransform = new RotateTransform(5, flappyBird.Width / 2, flappyBird.Height / 2); // roates bird downward to signify it's declining
gravity = 8;
}
/// <summary>
/// Starts application
/// </summary>
private void StartGame()
{
MyCanvas.Focus();
int temp = 300;
score = 0;
gameOver = false;
Canvas.SetTop(flappyBird, 190); // default position once started
foreach (var x in MyCanvas.Children.OfType<Image>()) // any item apart of the game will have a loop
{
if ((string)x.Tag == "obs1") // first pipe item obstacle
{
Canvas.SetLeft(x, 500);
}
if ((string)x.Tag == "obs2") // second pipe obstacle
{
Canvas.SetLeft(x, 800);
}
if ((string)x.Tag == "obs3") // third pipe obstacle
{
Canvas.SetLeft(x, 1100);
}
if ((string)x.Tag == "cloud") // background cloud item
{
Canvas.SetLeft(x, 300 + temp);
temp = 800;
}
}
gameTimer.Start();
}
/// <summary>
/// End's game run time, prompts user to restart
/// </summary>
private void EndGame()
{
gameTimer.Stop();
gameOver = true;
txtScore.Content += " Game Over, Press R to Restart";
}
}
}