-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
277 lines (236 loc) · 8.64 KB
/
main.cpp
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
#include<SFML\Graphics.hpp>
#include<Windows.h>
#include"Functions.cpp"
#include"textBox.cpp"
#include"screenButton.cpp"
#include<iostream>
using namespace sf;
using namespace std;
// Creates a loop that displays a pause screen until the game is unpaused.
// It continuously checks for user input to resume the game or close the window.
void Pause(bool game_pause, RenderWindow* window, Game* play, Event event) {
while (game_pause) {
window->clear();
play->Print(window);
window->display();
if (event.type == Event::Closed) {
window->close();
}
if (Keyboard::isKeyPressed(Keyboard::P)) {
game_pause = false;
return;
}
}
}
int main() {
// Renders the main window.
RenderWindow window(VideoMode(400, 400), "BOMBBRUSHER");
// Code that runs the easy, medium & hard mode windows of the game.
auto openGameModeWindow = [&](int difficultyValue, int windowWidth, int windowHeigth) {
RenderWindow GameWindow(VideoMode(windowWidth, windowHeigth), "BOMBBRUSHER");
GameWindow.clear(Color::White);
Event event;
Game* play = new Game(difficultyValue);
while (GameWindow.isOpen()) {
while (GameWindow.pollEvent(event)) {
if (event.type == Event::Closed) {
GameWindow.close();
}
GameWindow.clear();
play->gamecontroller(&GameWindow);
if (play->gameRunner == false) {
Pause(true, &GameWindow, play, event);
delete play;
play = new Game(difficultyValue);
play->gameRunner = true;
}
GameWindow.display();
}
}
};
// Opens highscore game mode window.
auto openHighScoreWindow = [&](RenderWindow& mainWindow) {
// Renders the highscore game mode.
RenderWindow HighScoreWindow(VideoMode(600, 600), "BOMBBRUSHER");
Event event;
ScreenButton backButton(Vector2f(210, 500), Vector2f(200, 50), "Back", [&]() { // Object of back button.
cout << "Back button clicked!\n";
HighScoreWindow.close();
mainWindow.create(VideoMode(400, 400), "BOMBBRUSHER");
});
ScreenButton easyButton(Vector2f(10, 50), Vector2f(180, 50), "Easy", []() { // Object of easy difficulty button.
cout << "Easy button clicked!\n";
});
ScreenButton mediumButton(Vector2f(210, 50), Vector2f(180, 50), "Medium", []() { // Object of medium difficulty button.
cout << "Medium button clicked!\n";
});
ScreenButton hardButton(Vector2f(410, 50), Vector2f(180, 50), "Hard", []() { // Object of hard difficulty button.
cout << "Hard button clicked!\n";
});
while (HighScoreWindow.isOpen()) {
while (HighScoreWindow.pollEvent(event)) {
if (event.type == Event::Closed) {
HighScoreWindow.close();
}
// Handles button clicks.
if (event.type == Event::MouseButtonPressed) {
if (event.mouseButton.button == Mouse::Left) {
Vector2f mousePos = HighScoreWindow.mapPixelToCoords(Vector2i(event.mouseButton.x, event.mouseButton.y));
// Handle button click.
if (backButton.contains(mousePos)) {
backButton.handleClick();
}
else if (easyButton.contains(mousePos)) {
easyButton.handleClick();
}
else if (mediumButton.contains(mousePos)) {
mediumButton.handleClick();
}
else if (hardButton.contains(mousePos)) {
hardButton.handleClick();
}
}
}
// Gets mouse position
Vector2f mousePos = HighScoreWindow.mapPixelToCoords(Mouse::getPosition(HighScoreWindow));
HighScoreWindow.clear(Color::White);
// Handle hover effect for buttons.
backButton.handleHover(mousePos);
easyButton.handleHover(mousePos);
mediumButton.handleHover(mousePos);
hardButton.handleHover(mousePos);
// Draw the button
backButton.draw(HighScoreWindow);
easyButton.draw(HighScoreWindow);
mediumButton.draw(HighScoreWindow);
hardButton.draw(HighScoreWindow);
// Display window.
HighScoreWindow.display();
}
}
};
// Code for the difficulty selection window.
auto openSelectDifficultyWindow = [&](RenderWindow& mainWindow) {
// Closes the main window.
window.close();
RenderWindow selectDifficultyWindow(VideoMode(800, 600), "BOMBBRUSHER");
// Create the high score button outside of the lambda function
ScreenButton easyDifficultyButton(Vector2f(300, 150), Vector2f(200, 50), "Easy", [&]() { // Object of easy difficulty button.
cout << "Easy button clicked!\n";
selectDifficultyWindow.close();
openGameModeWindow(10, 340, 400);
});
ScreenButton mediumDifficultyButton(Vector2f(300, 250), Vector2f(200, 50), "Medium", [&]() { // Object of medium difficulty button.
cout << "Medium button clicked!\n";
selectDifficultyWindow.close();
openGameModeWindow(15, 500, 540);
});
ScreenButton hardDifficultyButton(Vector2f(300, 350), Vector2f(200, 50), "Hard", [&]() { // Object of hard difficulty button.
cout << "Hard button clicked!\n";
selectDifficultyWindow.close();
openGameModeWindow(20, 660, 700);
});
ScreenButton backButton(Vector2f(300, 450), Vector2f(200, 50), "Back", [&]() { // Object of back difficulty button.
cout << "Back button clicked!\n";
selectDifficultyWindow.close();
mainWindow.create(VideoMode(400, 400), "BOMBBRUSHER");
});
while (selectDifficultyWindow.isOpen()) {
Event event;
TextBox textBox(Vector2f(250, 50), Vector2f(300, 50), "Diffculty Selection");
while (selectDifficultyWindow.pollEvent(event)) {
if (event.type == Event::Closed)
selectDifficultyWindow.close();
// Handles button clicks.
if (event.type == Event::MouseButtonPressed) {
if (event.mouseButton.button == Mouse::Left) {
Vector2f mousePos = selectDifficultyWindow.mapPixelToCoords(Vector2i(event.mouseButton.x, event.mouseButton.y));
// Handle easyButton click.
if (easyDifficultyButton.contains(mousePos)) {
easyDifficultyButton.handleClick();
}
// Handle mediumButton click.
else if (mediumDifficultyButton.contains(mousePos)) {
mediumDifficultyButton.handleClick();
}
// Handle hardButton click.
else if (hardDifficultyButton.contains(mousePos)) {
hardDifficultyButton.handleClick();
}
// Handle backButton click.
else if (backButton.contains(mousePos)) {
backButton.handleClick();
}
}
}
// Gets mouse position
Vector2f mousePos = selectDifficultyWindow.mapPixelToCoords(Mouse::getPosition(selectDifficultyWindow));
// Handle hover effect for buttons.
easyDifficultyButton.handleHover(mousePos);
mediumDifficultyButton.handleHover(mousePos);
hardDifficultyButton.handleHover(mousePos);
backButton.handleHover(mousePos);
// Needs to be here, as if it is after Draw, it will hide buttons.
selectDifficultyWindow.clear(Color::White);
// Draw the button
easyDifficultyButton.draw(selectDifficultyWindow);
mediumDifficultyButton.draw(selectDifficultyWindow);
hardDifficultyButton.draw(selectDifficultyWindow);
backButton.draw(selectDifficultyWindow);
textBox.draw(selectDifficultyWindow);
// Display window.
selectDifficultyWindow.display();
}
}
};
// This loop is running the home window.
while (window.isOpen()) {
TextBox textBox(Vector2f(5, 35), Vector2f(400, 150));
textBox.setTexture();
// Create the play button.
ScreenButton playbutton(Vector2f(100, 200), Vector2f(200, 50), "Play", [&]() {
cout << "Play was pressed" << endl;
openSelectDifficultyWindow(window);
});
// Create the high score button.
ScreenButton highScoreButton(Vector2f(100, 300), Vector2f(200, 50), "High Score", [&]() {
cout << "High Score button clicked!\n";
window.close();
openHighScoreWindow(window);
});
// Running the main window.
while (window.isOpen()) {
// Process events
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed)
window.close();
// Handles button click for main window.
if (event.type == Event::MouseButtonPressed) {
if (event.mouseButton.button == Mouse::Left) {
Vector2f mousePos = window.mapPixelToCoords(Vector2i(event.mouseButton.x, event.mouseButton.y));
// Runs when "Play" on main screen is clicked.
if (playbutton.contains(mousePos)) {
playbutton.handleClick();
}
// Runs when "High Score" on main screen is clicked.
else if (highScoreButton.contains(mousePos)) {
highScoreButton.handleClick();
}
}
}
}
window.clear(Color::White);
// Get mouse position
Vector2f mousePos = window.mapPixelToCoords(Mouse::getPosition(window));
// Handle hover effect for button.
highScoreButton.handleHover(mousePos);
playbutton.handleHover(mousePos);
// Draw all the buttons and the text box.
textBox.draw(window);
playbutton.draw(window);
highScoreButton.draw(window);
window.display();
}
}
}