forked from gaworekkk/projekt-c-pwi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
892 lines (809 loc) · 50.3 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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Player.h"
#include "Button.h"
#include "ObstacleManager.h"
#include "Utils.h"
#include "CoinManager.h"
#include "StatisticsManager.h"
#include "Difficulty.h"
#include "Slider.h"
#include "ResourceLoader.h"
#include "Achivements.h"
#include <stdexcept>
#include <iostream>
#include <algorithm>
#include <vector>
enum GameState { MainMenu, OptionsMenu, Achievements, Statistics, Gameplay, Pause, GameOver, Shop };
// Ustawienie domyślnego stanu gry
GameState gameState = MainMenu;
// Poziom trudności
Difficulty difficulty;
int main() {
// Ustawienia okna
sf::VideoMode videoMode(1200, 640);
sf::Uint32 style = sf::Style::Titlebar | sf::Style::Close;
// Tworzenie okna
sf::RenderWindow window(videoMode, "Dino Game", style);
window.setFramerateLimit(60); // Ograniczenie FPS do 60
// Zmienne stanu gry
int coinCount = 0, currentCoinCount, cactusCount = 0, birdCount = 0;
float distance;
// Zaladowanie statystyk
int bestDistance = 0, totalDistance = 0, jumpCount = 0, deathCount = 0, gamesPlayed = 0;
int skinState[6];
int prices[6] = {0 , 80, 200 , 300, 1000, 10000};
std::string names[6] = {"Dino", "Rascal", "Albino", "Smurf", "Shadow", "Dragon"};
float musicVolume, soundVolume;
StatisticsManager::loadStatistics(coinCount, bestDistance, totalDistance, jumpCount, deathCount, gamesPlayed, cactusCount, birdCount, skinState, musicVolume, soundVolume, difficulty);
// Załaduj wszystkie zasoby
try {
loadAllResources(window, coinCount, musicVolume, soundVolume);
} catch (const std::runtime_error& e) {
// W przypadku błędu wyświetl komunikat i zakończ program
std::cerr << e.what() << std::endl;
return -1;
}
cactusManager.setDifficulty(difficulty);
birdManager.setDifficulty(difficulty);
menuMusic.setVolume(musicVolume);
backgroundMusic.setVolume(musicVolume);
buttonSound.setVolume(soundVolume);
jumpSound.setVolume(soundVolume);
deathSound.setVolume(soundVolume);
// Teksty do wyświetlania
std::vector<std::wstring> achievements;
achievements.push_back(L"Biegacz (" + std::to_wstring(totalDistance) + L"/" + std::to_wstring(range_distance(totalDistance)) + L")");
achievements.push_back(L"Jump man (" + std::to_wstring(jumpCount) + L"/" + std::to_wstring(range_jump(jumpCount)) + L")");
achievements.push_back(L"Pora umierać (" + std::to_wstring(deathCount) + L"/" + std::to_wstring(range_death(deathCount)) + L")");
achievements.push_back(L"Ja to zawsze miałem łeb do interesów (" + std::to_wstring(coinCount) + L"/" + std::to_wstring(range_coins(coinCount)) + L")");
std::vector<std::wstring> statisticsLabels = {
L"Zdobyte monety: ",
L"Najlepszy wynik: ",
L"Całkowity dystans: ",
L"Liczba skoków: ",
L"Liczba śmierci: ",
L"Liczba gier: ",
L"Liczba kaktusów: ",
L"Liczba ptaków: "
};
std::vector<std::wstring> statisticsCounters = {
std::to_wstring(coinCount),
std::to_wstring(bestDistance),
std::to_wstring(totalDistance),
std::to_wstring(jumpCount),
std::to_wstring(deathCount),
std::to_wstring(gamesPlayed),
std::to_wstring(cactusCount),
std::to_wstring(birdCount)
};
float deltaTime;
sf::Clock clock;
// Zmienne "zapadki"
bool if_changed_speed = false;
bool isJumping = false;
const float standardLevel = 500.0f; // Standardowy poziom gracza
sf::Texture aktualna;
int pressed = -5;
for(int i=0;i<6;i++){
if (skinState[i]==0){
pressed=i;
nameSkin.setText(names[pressed]);
priceSkin.setText(std::to_string(prices[pressed]));
poorSkin.setText("");
//buyButton[i]->setText("Wybrano");
//buyButton[i]->setTextColor(sf::Color::Blue);
//buyButton[i]->setButtonColor(sf::Color::Green);
buyButton[i]->setTexture("Tekstury/extinguished-przyciskUBRANO(small).png", "Tekstury/extinguished-przyciskUBRANO(small).png");
player.setTexture(dinoTexturePath[i]);
//buyButtonSkin.setText("Wybrano");
buyButtonSkin.setTexture("Tekstury/extinguished-przyciskUBRANO(small).png", "Tekstury/extinguished-przyciskUBRANO(small).png");
}
else {
//buyButton[i]->setText("Skin" + std::to_string(i + 1));
buyButton[i]->setTexture("Tekstury/przyciskUBIERZ(small).png", "Tekstury/extinguished-przyciskUBIERZ(small).png");
}
}
// Zmienne do śledzenia miniętych przeszkód
std::vector<bool> passedCacti;
std::vector<bool> passedBirds;
// Główna pętla gry
while (window.isOpen()) {
deltaTime = clock.restart().asSeconds();
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed){
totalDistance += static_cast<int>(distance);
StatisticsManager::saveStatistics(coinCount, bestDistance, totalDistance, jumpCount, deathCount, gamesPlayed, cactusCount, birdCount, skinState, musicVolume, soundVolume, difficulty);
window.close();
}
// Obsługa kliknięć w różnych stanach
if (event.type == sf::Event::MouseButtonPressed) {
if (gameState == MainMenu) {
//Aktualizacja statystyk
statisticsCounters[0] = std::to_wstring(coinCount);
statisticsCounters[1] = std::to_wstring(bestDistance);
statisticsCounters[2] = std::to_wstring(totalDistance);
statisticsCounters[3] = std::to_wstring(jumpCount);
statisticsCounters[4] = std::to_wstring(deathCount);
statisticsCounters[5] = std::to_wstring(gamesPlayed);
statisticsCounters[6] = std::to_wstring(cactusCount);
statisticsCounters[7] = std::to_wstring(birdCount);
if (storyButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
player.resetPosition();
Player player(sf::Vector2f(80, 80), sf::Vector2f(100, 500), sf::Color::White, "Tekstury/skórki dino/dino_sprite_sheet.png", 3, 0.1f); // 3 klatki animacji, 0.1 sekundy na klatkę
buttonSound.play(); // Odtwarzanie dźwięku przycisku
distance = 0.0f;
currentCoinCount = 0.0f;
cactusManager.restart();
birdManager.restart();
coinManager.restart();
passedCacti.clear();
passedBirds.clear();
player.resetPosition();
gameState = Gameplay; // Przejście do Gameplay
gamesPlayed++;
menuMusic.stop(); // Zatrzymanie muzyki tła dla menu
backgroundMusic.play(); // Odtwarzanie muzyki tła dla gry
}
/*
if (endlessButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
distance = 0.0f;
currentCoinCount = 0.0f;
cactusManager.restart();
birdManager.restart();
coinManager.restart();
passedCacti.clear();
passedBirds.clear();
player.resetPosition();
gameState = Gameplay; // Przejście do Gameplay
gamesPlayed++;
menuMusic.stop(); // Zatrzymanie muzyki tła dla menu
backgroundMusic.play(); // Odtwarzanie muzyki tła dla gry
}
*/
if (optionsButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = OptionsMenu; // Przejście do OptionsMenu
}
if (achievementsButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = Achievements; // Przejście do Achievements
}
if (statisticsButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = Statistics; // Przejście do Statistics
}
if (exitButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
totalDistance += static_cast<int>(distance);
StatisticsManager::saveStatistics(coinCount, bestDistance, totalDistance, jumpCount, deathCount, gamesPlayed, cactusCount, birdCount, skinState, musicVolume, soundVolume, difficulty);
window.close(); // Zamknięcie okna
}
if (shopButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = Shop; // Przejście do Shop
aktualna=player.getTexture();
player.setPosition(sf::Vector2f(800, 160));
player.setSize(sf::Vector2f(200, 200));
StatisticsManager::loadStatistics(coinCount, bestDistance, totalDistance, jumpCount, deathCount, gamesPlayed, cactusCount, birdCount, skinState, musicVolume, soundVolume, difficulty);
}
} else if (gameState == OptionsMenu) {
if (easyButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play();
difficulty = Difficulty::Easy;
}
if (normalButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play();
difficulty = Difficulty::Normal;
}
if (hardButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play();
difficulty = Difficulty::Hard;
}
if (backButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
cactusManager.setDifficulty(difficulty);
birdManager.setDifficulty(difficulty);
musicSlider->reset();
soundSlider->reset();
musicVolume = musicSlider->getValue();
soundVolume = soundSlider->getValue();
StatisticsManager::saveStatistics(coinCount, bestDistance, totalDistance, jumpCount, deathCount, gamesPlayed, cactusCount, birdCount, skinState, musicVolume, soundVolume, difficulty);
gameState = MainMenu; // Powrót do MainMenu
}
} else if (gameState == Achievements) {
if (backButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = MainMenu; // Powrót do MainMenu
}
} else if (gameState == Statistics) {
if (backButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = MainMenu; // Powrót do MainMenu
}
} else if (gameState == Gameplay) {
if (pauseButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = Pause; // Przejście do Pause
backgroundMusic.pause(); // Zatrzymanie muzyki tła dla gry
}
} else if (gameState == Pause) {
if (resumeButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = Gameplay; // Powrót do Gameplay
backgroundMusic.play(); // Wznowienie muzyki tła dla gry
// Reset dino icon position
dinoSprite.setPosition(20, 104);
// Reset coin icon position
coinSprite.setPosition(20, 104 + dinoSprite.getGlobalBounds().height + 20);
// Reset coin counter position
coinCountText.setPosition(49 + coinSprite.getGlobalBounds().width, 99 + dinoSprite.getGlobalBounds().height + 20 + (coinSprite.getGlobalBounds().height / 2) - (coinCountText.getGlobalBounds().height / 2) - 20);
// Reset distance counter position
distanceText.setPosition(40 + dinoSprite.getGlobalBounds().width, 104 + (dinoSprite.getGlobalBounds().height / 2) - (distanceText.getGlobalBounds().height / 2) - 20);
}
if (restartButtonPause.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
totalDistance += static_cast<int>(distance);
player.resetPosition();
Player player(sf::Vector2f(80, 80), sf::Vector2f(100, 500), sf::Color::White, "Tekstury/skórki dino/dino_sprite_sheet.png", 3, 0.1f); // 3 klatki animacji, 0.1 sekundy na klatkę
distance = 0.0f;
currentCoinCount = 0.0f;
cactusManager.restart();
birdManager.restart();
coinManager.restart();
distanceText.setPosition(40 + dinoSprite.getGlobalBounds().width, 104 + (dinoSprite.getGlobalBounds().height / 2) - (distanceText.getGlobalBounds().height / 2) - 20);
coinCountText.setPosition(49 + coinSprite.getGlobalBounds().width, 99 + dinoSprite.getGlobalBounds().height + 20 + (coinSprite.getGlobalBounds().height / 2) - (coinCountText.getGlobalBounds().height / 2) - 20);
gameState = Gameplay; // Restart gry
gamesPlayed++;
backgroundMusic.play(); // Odtwarzanie muzyki tła od nowa
// Reset dino icon position
dinoSprite.setPosition(20, 104);
// Reset coin icon position
coinSprite.setPosition(20, 104 + dinoSprite.getGlobalBounds().height + 20);
// Reset coin counter position
coinCountText.setPosition(49 + coinSprite.getGlobalBounds().width, 99 + dinoSprite.getGlobalBounds().height + 20 + (coinSprite.getGlobalBounds().height / 2) - (coinCountText.getGlobalBounds().height / 2) - 20);
// Reset distance counter position
distanceText.setPosition(40 + dinoSprite.getGlobalBounds().width, 104 + (dinoSprite.getGlobalBounds().height / 2) - (distanceText.getGlobalBounds().height / 2) - 20);
}
if (mainMenuButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
totalDistance += static_cast<int>(distance);
StatisticsManager::saveStatistics(coinCount, bestDistance, totalDistance, jumpCount, deathCount, gamesPlayed, cactusCount, birdCount, skinState, musicVolume, soundVolume, difficulty);
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = MainMenu; // Powrót do MainMenu
backgroundMusic.stop(); // Zatrzymanie muzyki tła dla gry
menuMusic.play(); // Odtwarzanie muzyki tła dla menu
// Reset dino icon position
dinoSprite.setPosition(20, 104);
// Reset coin icon position
coinSprite.setPosition(20, 104 + dinoSprite.getGlobalBounds().height + 20);
// Reset coin counter position
coinCountText.setPosition(49 + coinSprite.getGlobalBounds().width, 99 + dinoSprite.getGlobalBounds().height + 20 + (coinSprite.getGlobalBounds().height / 2) - (coinCountText.getGlobalBounds().height / 2) - 20);
// Reset distance counter position
distanceText.setPosition(40 + dinoSprite.getGlobalBounds().width, 104 + (dinoSprite.getGlobalBounds().height / 2) - (distanceText.getGlobalBounds().height / 2) - 20);
}
} else if (gameState == GameOver) {
bestDistance = std::max(bestDistance, static_cast<int>(distance));
coinCount += currentCoinCount;
StatisticsManager::saveStatistics(coinCount, bestDistance, totalDistance, jumpCount, deathCount, gamesPlayed, cactusCount, birdCount, skinState, musicVolume, soundVolume, difficulty);
currentCoinCount = 0.0f;
coinCountMainMenuText.setString(std::to_wstring(coinCount));
if (mainMenuButtonOver.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
totalDistance += static_cast<int>(distance);
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = MainMenu; // Powrót do MainMenu
backgroundMusic.stop(); // Zatrzymanie muzyki tła dla gry
menuMusic.play(); // Odtwarzanie muzyki tła dla menu
// Reset dino icon position
dinoSprite.setPosition(20, 104);
// Reset coin icon position
coinSprite.setPosition(20, 104 + dinoSprite.getGlobalBounds().height + 20);
// Reset coin counter position
coinCountText.setPosition(49 + coinSprite.getGlobalBounds().width, 99 + dinoSprite.getGlobalBounds().height + 20 + (coinSprite.getGlobalBounds().height / 2) - (coinCountText.getGlobalBounds().height / 2) - 20);
// Reset distance counter position
distanceText.setPosition(40 + dinoSprite.getGlobalBounds().width, 104 + (dinoSprite.getGlobalBounds().height / 2) - (distanceText.getGlobalBounds().height / 2) - 20);
}
if (restartButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
player.resetPosition();
Player player(sf::Vector2f(80, 80), sf::Vector2f(100, 500), sf::Color::White, "Tekstury/skórki dino/dino_sprite_sheet.png", 3, 0.1f); // 3 klatki animacji, 0.1 sekundy na klatkę
distance = 0.0f;
cactusManager.restart();
birdManager.restart();
coinManager.restart();
passedCacti.clear();
passedBirds.clear();
currentCoinCount = 0.0f;
coinCountMainMenuText.setString(std::to_wstring(coinCount));
distanceText.setPosition(40 + dinoSprite.getGlobalBounds().width, 104 + (dinoSprite.getGlobalBounds().height / 2) - (distanceText.getGlobalBounds().height / 2) - 20);
coinCountText.setPosition(49 + coinSprite.getGlobalBounds().width, 99 + dinoSprite.getGlobalBounds().height + 20 + (coinSprite.getGlobalBounds().height / 2) - (coinCountText.getGlobalBounds().height / 2) - 20);
gameState = Gameplay; // Restart gry
gamesPlayed++;
backgroundMusic.play(); // Odtwarzanie muzyki tła od nowa
}
// Remove the exit button functionality from the GameOver screen
// if (exitButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
// buttonSound.play(); // Odtwarzanie dźwięku przycisku
// window.close(); // Zamknięcie okna
// }
} else if (gameState == Shop) {
if (backButton.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
gameState = MainMenu; // Powrót do MainMenu
player.resetPosition();
player.resetSize();
player.setFrameDuration(0.1f);
player.setTexture(aktualna);
StatisticsManager::saveStatistics(coinCount, bestDistance, totalDistance, jumpCount, deathCount, gamesPlayed, cactusCount, birdCount, skinState, musicVolume, soundVolume, difficulty);
for (int i = 0;i<6;i++){
if (skinState[i] == 0){
poorSkin.setText("");
//buyButton[i]->setText("Wybrano");
buyButton[i]->setTexture("Tekstury/extinguished-przyciskUBRANO(small).png", "Tekstury/extinguished-przyciskUBRANO(small).png");
} else {
//buyButton[i]->setText("Skin" + std::to_string(i+1));
buyButton[i]->setTexture("Tekstury/przyciskUBIERZ(small).png", "Tekstury/extinguished-przyciskUBIERZ(small).png");
}
}
}
for (int i=0;i<6;i++){
poorSkin.setText("");
if (buyButton[i]->isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
buttonSound.play(); // Odtwarzanie dźwięku przycisku
player.setTexture(dinoTexturePath[i]);
pressed = i;
nameSkin.setText(names[pressed]);
priceSkin.setText(std::to_string(prices[pressed]));
//buyButton[i]->setText("Wybrano");
buyButton[i]->setTexture("Tekstury/extinguished-przyciskUBRANO(small).png", "Tekstury/extinguished-przyciskUBRANO(small).png");
for (int j=0;j<6;j++){
if (j!=i){
//buyButton[j]->setText("Skin" + std::to_string(j+1));
buyButton[j]->setTexture("Tekstury/przyciskUBIERZ(small).png", "Tekstury/extinguished-przyciskUBIERZ(small).png");
}
}
if (skinState[i] == 0){
//buyButtonSkin.setText("Wybrano");
buyButtonSkin.setTexture("Tekstury/extinguished-przyciskUBRANO(small).png", "Tekstury/extinguished-przyciskUBRANO(small).png");
} else if (skinState[i] == 2){
//buyButtonSkin.setText("Kup");
buyButtonSkin.setTexture("Tekstury/przyciskKUP(small).png", "Tekstury/extinguished-przyciskKUP(small).png");
} else {
//buyButtonSkin.setText("Wybierz");
buyButtonSkin.setTexture("Tekstury/przyciskUBIERZ(small).png", "Tekstury/extinguished-przyciskUBIERZ(small).png");
}
}
}
if (buyButtonSkin.isClicked(sf::Mouse::getPosition(window), event.mouseButton)) {
if (skinState[pressed] == 2){
poorSkin.setText("");
if (coinCount >= prices[pressed]){
coinCount -= prices[pressed];
coinCountMainMenuText.setString(std::to_wstring(coinCount));
skinState[pressed] = 0;
aktualna=player.getTexture();
//buyButtonSkin.setText("Wybrano");
buyButtonSkin.setTexture("Tekstury/extinguished-przyciskUBRANO(small).png", "Tekstury/extinguished-przyciskUBRANO(small).png");
for (int j=0;j<6;j++){
if (j!=pressed && skinState[j] == 0){
skinState[j] = 1;
buyButton[j]->setTexture("Tekstury/przyciskUBIERZ(small).png", "Tekstury/extinguished-przyciskUBIERZ(small).png");
}
}
}else{
poorSkin.setText("Nie masz "+std::to_string(prices[pressed])+" coin");
poorSkin.setTextColor(sf::Color::Black);
poorSkin.draw(window);
}
} else if (skinState[pressed] == 1){
skinState[pressed] = 0;
//buyButtonSkin.setText("Wybrano");
buyButtonSkin.setTexture("Tekstury/extinguished-przyciskUBRANO(small).png", "Tekstury/extinguished-przyciskUBRANO(small).png");
aktualna=player.getTexture();
for (int j=0;j<6;j++){
if (j!=pressed && skinState[j] != 2){
skinState[j] = 1;
}
}
}
}
}
}
// Obsługa klawiszy w różnych stanach
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape) {
if (gameState == OptionsMenu) {
cactusManager.setDifficulty(difficulty);
birdManager.setDifficulty(difficulty);
coinManager.setObstacleSpawnSpeed(cactusManager.getInitialSpeed());
musicSlider->reset();
soundSlider->reset();
musicVolume = musicSlider->getValue();
soundVolume = soundSlider->getValue();
StatisticsManager::saveStatistics(coinCount, bestDistance, totalDistance, jumpCount, deathCount, gamesPlayed, cactusCount, birdCount, skinState, musicVolume, soundVolume, difficulty);
gameState = MainMenu; // Powrót do MainMenu
}
if (gameState == Achievements) {
gameState = MainMenu; // Powrót do MainMenu
}
if (gameState == Statistics) {
gameState = MainMenu; // Powrót do MainMenu
}
if (gameState == Gameplay) {
gameState = Pause; // Przejście do Pause
backgroundMusic.pause(); // Zatrzymanie muzyki tła dla gry
}
else if (gameState == Pause) {
gameState = Gameplay; // Powrót do Gameplay
backgroundMusic.play(); // Wznowienie muzyki tła dla gry
// Reset dino icon position
dinoSprite.setPosition(20, 104);
// Reset coin icon position
coinSprite.setPosition(20, 104 + dinoSprite.getGlobalBounds().height + 20);
// Reset coin counter position
coinCountText.setPosition(49 + coinSprite.getGlobalBounds().width, 99 + dinoSprite.getGlobalBounds().height + 20 + (coinSprite.getGlobalBounds().height / 2) - (coinCountText.getGlobalBounds().height / 2) - 20);
// Reset distance counter position
distanceText.setPosition(40 + dinoSprite.getGlobalBounds().width, 104 + (dinoSprite.getGlobalBounds().height / 2) - (distanceText.getGlobalBounds().height / 2) - 20);
}
else if (gameState == Shop) {
gameState = MainMenu; // Powrót do MainMenu
player.resetPosition();
player.resetSize();
player.setFrameDuration(0.1f);
player.setTexture(aktualna);
StatisticsManager::saveStatistics(coinCount, bestDistance, totalDistance, jumpCount, deathCount, gamesPlayed, cactusCount, birdCount, skinState, musicVolume, soundVolume, difficulty);
for (int i = 0;i<6;i++){
if (skinState[i] == 0){
//buyButton[i]->setText("Wybrano");
buyButtonSkin.setTexture("Tekstury/extinguished-przyciskUBRANO(small).png", "Tekstury/extinguished-przyciskUBRANO(small).png");
} else {
//buyButton[i]->setText("Skin" + std::to_string(i+1));
buyButtonSkin.setTexture("Tekstury/przyciskUBIERZ(small).png", "Tekstury/extinguished-przyciskUBIERZ(small).png");
}
}
}
}
if (event.key.code == sf::Keyboard::Q) {
window.close(); // Zamknięcie okna
}
if (gameState != MainMenu && gameState != OptionsMenu && gameState != Statistics && gameState != Achievements && gameState != Shop) { // Block 'R' key in MainMenu, OptionsMenu, Statistics, Achievements, and Shop
if (event.key.code == sf::Keyboard::R) {
player.resetPosition();
Player player(sf::Vector2f(80, 80), sf::Vector2f(100, 500), sf::Color::White, "Tekstury/skórki dino/dino_sprite_sheet.png", 3, 0.1f);
coinManager.restart();
cactusManager.restart();
birdManager.restart();
distance = 0.0f;
coinCount = 0.0f;
currentCoinCount = 0.0f; // Reset coin counter
gameState = Gameplay; // Restart gry
gamesPlayed++;
backgroundMusic.stop(); // Zatrzymanie muzyki tła
backgroundMusic.play(); // Odtwarzanie muzyki tła od nowa
// Reset dino icon position
dinoSprite.setPosition(20, 104);
// Reset coin icon position
coinSprite.setPosition(20, 104 + dinoSprite.getGlobalBounds().height + 20);
// Reset coin counter position
coinCountText.setPosition(49 + coinSprite.getGlobalBounds().width, 99 + dinoSprite.getGlobalBounds().height + 20 + (coinSprite.getGlobalBounds().height / 2) - (coinCountText.getGlobalBounds().height / 2) - 20);
// Reset distance counter position
distanceText.setPosition(40 + dinoSprite.getGlobalBounds().width, 104 + (dinoSprite.getGlobalBounds().height / 2) - (distanceText.getGlobalBounds().height / 2) - 20);
}
}
if (event.key.code == sf::Keyboard::Space || event.key.code == sf::Keyboard::Up) {
if (!isJumping && player.getGlobalBounds().top >= standardLevel && gameState == Gameplay) {
jumpSound.play(); // Odtwarzanie dźwięku skoku
jumpCount++;
isJumping = true;
}
}
}
// Obsługa klawiszy w różnych stanach
if (event.type == sf::Event::KeyReleased) {
if (event.key.code == sf::Keyboard::Space || event.key.code == sf::Keyboard::Up) {
isJumping = false;
}
}
// Obsługa przesuwania sliderów
if (gameState == OptionsMenu) {
musicSlider->update(event, window);
soundSlider->update(event, window);
musicVolume = musicSlider->getValue();
soundVolume = soundSlider->getValue();
menuMusic.setVolume(musicVolume);
backgroundMusic.setVolume(musicVolume);
buttonSound.setVolume(soundVolume);
jumpSound.setVolume(soundVolume);
deathSound.setVolume(soundVolume);
}
}
// Logika w zależności od stanu gry
if (gameState == Gameplay) {
player.handleInput(deltaTime, cactusManager.getSpeed());
player.update(deltaTime);
cactusManager.update(deltaTime);
birdManager.update(deltaTime);
std::vector<sf::FloatRect> allObstacles;
auto cactusBounds = cactusManager.getObstacleBounds();
auto birdBounds = birdManager.getObstacleBounds();
allObstacles.insert(allObstacles.end(), cactusBounds.begin(), cactusBounds.end());
allObstacles.insert(allObstacles.end(), birdBounds.begin(), birdBounds.end());
coinManager.update(deltaTime, player.getGlobalBounds(), currentCoinCount, allObstacles);
// Obliczanie odległości
float cactusSpeed = cactusManager.getSpeed();
float birdSpeed = birdManager.getSpeed();
float obstacleInitialSpeed = cactusManager.getInitialSpeed();
float playerVelocity = player.getVelocity().x;
distance += (deltaTime * (cactusSpeed + playerVelocity)) / obstacleInitialSpeed;
// Zmiana prędkości co 50 jednostek
int distanceInt = static_cast<int>(distance);
if(distanceInt % 50 == 0 && distanceInt <= 1000 && if_changed_speed == false){
cactusManager.setSpeed(cactusSpeed + 20);
birdManager.setSpeed(birdSpeed + 20);
coinManager.setObstacleSpawnSpeed(cactusSpeed + 20);
coinManager.setSpeed(cactusSpeed + 20);
if_changed_speed = true;
} else if(distanceInt % 50 != 0) {
if_changed_speed = false;
}
// Zmiana rodzaju poziomu ze zwykłego na powietrzny i odwrotnie
if(distanceInt % 100 == 0 && distanceInt != 0){
if(distanceInt % 200 != 0){
cactusManager.turnSkyLevelOn();
birdManager.turnSkyLevelOn();
coinManager.turnSkyLevelOn();
player.turnSkyLevelOn();
}else{
cactusManager.turnSkyLevelOff();
birdManager.turnSkyLevelOff();
coinManager.turnSkyLevelOff();
}
}
int playerFallDelay = 4; //do wyczucia
if(((distanceInt-playerFallDelay) % 100 == 0) && ((distanceInt - playerFallDelay)% 200 == 0) && ((distanceInt- playerFallDelay)!=0)){
player.turnSkyLevelOff();
}
distanceText.setString(L"" + std::to_wstring(distanceInt)); // Ustawienie tekstu odległości
coinCountText.setString(L"" + std::to_wstring(currentCoinCount)); // Usunięcie napisu "Monety:"
// Sprawdzenie kolizji i koniec gry lub restart
if ((cactusManager.checkCollisions(player.getGlobalBounds())) || (birdManager.checkCollisions(player.getGlobalBounds()))) {
deathCount++;
totalDistance += static_cast<int>(distance);
gameState = GameOver;
backgroundMusic.stop(); // Zatrzymanie muzyki tła dla gry
deathSound.play(); // Odtwarzanie dźwięku śmierci
}
// Zliczanie miniętych kaktusów i ptaków
float playerRight = player.getGlobalBounds().left + player.getGlobalBounds().width;
if (passedCacti.size() > cactusBounds.size()) {
passedCacti.assign(cactusBounds.size(), false);
} else if (passedCacti.size() < cactusBounds.size()){
passedCacti.resize(cactusBounds.size(), false);
}
if (passedBirds.size() > birdBounds.size()) {
passedBirds.assign(birdBounds.size(), false);
} else if (passedBirds.size() < birdBounds.size()){
passedBirds.resize(birdBounds.size(), false);
}
for (size_t i = 0; i < cactusBounds.size(); ++i) {
if (!passedCacti[i] && playerRight > cactusBounds[i].left + cactusBounds[i].width) {
cactusCount++;
passedCacti[i] = true;
}
}
// Zliczanie miniętych ptaków
for (size_t i = 0; i < birdBounds.size(); ++i) {
if (!passedBirds[i] && playerRight > birdBounds[i].left + birdBounds[i].width) {
birdCount++;
passedBirds[i] = true;
}
}
} else if (gameState == Shop) {
player.updateAnimation(deltaTime);
player.setFrameDuration(0.5f);
}
// Aktualizacja przycisków i sliderów zależnie od stanu
if (gameState == MainMenu) {
if (menuMusic.getStatus() != sf::Music::Playing) {
menuMusic.play(); // Odtwarzanie muzyki tła dla menu
}
storyButton.update(sf::Mouse::getPosition(window));
//endlessButton.update(sf::Mouse::getPosition(window));
optionsButton.update(sf::Mouse::getPosition(window));
achievementsButton.update(sf::Mouse::getPosition(window));
statisticsButton.update(sf::Mouse::getPosition(window));
exitButton.update(sf::Mouse::getPosition(window));
shopButton.update(sf::Mouse::getPosition(window));
} else if (gameState == OptionsMenu) {
easyButton.update(sf::Mouse::getPosition(window));
normalButton.update(sf::Mouse::getPosition(window));
hardButton.update(sf::Mouse::getPosition(window));
backButton.update(sf::Mouse::getPosition(window));
} else if (gameState == Achievements) {
backButton.update(sf::Mouse::getPosition(window));
} else if (gameState == Statistics) {
window.draw(statisticsBackgroundSprite); // Rysowanie tła trybu Statistics
float leftMargin = 160.0f; // Distance from the left edge of the screen
float rightMargin = 165.0f; // Distance from the right edge of the screen
float yPosition = 100.0f; // Starting y position
unsigned int fontSize = 40; // Reduced the font size by 5
// Draw the statistics labels and counters
for (size_t i = 0; i < statisticsLabels.size(); ++i) {
sf::Text labelText(statisticsLabels[i], font, fontSize);
labelText.setFillColor(sf::Color::Black); // Set the color of labelText to black
labelText.setPosition(leftMargin, yPosition);
window.draw(labelText);
sf::Text counterText(statisticsCounters[i], font, fontSize);
counterText.setFillColor(sf::Color::Black); // Set the color of counterText to black
counterText.setPosition(window.getSize().x - rightMargin - counterText.getGlobalBounds().width, yPosition);
window.draw(counterText);
yPosition += labelText.getGlobalBounds().height + 10.0f; // Move to the next line
}
backButton.draw(window);
} else if (gameState == Gameplay) {
pauseButton.update(sf::Mouse::getPosition(window));
} else if (gameState == Pause) {
resumeButton.update(sf::Mouse::getPosition(window));
restartButtonPause.update(sf::Mouse::getPosition(window));
mainMenuButton.update(sf::Mouse::getPosition(window));
} else if (gameState == GameOver) {
window.clear(sf::Color::Black);
window.draw(gameOverBackgroundSprite); // Rysowanie tła trybu GameOver
restartButton.draw(window); // Draw the restart button for GameOver
// Remove the exit button from the GameOver screen
// exitButton.draw(window);
// Center the distance and coin counters under the "Game Over" text
bestScoreText.setString(L"Najlepszy wynik: " + std::to_wstring(bestDistance)); // Set the best score text
float totalWidthBestScore = bestScoreText.getGlobalBounds().width + distanceText.getGlobalBounds().width + 20;
float startXBestScore = (window.getSize().x - totalWidthBestScore) / 2;
bestScoreText.setPosition(startXBestScore, gameOverText.getPosition().y + gameOverText.getGlobalBounds().height + 150);
distanceText.setPosition(startXBestScore + bestScoreText.getGlobalBounds().width + 20, bestScoreText.getPosition().y);
yourScoreText.setString(L"Twój wynik:"); // Change text to "Twój wynik:"
float totalWidthYourScore = yourScoreText.getGlobalBounds().width + distanceText.getGlobalBounds().width + 20;
float startXYourScore = (window.getSize().x - totalWidthYourScore) / 2 - 20; // Shift 20 pixels to the left
yourScoreText.setPosition(startXYourScore, bestScoreText.getPosition().y + bestScoreText.getGlobalBounds().height + 10);
distanceText.setPosition(startXYourScore + yourScoreText.getGlobalBounds().width + 20, yourScoreText.getPosition().y);
coinsText.setString(L"Monety:"); // Change text to "Monety:"
coinsText.setPosition((window.getSize().x - coinsText.getGlobalBounds().width) / 2 - 45, distanceText.getPosition().y + distanceText.getGlobalBounds().height + 8); // Podniesienie o 2 piksele
coinCountText.setPosition(coinsText.getPosition().x + coinsText.getGlobalBounds().width + 20, coinsText.getPosition().y);
distanceText.setFillColor(sf::Color::White); // Set the color of distanceText to white
coinCountText.setFillColor(sf::Color::White); // Set the color of coinCountText to white
window.draw(yourScoreText); // Draw the "Twój wynik:" text
window.draw(distanceText); // Draw the distance counter
window.draw(coinsText); // Draw the "Monety:" text
window.draw(coinCountText); // Draw the coin counter
window.draw(bestScoreText); // Draw the best score text
} else if (gameState == Shop) {
backButton.update(sf::Mouse::getPosition(window));
}
// Rysowanie
window.clear(sf::Color::Black);
if (gameState == MainMenu) {
window.draw(backgroundSprite); // Rysowanie tła
storyButton.draw(window);
//endlessButton.draw(window);
optionsButton.draw(window);
achievementsButton.draw(window);
statisticsButton.draw(window);
exitButton.draw(window);
shopButton.draw(window);
window.draw(nameText);
window.draw(coinIconMainMenu); // Draw the coin icon
window.draw(coinCountMainMenuText); // Draw the coin count text
} else if (gameState == OptionsMenu) {
window.draw(optionsMenuText);
window.draw(optionsMenuBackgroundSprite); // Rysowanie tła trybu OptionsMenu
easyButton.draw(window);
normalButton.draw(window);
hardButton.draw(window);
musicSlider->draw(window);
soundSlider->draw(window);
backButton.draw(window);
} else if (gameState == Achievements) {
window.draw(achievementsBackgroundSprite); // Rysowanie tła trybu Achievements
drawScrollableList(window, achievements, font);
backButton.draw(window);
} else if (gameState == Statistics) {
window.draw(statisticsBackgroundSprite); // Rysowanie tła trybu Statistics
float leftMargin = 160.0f; // Distance from the left edge of the screen
float rightMargin = 165.0f; // Distance from the right edge of the screen
float yPosition = 100.0f; // Starting y position
unsigned int fontSize = 40; // Reduced the font size by 5
// Draw the statistics labels and counters
for (size_t i = 0; i < statisticsLabels.size(); ++i) {
sf::Text labelText(statisticsLabels[i], font, fontSize);
labelText.setFillColor(sf::Color::Black); // Set the color of labelText to black
labelText.setPosition(leftMargin, yPosition);
window.draw(labelText);
sf::Text counterText(statisticsCounters[i], font, fontSize);
counterText.setFillColor(sf::Color::Black); // Set the color of counterText to black
counterText.setPosition(window.getSize().x - rightMargin - counterText.getGlobalBounds().width, yPosition);
window.draw(counterText);
yPosition += labelText.getGlobalBounds().height + 10.0f; // Move to the next line
}
backButton.draw(window);
} else if (gameState == Gameplay) {
window.draw(storyBackgroundSprite); // Rysowanie tła trybu fabularnego
player.draw(window); // Rysowanie gracza
cactusManager.draw(window); // Rysowanie przeszkód
coinManager.draw(window);
birdManager.draw(window);
// window.draw(gameplayText); // Usunięcie napisu "Gejmplej!"
pauseButton.draw(window);
window.draw(dinoSprite); // Rysowanie dino2
window.draw(distanceText); // Rysowanie tekstu odległości
window.draw(coinSprite); // Rysowanie monety
window.draw(coinCountText); // Rysowanie tekstu obok monety
} else if (gameState == Pause) {
window.draw(pauseText);
window.draw(pauseBackgroundSprite); // Rysowanie tła trybu Pause
resumeButton.draw(window);
restartButtonPause.draw(window); // Draw the restart button for Pause
mainMenuButton.draw(window); // Draw the main menu button for Pause
// Move the dino icon to coordinates (375, 400) on the pause screen
dinoSprite.setPosition(375, 400);
// Move the distance counter to coordinates (445, 385) on the pause screen
distanceText.setPosition(445, 385);
// Move the coin icon to coordinates (625, 409) on the pause screen
coinSprite.setPosition(625, 409);
// Move the coin counter to coordinates (695, 385) on the pause screen
coinCountText.setPosition(695, 385);
window.draw(dinoSprite); // Draw the dino icon
window.draw(distanceText); // Draw the distance counter
window.draw(coinSprite); // Draw the coin icon
window.draw(coinCountText); // Draw the coin counter
} else if (gameState == GameOver) {
window.clear(sf::Color::Black);
window.draw(gameOverBackgroundSprite); // Rysowanie tła trybu GameOver
restartButton.draw(window); // Draw the restart button for GameOver
mainMenuButtonOver.draw(window);
// Remove the exit button from the GameOver screen
// exitButton.draw(window);
// Center the distance and coin counters under the "Game Over" text
bestScoreText.setString(L"Najlepszy wynik: " + std::to_wstring(bestDistance)); // Set the best score text
float totalWidthBestScore = bestScoreText.getGlobalBounds().width + distanceText.getGlobalBounds().width + 20;
float startXBestScore = (window.getSize().x - totalWidthBestScore) / 2;
bestScoreText.setPosition(startXBestScore, gameOverText.getPosition().y + gameOverText.getGlobalBounds().height + 150);
distanceText.setPosition(startXBestScore + bestScoreText.getGlobalBounds().width + 20, bestScoreText.getPosition().y);
yourScoreText.setString(L"Twój wynik:"); // Change text to "Twój wynik:"
float totalWidthYourScore = yourScoreText.getGlobalBounds().width + distanceText.getGlobalBounds().width + 20;
float startXYourScore = (window.getSize().x - totalWidthYourScore) / 2 - 20; // Shift 20 pixels to the left
yourScoreText.setPosition(startXYourScore, bestScoreText.getPosition().y + bestScoreText.getGlobalBounds().height + 10);
distanceText.setPosition(startXYourScore + yourScoreText.getGlobalBounds().width + 20, yourScoreText.getPosition().y);
coinsText.setString(L"Monety:"); // Change text to "Monety:"
coinsText.setPosition((window.getSize().x - coinsText.getGlobalBounds().width) / 2 - 45, distanceText.getPosition().y + distanceText.getGlobalBounds().height + 8); // Podniesienie o 2 piksele
coinCountText.setPosition(coinsText.getPosition().x + coinsText.getGlobalBounds().width + 20, coinsText.getPosition().y);
distanceText.setFillColor(sf::Color::White); // Set the color of distanceText to white
coinCountText.setFillColor(sf::Color::White); // Set the color of coinCountText to white
window.draw(yourScoreText); // Draw the "Twój wynik:" text
window.draw(distanceText); // Draw the distance counter
window.draw(coinsText); // Draw the "Monety:" text
window.draw(coinCountText); // Draw the coin counter
window.draw(bestScoreText); // Draw the best score text
} else if (gameState == Shop) {
window.draw(shopBackgroundSprite); // Rysowanie tła trybu Shop
window.draw(coinIconMainMenu); // Draw the coin icon
window.draw(coinCountMainMenuText); // Draw the coin count text
backButton.draw(window);
priceSkin.draw(window);
nameSkin.draw(window);
buyButtonSkin.draw(window);
poorSkin.draw(window);
for (int i = 0; i < 6; i++) {
buyButton[i]->draw(window);
}
window.draw(dinoFrameSprite); // Rysowanie tekstury dino-frame(big) pod player
player.draw(window);
}
window.display();
}
// sound.stop();
// music.stop();
for (int i = 0; i < 6; i++) {
delete buyButton[i];
}
delete musicSlider;
delete soundSlider;
return 0;
}