-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
454 lines (413 loc) · 17.7 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//Brandon Caserto
//COP 3003 - CRN 15050 - TR - 202301
//Class Project
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <cmath>
#include <random>
#include <vector>
#include <array>
//class for moles being spawned and added to vector
class Mole {
private:
sf::Texture moleTexture;
sf::Sprite moleSprite;
sf::FloatRect moleRect;
int upTime = 0;
sf::Sprite small_hole;
sf::Sprite medium_hole;
sf::Sprite large_hole;
sf::Sprite mole_1;
sf::Sprite mole_2;
sf::Sprite mole_3;
public:
Mole () {
moleTexture.loadFromFile("mole_sheet.png");
small_hole.setTexture(moleTexture);
small_hole.setTextureRect(sf::IntRect(540,1130,125,85));
medium_hole.setTexture(moleTexture);
medium_hole.setTextureRect(sf::IntRect(280,1125,200,85));
large_hole.setTexture(moleTexture);
large_hole.setTextureRect(sf::IntRect(0,1110,255,85));
mole_1.setTexture(moleTexture);
mole_1.setTextureRect(sf::IntRect(750,800,230,125));
mole_2.setTexture(moleTexture);
mole_2.setTextureRect(sf::IntRect(500,800,230,125));
mole_3.setTexture(moleTexture);
mole_3.setTextureRect(sf::IntRect(25,700,230,210));
moleSprite.setTexture(moleTexture);
moleSprite.setTextureRect(sf::IntRect(225,0,225,350));
moleRect = small_hole.getLocalBounds();
small_hole.setOrigin(moleRect.left + moleRect.width/2.0f, moleRect.top + moleRect.height/2.0f);
}
//function to get mole sprite
sf::Sprite get_sprite () {
switch(upTime) {
case 0:
moleSprite = small_hole;
return moleSprite;
break;
case 1:
moleSprite = medium_hole;
return moleSprite;
break;
case 2:
moleSprite = large_hole;
return moleSprite;
break;
case 3 ... 4:
moleSprite = mole_1;
return moleSprite;
break;
case 5 ... 6:
moleSprite = mole_2;
return moleSprite;
break;
case 7 ... 8:
moleSprite = mole_3;
return moleSprite;
break;
case 9 ... 10:
moleSprite = mole_2;
return moleSprite;
break;
case 11 ... 12:
moleSprite = mole_1;
return moleSprite;
break;
case 13 ... 14:
moleSprite = large_hole;
return moleSprite;
break;
case 15 ... 16:
moleSprite = medium_hole;
return moleSprite;
break;
case 17 ... 18:
moleSprite = small_hole;
return moleSprite;
break;
default:
moleSprite = small_hole;
return moleSprite;
}
}
//function to set mole interaction bounds
void set_mole_rect (sf::FloatRect float_rect) {
moleRect = float_rect;
}
//function to get mole bounds
sf::FloatRect get_mole_rect () {
return moleRect;
}
//function to set position to window location
void set_position (sf::Vector2f windowLocation) {
moleSprite.setPosition(windowLocation);
}
//function to set a random position for mole
void set_position_random (int window_x, int window_y) {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> xGenerator((int) moleRect.width / 2, window_x - (int) moleRect.height / 2);
int x_location = xGenerator(mt);
std::uniform_int_distribution<int> yGenerator((int) moleRect.width / 2, window_y - (int) moleRect.height / 2);
int y_location = yGenerator(mt);
moleRect = small_hole.getGlobalBounds();
small_hole.setOrigin(moleRect.left + moleRect.width/2.0f, moleRect.top + moleRect.height/2.0f);
small_hole.setPosition((float) x_location, (float) y_location);
moleRect = medium_hole.getGlobalBounds();
medium_hole.setOrigin(moleRect.left + moleRect.width/2.0f, moleRect.top + moleRect.height/2.0f);
medium_hole.setPosition((float) x_location, (float) y_location);
moleRect = large_hole.getGlobalBounds();
large_hole.setOrigin(moleRect.left + moleRect.width/2.0f, moleRect.top + moleRect.height/2.0f);
large_hole.setPosition((float) x_location, (float) y_location);
moleRect = mole_1.getGlobalBounds();
mole_1.setOrigin(moleRect.left + moleRect.width/2.0f, moleRect.top + moleRect.height/2.0f);
mole_1.setPosition((float) x_location, (float) y_location);
moleRect = mole_2.getGlobalBounds();
mole_2.setOrigin(moleRect.left + moleRect.width/2.0f, moleRect.top + moleRect.height/2.0f);
mole_2.setPosition((float) x_location, (float) y_location);
moleRect = mole_3.getGlobalBounds();
mole_3.setOrigin(moleRect.left + moleRect.width/2.0f, moleRect.top + moleRect.height/2.0f);
mole_3.setPosition((float) x_location, (float) y_location);
}
//function to add to mole's uptime
void add_uptime () {
upTime ++;
}
//function to get mole's uptime
int get_uptime () {
return upTime;
}
};
//functions before main for handling main menu and game
void main_menu ();
void game_one ();
//main loop for opening and closing windows
int main() {
while (true) {
main_menu();
game_one();
}
//creation of window
return 0;
}
//function for handling main menu
void main_menu (){
int windowX = 1000;
int windowY = 1000;
sf::RenderWindow mainMenu(sf:: VideoMode(windowX, windowY), "Main Menu",
sf:: Style:: Close | sf:: Style:: Titlebar | sf:: Style:: Resize);
mainMenu.setVerticalSyncEnabled(true);
sf::View view = mainMenu.getDefaultView();
sf::Image icon;
icon.loadFromFile("mole_icon.png");
mainMenu.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
sf::Font font;
int fontSize = 100;
font.loadFromFile("Apple Chancery.ttf");
sf::Text text;
text.setFont(font);
text.setCharacterSize(fontSize);
text.setString("Main Menu");
sf::FloatRect textRect = text.getLocalBounds();
text.setOrigin(textRect.left + textRect.width/2.0f, textRect.top + textRect.height/2.0f);
text.setPosition((float) mainMenu.getSize().x/2, (float) mainMenu.getSize().y/10);
//loop of main
while (mainMenu.isOpen()) {
sf::Event sfmlEvent;
while (mainMenu.pollEvent(sfmlEvent)) {
if(sfmlEvent.type == sfmlEvent.Closed) {
mainMenu.close();
exit(1);
}
else if (sfmlEvent.type == sf::Event::Resized) {
// resize my view
windowX = (int)mainMenu.getSize().x;
std::cout << "WindowX: " << windowX << std::endl;
std::cout << "View X: " << view.getSize().x << std::endl;
windowY = (int)mainMenu.getSize().y;
std::cout << "WindowY: " << windowY << std::endl;
std::cout << "View Y: " << view.getSize().y << std::endl;
view.reset(sf::FloatRect(0, 0, (float) windowX, (float) windowY));
}
}
//creating variables for box origin and cursor location. Will potentially not need box origin but I
//created it for testing purposes and better understanding of declaring vector values in sfml
sf::Vector2i cursorLocation;
sf::Vector2f realCursorLocation;
sf::Vector2f boxOrigin;
float xDifference = 100;
float yDifference = 100;
//Checking for clicking of mouse left click and getting values for box location and mouse pointer. Temporarily
//moving mouse to pointer location as I test distance calculation
if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && mainMenu.hasFocus() && sf::Mouse::getPosition(mainMenu).y >= 0) {
cursorLocation = sf::Mouse::getPosition(mainMenu);
realCursorLocation = mainMenu.mapPixelToCoords(cursorLocation);
mainMenu.close();
}
//checking to see if box and cursor location are within 5 units
//if (xDifference <= objectX / 2 && yDifference <= objectY / 2) {
//}
//outputting frame
mainMenu.clear();
mainMenu.setView(view);
mainMenu.draw(text);
mainMenu.display();
}
};
void game_one (){
sf::Music music;
sf::Time time;
sf::Clock clock;
clock.restart();
sf::Time gameTime = clock.getElapsedTime();
float gameSeconds = gameTime.asSeconds();
std::cout << gameSeconds << std::endl;
if (!music.openFromFile("game_music_1.wav"))
std::cout << "Error\n";
music.setLoop(true);
music.play();
std::vector <Mole> moleList;
int score = 0;
double finalScore = 0.0;
int windowX = 1000;
int windowY = 1000;
sf::RenderWindow window(sf:: VideoMode(windowX, windowY), "Game One",
sf:: Style:: Close | sf:: Style:: Titlebar | sf:: Style:: Resize);
sf::View view = window.getDefaultView();
window.setVerticalSyncEnabled(true);
//background image
sf::Texture texture;
texture.loadFromFile("grass3.jpg");
sf::Sprite sprite;
sprite.setTexture(texture);
sf::FloatRect spriteRect = sprite.getLocalBounds();
sprite.setOrigin(spriteRect.left + spriteRect.width/2.0f, spriteRect.top + spriteRect.height/2.0f);
sprite.setPosition((float) window.getSize().x/2, (float) window.getSize().y/10);
Mole temp_mole;
//text for time elapsed in top left
sf::Font font;
int fontSize = 50;
font.loadFromFile("Apple Chancery.ttf");
sf::Text text;
text.setFont(font);
text.setCharacterSize(fontSize);
text.setStyle(sf::Text::Bold | sf::Text::Underlined);
text.setString("Timer: " + std::to_string(trunc(gameSeconds)));
sf::FloatRect textRect = text.getLocalBounds();
text.setOrigin(textRect.left + textRect.width/2.0f, textRect.top + textRect.height/2.0f);
text.setPosition((float) window.getSize().x/5, (float) window.getSize().y/25);
//text for score in top left
sf::Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(fontSize);
scoreText.setStyle(sf::Text::Bold | sf::Text::Underlined);
scoreText.setString("Score: " + std::to_string(trunc(score)));
sf::FloatRect scoreRect = scoreText.getLocalBounds();
scoreText.setOrigin(scoreRect.left + scoreRect.width/2.0f, scoreRect.top + scoreRect.height/2.0f);
scoreText.setPosition((float) window.getSize().x/5, (float) window.getSize().y/10);
//program loop to keep window open as game is being played
gameTime = clock.getElapsedTime();
gameSeconds = gameTime.asSeconds();
float oldTime = gameSeconds;
float spawnTick = 0.0;
while (window.isOpen()) {
sf::Event sfmlEvent;
//code for handling closure of window
while (window.pollEvent(sfmlEvent)) {
if(sfmlEvent.type == sfmlEvent.Closed) {
window.close();
}
//code for handling resize
else if (sfmlEvent.type == sf::Event::Resized) {
int changeX = (int)window.getSize().x - windowX;
int changeY = (int)window.getSize().y - windowY;
windowX = (int)window.getSize().x;
windowY = (int)window.getSize().y;
float xScale = (float) windowX / 1000;
float yScale = (float) windowY / 1000;
view.reset(sf::FloatRect(0, 0, (float) window.getSize().x, (float) window.getSize().y));
sprite.setPosition((float) window.getSize().x/2, (float) window.getSize().y/2);
}
}
//creating variables for box origin and cursor location. Will potentially not need box origin but I
//created it for testing purposes and better understanding of declaring vector values in sfml
sf::Vector2i cursorLocation;
sf::Vector2f realCursorLocation;
sf::Vector2f boxOrigin;
float xDifference = 100;
float yDifference = 100;
//Checking for clicking of mouse left click and getting values for box location and mouse pointer. Temporarily
//moving mouse to pointer location as I test distance calculation
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && window.hasFocus() && sf::Mouse::getPosition(window).y >= 0) {
cursorLocation = sf::Mouse::getPosition(window);
realCursorLocation = window.mapPixelToCoords(cursorLocation);
for (int i = 0; i < moleList.size(); i++) {
//window.draw(mole.get_sprite());
moleList[i].set_mole_rect(moleList[i].get_sprite().getGlobalBounds());
boxOrigin = moleList[i].get_sprite().getPosition();
xDifference = abs((float) realCursorLocation.x - boxOrigin.x);
yDifference = abs((float) realCursorLocation.y - boxOrigin.y);
//checking to see if box and cursor location are within 5 units
if (xDifference <= moleList[i].get_mole_rect().width / 2 && yDifference <= moleList[i].get_mole_rect().height / 2) {
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> xGenerator((int) moleList[i].get_mole_rect().width / 2, windowX - (int) moleList[i].get_mole_rect().height / 2);
int x_location = xGenerator(mt);
std::uniform_int_distribution<int> yGenerator((int) moleList[i].get_mole_rect().width / 2, windowY - (int) moleList[i].get_mole_rect().height / 2);
int y_location = yGenerator(mt);
//moleList[i].set_position_random((int) window.getSize().x, (int) window.getSize().x);
moleList.erase(moleList.begin() + i);
score += 1;
}
}
}
//calculating time since start
gameTime = clock.getElapsedTime();
gameSeconds = gameTime.asSeconds();
//loop to progress moles
if (gameSeconds - oldTime >= .10) {
oldTime = gameSeconds;
spawnTick += .10;
for (int i = 0; i < moleList.size(); i++) {
moleList[i].add_uptime();
}
}
//loop to spawn moles
if (spawnTick >= 1) {
spawnTick = 0;
moleList.push_back(temp_mole);
moleList[moleList.size() - 1].set_position_random((int) window.getSize().x, (int) window.getSize().y);
moleList.push_back(temp_mole);
moleList[moleList.size() - 1].set_position_random((int) window.getSize().x, (int) window.getSize().y);
}
//loop to delete moles
for (int i = 0; i < moleList.size(); i++) {
if (moleList[i].get_uptime() >= 18) {
moleList.erase(moleList.begin() + i);
}
}
//outputting frame for gameplay
if (gameSeconds <= 30) {
window.clear();
window.setView(view);
window.draw(sprite);
for (auto mole :moleList) {
window.draw(mole.get_sprite());
}
text.setString("Timer: " + std::to_string(trunc(gameSeconds)));
scoreText.setString("Score: " + std::to_string(trunc(score)));
window.draw(text);
window.draw(scoreText);
window.display();
finalScore = score / gameSeconds;
}
//outputting endgame frame
else {
window.clear();
//amateur text output
sf::Text amateur;
amateur.setFont(font);
amateur.setCharacterSize(fontSize);
amateur.setString("Amateur");
sf::FloatRect amateurRect = text.getLocalBounds();
amateur.setOrigin(amateurRect.left + amateurRect.width/2.0f, amateurRect.top + amateurRect.height/2.0f);
amateur.setPosition((float) window.getSize().x/2, (float) window.getSize().y/30);
//above average text output
sf::Text aboveAverage;
aboveAverage.setFont(font);
aboveAverage.setCharacterSize(fontSize);
aboveAverage.setString("Above Average");
sf::FloatRect aboveAverageRect = text.getLocalBounds();
aboveAverage.setOrigin(aboveAverageRect.left + aboveAverageRect.width/2.0f, aboveAverageRect.top + aboveAverageRect.height/2.0f);
aboveAverage.setPosition((float) window.getSize().x/2, (float) window.getSize().y/10);
//professional text output
sf::Text professional;
professional.setFont(font);
professional.setCharacterSize(fontSize);
professional.setString("Professional");
sf::FloatRect professionalRect = text.getLocalBounds();
professional.setOrigin(professionalRect.left + professionalRect.width/2.0f, professionalRect.top + professionalRect.height/2.0f);
professional.setPosition((float) window.getSize().x/2, (float) window.getSize().y/6);
//letting user know how they performed
if (finalScore >= 2.0) {
professional.setStyle(sf::Text::Bold | sf::Text::Underlined);
}
else if (finalScore >= 1.5) {
aboveAverage.setStyle(sf::Text::Bold | sf::Text::Underlined);
}
else {
amateur.setStyle(sf::Text::Bold | sf::Text::Underlined);
}
window.setView(view);
window.draw(amateur);
window.draw(aboveAverage);
window.draw(professional);
window.display();
}
}
};