-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopTheBubble.xaml.cs
208 lines (174 loc) · 6.11 KB
/
PopTheBubble.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
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 Clue
{
/// <summary>
/// Interaction logic for PopTheBubble.xaml
/// </summary>
public partial class PopTheBubble : UserControl
{
DispatcherTimer gameTimer = new DispatcherTimer(); // create a new instance of the dispatcher timer called gameTimer
List<Ellipse> removeThis = new List<Ellipse>(); // list of ellipses to be removed
// necessary game variables
int spawnRate = 60; // default spawn rate of circles
int currentRate; // current rate for spawning circles
int health = 350; // player health at the start
int posX; // x position of the circles
int posY; // y position of the circles
int score = 0; // current score for the game
double growthRate = 0.6; // default growth rate for each circle
Random rand = new Random(); // random number generator
bool gameCompleted = false; // prevents further play once the player reaches 25 points
// colour for the circles
Brush brush;
public PopTheBubble()
{
InitializeComponent();
// initialize game settings
gameTimer.Tick += GameLoop; // set the game loop
gameTimer.Interval = TimeSpan.FromMilliseconds(20); // timer ticks every 20 ms
gameTimer.Start(); // start the timer
currentRate = spawnRate; // set the current rate to the default spawn rate
gameTimer.Stop();
}
private void GameLoop(object sender, EventArgs e)
{
if (gameCompleted) return; // Stop the game loop if the game is completed
// game loop logic
txtScore.Content = "Score: " + score;
currentRate -= 2;
if (currentRate < 1)
{
currentRate = spawnRate;
posX = rand.Next(15, 700);
posY = rand.Next(50, 350);
// Generate a random shade of blue
brush = new SolidColorBrush(Color.FromRgb(0, 0, (byte)rand.Next(100, 255)));
Ellipse circle = new Ellipse
{
Tag = "circle",
Height = 10,
Width = 10,
Stroke = Brushes.Black,
StrokeThickness = 1,
Fill = brush
};
Canvas.SetLeft(circle, posX);
Canvas.SetTop(circle, posY);
MyCanvas.Children.Add(circle);
}
foreach (var x in MyCanvas.Children.OfType<Ellipse>())
{
x.Height += growthRate;
x.Width += growthRate;
x.RenderTransformOrigin = new Point(0.5, 0.5);
if (x.Width > 70)
{
removeThis.Add(x);
health -= 15;
}
}
if (health > 1)
{
healthBar.Width = health;
}
else
{
GameOverFunction();
}
foreach (Ellipse i in removeThis)
{
MyCanvas.Children.Remove(i);
}
if (score > 5)
{
spawnRate = 25;
}
if (score > 20)
{
spawnRate = 15;
growthRate = 1.5;
}
}
private void ClickOnCanvas(object sender, MouseButtonEventArgs e)
{
if (gameCompleted) return; // Stop interaction if the game is completed
if (e.OriginalSource is Ellipse)
{
Ellipse circle = (Ellipse)e.OriginalSource;
MyCanvas.Children.Remove(circle);
score++;
}
}
private void GameOverFunction()
{
gameTimer.Stop();
// If the game is already completed (win or loss), exit the function
if (gameCompleted) return;
// Mark the game as completed
gameCompleted = true;
if (score >= 25)
{
gameCompleted = true; // Lock the game from further play
//MessageBox.Show("Congratulations! You scored " + score + " points! Press \"OK\" to clam your cards!", "Game Over");
MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
if (mainWindow != null)
{
mainWindow.GameWin();
gameTimer.Stop();
}
}
else
{
//MessageBox.Show("Game Over! You scored " + score + " points. Try again.", "Game Over");
gameCompleted = true;
MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
if (mainWindow != null)
{
mainWindow.GameLose();
}
// Reset game values if the score is below 25
//ResetGame();
}
}
private void ResetGame()
{
foreach (var y in MyCanvas.Children.OfType<Ellipse>())
{
removeThis.Add(y);
}
foreach (Ellipse i in removeThis)
{
MyCanvas.Children.Remove(i);
}
growthRate = .6;
spawnRate = 60;
score = 0;
currentRate = 5;
health = 350;
removeThis.Clear();
gameTimer.Start();
}
private void UserControl_GotFocus(object sender, RoutedEventArgs e)
{
gameCompleted = false;
ResetGame();
}
private void UserControl_LostFocus(object sender, RoutedEventArgs e)
{
gameTimer.Stop();
}
}
}