-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
372 lines (309 loc) · 12.5 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <stdexcept>
#include <chrono>
#include <thread>
class Paddle {
public:
sf::Sprite sprite;
Paddle(sf::Texture& texture) {
sprite.setTexture(texture);
}
void setPosition(float x, float y) {
sprite.setPosition(x, y);
}
void move(float offsetX, float offsetY) {
sprite.move((0.5*offsetX), (0.5*offsetY));
}
};
class Ball {
public:
sf::CircleShape shape;
Ball(float radius) {
shape.setRadius(radius);
shape.setFillColor(sf::Color::Red);
}
void setPosition(float x, float y) {
shape.setPosition(x, y);
}
void move(float offsetX, float offsetY) {
shape.move((0.5*offsetX), (0.5*offsetY));
}
};
class Brick {
public:
sf::Sprite sprite;
Brick(sf::Texture& texture) {
sprite.setTexture(texture);
}
void setPosition(float x, float y) {
sprite.setPosition(x, y);
}
};
class Menu {
public:
sf::Text startText;
sf::Text aboutUsText;
sf::Text quitText;
Menu(sf::Font& font) {
startText.setFont(font);
startText.setCharacterSize(40);
startText.setFillColor(sf::Color::White);
startText.setString("Start Game");
startText.setPosition(50, 300);
aboutUsText.setFont(font);
aboutUsText.setCharacterSize(40);
aboutUsText.setFillColor(sf::Color::White);
aboutUsText.setString("About Us");
aboutUsText.setPosition(50, 400);
quitText.setFont(font);
quitText.setCharacterSize(40);
quitText.setFillColor(sf::Color::White);
quitText.setString("Quit Game");
quitText.setPosition(50, 500);
}
void draw(sf::RenderWindow& window) {
window.draw(startText);
window.draw(aboutUsText);
window.draw(quitText);
}
int handleInput(sf::RenderWindow& window) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
} else if (event.type == sf::Event::MouseButtonPressed) {
sf::Vector2f mousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));
if (startText.getGlobalBounds().contains(mousePosition)) {
return 1; // Start Game
} else if (aboutUsText.getGlobalBounds().contains(mousePosition)) {
return 2; // About Us
} else if (quitText.getGlobalBounds().contains(mousePosition)) {
return 3; // Quit Game
}
} else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::BackSpace) {
return 4; // Return to main menu
}
}
}
return 0; // No selection made
}
};
void drawBlinkingText(sf::RenderWindow& window, sf::Text& text, int blinkCount) {
for (int i = 0; i < blinkCount; ++i) {
window.clear();
window.display();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
window.clear();
window.draw(text);
window.display();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
int main() {
sf::RenderWindow window(sf::VideoMode::getFullscreenModes()[0], "Brick Breaker Game", sf::Style::Fullscreen);
try {
sf::Texture backgroundTexture;
if (!backgroundTexture.loadFromFile("background.jpg")) {
throw std::runtime_error("Unable to load background image");
}
sf::Sprite backgroundSprite(backgroundTexture);
sf::Music backgroundMusic;
if (!backgroundMusic.openFromFile("collision_sound.wav")) {
throw std::runtime_error("Unable to load background music");
}
backgroundMusic.setLoop(true);
backgroundMusic.play();
sf::SoundBuffer gameOverBuffer;
if (!gameOverBuffer.loadFromFile("game_over_sound.wav")) {
throw std::runtime_error("Unable to load game over sound");
}
sf::Sound gameOverSound;
gameOverSound.setBuffer(gameOverBuffer);
// Load paddle texture
sf::Texture paddleTexture;
if (!paddleTexture.loadFromFile("paddle_texture.png")) {
throw std::runtime_error("Unable to load paddle texture");
}
Paddle paddle(paddleTexture);
paddle.setPosition(350, window.getSize().y - 60);
Ball ball(15);
ball.setPosition(400, window.getSize().y - 100);
float ballSpeedX = 2.0f;
float ballSpeedY = -2.0f;
// Load brick texture
sf::Texture brickTexture;
if (!brickTexture.loadFromFile("brick_texture.png")) {
throw std::runtime_error("Unable to load brick texture");
}
const int numBricksX = 12;
const int numBricksY = 5;
std::vector<Brick> bricks;
// Adjust the size of bricks
double brickWidth = 0.2;
double brickHeight = 0.1;
for (int i = 0; i < numBricksX; ++i) {
for (int j = 0; j < numBricksY; ++j) {
Brick brick(brickTexture);
brick.setPosition(i * (brickWidth + 160) + 10, j * (brickHeight + 80) + 20);
bricks.push_back(brick);
}
}
// Adjust the size of the paddle
float paddleWidth = 0.0002;
float paddleHeight = 0.00001;
sf::RectangleShape redLine(sf::Vector2f(window.getSize().x, 50));
redLine.setFillColor(sf::Color::Red);
redLine.setPosition(0, window.getSize().y - 10);
bool gameover = false;
int score = 0;
sf::Font font;
if (!font.loadFromFile("ALIEN5.ttf")) {
throw std::runtime_error("Unable to load font");
}
sf::Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(80);
scoreText.setFillColor(sf::Color::White);
scoreText.setString("Score: 0");
scoreText.setPosition(10, 10);
sf::Text gameOverText;
gameOverText.setFont(font);
gameOverText.setCharacterSize(80);
gameOverText.setFillColor(sf::Color::White);
gameOverText.setString("Game Over");
sf::FloatRect gameOverBounds = gameOverText.getLocalBounds();
gameOverText.setOrigin(gameOverBounds.left + gameOverBounds.width / 2, gameOverBounds.top + gameOverBounds.height / 2);
gameOverText.setPosition(window.getSize().x / 2, window.getSize().y / 2);
Menu menu(font);
int menuChoice = 0; // 0: No selection, 1: Start Game, 2: About Us, 3: Quit Game
while (window.isOpen() && menuChoice == 0) {
menu.draw(window);
window.display();
menuChoice = menu.handleInput(window);
}
if (menuChoice == 1) {
// Start Game
while (window.isOpen() && !gameover) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && paddle.sprite.getPosition().x > 0) {
paddle.move(-10, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) &&
paddle.sprite.getPosition().x + paddle.sprite.getGlobalBounds().width < window.getSize().x) {
paddle.move(10, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
// Return to main menu
gameover = true;
menuChoice = 0;
}
ball.move(ballSpeedX, ballSpeedY);
if (ball.shape.getPosition().x < 0 || ball.shape.getPosition().x + ball.shape.getRadius() * 0.1 > window.getSize().x) {
ballSpeedX = -ballSpeedX;
}
if (ball.shape.getPosition().y < 0) {
ballSpeedY = -ballSpeedY;
}
if (ball.shape.getGlobalBounds().intersects(paddle.sprite.getGlobalBounds())) {
ballSpeedY = -ballSpeedY;
}
for (auto& brick : bricks) {
if (ball.shape.getGlobalBounds().intersects(brick.sprite.getGlobalBounds())) {
brick.setPosition(-100, -100);
ballSpeedY = -ballSpeedY;
// Increase score when the ball hits a brick
score++;
// Update the score text
scoreText.setString("Score: " + std::to_string(score));
}
}
// Check if all bricks are destroyed
bool allBricksDestroyed = true;
for (const auto& brick : bricks) {
if (brick.sprite.getPosition().x != -100 || brick.sprite.getPosition().y != -100) {
allBricksDestroyed = false;
break;
}
}
if (allBricksDestroyed) {
// All bricks are destroyed, level cleared
gameover = true;
backgroundMusic.stop();
// Additional actions for level cleared, if needed
}
if (ball.shape.getPosition().y + ball.shape.getRadius() * 2 > redLine.getPosition().y) {
gameover = true;
backgroundMusic.stop();
gameOverSound.play();
// Display the final score in the center with blinking effect
scoreText.setPosition(window.getSize().x / 2 - 100, window.getSize().y / 2 - 40);
drawBlinkingText(window, scoreText, 5);
}
window.clear();
window.draw(backgroundSprite);
window.draw(paddle.sprite);
window.draw(ball.shape);
for (const auto& brick : bricks) {
window.draw(brick.sprite);
}
window.draw(redLine);
// Draw the score during the game, only if the game is not over
if (!gameover) {
window.draw(scoreText);
}
// Draw "Game Over" text only when the game is over
if (gameover) {
window.draw(gameOverText);
}
window.display();
}
} else if (menuChoice == 2) {
// About Us
sf::Text aboutUs;
aboutUs.setFont(font);
aboutUs.setCharacterSize(30);
aboutUs.setFillColor(sf::Color::White);
aboutUs.setString("Authors:\nAhsan Raza\nHassan zaib jadoon\nMutahhar Fayyaz");
aboutUs.setPosition(50, 300);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::BackSpace) {
// Return to the main menu when BackSpace is pressed
return 0;
}
}
window.clear();
window.draw(backgroundSprite);
window.draw(aboutUs);
// Draw "Back" text
sf::Text backText;
backText.setFont(font);
backText.setCharacterSize(30);
backText.setFillColor(sf::Color::White);
backText.setString("Back (Press Backspace)");
backText.setPosition(50, 550);
window.draw(backText);
window.display();
}
} else if (menuChoice == 3) {
// Quit Game
window.close();
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return -1;
}
return 0;
}